Skip to content

Instantly share code, notes, and snippets.

@pietvanzoen
Last active December 1, 2018 08:07
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 pietvanzoen/92c47aa810506ec113e42667fc6e1b7d to your computer and use it in GitHub Desktop.
Save pietvanzoen/92c47aa810506ec113e42667fc6e1b7d to your computer and use it in GitHub Desktop.
A tool for cloning and automatically organising local git repos.

Git get

Like go get, git-get organizes repos in directories according to 1) host, 2) user, and 3) project name.

Example

git get git@github.com:pietvanzoen/dotfiles.git will clone the project into $GIT_PATH/github.com/pietvanzoen/dotfiles.

Installation

cd <where-you-want-the-script>
curl -O https://gist.githubusercontent.com/pietvanzoen/92c47aa810506ec113e42667fc6e1b7d/raw/git-get
chmod +x git-get
git config --global alias.get '!<path-to-script>/git-get'

Then add export GIT_PATH=$HOME/repos into your shell profile such as .bash_profile, .bashrc or .zshrc and close and restart your shell.

#!/usr/bin/env python
# Like go get but without the need for go.
import sys
import os
import subprocess
import urlparse
import tempfile
import string
def eprint(msg):
sys.stderr.write(msg + '\n')
USAGE = """Usage: git get URL
Like 'go get' without the go. Clone the repository URL into directories using the host, user, and project name.
Set $GIT_PATH as the base directory to clone repositories into.
"""
if len(sys.argv) == 1:
eprint(USAGE)
sys.exit(1)
git_url = sys.argv[1]
base_dir = os.getenv('GIT_PATH')
if git_url == '-h':
eprint(USAGE)
sys.exit(0)
if not base_dir:
eprint('$GIT_PATH must be set')
sys.exit(1)
if not os.path.exists(base_dir):
eprint('$GIT_PATH is set to %s, which does not exist.' % base_dir)
sys.exit(1)
url_for_path_parsing = git_url
if git_url.startswith('git@'):
url_for_path_parsing = 'https://' + git_url.replace(':', '/')
parsed_url = urlparse.urlparse(url_for_path_parsing)
if not parsed_url.scheme:
eprint('%s is not a valid git url. Skipping.' % git_url)
sys.exit(1)
if not parsed_url.path.endswith('.git'):
eprint('%s is not a valid git url. Skipping.' % git_url)
sys.exit(1)
pathend = string.strip(os.path.splitext(parsed_url.path)[0], '/')
path = os.path.join(base_dir, parsed_url.hostname, pathend)
if os.path.exists(path):
eprint('Path %s already exists.' % path)
sys.exit(1)
temp_dir = tempfile.mkdtemp()
subprocess.call(['git', 'clone', git_url, temp_dir])
parent_dir = os.path.dirname(path)
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
os.rename(temp_dir, path)
print(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment