Snippets

Atlassian Prepare Commit With JIRA Issue

Created by Jonathan Doklovic last modified Pramod Busam
#!/bin/sh
#
# git prepare-commit-msg hook for automatically prepending an issue key
# from the start of the current branch name to commit messages.

# check if commit is merge commit or a commit ammend
if [ $2 = "merge" ] || [ $2 = "commit" ]; then
    exit
fi
ISSUE_KEY=`git branch | grep -o "\* \(.*/\)*[A-Z]\{2,\}-[0-9]\+" | grep -o "[A-Z]\{2,\}-[0-9]\+"`
if [ $? -ne 0 ]; then
    # no issue key in branch, use the default message
    exit
fi
# issue key matched from branch prefix, prepend to commit message
TEMP=`mktemp /tmp/commitmsg-XXXXX`
(echo "$ISSUE_KEY: $(cat  $1)") > $TEMP
cat $TEMP > $1

Comments (8)

  1. Jonathan Doklovic

    This commit hook will extract a Jira issue key from your branch name (you are naming your branches with issue key’s aren’t you?) and append it to all commit messages so that many places across our products can glue commits together with issues.

    To use this:

    1. make sure the folder(s) ~/.git_template/hooks exists
    2. drop this file in there and make sure it’s named prepare-commit-msg
    3. make ~/.git_template/hooks/prepare-commit-msg executable. (chmod +x)
    4. make sure your ~/.gitconfig contains
      [init]
      templatedir = ~/.git_template

    Now anytime you checkout a repo OR use git init in a directory, the prepare-commit-msg will be copied into your project’s .git/hooks folder.

    Note: You can safely run git init within pre-existing git projects to get the file copied over

    1. Brett Taylor

      On Windows, it might not be clear where the `~` folder is. Ultimately, this folder is where Git creates your user global .git_config file. You probably have a global config.

      git config --global --list --show-origin will show you a list of existing config options and where they were found, including the filename.

      Once you found where your .git_config file is, follow the above instructions. I did not need to change the proposed templatedir value in step 4, nor the shebang in the file, but your mileage may vary.


      and… if the script isn’t running, double check your settings for core.hooksPath interfering:
      git config --show-origin core.hooksPath

  2. Joshua Jacobson

    Hi Jonathan. Works great. I had to add another step between steps 2 and 3 to make ~/.git_template/hooks/prepare-commit-msg executable.

  3. Abhishek Madhu

    I like this a lot. This helps me not waste my time on management and issue tracking.

    But for some reason, there is a project that I have to work on in a Windows-10 environment.

    Can you please point me in the direction to implement something like this here? It is sad to have to leave this.

    Thanks in advance.

  4. Erwin Vrolijk

    Instead of creating a temporary file with mktemp I would inline update the draft commit file like this:

    #!/bin/sh
    #
    # git prepare-commit-msg hook for automatically prepending an issue key
    # from the start of the current branch name to commit messages.
    
    # check if commit is merge commit or a commit ammend
    if [ $2 = "merge" ] || [ $2 = "commit" ]; then
        exit
    fi
    ISSUE_KEY=`git branch | grep -o "\* \(.*/\)*[A-Z]\{2,\}-[0-9]\+" | grep -o "[A-Z]\{2,\}-[0-9]\+"`
    if [ $? -ne 0 ]; then
        # no issue key in branch, use the default message
        exit
    fi
    # issue key matched from branch prefix, prepend to commit message
    sed -i -e "1s/^/$ISSUE_KEY /" $1
    

  5. Vytenis Kučiauskas

    To add this automatically to hooks I use NPM

    npm init -y
    npm install -D husky jira-prepare-commit-msg
    

    Then modify package.json and add the following

    {
      "husky": {
        "hooks": {
          "prepare-commit-msg": "jira-prepare-commit-msg"
        }
      },
      "jira-prepare-commit-msg": {
        "messagePattern": "$J $M"
      }
    }
    

    More info: https://github.com/bk201-/jira-prepare-commit-msg