Created
November 6, 2019 08:55
-
-
Save fosemberg/69685caf5a1b9a5555e8c7533266cdd0 to your computer and use it in GitHub Desktop.
git hook commit-msg (path to insert: .git\hooks\)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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