#!/bin/bash # To update or install in a new repository, run the following command # N=.git/hooks/prepare-commit-msg ; curl -L https://goo.gl/5Mmuoi -o $N --create-dirs; chmod 744 $N # Assuming all branches are created as `WWW-nnn-Human-readable-suffixes` # this commit-msg hook will prepend all commit messages with the ticket # name/number, for example: # Branch: ADV-007-License-to-kill # Message: Bad guy has been identified. # Prepends: ADV-007: # Result: ADV-007: Bad guy has been identified. # This way you can customize which branches should be skipped when # prepending commit message. (Untested after implementing trimming) if [ -z "$BRANCHES_TO_SKIP" ]; then BRANCHES_TO_SKIP=(master develop test) fi # Get the full branch name BRANCH_NAME=$(git symbolic-ref --short HEAD) # Chop of sub-dirs (like `feature/`) BRANCH_NAME="${BRANCH_NAME##*/}" # Keep only ticket-number, ditch the human readable part i.e. `ADV-000` BRANCH_NAME=$(echo $BRANCH_NAME | sed -e 's:^\([^-]*-[^-]*\)-.*:\1:' -e \ 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/') BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$") BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1) if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then sed -i.bak -e "1s/^/$BRANCH_NAME: /" $1 fi