Last Updated: February 25, 2016
·
875
· unsymbol

Open Github/Bitbucket Page From Command Line

Here's little script that'll open a git repository's Github or Bitbucket page from the command line. Just add it to your path as an executable and use it from within a repo.

#!/usr/bin/env ruby

remote = begin `git config --get remote.origin.url` rescue nil end

if remote && remote != ""
  remote.gsub!("\n", "")

  if remote.include?("http")
    remote.gsub!(".git", "")
    system("open #{remote}")
  else
    host = /(?<=\@)(.*?)(?=\:)/.match(remote)[0]
    user = /(?<=\:)(.*?)(?=\/)/.match(remote)[0]
    repo = /(?<=\/)(.*?)(?=\.git)/.match(remote)[0]
    url  = "https://#{host}/#{user}/#{repo}"
    system("open #{url}")
  end
else
  puts "not a git repo..."
end

Note, it'll only work on Mac OS X due to the open command. There's probably an equivalent for other operating systems.