Skip to content

Instantly share code, notes, and snippets.

@fosemberg
Created November 6, 2019 08:55
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 fosemberg/69685caf5a1b9a5555e8c7533266cdd0 to your computer and use it in GitHub Desktop.
Save fosemberg/69685caf5a1b9a5555e8c7533266cdd0 to your computer and use it in GitHub Desktop.
git hook commit-msg (path to insert: .git\hooks\)
#!/bin/sh
#
# Hook script to prepend the commit log message with a branch name
# Not prepend the name of the branch only if:
# - branch name starts with one of the options in $NO_PREFIX_IF_BRANCH_STARTSWITH
# - commit message starts with one of the options in $NO_PREFIX_IF_THIS_IN_COMMIT_MESSAGE
# - branch name has been manually prepended in the commit message
# lnk: https://gist.github.com/shevaroller/680719f31e610cff3e6d8c930a078eb0
# edited by fosemberg
NO_PREFIX_IF_BRANCH_STARTSWITH=(master develop)
NO_PREFIX_IF_THIS_IN_COMMIT_MESSAGE=(Merge)
BRANCH_NAME=$(git symbolic-ref --short HEAD)
COMMIT_MESSAGE=$(cat $1)
IS_PREPEND=1
for PREFIX in "${NO_PREFIX_IF_BRANCH_STARTSWITH[@]}"
do
if [[ $BRANCH_NAME == $PREFIX* ]]; then
IS_PREPEND=0
break
fi
done
for PREFIX in "${NO_PREFIX_IF_THIS_IN_COMMIT_MESSAGE[@]}"
do
if [[ $COMMIT_MESSAGE == $PREFIX* ]]; then
IS_PREPEND=0
break
fi
done
if [ "$IS_PREPEND" -eq 1 ] && [ -n "$BRANCH_NAME" ] && ! [[ $COMMIT_MESSAGE == $BRANCH_NAME* ]]; then
sed -i.bak -e "1s/^/$BRANCH_NAME /" $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment