Skip to content

Instantly share code, notes, and snippets.

@pR0Ps
Last active August 29, 2015 14:09
Show Gist options
  • Save pR0Ps/ab33c11f525f93bbe85d to your computer and use it in GitHub Desktop.
Save pR0Ps/ab33c11f525f93bbe85d to your computer and use it in GitHub Desktop.
Server-side commit message validation
#!/bin/sh
read oldrev newrev refname
NULL_SHA1="0000000000000000000000000000000000000000"
revs=""
case $oldrev,$newrev in
*,$NULL_SHA1) # Deleting ref
;;
$NULL_SHA1,*) # Initial push
revs=$(git rev-list --no-merges $newrev);;
*,*) # Update
revs=$(git rev-list --no-merges $oldrev..$newrev);;
esac
bad=0
for rev in $revs; do
# Get first non-comment line of the commit message
fl=$(git cat-file commit $rev | sed '1,/^$/d' | head -1)
# Match the line against a regex
if ! [[ $fl =~ ^[A-Z]+-[0-9]{4}:\ .* ]]; then
echo "Improperly formatted commit message '$fl'"
bad=1
fi
done
if [ $bad -eq 1 ]; then
echo "Example of a valid commit message: 'PROJECT-1234: Message'"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment