Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically prepend git commit message with a branch name
#!/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
You can’t perform that action at this time.