Created
May 17, 2022 14:48
-
-
Save tdgrunenwald/726e54875972b2e5a0960d0a02187860 to your computer and use it in GitHub Desktop.
Prepend issue number to commit message without duplicating it in special cases such as amend or rebase
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 | |
# Adds JIRA issue key from branch name to commit message. | |
# Takes into account the possibility that the key is already in the message (e.g. | |
# when rebasing or amending existing commits). | |
REGEX_ISSUE_ID="((?!([A-Z0-9a-z]{1,10})-?$)[A-Z]{1}[A-Z0-9]+-\d+)" | |
get_issue_id_from_string() | |
{ | |
echo "$1" | LC_ALL=en_US.utf8 grep -o -P "$REGEX_ISSUE_ID" | |
} | |
# Find current branch name | |
BRANCH_NAME=$(git symbolic-ref --short HEAD) | |
if [[ -z "$BRANCH_NAME" ]]; then | |
echo "No branch name... "; exit 0 | |
fi | |
# Extract issue id from branch name | |
ISSUE_ID=$(get_issue_id_from_string "$BRANCH_NAME") | |
if [[ -z "$ISSUE_ID" ]]; then | |
# No issue ID found. Do nothing | |
exit 0 | |
else | |
SHORT_MSG=$(head -n 1 "$1") | |
MSG=$(cat "$1") | |
ID=$(get_issue_id_from_string "$SHORT_MSG") | |
# Prepend issue ID only if it is not already in the message | |
if [[ -z "$ID" ]]; then | |
echo -n "[$ISSUE_ID]: " > "$1" | |
echo "$MSG" >> "$1" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment