Skip to content

Instantly share code, notes, and snippets.

@rpbaltazar
Created July 18, 2022 06:47
Show Gist options
  • Save rpbaltazar/bdbdff3ee56a63db9e5001cdce03fdeb to your computer and use it in GitHub Desktop.
Save rpbaltazar/bdbdff3ee56a63db9e5001cdce03fdeb to your computer and use it in GitHub Desktop.
git hook to add ticket number in the commit
init:
find .git/hooks -type l -exec rm {} \;
find .githooks -type f -exec ln -sf ../../{} .git/hooks/ \;
# inside .githooks/prepare-commit-msg
#!/bin/bash
FILE=$1
MESSAGE=$(cat $FILE)
TICKET=[$(git rev-parse --abbrev-ref HEAD | grep -Eo --color=never '^(\w+/)?(\w+[-_])?[0-9]+' | grep -Eo --color=never '(\w+[-])?[0-9]+')]
if [[ $TICKET == "[]" || "$MESSAGE" == "$TICKET"* ]];then
exit 0;
fi
echo "$TICKET $MESSAGE" > $FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment