Created
May 22, 2024 10:51
-
-
Save janeklb/5351f1d5fd776ae35eae095e8dff2da1 to your computer and use it in GitHub Desktop.
helper functions for cloning git repos
This file contains hidden or 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
function clone-repo() { | |
local GIT_HOME=$HOME/git | |
local GITHUB_REPO_URL=$1 | |
if [[ $GITHUB_REPO_URL == git@* ]]; then | |
ORG=${GITHUB_REPO_URL#*:} | |
ORG=${ORG%/*} | |
elif [[ $GITHUB_REPO_URL == https://* ]]; then | |
ORG=${GITHUB_REPO_URL%/*} | |
ORG=${ORG##*/} | |
else | |
echo "Please supply a valid repo url. '$GITHUB_REPO_URL' is not valid" | |
return | |
fi | |
if [[ "$ORG" =~ [^a-zA-Z0-9_-] ]]; then | |
echo "Something doesn't look right.. org: $ORG" | |
return | |
fi | |
ORG_PATH=$GIT_HOME/$ORG | |
[ ! -d "$ORG_PATH" ] && mkdir -p $ORG_PATH && echo "Creating $ORG_PATH" | |
cd $ORG_PATH && echo "Changed current directory to $ORG_PATH" | |
REPO=$(basename $GITHUB_REPO_URL .git) | |
if [ -d "$ORG_PATH/$REPO" ]; then | |
echo "Repo already checked out locally ..." | |
else | |
echo "Running git clone $GITHUB_REPO_URL" | |
git clone $GITHUB_REPO_URL | |
fi | |
echo "Changed current directory to $ORG_PATH/$REPO" | |
cd $REPO | |
} | |
function clone-github-repo { | |
clone-repo "[email protected]:$1.git" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment