Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 13, 2014

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

@wingleung
Copy link
Author

wingleung commented Dec 13, 2014

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

mcascone commented May 20, 2019

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

mcascone commented Mar 31, 2020

@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