Last active
December 26, 2022 07:17
-
-
Save hayeah/8dcce661f0461d071147ebdb829a76a9 to your computer and use it in GitHub Desktop.
shortcut to clone git repo to a `user/repo` path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
: <<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