Skip to content

Instantly share code, notes, and snippets.

@rymndhng
Last active February 29, 2024 05:09
Show Gist options
  • Save rymndhng/cf96f16dd8510d9b417702a00a8c3349 to your computer and use it in GitHub Desktop.
Save rymndhng/cf96f16dd8510d9b417702a00a8c3349 to your computer and use it in GitHub Desktop.
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