Skip to content

Instantly share code, notes, and snippets.

@andrewle
Created January 11, 2012 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewle/1593841 to your computer and use it in GitHub Desktop.
Save andrewle/1593841 to your computer and use it in GitHub Desktop.
Automatically append a ticket number reference to git commit messages
#!/bin/bash
NUMLINES=$(cat "$1" |
sed '{
# Ignore the "References #blah" line
/^References \#[0-9]*/d
# Remove blank lines
/^$/d
# Remove lines containing only whitespace
/^[ \t]*$/d
# Remove comments that will be ignored by git
/^\#.*/d
}' |
wc -l)
if [ $NUMLINES -eq "0" ]; then
echo >&2 Aborting commit due to empty commit message.
exit 1
fi
#!/bin/bash
# Don't do anything if we're editing a commit as a result of a merge,
# rebase, amend, etc
[[ $2 != "" ]] && [[ $3 != "" ]] && exit 0
TICKET_NUM=$(git branch | grep '^\*' | awk -F '[/-]' '{print $2}')
# The ticket number must be all digits
echo $TICKET_NUM | grep "^[[:digit:]]\+$" > /dev/null
[[ "$?" == "1" ]] && exit 0
grep "References #[[:digit]]\+"
if [ "$?" == "1" ]; then
echo >> "$1"
echo "References #$TICKET_NUM" >> "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment