Skip to content

Instantly share code, notes, and snippets.

@alexshevch
Created February 14, 2018 16:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alexshevch/680719f31e610cff3e6d8c930a078eb0 to your computer and use it in GitHub Desktop.
Save alexshevch/680719f31e610cff3e6d8c930a078eb0 to your computer and use it in GitHub Desktop.
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