Git Get
#!/bin/bash -x | |
# | |
# Clones Git Projects into directory of <projects_dir>/<group>/<repo_name> | |
# | |
# Tested with support for Github, Bitbucket & Sourcehut. | |
# | |
# By default, clones to $HOME/projects. This can be overriden by setting | |
# GIT_GET_PROJECTS_DIR to another directory. | |
# | |
# Usage: | |
# | |
# git get https://github.com/foo/bar.git | |
# git get git@github.com:foo/bar.git | |
# | |
PROJECTS_DIR=${GIT_GET_PROJECTS_DIR:-"$HOME/projects"} | |
# Regex to extract group & project from projects. Tested with these patterns to | |
# extract group=foo and repo_name=bar. | |
# | |
# https://github.com/foo/bar.git | |
# git@github.com:foo/bar.git | |
# https://bitbucket.org/mituharu/emacs-mac.git | |
# git@git.sr.ht:~foo/bar | |
# | |
GROUP_AND_PROJECT_PATTERN="(.*)[:/~](.+)/(.+)" | |
main () { | |
repo_url=$1 | |
if [ $# -eq 0 ]; then | |
echo "Missing repo_url" | |
echo "" | |
echo "Usage: git get <repo_url>" | |
exit 1 | |
fi | |
if [[ $repo_url =~ $GROUP_AND_PROJECT_PATTERN ]]; then | |
group=${BASH_REMATCH[2]} | |
project=${BASH_REMATCH[3]} | |
project=${project%".git"} # remove trailing .git | |
else | |
echo "Unable to parse URL of format: ${repo_url}" | |
exit 2 | |
fi | |
git clone "$repo_url" "$PROJECTS_DIR/$group/$project" | |
} | |
main $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment