Created
October 4, 2013 18:25
-
-
Save lukecampbell/6830365 to your computer and use it in GitHub Desktop.
github.rb A Ruby script to make dealing with github easier and from the command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
require 'slop' | |
require 'git' | |
class GitHub | |
def initialize(remote=nil, branch=nil, url='https://github.com/') | |
if File.directory? '.git' | |
@g = Git.open('./') | |
else | |
git_dir = `cat .git`.split('gitdir: ')[1].chomp() | |
@g = Git.open('./', :repository => git_dir, :index => git_dir + '/index') | |
end | |
@remote = @g.remote(remote || 'origin') | |
if @g.branches.to_a.any? {|b| b.name == branch} | |
@branch = @g.branch(branch) | |
else | |
@branch = @g.branch(@g.current_branch) | |
end | |
@url = url | |
end | |
def compare(end_branch=nil, begin_branch=nil) | |
if not end_branch | |
end_branch = @branch.name | |
end | |
if not begin_branch | |
url = self._remote + 'compare/' + end_branch | |
else | |
c_end = @g.branch(end_branch).gcommit.sha | |
c_begin = @g.branch(begin_branch).gcommit.sha | |
url = self._remote + 'compare/' + c_begin + '...' + c_end | |
end | |
return url | |
end | |
def file(file, line1=nil, line2=nil) | |
url = self._remote + 'blob/' + @branch.name + '/' + file | |
if line1 | |
url += '#L' + line1.to_s | |
end | |
if line2 | |
url += '-' + line2.to_s | |
end | |
return url | |
end | |
def pulls(num=nil) | |
return self.pull(num) | |
end | |
def pull(num=nil) | |
url = self._remote + 'pulls/' | |
if num | |
url = self._remote + 'pull/' + num.to_s | |
end | |
return url | |
end | |
def show(commit) | |
if commit and commit.include? 'HEAD' | |
commit = `git rev-parse #{commit}` | |
end | |
commit = commit || @branch.gcommit.sha | |
url = self._remote + 'commit/' + commit | |
return url | |
end | |
def remote() | |
url = self._remote | |
return url | |
end | |
def _remote() | |
remote_url = @g.remote(@remote).url | |
if remote_url.include? '@' | |
remote_url = @url + remote_url.split(':')[1] | |
elsif remote_url.include? 'github.com/' | |
remote_url = @url + remote_url.split('github.com/')[1] | |
else | |
puts 'Failed to determine _remote' | |
end | |
remote_url = remote_url.sub('.git','/') | |
if not remote_url.end_with? '/' | |
remote_url += '/' | |
end | |
return remote_url | |
end | |
def _branch() | |
@branch | |
end | |
def branch() | |
remote = self._remote | |
if remote[-1] != '/' | |
url = self._remote + '/tree/' + @branch.name | |
else | |
url = self._remote + 'tree/' + @branch.name | |
end | |
return url | |
end | |
def blob(file, line1=nil, line2=nil) | |
commit = @branch.gcommit.sha | |
url = self._remote + 'blob/' + commit.to_s + '/' + file | |
if line1 | |
url += '#L' + line1.to_s | |
end | |
if line2 | |
url += '-' + line2.to_s | |
end | |
return url | |
end | |
end | |
# pattern is http://github.com/<user>/<repo>/ | |
# args: remote/repo branch | |
# | |
opts = Slop.parse do | |
banner = 'github [-b branch] [-r remote] <command>' | |
on :b, :branch, 'Branch Name', :argument=>:optional | |
on :r, :remote, 'Remote name', :argument=>:required | |
on :c, :compare,'Compare Branch', :argument=>:optional | |
on :f, :file, 'Show File', :argument=>:required | |
on :l, :line, 'Line Number', :argument=>:required | |
on :n, :endline,'Ending Line Number', :argument=>:required | |
on :s, :show, 'Show Commit', :argument=>:optional | |
on :p, :pull, 'Pull requests', :argument=>:optional | |
on :o, :blob, 'Blob', :argument=>:required | |
end | |
gh = GitHub.new(opts[:remote], opts[:branch]) | |
url = gh.remote | |
if opts[:blob] | |
url = gh.blob(opts[:blob], opts[:line], opts[:endline]) | |
elsif opts[:file] | |
url = gh.file(opts[:file], opts[:line], opts[:endline]) | |
elsif opts.pull? | |
url = gh.pull(opts[:pull]) | |
elsif opts.compare? | |
url = gh.compare(opts[:compare]) | |
elsif opts.show? | |
url = gh.show(opts[:show]) | |
elsif opts.branch? | |
url = gh.branch | |
else | |
url = gh.remote | |
end | |
puts url | |
exec 'open ' + url | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment