Git hooks - bonkey/zalando-commerce-ios GitHub Wiki

commit-msg

It will prepend all the commit messages with an issue number taken from branch pattern issue-NUMBER, under following conditions:

  1. NUMBER in branch name exists
  2. There's no number in the commit message already

Results

branch orig. message result changed?
issue-123 quick fix #123: quick fix yes
quick-fix very, very quick fix very, very quick fix no
issue-123 #599: very, very quick fix #599: very, very quick fix no
quick-fix #599: very, very quick fix #599: very, very quick fix no

Installation

Put it in [repository_root]/.git/hooks/commit-msg and make it executable: chmod +x commit-msg.

#!/bin/bash

MSGFILE=$1
TMPFILE=$1.tmp

LINE1=$(head -1 "${MSGFILE}")
echo "${LINE1}" | grep -q -E "^#[0-9]+:?\s" && exit 0

BRANCHID=$(git symbolic-ref HEAD | sed -E 's:.*/(issue-)*([0-9]*)[^/]*:\2:')
test -z "$BRANCHID" && exit 0

echo "#${BRANCHID}: ${LINE1}" > $TMPFILE
tail +2 "${MSGFILE}" >> "${TMPFILE}"
mv "${TMPFILE}" "${MSGFILE}"