Created
January 28, 2026 14:32
-
-
Save massahud/da1f6b0c8439fc46a071955da17994de to your computer and use it in GitHub Desktop.
git: add ticket id from branch name as template commit message. Original from atlassian's https://github.com/atlassian/atlascode/blob/9cd9ae156dd7666c3b71b027afe7513a17c16b12/scripts/prepare-commit-jira.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # | |
| # Original from: https://github.com/atlassian/atlascode/blob/9cd9ae156dd7666c3b71b027afe7513a17c16b12/scripts/prepare-commit-jira.sh | |
| # | |
| # git prepare-commit-msg hook for automatically prepending an issue key | |
| # from the start of the current branch name to commit messages. | |
| # 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: | |
| # | |
| # Already cloned repositories: | |
| # | |
| # copy the file into the .git/hooks directory of the cloned directory, or run git init after configuring the templates (see bellow) | |
| # | |
| # | |
| # To automatically add to hooks on git clone: | |
| # | |
| # make sure the folder(s) ~/.git-templates/hooks exists | |
| # drop this file in there and make sure it’s named prepare-commit-msg | |
| # make ~/.git-templates/hooks/prepare-commit-msg executable. (chmod +x) | |
| # 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 | |
| # | |
| # and… if the script isn’t running, double check your settings for core.hooksPath interfering: | |
| # git config --show-origin core.hooksPath | |
| # | |
| # many thanks to contributions from | |
| # @foo @Erwin Vrolijk @Joshua Jacobson @Brett Taylor @Jonathan Doklovic | |
| # 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 | |
| # check if a fixup commit, if it is, use default message | |
| FIXUP_COMMIT=`grep -o "fixup\!" "$1"` | |
| if [ $? -eq 0 ]; then | |
| exit | |
| fi | |
| # issue key matched from branch prefix, prepend to commit message | |
| 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