3

We would like to ensure that every commit message have a Jira ticket number in the subject. For instance, it should be something like "MA-12: Fixed issue about ...".

I know that this can easily be done on client side using commit-msg hook. But this will not be automatically setup for all developers. Is there any way we could do this on server side ?

Simon Rolin
  • 940
  • 13
  • 34

1 Answers1

3

You could setup an update hook on the server side, similar to this script (by Matthias Hryniszak padcom):

If the commit message received doesn't respect the right policy, the push will be rejected.

#!/bin/bash

refname="$1"
oldrev="$2"
newrev="$3"
result=0

# Make sure we handle the situation when the branch does not exist yet
if ! [ $oldrev = 0000000000000000000000000000000000000000 ] ; then
    excludes=( ^$oldrev )
else
    excludes=( $(git for-each-ref --format '^%(refname:short)' refs/heads/) )
fi

# Get the list of incomming commits
commits=`git rev-list $newrev "${excludes[@]}"`

# For every commit in the list
for commit in $commits
do
  # check the log message for ticket number
  message=`git log --format=%s -1 $commit`
  ticket=`echo "$message" | grep -o "^[A-Z]\{2,3\}-[0-9]\+"`
  if [ "$ticket" = "" ] ; then
    echo "Commit $commit does not start with a ticket number"
    result=1
  fi
done

exit $result
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250