daisuke-s
10/29/2015 - 11:50 AM

Script to open Git Repository's Remote URL

Script to open Git Repository's Remote URL

#! /usr/bin/env ruby

# Please run the below command first.
# gem install git uri-ssh_git launchy

require 'git'
require 'uri/ssh_git'
require 'launchy'

dir = '.'
remote_name = 'origin'
nobrowser = false

OptionParser.new do |opts|
  opts.banner = "Usage: #{__FILE__} [options]"

  opts.on('-d DIRECTORY', '--directory DIRECTORY', 'Directory. Defaults to current directory') do |d|
    dir = d
  end
  opts.on('-n', '--no-browser', 'Do not open URL in browser.') do |n|
    nobrowser = true
  end
  opts.on('-r REMOTE_NAME', '--remote REMOTE_NAME', 'Remote name. Defaults to origin') do |r|
    remote_name = r
  end
end.parse!

git = Git.open(dir)

remotes = git.remotes

remote = remotes.select { |r| r.name == remote_name }.first

remote_url = remote.url

url = URI::SshGit.parse(remote_url)

http_url = "https://#{url.host}#{url.path}"

puts http_url

Launchy.open(http_url) unless nobrowser