Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Created August 12, 2011 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save loganlinn/1143079 to your computer and use it in GitHub Desktop.
Save loganlinn/1143079 to your computer and use it in GitHub Desktop.
Quick script to open GitHub pages for the repository in the working directory.
#!/usr/bin/ruby
require 'rubygems'
require 'grit'
begin
r = Grit::Repo.new Dir.pwd
rescue
puts "Did not detect git repository"
exit
end
def repo_descriptor
descrip = %x{git remote -v}.match(/^.+git@github\.com:(.+\/.+)\.git/)
if descrip.nil?
puts "Could not find github remote"
exit
end
descrip[1]
end
def url_base
"https://github.com/#{repo_descriptor}"
end
# open a url relative to current repository github URL
#
def gh_open(path)
%x{open #{url_base}#{path}}
end
command = unless ARGV[0].nil?
ARGV[0]
else
'open'
end
case command
when 'open', 'o'
branch = if ARGV[1].nil?
r.head.name
else
ARGV[1]
end
gh_open "/tree/#{branch}"
when 'compare'
if ARGV[1].nil?
puts 'Missing branch to diff with'
exit
end
# optionaly provide two branches to compare
unless ARGV[2].nil?
current = ARGV[1]
other = ARGV[2]
else # otherwise compare to current branch
current = r.head.name
other = ARGV[1]
end
gh_open "/compare/#{other}...#{current}"
when 'commits', 'c'
branch = if ARGV[1].nil?
r.head.name
else
ARGV[1]
end
gh_open "/commits/#{branch}"
when 'commit', 'sha'
if ARGV[1].nil?
puts 'Missing commit id'
exit
end
commit_hash = ARGV[1]
gh_open "/commit/#{commit_hash}"
when 'tree'
if ARGV[1].nil?
puts 'Missing tree id'
exit
end
tree_hash = ARGV[1]
gh_open "/tree/#{tree_hash}"
when 'network', 'n'
gh_open '/network'
when 'branches', 'b'
gh_open '/branches'
when 'pulls'
gh_open '/pulls'
else
puts 'Unknown command'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment