Skip to content

Instantly share code, notes, and snippets.

@amirali
Last active March 26, 2025 07:28
Show Gist options
  • Select an option

  • Save amirali/ae9e546e0adefd4d5657f14bd1e1a3d7 to your computer and use it in GitHub Desktop.

Select an option

Save amirali/ae9e546e0adefd4d5657f14bd1e1a3d7 to your computer and use it in GitHub Desktop.
Adds jira task ID as commit message prefix when presented in branch name
#!/bin/bash
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
JIRA_ID=$(echo "$BRANCH_NAME" | grep -oE '[A-Z]+-[0-9]+')
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
if [[ "$COMMIT_SOURCE" == "merge" || "$COMMIT_SOURCE" == "squash" || "$COMMIT_SOURCE" == "rebase" ]]; then
exit 0
fi
# Amend commit
LAST_COMMIT_HASH=$(git rev-parse HEAD 2>/dev/null)
LAST_COMMIT_MSG=$(git log -1 --pretty=%B 2>/dev/null)
NEW_COMMIT_MSG=$(cat "$COMMIT_MSG_FILE" | egrep -v "^#")
if [ -n "$LAST_COMMIT_HASH" ] && [ "$NEW_COMMIT_MSG" = "$LAST_COMMIT_MSG" ]; then
exit 0
fi
if [[ -n "$JIRA_ID" ]]; then
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
if [[ ! "$(echo $COMMIT_MSG | egrep -v "^#")" =~ $JIRA_ID ]]; then
echo "[$JIRA_ID] $COMMIT_MSG" > "$COMMIT_MSG_FILE"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment