Skip to content

Instantly share code, notes, and snippets.

@thealexbaron
Forked from bartoszmajsak/prepare-commit-msg.sh
Last active November 16, 2017 16:40
Show Gist options
  • Save thealexbaron/fd2a390dc6f8958d1b1ffe8b66b676bb to your computer and use it in GitHub Desktop.
Save thealexbaron/fd2a390dc6f8958d1b1ffe8b66b676bb to your computer and use it in GitHub Desktop.
How to automatically prepend git commit with a branch name
#!/bin/bash
# per @madeindjs's suggestion:
# For a quick install, into your project folder enter this command line:
#
# curl https://gist.githubusercontent.com/thealexbaron/fd2a390dc6f8958d1b1ffe8b66b676bb/raw/5f5ed22c281a8488116a41e4496075f8b28d106a/prepare-commit-msg.sh > .git/hooks/prepare-commit-msg && chmod u+x .git/hooks/prepare-commit-msg
#
# which does:
# - get Gist
# - copy into .git/hooks/prepare-commit-msg file
# - make it executable
#
# forked from https://gist.github.com/bartoszmajsak/1396344/97081e76ab275f5fe526347908503febd1340495
# This way you can customize which branches should be skipped when
# prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master dev ec_rc ec_master)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; 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