-
-
Save wingleung/1b6efce388b81f5554ca to your computer and use it in GitHub Desktop.
#!/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 |
Updated to support branches from git flow (eg. feature/JIRA-0000-Branch-name)
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
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"
this is fantastic, thanks!
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.
@pnmcosta not really. What editor are you using?
Also, can you upgrade off Win7?
VS2017, and a locked down Win7 too, I wish I could upgrade but it's a firmwide setup! I've begged, no result yet 😂
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.
Unfortunately we're one of the firms that extended the support 😿
Ok then, let's work with what we have. What prompts are opening? Do you get to the editor eventually?
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.
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
Install globally
This will enable the prepend functionality for all your local git repos
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