Last active
November 20, 2024 17:09
-
-
Save runswithd6s/e70c9709330d4f21c4d0119c7ec0563a to your computer and use it in GitHub Desktop.
Ensure the Jira slug is in the commit summary line
This file contains 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
#!/usr/bin/env bash | |
# -*- mode: sh; -*- | |
# | |
# commit-msg | |
# | |
# Ensure that the commit message has the correct summary line with Jira slug. | |
# | |
# See also: git-hook(1), githooks(5) | |
# | |
set -euo pipefail | |
COMMIT_MSG_FILE="$1" | |
COMMIT_MSG=`head -n1 $COMMIT_MSG_FILE` | |
PATTERN="^([A-Z][A-Z0-9]+-[0-9]+)" | |
if [[ ! ${COMMIT_MSG} =~ $PATTERN ]] | |
then | |
echo "ERROR! Commit message lacks Jira slug." >&2 | |
BRANCH_NAME=$(git symbolic-ref --short HEAD) | |
if [[ $BRANCH_NAME =~ $PATTERN ]] | |
then | |
echo "Branch name matches Jira slug. Prepending to commit summary." >&2 | |
sed -i '' "1s/^/${BASH_REMATCH[1]} /" "$COMMIT_MSG_FILE" | |
else | |
echo "ERROR! Bad commit message. Add a Jira slug and retry." >&2 | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment