Created
February 27, 2012 20:05
-
-
Save joshmcarthur/1926673 to your computer and use it in GitHub Desktop.
git-browse: Open repositories in Github from a Terminal
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 | |
#### git-browse | |
### Open a github repository in the default browser | |
### Author: @sudojosh | |
### Usage: Run `git browse` inside a repo with at least one github remote | |
### Installation: Download, run `chmod +x git-browse`, and then copy somewhere on your path | |
### for example, /usr/local/bin/ | |
remotes = `git remote -v` | |
if ARGV.first | |
branch = "/tree/#{ARGV.first.gsub(/\//, '')}/" | |
else | |
branch = `git symbolic-ref HEAD` | |
branch = branch.gsub('refs/heads/', '/tree/') | |
end | |
remotes = remotes.split("\n").map { |remote| | |
remote = remote.split(/\s+/)[1] | |
remote =~ /github.com/ ? remote : nil | |
}.compact | |
if remotes.empty? | |
puts "No Github remotes" | |
exit | |
else | |
command = RUBY_PLATFORM.downcase.include?("darwin") ? "open" : "xdg-open" | |
remote_url = remotes.first.gsub("[email protected]:", "https://github.com/").gsub("git://github.com:", "https://github.com/").gsub(".git", "") | |
remote_url += branch | |
system("#{command} #{remote_url}") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment