Skip to content

Instantly share code, notes, and snippets.

@ubermajestix
Last active December 12, 2015 07:59
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 ubermajestix/4740949 to your computer and use it in GitHub Desktop.
Save ubermajestix/4740949 to your computer and use it in GitHub Desktop.
A git command to open remotes and branches in a browser
#! /usr/bin/env ruby
# If the remote repository is hosted on GitHub.com (or GitHub Enterprise) this
# git command will allow you to open your browser to the specified branch on a
# given remote.
#
# For example:
# Given the remote:
# origin git@github.com:ubermajestix/some_app.git
#
# Running:
# git open master feature/awesomeness
#
# Opens http://github.com/ubermajestix/some_app/tree/feature/awesomeness
#
# For bash completion of remotes and branch names, copy the code below into
# git-completion.bash, my copy lives at:
# /usr/local/Cellar/git/1.7.10.2/etc/bash_completion.d/git-completion.bash
# _git_open(){
# if [ $cword = 2 ]; then
# __gitcomp_nl "$(__git_remotes)" && return
# else
# __gitcomp_nl "$(__git_heads)" && return
# fi
# }
target_repo = ARGV.shift
target_branch = ARGV.shift
repos = `git remote -v`.split("\n").map{|r| r.split[0,2] }.uniq
repo = repos.detect{|r| r.first == target_repo }.last
url = repo.gsub(/git@/,'').gsub(':', '/').gsub('.git','')
if target_branch
remote_branches = `git branch -v -r | cut -d " " -f3`.split
if remote_branches.include?("#{target_repo}/#{target_branch}")
url << "/tree/#{target_branch}"
else
puts "Branch #{target_branch} does not exist on the remote repo #{target_repo}"
end
end
`open "http://#{url}"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment