Skip to content

Instantly share code, notes, and snippets.

@edwardtheharris
Created April 25, 2023 16:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardtheharris/d84b0d208ad2d0abbb361aadb368ee78 to your computer and use it in GitHub Desktop.
Save edwardtheharris/d84b0d208ad2d0abbb361aadb368ee78 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# git prepare-commit-msg hook for automatically prepending an issue key
# from the start of the current branch name to commit messages.
# check if commit is merge commit or a commit ammend
# if [ "$2" = "merge" ] || [ "$2" = "commit" ]; then
# exit
# fi
ISSUE_KEY=$(git branch --show-current)
ISSUE_KEY="${ISSUE_KEY/[a-z]*\/}"
ISSUE_NUMBER=${ISSUE_KEY/-*[a-zA-Z]*}
TMP=$(mktemp /tmp/issuekey-XXXXX)
echo "$ISSUE_KEY" > "$TMP"
sed -E "s/[0-9]*-([A-Z]*-[0-9]*)-.*/\1/" < "$TMP"
ISSUE_KEY=$(sed -E "s/[0-9]*-([A-Z]*-[0-9]*)-.*/\1/" < "$TMP")
if [ $? -ne 0 ]; then
# no issue key in branch, use the default message
printf "exiting %s" "$ISSUE_KEY"
exit
fi
# issue key matched from branch prefix, prepend to commit message
TEMP=$(mktemp /tmp/commitmsg-XXXXX)
(printf "\n\n%s\nCloses #%s\n\nChangelog: changed" "$ISSUE_KEY $(cat "$1")" "${ISSUE_NUMBER}") > "$TEMP"
# (printf "\n\n%s" "$ISSUE_KEY $(cat $1)") > $TEMP
cat "$TEMP" > "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment