Skip to content

Instantly share code, notes, and snippets.

@ross-nordstrom
Last active March 26, 2016 16:50
Show Gist options
  • Save ross-nordstrom/ab3636334441ab60d410 to your computer and use it in GitHub Desktop.
Save ross-nordstrom/ab3636334441ab60d410 to your computer and use it in GitHub Desktop.
Takes a branch name like `myBranch_name-with/any^separators#12345` and adds `\n[#12345]` after the first line.
#!/bin/sh
#
# Automatically add branch name to every commit message except merge commits.
#
COMMIT_MSG=$1
addBranchName() {
branchPath=$(git symbolic-ref -q HEAD)
branchName=${branchPath##*/}
branchId=$(echo $branchName | sed -n 's/.*#\([0-9]\)/\1/p')
# No branch id
if [ "" = "$branchId" ] ; then
echo "$(cat ${COMMIT_MSG})" > "${COMMIT_MSG}"
# Branch id already in message
elif cat ${COMMIT_MSG} | grep "\[#$branchId\]" -q; then
echo "$(cat ${COMMIT_MSG})" > "${COMMIT_MSG}"
# Add the branch id
else
sed -i "2i \ \n[#$branchId]" ${COMMIT_MSG}
fi
}
# Don't inject branch IDs when merging
MERGE=$(cat $COMMIT_MSG|grep -i 'merge'|wc -l)
if [ $MERGE -eq 0 ] ; then
addBranchName
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment