Skip to content

Instantly share code, notes, and snippets.

@l0ser140
Created June 10, 2022 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save l0ser140/80a757356d405debdf02f9bf6104ff08 to your computer and use it in GitHub Desktop.
Save l0ser140/80a757356d405debdf02f9bf6104ff08 to your computer and use it in GitHub Desktop.
Git hook adds JIRA issue to commit text
  • make sure the folder(s) ~/.git_template/hooks exists
  • drop this file in there and make sure it’s named prepare-commit-msg
  • make ~/.git_template/hooks/prepare-commit-msg executable. (chmod +x)
  • make sure your ~/.gitconfig contains
[init]
templatedir = ~/.git_template
#!/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
grep "^[A-Z]\{2,\}-[0-9]\+:" $1 > /dev/null || sed -i -e "1s/^/$ISSUE_KEY: /" $1
@Sonare
Copy link

Sonare commented Nov 13, 2022

#!/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 rev-parse --abbrev-ref HEAD | grep -o "[A-Z0-9]\+-[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
grep "^[A-Z0-9]\+-[0-9]\+" $1 > /dev/null || sed -i -e "1s/^/$ISSUE_KEY /" $1

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