Skip to content

Instantly share code, notes, and snippets.

@mogproject
Last active August 29, 2015 14:19
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 mogproject/f6c18bc3e4478a50034c to your computer and use it in GitHub Desktop.
Save mogproject/f6c18bc3e4478a50034c to your computer and use it in GitHub Desktop.
#!/bin/bash
GIT=/usr/local/bin/git
SCRIPT_DIR=$( cd "$( dirname "$0" )" && pwd -P )
if [ "$1" = "clone" ]; then
origin_url="$2"
else
# check if current directory is under git repository
origin_url=$("$GIT" config --get remote.origin.url) || {
"${GIT}" "$@"
exit $?
}
fi
# definitions
case ${origin_url} in
git@github.com:your-company/*)
readonly user_name='Your name 1'
readonly user_email='youremail1@example.com'
readonly git_ssh='ssh-yourname-1'
;;
*)
readonly user_name='Your name 2'
readonly user_email='youremail2@example.com'
readonly git_ssh='ssh-yourname-2'
;;
esac
# check name and email
if [ "$1" != "clone" ]; then
current_name=$("$GIT" config --get user.name) || {
echo "Failed to get user name. Aborted."
exit 1
}
current_email=$("$GIT" config --get user.email) || {
echo "Failed to get user email. Aborted."
exit 1
}
if [ "${current_name}" != "${user_name}" ]; then
echo "Setting user name: \"${GIT}\" config user.name \"${user_name}\""
"${GIT}" config user.name "${user_name}"
fi
if [ "${current_email}" != "${user_email}" ]; then
echo "Setting user email: \"${GIT}\" config user.email \"${user_email}\""
"${GIT}" config user.email "${user_email}"
fi
fi
# launch git command
GIT_SSH="${SCRIPT_DIR}/../ssh/${git_ssh}" "${GIT}" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment