Skip to content

Instantly share code, notes, and snippets.

@antonrogov
Created May 31, 2011 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonrogov/1000122 to your computer and use it in GitHub Desktop.
Save antonrogov/1000122 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def find_branches(name)
`git branch -r|grep '/#{name.gsub("'", '')}'`.to_s.split("\n").map(&:strip)
end
def last_commit_url(branch)
remote, name = branch.split('/')
sha = `git log -n 1 #{branch}|head -n 1|awk '{print $2}'`.strip
url = `git remote -v|grep #{remote}|grep '(push)'|awk '{print $2}'`.strip
url = "https://github.com/#{$1}" if url =~ /git@github.com:(.*)\.git/
"#{url}/commit/#{sha}/"
end
def open_in_browser(path)
if RUBY_PLATFORM =~ /darwin/
`open "#{path}"`
else
require "rubygems"
require "launchy1"
Launchy::Browser.run(path)
end
rescue LoadError
puts path
warn "Sorry, you need to install launchy to open pages: `gem install launchy`"
end
if ARGV.size < 1
puts "USAGE: git review PART_OF_BRANCH_NAME [INDEX]"
exit 1
end
branch_name = ARGV[0]
branches = find_branches(branch_name)
index = ARGV[1].to_i
if branches.size > 1 && ARGV.size < 2
puts "Multiple matching branches found:"
branches.each_with_index do |branch, i|
puts "%5i: %s" % [i+1, branch]
end
puts "Use git review '#{branch_name}' INDEX to choose one"
exit 1
else
branch = branches[index - 1]
end
open_in_browser(last_commit_url(branch))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment