Created
February 2, 2024 16:02
-
-
Save arranzropablo/c8d95614d4ea059f603afc216ca151b6 to your computer and use it in GitHub Desktop.
prepare-commit-msg git hook
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
#!/bin/bash | |
BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
BRANCH_PREFIX=$(echo "$BRANCH" | cut -d'/' -f1) | |
JIRA_ID=$(echo "$BRANCH_PREFIX" | awk '{print toupper($1)}') | |
JIRA_ID_MATCHER="^[a-zA-Z]+-[0-9]+$" | |
NO_TICKET_MATCHER="^NO-TICKET$" | |
COMMIT_MSG=$(head -n1 "$1") | |
COMMIT_PATTERN_JIRA_ID="^\[?[a-zA-Z]+-[0-9]+\]?" | |
COMMIT_PATTERN_NO_TICKET="^\[?NO-TICKET\]?" | |
if [[ "$JIRA_ID" =~ $JIRA_ID_MATCHER || "$JIRA_ID" =~ $NO_TICKET_MATCHER ]]; then | |
if ! [[ "$COMMIT_MSG" =~ $COMMIT_PATTERN_JIRA_ID || "$COMMIT_MSG" =~ $COMMIT_PATTERN_NO_TICKET ]]; then | |
sed -i.bak -e "1s/^/[$JIRA_ID] /" "$1" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pretty useful for every company I've been to, always similar commit pattern