bryce-canyon

When you install git on your computer there is a .gitconfig file that gets set in your home directory. This file contains the default settings for your git as well as the personal information that will be recorded in your commits. I have one computer on which I write code for multiple clients. Some clients require commits to their repo be made with a git user profile that belongs to them (i.e. the git user email is a company email address). Obviously it would be ideal to have a hook in place to enforce this. Alas we’re still working on that. In the meantime, checking the list of unique committers to the company repo…

git log --pretty=format:'%ae' | uniq -i
# yours.truly@foobar.com
# someone.else@foobar.com
# yours.truly@gmail.com

…reveals commits by yours.truly@gmail.com. Since Gmail is (as of yet) not a client of mine, that’s not good. This is because I regularly forget to switch to the correct profile between projects. It’s also become a pain to manually maintain multiple .gitconfig files. In order to better manage all this I added some things to my .bash_profile.

The first thing is a function that sets a shell variable $GITUSER_EMAIL. This variable will hold the email address of the currently active git profile.

setgituser(){
	GITUSER_EMAIL=`cat ~/.gitconfig | egrep email | awk -F"=" '{print $2}' | sed -e 's/^ //'`
}

Then I add the following lines that will run every time a new terminal shell opens:

setgituser
if [ $GITUSER_EMAIL != '<yours.truly@foobar.com>' ]; then
	tput bel
	tput bel
	tput bel
	echo "############################################################################"
	echo "#####           Your .gitconfig has the wrong email address!!          #####"
	echo "############################################################################"
fi

These lines call the function to set the $GITUSER_EMAIL shell variable and then check to make sure it is set to the git user I use most often. If the email address does not match my primary client then I’ll get a big warning message and some sound effects.

Here’s the nice part. The following function that is also put in .bash_profile will tell me the currently active git user as well as let me switch between users:

gituser(){
	while getopts ":ab" opt; do
		case $opt in
			a)
				if [ "$GITUSER_EMAIL" != 'yourstruly@a.com' ]; then
					echo "switching to personal git user"
					mv ~/.gitconfig ~/.gitconfigWork
					mv ~/.gitconfigPersonal ~/.gitconfig
					setgituser
				fi
				;;
			b)
				if [ "$GITUSER_EMAIL" != 'yourstruly@b.com' ]; then
					echo "switching to work git user"
					mv ~/.gitconfig ~/.gitconfigPersonal
					mv ~/.gitconfigWork ~/.gitconfig
					setgituser
				fi
				;;
			\?)
				echo "Invalid option: -$OPTARG" >&2
				echo "USAGE: -p [switch to personal], -w [switch to work]"
				;;
		esac
	done
	echo "Git user is $GITUSER_EMAIL"
}

Now i can simply run gituser -a to switch to company a’s git profile and gituser -b to switch to company b’s git profile. Either way it’ll tell me who the current git profile user is. Hopefully this should help.

Leave a Reply

Your email address will not be published. Required fields are marked *