Created
August 13, 2021 05:04
-
-
Save erelson/35a23ac28aa800af9a06ba7822afdaa7 to your computer and use it in GitHub Desktop.
Set an email when cloning repos
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
# Use this with a git alias like `cl = "!sh ~/clone_helper.sh"` | |
# A bash function to replace `git clone` could also be done | |
# like in https://stackoverflow.com/a/39357426/999505 | |
set -e | |
if [ $# = 0 ]; then # forgot to pass anything, d'oh | |
echo ERROR Must pass repo path to clone | |
exit 1 | |
fi | |
echo "Enter user.name to use with this repo (e.g. John Smith) [$(git config --get user.name)]:" | |
read name | |
echo "Enter user.email to use with this repo (e.g. john.smith@aol.com) [$(git config --get user.email)]:" | |
read email | |
# If a second arg was supplied, it's the target folder for the cloned repo | |
if [ $2 ]; then | |
echo going to run: | |
echo " git clone $1 $2" | |
git clone $1 $2 | |
cd $2 || (echo "Failed to enter cloned directory. Exiting early." && exit 1) | |
# Infer resulting folder from clone path: take last token from a split on '/', and also strip tailing '.git'. | |
elif [ $1 ]; then | |
newdir=$(echo $1 | awk '{n=split($1,A,"/"); print A[n]}' | sed 's/.git$//') | |
echo going to run: | |
echo " git clone $1" | |
echo Inferred that this repo will end up in: $newdir | |
git clone $1 | |
cd $newdir || (echo "Failed to enter cloned directory. Exiting early." && exit 1) | |
fi | |
if [ $name ]; then | |
git config user.name $name | |
fi | |
if [ $email ]; then | |
git config user.email $email | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment