Skip to content

Instantly share code, notes, and snippets.

@radlinskii
Last active March 22, 2020 23:01
Show Gist options
  • Save radlinskii/0745b7a1b85a8c2e7fdcb139e212deb2 to your computer and use it in GitHub Desktop.
Save radlinskii/0745b7a1b85a8c2e7fdcb139e212deb2 to your computer and use it in GitHub Desktop.
git clone and git init alias that adds setting the git user name and email address after cloning or initializing the repo.
function set_git_config_variable() {
local defaultvalue="$(git config user.$1)"
local exit_status
echo "Please provide the repository config user $1 (default: $defaultvalue):"
read customvalue
if [ -z $customvalue ]; then
git config user.$1 $defaultvalue
exit_status=$?
else
git config user.$1 $customvalue
exit_status=$?
fi
if [ $exit_status -ne 0 ]; then
echo "Error occured while setting the repository config user $1."
return $exit_status
fi
return 0
}
git() {
local tmp=$(mktemp -t git)
local repo_name
local exit_status
local quiet_flag=$(echo "$@" | grep "\ -q\|\ --quiet")
if [ "$1" = clone ] || [ "$1" = init ] && [ "$2" != --help ] && [ -z "$quiet_flag" ]; then
command git "$@" 2>&1 | tee $tmp
if [ -n "$(awk '/error:|fatal:|usage:/ {print $0}' $tmp)" ]; then
rm $tmp
return 1
fi
if [ "$1" = init ]; then
repo_name=$(awk '/Initialized empty Git repository in/ {print $6}' $tmp)
repo_name=$(echo ${repo_name/\/\.git\/})
elif [ "$1" = clone ]; then
repo_name=$(awk -F\' '/Cloning into/ {print $2}' $tmp)
fi
rm $tmp
printf "Changing directory to '%s'\n" "$repo_name"
cd "$repo_name"
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "Error occured while changing current directory to the local repository directory."
return $exit_status
fi
set_git_config_variable "name"
exit_status=$?
if [ $exit_status -ne 0 ]; then
return $exit_status
fi
set_git_config_variable "email"
exit_status=$?
return $exit_status
else
command git "$@"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment