lib/git/whence.rb in git-whence-0.1.0 vs lib/git/whence.rb in git-whence-0.1.1

- old
+ new

@@ -5,32 +5,47 @@ module CLI class << self def run(argv) options = parse_options(argv) commit = argv[0] - unless system("git rev-parse --git-dir 1>&2>/dev/null") + unless system("git rev-parse --git-dir 2>&1 >/dev/null") puts "Not in a git directory" return 1 end - merge = find_merge(commit) - if merge - if options[:open] && (pr = merge[/Merge pull request #(\d+) from /, 1]) && (url = origin) - repo = url[%r{(\w+/[-\w\.]+)}i, 1].to_s.sub(/\.git$/, "") - exec %Q{open "https://github.com/#{repo}/pull/#{pr}"} + if is_merge?(commit) + $stderr.puts "Commit is a merge" + finished_with_commit(commit, options) + 1 + else + merge = find_merge(commit) + if merge + finished_with_commit(merge, options) + 0 else - puts merge + $stderr.puts "Unable to find merge" + 1 end - 0 - else - $stderr.puts "Unable to find commit" - 1 end end private + def is_merge?(commit) + sh("git cat-file -p #{commit}").split("\n")[1..2].grep(/parent /).size > 1 + end + + def finished_with_commit(merge, options) + info = sh("git show -s --oneline #{merge}").strip + if options[:open] && (pr = info[/Merge pull request #(\d+) from /, 1]) && (url = origin) + repo = url[%r{(\w+/[-\w\.]+)}i, 1].to_s.sub(/\.git$/, "") + exec %Q{open "https://github.com/#{repo}/pull/#{pr}"} + else + puts info + end + end + def origin remotes = sh("git remote -v").split("\n") remotes.detect { |l| l.start_with?("origin\t") }.split(" ")[1] end @@ -54,10 +69,10 @@ found = same.split("\n").map { |x| x.split(" ", 2) }.detect { |commit, message| message == search } found && found.first end def find_merge_simple(commit, branch) - result = sh "git log #{commit}..#{branch} --ancestry-path --merges --oneline 2>/dev/null | tail -n 1" + result = sh "git log #{commit}..#{branch} --ancestry-path --merges --pretty='%h' 2>/dev/null | tail -n 1" result unless result.strip.empty? end def sh(command) result = `#{command}`