Skip to content

Instantly share code, notes, and snippets.

@burke
Last active August 29, 2015 14:00
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 burke/11226536 to your computer and use it in GitHub Desktop.
Save burke/11226536 to your computer and use it in GitHub Desktop.
# ~ $ gh libressl libressl
# ~/src/github.com/libressl/libressl $ gh https://github.com/rails/rails
# ~/src/github.com/rails/rails $
gh () { cd $(_gh $@); }
#!/usr/bin/env ruby
require 'fileutils'
module Github
GH_ROOT = "#{ENV["HOME"]}/src/github.com"
def self.open(*args)
clone_repo(repo_path(*args))
end
# Takes:
# "https://github.com/libressl/libressl"
# "github.com/libressl/libressl"
# "libressl/libressl"
# "libressl", "libressl"
# Returns:
# "libressl/libressl"
def self.repo_path(*args)
repo = args[0]
if args.size > 1
repo = args.join("/")
end
repo.
sub(/.*github.com[\/:]/, '').
sub(/\.git$/, '')
end
# Takes:
# "libressl/libressl"
# Returns
# "/Users/myuser/src/github.com/libressl/libressl"
# (after cloning if not present)
def self.clone_repo(path)
acct, _ = path.split('/')
FileUtils.mkdir_p(GH_ROOT + "/" + acct)
fullpath = GH_ROOT + "/" + path
unless Dir.exists?(fullpath)
system("git clone git@github.com:#{path}.git #{fullpath}")
end
fullpath
end
end
__FILE__ == $0 and puts Github.open(*ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment