lib/git/whence.rb in git-whence-0.1.3 vs lib/git/whence.rb in git-whence-0.2.0
- old
+ new
@@ -6,61 +6,68 @@
class << self
def run(argv)
options = parse_options(argv)
commit = argv[0]
unless system("git rev-parse --git-dir 2>&1 >/dev/null")
- puts "Not in a git directory"
+ warn "Not in a git directory"
return 1
end
commit = expand(commit)
if is_merge?(commit)
- $stderr.puts "Commit is a merge"
+ warn "Commit is a merge"
finished_with_commit(commit, options)
- 1
else
merge = find_merge(commit)
if merge
finished_with_commit(merge, options)
- 0
else
- $stderr.puts "Unable to find merge"
- 1
+ warn "Unable to find merge"
+ options[:open] ? finished_with_commit(commit, options) : 1
end
end
end
private
def expand(commit)
- sh("git show #{commit} -s --format='%H'").strip
+ sh("git rev-parse #{commit}").strip
end
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}"}
+ if options[:open]
+ if pr = info[/Merge pull request #(\d+) from /, 1]
+ exec "open", "https://github.com/#{origin}/pull/#{pr}"
+ else
+ warn "Unable to find PR number in #{info}"
+ exec "open", "https://github.com/#{origin}/commit/#{merge}"
+ end
else
puts info
+ 0
end
end
+ # https://github.com/foo/bar or git@github.com:foo/bar.git -> foo/bar
def origin
- remotes = sh("git remote -v").split("\n")
- remotes.detect { |l| l.start_with?("origin\t") }.split(" ")[1]
+ repo = sh("git remote get-url origin").strip
+ repo.sub!(/\.git$/, "")
+ repo.split(/[:\/]/).last(2).join("/")
end
def find_merge(commit)
- commit, merge = find_merge_simple(commit, "HEAD") ||
+ commit, merge = (
+ find_merge_simple(commit, "HEAD") ||
find_merge_simple(commit, "master") ||
find_merge_fuzzy(commit, "master")
+ )
merge if merge && merge_include_commit?(merge, commit)
end
def merge_include_commit?(merge, commit)
@@ -77,21 +84,21 @@
def find_similar(commit, branch)
month = 30 * 24 * 60 * 60
time, search = sh("git show -s --format='%ct %an %s' #{commit}").strip.split(" ", 2)
time = time.to_i
same = sh("git log #{branch} --pretty=format:'%H %an %s' --before #{time + month} --after #{time - month}")
- found = same.split("\n").map { |x| x.split(" ", 2) }.detect { |commit, message| message == search }
+ found = same.split("\n").map { |x| x.split(" ", 2) }.detect { |_, message| message == search }
found && found.first
end
def find_merge_simple(commit, branch)
result = sh "git log #{commit}..#{branch} --ancestry-path --merges --pretty='%H' 2>/dev/null | tail -n 1"
- [commit, result] unless result.strip.empty?
+ [commit, result.strip] unless result.strip.empty?
end
def sh(command)
result = `#{command}`
- raise unless $?.success?
+ raise "Command failed\n#{command}\n#{result}" unless $?.success?
result
end
def parse_options(argv)
options = {}