Skip to content

Instantly share code, notes, and snippets.

@patrickmch
Created December 27, 2018 18:19
Show Gist options
  • Save patrickmch/bd862c881f79aa4d1dcfaa4caea9d40f to your computer and use it in GitHub Desktop.
Save patrickmch/bd862c881f79aa4d1dcfaa4caea9d40f to your computer and use it in GitHub Desktop.
prepare-commit-msg
#!/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