Skip to content

Instantly share code, notes, and snippets.

@arranzropablo
Created February 2, 2024 16:02
Show Gist options
  • Save arranzropablo/c8d95614d4ea059f603afc216ca151b6 to your computer and use it in GitHub Desktop.
Save arranzropablo/c8d95614d4ea059f603afc216ca151b6 to your computer and use it in GitHub Desktop.
prepare-commit-msg git hook
#!/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
@arranzropablo
Copy link
Author

pretty useful for every company I've been to, always similar commit pattern

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment