Skip to content

Instantly share code, notes, and snippets.

@wingleung
Last active April 19, 2024 02:51
Show Gist options
  • Save wingleung/1b6efce388b81f5554ca to your computer and use it in GitHub Desktop.
Save wingleung/1b6efce388b81f5554ca to your computer and use it in GitHub Desktop.
Automatically prepend JIRA issue key to commit message
#!/bin/sh
#
# Automatically adds Jira key to commit message
#
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_JIRA_KEY=$(echo $BRANCH_NAME | grep -E -o '^([A-Z]+-[0-9]+)')
if ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $(cat "$1") == "$BRANCH_JIRA_KEY"* ]] && ! [ -z "$BRANCH_JIRA_KEY" ]; then
echo "$BRANCH_JIRA_KEY $(cat "${1}")" > "$1"
fi
@wingleung
Copy link
Author

wingleung commented Dec 12, 2014

Automatically add JIRA issue key from branch name to commit message.

It will:
... get the JIRA issue key of the branch
... only prepend the JIRA key if branch name is not blacklisted AND jira issue key isn't already in the commit message

Install

  • In your git repository go to .git/hooks/
  • Make a copy of prepare-commit-msg.sample and rename it to prepare-commit-msg
  • Paste in the code

Install globally

This will enable the prepend functionality for all your local git repos

  • Create a global ~/git-templates/hooks folder in your home directory
  • Add this prepare-commit-msg file in this hooks folder, make sure to remove the extension
  • Point your git hooks config to this folder: git config --global core.hooksPath ~/.git-templates/hooks

Credit

Credits go to @bartoszmajsak for the script this is based on.
https://gist.github.com/bartoszmajsak/1396344

@wingleung
Copy link
Author

Updated to support branches from git flow (eg. feature/JIRA-0000-Branch-name)

@wingleung
Copy link
Author

Updated to use a regex to get the jira issue key.
Jira keys are selected by the following regex ^([A-Z]+-[0-9]+)

  • Begins with 1 or more uppercase letters
  • followed by a hyphen '-'
  • followed by 1 or more numbers

@v-sh
Copy link

v-sh commented Nov 30, 2017

This script ingnores newlines and merges averything in one line if you don't use git commit -m, we changed to this line:

echo -e "[$BRANCH_JIRA_KEY] $(cat "${1}")" > "$1"

@mcascone
Copy link

this is fantastic, thanks!

@pnmcosta
Copy link

pnmcosta commented Mar 26, 2020

Hi, new job requires JIRA ticket in every single commit, and this is working great, thank you so much.

I do have a question, is it expected for some prompts to open up, about 3 or 4 times, when I commit now? I'm on Windows 7 with git version 2.17.1.

I've never had to use git hooks before and I setup this locally for a single project.

Thanks,
P.

@mcascone
Copy link

@pnmcosta not really. What editor are you using?

Also, can you upgrade off Win7?

@pnmcosta
Copy link

pnmcosta commented Apr 3, 2020

VS2017, and a locked down Win7 too, I wish I could upgrade but it's a firmwide setup! I've begged, no result yet 😂

@mcascone
Copy link

mcascone commented Apr 3, 2020

Windows 7 is EOL and unsupported, which means it's full of security holes, which might be leverage to get it updated: https://support.microsoft.com/en-us/help/4057281/windows-7-support-ended-on-january-14-2020

I might also suggest a lighter-weight editor for commit messages - the lightest, actually: vim or vi is great for the quick stuff needed for them. It's a bit of a learning curve but the few commands you really need to know are easily memorized.

@pnmcosta
Copy link

pnmcosta commented Apr 8, 2020

Unfortunately we're one of the firms that extended the support 😿

@mcascone
Copy link

mcascone commented Apr 8, 2020

Ok then, let's work with what we have. What prompts are opening? Do you get to the editor eventually?

@pnmcosta
Copy link

pnmcosta commented Apr 9, 2020

Command prompts flashes, can't really see what they are and/or doing. But don't worry, I can't really change editor, and it's probably related to the way git handles the hooks on my setup. So I'll have to live with it. Thanks anyway.

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