Skip to content

Instantly share code, notes, and snippets.

@akash1233
Last active June 20, 2024 12:12
Show Gist options
  • Save akash1233/5f4ac0c9c5ab17ba65cb1f75e8d88f4e to your computer and use it in GitHub Desktop.
Save akash1233/5f4ac0c9c5ab17ba65cb1f75e8d88f4e to your computer and use it in GitHub Desktop.
pre-receive git hooks for checking the commit message
#!/bin/bash
#
# check commit messages for pivotal tracker issue numbers formatted as [#stort-id]along with commit message
REGEX="\[#[0-9]*\]"
ERROR_MSG="[POLICY] The commit doesn't reference a PIVOTAL tracker issue"
while read OLDREV NEWREV REFNAME ; do
for COMMIT in `git rev-list $OLDREV..$NEWREV`;
do
MESSAGE=`git cat-file commit $COMMIT | sed '1,/^$/d'`
if ! echo $MESSAGE | grep -iqE "$REGEX"; then
echo "$ERROR_MSG: $MESSAGE" >&2
exit 1
fi
done
done
exit 0
@Red3Tango
Copy link

I found this extremely helpful, searched for hours before I found this!!

I wanted to only check the first commit line, adding q to the sed command only checks the commit 'message' and not the 'description'
sed '1,/^$/d;q'

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