Created
December 27, 2018 18:19
-
-
Save patrickmch/bd862c881f79aa4d1dcfaa4caea9d40f to your computer and use it in GitHub Desktop.
prepare-commit-msg
This file contains 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 | |
# This adds "fixes #0000" to the third line of the commit message automatically | |
# if there is an issue number in front of the branch name. | |
BRANCH_PATH=$(git symbolic-ref -q HEAD) # Something like refs/heads/my-branch-name | |
BRANCH_NAME=${BRANCH_PATH##*/} # Get text behind the last / of the branch path | |
# Make sure we have an issue number in front before doing anything | |
if [[ $BRANCH_NAME =~ (^[0-9]{4}) ]] ;then | |
ISSUE_NUMBER=${BASH_REMATCH[1]} | |
FIRST_LINE=$(head -n1 $1) | |
# Check that this is not an amend by checking that the first line is empty: | |
if [ -z "$FIRST_LINE" ] ;then | |
# Insert "fixes #ISSUE_NUMBER" on the third line of the commit message": | |
echo -ne "\n\nfixes #$ISSUE_NUMBER"|cat - "$1" > /tmp/out && mv /tmp/out "$1" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment