Skip to content

Instantly share code, notes, and snippets.

@Geekfish
Last active December 12, 2018 10:49
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 Geekfish/ac19370af3fe06425e899b30e4eb3e7d to your computer and use it in GitHub Desktop.
Save Geekfish/ac19370af3fe06425e899b30e4eb3e7d to your computer and use it in GitHub Desktop.
Ticket in commit msg (Github and CodebaseHQ versions)

This is a hook that adds the ticket number to the commit message, if it can be infered by the branch name. (ex. branch name bugfix/5234-fix-a-thing would result in #5234 for the github version or [t: 5234] for the codebase version.

[core]
...
hooksPath = /path/to/hooks/
#!/bin/sh
# CodebaseHQ version
LF=$'\\\x0A'
BRANCH_NAME=$(git symbolic-ref --short HEAD)
if [[ $BRANCH_NAME =~ .*\/[0-9]* ]]; then
NUMBER=$(echo $BRANCH_NAME | sed 's/[^0-9]*//g')
MESSAGE="\[t: $NUMBER\]"
MESSAGE_IN_COMMIT=$(grep -c "$MESSAGE" $1)
if [ $NUMBER ] && [ $MESSAGE_IN_COMMIT -lt 1 ]; then
sed -i.back "1s/$/$LF$LF$MESSAGE$LF/" "$1"
fi
fi
#!/bin/sh
# Github version
LF=$'\\\x0A'
BRANCH_NAME=$(git symbolic-ref --short HEAD)
if [[ $BRANCH_NAME =~ .*\/[0-9]* ]]; then
NUMBER=$(echo $BRANCH_NAME | sed 's/[^0-9]*//g')
MESSAGE="#$NUMBER"
MESSAGE_IN_COMMIT=$(grep -c "$MESSAGE" $1)
if [ $NUMBER ] && [ $MESSAGE_IN_COMMIT -lt 1 ]; then
sed -i.back "1s/$/$LF$LF$MESSAGE$LF/" "$1"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment