Skip to content

Instantly share code, notes, and snippets.

@paleite
Created April 15, 2021 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paleite/0d0d4c9c77169c7380996d206fbd4b18 to your computer and use it in GitHub Desktop.
Save paleite/0d0d4c9c77169c7380996d206fbd4b18 to your computer and use it in GitHub Desktop.
Automatically prepend the JIRA issue-key to the commit message
#!/bin/sh
# Automatically prepend the JIRA issue-key to the commit message
readonly COMMIT_MESSAGE_PATH="$1"
readonly COMMIT_TYPE="$2"
readonly GIT_BRANCH=$(git branch --show-current)
# Pick the last issue key in a branch name or return empty result.
# - "feature/AB-123-is-not/the/AB-987-last" will yield "AB-987"
# - "main" will yield ""
readonly ISSUE_KEY=$(node -e 'console.log(process.argv.pop().split("/").reduce(((e,o)=>(o=/^[A-Z]{2,}-\d+/.exec(o)?.pop())?o:e),""))' "${GIT_BRANCH}")
# Don't change merge-commit messages
if [ "$COMMIT_TYPE" = "merge" ]; then
exit
fi
if [ -z "$ISSUE_KEY" ]; then
echo "$(tput setaf 3)warning$(tput sgr0) No issue key found in branch name $(tput bold)${GIT_BRANCH}$(tput sgr0)"
exit
fi
# Issue key already prepended
if grep -q "^${ISSUE_KEY}\b" "$COMMIT_MESSAGE_PATH"; then
exit
fi
# Issue key required; prepend to commit message
echo "$(tput setaf 4)info$(tput sgr0) Prepending commit message with issue key $(tput bold)${ISSUE_KEY}$(tput sgr0)"
sed -i -e "1s/^/${ISSUE_KEY}: /" "$COMMIT_MESSAGE_PATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment