Created
February 14, 2018 16:36
-
-
Save alexshevch/680719f31e610cff3e6d8c930a078eb0 to your computer and use it in GitHub Desktop.
Automatically prepend git commit message with a branch name
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 | |
# Prepend the name of the branch only if: | |
# - branch name starts with one of the options in $BRANCH_STARTSWITH | |
# - branch name has not been manually prepended in the commit message | |
BRANCH_STARTSWITH=(dev- WIP XYZ-) | |
BRANCH_NAME=$(git symbolic-ref --short HEAD) | |
COMMIT_MESSAGE=$(cat $1) | |
PREPEND=0 | |
for PREFIX in "${BRANCH_STARTSWITH[@]}" | |
do | |
if [[ $BRANCH_NAME == $PREFIX* ]]; then | |
PREPEND=1 | |
break | |
fi | |
done | |
echo $PREPEND | |
if [ "$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