Quality of Life with Git and Pivotal

At Spotlight we are currently using Pivotal Tracker as our planning tool and git for version control. One of the obvious things to do is to connect code changes to stories by including the story id in the branch and commit messages. This allows you to cross reference between the two and track why you did what code changes.

But developers are lazy beasts, the pivotal numbers are long, and life is too short to write [#123456789] a gazillion times a day. Let’s see how we can improve it!

Automated branch creation

If you follow the standard git workflow, the first thing to do when starting some work is to create a branch. The branch should have a story number, as well as a meaningful title. Luckily, pivotal has a very nice REST api we can use directly from curl to free us from burden of typing the branch name:

#!/bin/sh
TOKEN='pivotal-token-goes-here'
PROJECT_ID='your-project-id-goes-here'
STORY_ID=`grep -o '[0-9]*' <<< \$1} `
NAME=`curl -X GET -H "X-TrackerToken: $TOKEN" "https://www.pivotaltracker.com/services/v5/projects/$PROJECT_ID/stories/$STORY_ID" | grep -o '"name":"[^"]*"'| head -1 | sed "s/'//" | sed s/'"name":"'// | sed s/'"'//g | sed s/' '/'_'/g | sed s/'#'//g | sed s~/~_~g | sed s/,//g`
branchName=${STORY_ID}_${NAME}

git checkout -b $branchName
git push origin -u $branchName

Yes, my bash is horrendous, so if your eyes are melting from reading it here is what it does: you pass the pivotal story id, the script curls the story as json, extracts the name and replaces characters that would upset git. Then it creates a branch and pushes it to the origin upstream repo.

Stick it into a file (eg. grab.sh), put this in your PATH and set chmod 755. For added automation, set it as your git alias and hey presto! Guaranteed to work 99% of the time.

hnrioma

This gets the job done, the only downside being that the branch names sometimes end up being too long. But that’s just an added incentive to keep the story titles short.

Add [#story id] if you are committing to a story branch

We can (hopefully) assume that people will not start their branch with a number, so we can try to filter them on commit:

#!/bin/sh
BranchName=`git rev-parse --abbrev-ref HEAD`
TicketNo=`grep -o '^[0-9]*' <<< $BranchName`
if [ -n "$TicketNo" ] 
 then 
 git commit -m "[#$TicketNo] $1"
 else
 git commit -m "$1"
fi

Now you can just type your commit message, and git will add the story number if you are on a story branch. That saves us 11 keystrokes per commit! How cool is that?

aa0g9cb

And just for completion, the git aliases I am using that go into .gitconfig

[alias]
 cm = !commit.sh
 grab = !grab.sh

Enjoy one line git-pivotal experience!

Quality of Life with Git and Pivotal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s