Skip to content

Instantly share code, notes, and snippets.

@ross-nordstrom
Last active December 30, 2015 23:59
Show Gist options
  • Save ross-nordstrom/7904007 to your computer and use it in GitHub Desktop.
Save ross-nordstrom/7904007 to your computer and use it in GitHub Desktop.
This goes in `.git/hooks/prepare-commit-msg`. Be sure to chmod +x it. It adds the user story id to every commit based on branch name. Intended for Pivotal tracker, but can be modified for other syntaxes.
#!/bin/sh
#
# Automatically add branch name and branch description to every commit message except merge commit.
# Prefix commit messages with "[US1234]: ""
#
COMMIT_EDITMSG=$1
addBranchName() {
branchPath=$(git symbolic-ref -q HEAD)
branchName=${branchPath##*/}
branchId=`expr match "$branchName" '\([a-zA-Z]*[0-9]*\)'`
# remove any existing entries of the branch id
COMMIT_EDITMSG=${COMMIT_EDITMSG##*]:}
echo "[$branchId]: $(cat $COMMIT_EDITMSG)" > $COMMIT_EDITMSG
}
MERGE=$(cat $COMMIT_EDITMSG|grep -i 'merge'|wc -l)
if [ $MERGE -eq 0 ] ; then
addBranchName
fi
@codyhanson
Copy link

What should the branch name look like then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment