Skip to content

Instantly share code, notes, and snippets.

@axxapy
Last active August 29, 2015 14:05
Show Gist options
  • Save axxapy/2062b1faea3c1db93d0b to your computer and use it in GitHub Desktop.
Save axxapy/2062b1faea3c1db93d0b to your computer and use it in GitHub Desktop.
Git hook which adds ticket number extracted from branch name (like TICKET-123_comment) to every commit message.
#!/bin/bash
#
# This hook adds ticket number from branch name to every commit message
#
BRANCH_NAME=$(git symbolic-ref --short HEAD)
MSG=$(head -n 1 $1)
[[ -z "$BRANCH_NAME" ]] && exit 0
if [ "$BRANCH_NAME" = "master" ]; then
APPEND="[master] "
else
TICKET=`echo "$BRANCH_NAME" | sed 's/\([A-Z]\+-[0-9]\+\|build\).*/\1/'`
[ -z "$TICKET" -o "$TICKET" = "$BRANCH_NAME" ] && exit 0
APPEND="[$TICKET] "
fi
[[ "${MSG:0:${#APPEND}}" == "$APPEND" ]] && exit 0
sed -i.bak -e "1s/^/${APPEND}/" $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment