14

I am looking for somehelp to write a pre-commit hook on windows to check for Jira issue key while commiting.Commit should not be allowed if Jira key is not present.I couldnt find any way.I am new to scripting.Any help would be highly appreciated.

0

3 Answers 3

13

I assume you are talking about a hooks in a Git repository.

  • Navigate to your local Git repository and go into the folder .git\hooks
  • Create a file named commit-msg
  • Insert the following content (no idea how to format it correctly)
#!/bin/bash
# The script below adds the branch name automatically to
# every one of your commit messages. The regular expression
# below searches for JIRA issue key's. The issue key will
# be extracted out of your branch name

REGEX_ISSUE_ID="[a-zA-Z0-9,\.\_\-]+-[0-9]+"

# Find current branch name
BRANCH_NAME=$(git symbolic-ref --short HEAD)

if [[ -z "$BRANCH_NAME" ]]; then
    echo "No branch name... "; exit 1
fi

# Extract issue id from branch name
ISSUE_ID=$(echo "$BRANCH_NAME" | grep -o -E "$REGEX_ISSUE_ID")

echo "$ISSUE_ID"': '$(cat "$1") > "$1"

If you have now a branch named like feature/MYKEY-1234-That-a-branch-name and add as commit message "Add a new feature" Your final commit message will look like MYKEY-1234: Add a new feature

You can put the hook globally when using Git 2.9. Please find here further useful information:

https://andy-carter.com/blog/automating-git-commit-messages-with-git-hooks

Git hooks : applying `git config core.hooksPath`

3
  • Great answer! My only suggestion is improving the REGEXP to use a better one using lookaround and PCRE flag on grep (current one matches a few false positives). Also a check for an invalid issue id not being found which processes the commit message as is.
    – Mo Beigi
    Jul 2, 2020 at 11:47
  • I made a post about this borrowing most of this solution: mobeigi.com/blog/programming/… Working great for me!
    – Mo Beigi
    Jul 25, 2020 at 13:29
  • This worked for me, but collapsed any pre-seeded, muti-line commit message into one line. Changing the last line to echo "${ISSUE_ID}: $(cat $1)" > "$1" preserves the newlines Oct 26, 2022 at 16:11
2

You can use git server-side pre-receive hook. https://git-scm.com/docs/git-receive-pack

In the code below for a successful push, you must specify the Jira issue key in the comment for commit.

#!/bin/bash
    #
    # check commit messages for JIRA issue numbers

    # This file must be named pre-receive, and be saved in the hook directory in a bare git repository.
    # Run "chmod +x pre-receive" to make it executable.
    #
    # Don't forget to change
    # - Jira id regex

    jiraIdRegex="\[JIRA\-[0-9]*\]"

    error_msg="[POLICY] The commit doesn't reference a JIRA issue"

    while read oldrev newrev refname
    do
      for sha1Commit in $(git rev-list $oldrev..$newrev);
      do
        echo "sha1 : $sha1Commit";
        commitMessage=$(git log --format=%B -n 1 $sha1Commit)


        jiraIds=$(echo $commitMessage | grep -Pqo $jiraIdRegex)

        if ! jiraIds; then
          echo "$error_msg: $commitMessage" >&2
          exit 1
        fi
      done
    done
    exit 0
2

You have to put the following script in your local Git repository at .git/hooks/prepare-commit-msg. This will be run whenever you add a new commit.

#!/bin/bash

# get current branch
branchName=`git rev-parse --abbrev-ref HEAD`

# search jira issue id in pattern
jiraId=$(echo $branchName | sed -nr 's,[a-z]*\/*([A-Z]+-[0-9]+)-.+,\1,p') 

# only prepare commit message if pattern matched and jiraId was found
if [[ ! -z $jiraId ]]; then
 # $1 is the name of the file containing the commit message
 sed -i.bak -e "1s/^/\n\n$jiraId: /" $1
fi

First, we get the branch name, for example feature/JIRA-2393-add-max-character-limit.

Next, we extract the key, removing the prefix feature.

The resulting commit message will be prefixed by "JIRA-2393: "

The script also works when there is no prefix, e.g. without feature/, bugfix/, etc.

2
  • I'm having a hard time with trying to achieve this on Windows. It just isn't working at all. Jun 4, 2021 at 18:54
  • this should work on both Windows and Linux. What problem are you running into? Jan 25, 2022 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.