Skip to content

Instantly share code, notes, and snippets.

@dberstein
Last active November 12, 2023 09:01
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dberstein/dcc50e171163c3f6e0f23b2b5de5dd49 to your computer and use it in GitHub Desktop.
Save dberstein/dcc50e171163c3f6e0f23b2b5de5dd49 to your computer and use it in GitHub Desktop.
Git commit hook that prepends message with Jira issue(s) found in branch name (PR branch) otherwise requires message contains at least one Jira issue

With this commit-msg git hook and your branch names have Jira reference(s), your commit messages will be automatically updated to include any missing reference(s) too.

Installation

Place contents of this gist's commit-msg file into your checkout's .git/hooks/commit-msg file and make it executable.

Bash

cd path/to/your/git/checkout \
&& install -vbm 755 <(curl -s https://gist.githubusercontent.com/dberstein/dcc50e171163c3f6e0f23b2b5de5dd49/raw/5e5372ff22a872321ad1f5469a4d579c15ce498a/commit-msg) "$(git rev-parse --git-dir)/hooks/commit-msg"

Zsh

cd path/to/your/git/checkout \
&& install -vbm 755 =(curl -s https://gist.githubusercontent.com/dberstein/dcc50e171163c3f6e0f23b2b5de5dd49/raw/5e5372ff22a872321ad1f5469a4d579c15ce498a/commit-msg) "$(git rev-parse --git-dir)/hooks/commit-msg"

Usage

  1. Create branch whose name contains one or more Jira issues (surrounded by non-word characters, regex "\b"):
git checkout -b feature/WAP-123.WAP-456.FixingPProblem
... work ...
  1. Upon commit any Jira reference not included in the commit message will be preprended.

For example, if you make commit with message fixed in this branch, full message will be WAP-123, WAP-456, fixed. If message was fix for WAP-123 full message will be WAP-456, fix for WAP-123 (only WAP-456 was missing). If message contains all Jira references of branch name commit message would not be altered.

#!/bin/sh
get_clean_message() {
egrep -o '^[^#].*' "$1"
}
# Do not allow empty messages after removing comments
[ -z "$(get_clean_message "$1")" ] && {
echo >&2 Commit message cannot be empty.
exit 1
}
# Prepend to message Jira issues found in branch name but not found within message
JIRAS_IN_BRANCH_NAME="$(git rev-parse --abbrev-ref HEAD | grep -Eio '\b[a-z]{3,}-[1-9][0-9]*\b')"
for JIRA in ${JIRAS_IN_BRANCH_NAME}; do
get_clean_message "$1" | egrep "\b${JIRA}\b" || sed -i -e "1s/^/${JIRA}, /" "$1"
done
[ ! -z "${JIRAS_IN_BRANCH_NAME}" ] && exit 0
# Branch names without Jira issues found in their name must have at leat one issue in their message
get_clean_message "$1" | egrep -qi '\b[a-z]{3,}-[1-9][0-9]*\b' || {
echo >&2 Commit message requires JIRA code.
exit 1
}
@venkatasykam
Copy link

venkatasykam commented Jul 13, 2018

I am giving commit messages without any jira issue, not getting any issues. I have followed the same steps you mentioned.

@dberstein
Copy link
Author

dberstein commented Nov 9, 2023

I am giving commit messages without any jira issue, not getting any issues. I have followed the same steps you mentioned.

Please see https://gist.github.com/dberstein/dcc50e171163c3f6e0f23b2b5de5dd49#usage . The branch name must also reference a Jira ticket. Example: in a branch named WIP-123, commits must include string "WIP-123".

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