Skip to content

Instantly share code, notes, and snippets.

@hayeah
Last active December 26, 2022 07:17
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 hayeah/8dcce661f0461d071147ebdb829a76a9 to your computer and use it in GitHub Desktop.
Save hayeah/8dcce661f0461d071147ebdb829a76a9 to your computer and use it in GitHub Desktop.
shortcut to clone git repo to a `user/repo` path
: <<COMMENT
# Clone the repository with the default repository name
gclone https://github.com/balancer-labs/balancer-v2-monorepo
# Clone the repository with a custom repository name
gclone https://github.com/balancer-labs/balancer-v2-monorepo my-custom-name
# Clone the repository without ".git" suffix
gclone https://github.com/balancer-labs/balancer-v2-monorepo.git my-custom-name
# Clone the repository using short form URL
gclone balancer-labs/balancer-v2-monorepo
COMMENT
# gclone https://github.com/balancer-labs/balancer-v2-monorepo
# gclone https://github.com/balancer-labs/balancer-v2-monorepo.git
# gclone https://github.com/balancer-labs/balancer-v2-monorepo.git dest
gclone() {
# Make sure that a URL was passed as an argument
if [ -z "$1" ]; then
echo "Error: No URL specified"
return 1
fi
# Prepend "https://github.com" to the URL if it is in the form "user/repo"
url="$1"
if echo "$url" | grep -Eq '^[^/]+/[^/]+$'; then
url="https://github.com/$url"
fi
# Append ".git" to the URL if it doesn't have that suffix
if ! echo "$url" | grep -Eq '\.git$'; then
url="$url.git"
fi
# If a clone destination was not specified as the second argument, extract it from the URL
if [ -z "$2" ]; then
repo_name=$(echo "$1" | sed -E 's/.*\/([^/]+\/[^/]+).*/\1/')
else
repo_name=$2
fi
# remove .git from repo_name
repo_name=$(echo "$repo_name" | sed -E 's/(.*)\.git/\1/')
# Clone the repository
git clone "$url" "$repo_name"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment