Last active
April 9, 2017 09:56
-
-
Save rm--/f2954c9eed5d8feadf71e1ce67044e30 to your computer and use it in GitHub Desktop.
add issue number from branch to commit msg
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/bash | |
| # Extact issue tag (e.g. feature/TAG-123 -> TAG-123, ANOTHERTAG-123-description -> ANOTHERTAG-123) | |
| fetch_issue_tag() { | |
| git rev-parse --abbrev-ref HEAD | grep -e '[A-Z]\+-[0-9]\+' -o | |
| } | |
| BRANCH_NAME=$( git branch | grep '*' | sed 's/* //' ) | |
| ISSUE_TAG=$( fetch_issue_tag ) | |
| FIRSTLINE="$( head -n 1 "$1" )" | |
| BODY="$( tail -n +2 "$1" )" | |
| # Skip editing the commit message when rebasing. | |
| if [[ "$BRANCH_NAME" != '(no branch'* ]] | |
| then | |
| # Append the issue tag if the issue tag is in the branch | |
| # and not already in the commit message. | |
| if [[ -z "${FIRSTLINE//}" ]] | |
| then | |
| if [[ -n "$ISSUE_TAG" ]] | |
| then | |
| NEW_MESSAGE="$ISSUE_TAG: \n$BODY" | |
| printf "$NEW_MESSAGE" > "$1" | |
| else | |
| NEW_MESSAGE="# Issue tag? \n$BODY" | |
| printf "$NEW_MESSAGE" > "$1" | |
| fi | |
| fi | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
chmod +x .git/hooks/prepare-commit-msg