Skip to content

Instantly share code, notes, and snippets.

@icetan
Last active July 4, 2019 09:56
Show Gist options
  • Save icetan/be059aab742ca301316c728bb82aabca to your computer and use it in GitHub Desktop.
Save icetan/be059aab742ca301316c728bb82aabca to your computer and use it in GitHub Desktop.
Git commit message hook for JIRA issue ID's

Jira commit message hook

Automatically add Jira issue ID to commit messages based on the current Git branch name.

Install

Install in local repo:

cp commit-msg path/to/repo/.git/hooks/commit-msg
chmod +x path/to/repo/.git/hooks/commit-msg

Global install:

mkdir -p ~/.git-template/hooks
cp commit-msg ~/.git-template/hooks/commit-msg
chmod +x ~/.git-template/hooks/commit-msg
git config --global init.templatedir ~/.git-template

When installing globally it will only work for repos that are cloned/inited after the install.

Usage

The workflow consists of checking out a branch with the Jira issue ID prefixed to the branch name.

The recognized Jira issue ID format is [A-Z]+-[0-1]+.

When creating a commit in such a branch the commit hook will atomically prefix the commit message with the Jira issue ID so that the commit can be tied to its issue in Jira.

E.g.:

git checkout -b PROJ-1-branch-description
git commit -m "Some info about this commit"
#!/usr/bin/env bash
set -e
grepit() { grep -Eo '^[A-Z]{1,}-[0-9]{1,}\b' "$@"; }
grepid() { grepit -q "$1"; }
grepid "$1" || {
branch_name=$(git rev-parse --abbrev-ref HEAD 2>&-)
grepid <(echo $branch_name) && {
jid=$(grepit <(echo "$branch_name "))
sed -i"" "1s/^/$jid /" "$1"
}
} || exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment