Last active
December 2, 2025 13:46
-
-
Save axxapy/2062b1faea3c1db93d0b to your computer and use it in GitHub Desktop.
Git hook which adds ticket number extracted from branch name (like TICKET-123_comment) to every commit message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # This hook adds ticket number from branch name to every commit message | |
| # | |
| BRANCH_NAME=$(git symbolic-ref --short HEAD) | |
| MSG=$(head -n 1 $1) | |
| # Check if commit message starts with "fixup!", if so, do nothing and exit | |
| # https://git-scm.com/docs/git-commit/2.32.0#Documentation/git-commit.txt---fixupamendrewordltcommitgt | |
| [[ "$MSG" =~ ^fixup! ]] && exit 0 | |
| [[ -z "$BRANCH_NAME" ]] && exit 0 | |
| if [[ "$BRANCH_NAME" =~ ^(master|main)$ ]]; then | |
| APPEND="[${BRANCH_NAME}] " | |
| else | |
| #Darwin comes with version of sed from dated FreeBSD. commented code works only with gnu-sed | |
| TICKET=`echo "$BRANCH_NAME" | sed 's/\([A-Z][A-Z]*[A-Z]-[0-9][0-9]*\).*/\1/'` | |
| [ -z "$TICKET" -o "$TICKET" = "$BRANCH_NAME" ] && exit 0 | |
| APPEND="[$TICKET] " | |
| fi | |
| [[ "${MSG:0:${#APPEND}}" == "$APPEND" ]] && exit 0 | |
| sed -i.bak -e "1s#^#${APPEND}#" $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment