Skip to content

Instantly share code, notes, and snippets.

@patrickmch
Last active December 27, 2018 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickmch/b3764cdcdffbe76dfb8d3c3bb679d394 to your computer and use it in GitHub Desktop.
Save patrickmch/b3764cdcdffbe76dfb8d3c3bb679d394 to your computer and use it in GitHub Desktop.
commit-msg
#!/bin/bash
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)
# make sure there isn't already a "fixes ISSUE_NUMBER" line in message (ex. if it's an amend)
ISSUE_NUM_RE="[Ff]ixes #[0-9]{4}"
if ! [[ "$(cat $1)" =~ $ISSUE_NUM_RE ]] ;then
cat "$1" > /tmp/out && mv /tmp/out "$1"
echo -ne "\nfixes #$ISSUE_NUMBER" >> "$1"
fi
fi
@patrickmch
Copy link
Author

ISSUE_NUM_RE needs to be a variable. Everything after # will be evaluated as a comment if that expression isn't surrounded by quotes, and newer bash versions handle quotes a bit strangely. See this SO answer.

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