Last active
December 12, 2015 07:59
-
-
Save ubermajestix/4740949 to your computer and use it in GitHub Desktop.
A git command to open remotes and branches in a browser
This file contains 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 | |
# 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 [email protected]: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