Skip to content

Instantly share code, notes, and snippets.

@hsuh
Last active February 24, 2023 15:07
Show Gist options
  • Save hsuh/ecf509cfaaa04347728f0d7885930b09 to your computer and use it in GitHub Desktop.
Save hsuh/ecf509cfaaa04347728f0d7885930b09 to your computer and use it in GitHub Desktop.
Step 1.
Rename C:\code\ifrs17\.git\hooks\prepare-commit-msg.sample
and remove .sample
Step 2.
copy/paste the following bash script into prepare-commit-msg
#!/bin/bash
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
USER_STORY_ID_LOWERCASE_REGEXP='^(us)[0-9]{6,7}'
COMMIT_MESSAGE=$(cat $1)
if [ -n "$BRANCH_NAME" ] && ! [[ "${COMMIT_MESSAGE}" =~ ${USER_STORY_ID_LOWERCASE_REGEXP} ]]; then
[[ "${BRANCH_NAME}" =~ ${USER_STORY_ID_LOWERCASE_REGEXP} ]]
sed -i.bak -e "1s/^/${BASH_REMATCH[0]}: /" $1
fi
Step 2. Test your hook.
If the hook is active, when you do $git commit -m "My awesome commit"
then it appears on git log as US12345 My awesome commit
Note: Your branch should be named properly such as feature/edi/US12345_awesomeness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment