Skip to content

Instantly share code, notes, and snippets.

@jatubio
Last active December 30, 2022 07:36
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jatubio/4b3d50ffa8fab197789f to your computer and use it in GitHub Desktop.
Save jatubio/4b3d50ffa8fab197789f to your computer and use it in GitHub Desktop.
prepare-commit-msg git hook to add branch name to commit message
#!/bin/sh
#
# An example hook script to prepare the commit log message.
# Called by "git commit" with the name of the file that has the
# commit message, followed by the description of the commit
# message's source. The hook's purpose is to edit the commit
# message file. If the hook fails with a non-zero status,
# the commit is aborted.
#
# To enable this hook, rename this file to "prepare-commit-msg".
### http://hugogiraudel.com/2014/03/17/git-tips-and-tricks-part-2/
COMMIT_MSG=$1
COMMIT_MODE=$2
# This way you can customize which branches should be skipped when
# prepending commit message.
# http://blog.bartoszmajsak.com/blog/2012/11/07/lazy-developers-toolbox-number-1-prepend-git-commit-messages/
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop temp test)
fi
#BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
# Converts '_' to '/'. By sample: 'feature/Users_Mail' to 'feature/Users/Mail'
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //' | tr '_' '/')
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_MESSAGE=$(grep -c "\[$BRANCH_NAME\]" $1)
BRACKETS_IN_MESSAGE=$(grep -c "\[.*\]" $1)
REBASING=$(echo $BRANCH_NAME | grep 'rebasing')
# $2 is the commit mode
# if $2 == 'commit' => user used `git commit`
# if $2 == 'message' => user used `git commit -m '...'`
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_MESSAGE -ge 1 ]] && [ -z "$REBASING" ] && ! [[ $BRACKETS_IN_MESSAGE -ge 1 ]] ; then
# We check the fist line of the commit message file.
# If it's an empty string then user didn't use `git commit --amend` so we can fill the commit msg file
firstline=`head -n1 $COMMIT_MSG`
if [ "$COMMIT_MODE" = "message" ] || [ -z "$firstline" ] ; then
sed -i.bak -e "1s:^:[$BRANCH_NAME] :" $COMMIT_MSG
fi
else
if [ -z "$BRANCH_NAME" ]; then
echo 'prepare-commit-msg: Empty branch name'
fi
if [ -n "$REBASING" ]; then
echo 'prepare-commit-msg: Rebasing'
fi
if [[ $BRANCH_EXCLUDED -eq 1 ]]; then
echo 'prepare-commit-msg: Branch excluded from commit message'
fi
if [[ $BRANCH_IN_MESSAGE -ge 1 ]]; then
echo 'prepare-commit-msg: Branch already on commit message'
fi
fi
if [ -z "$REBASING" ]; then
# Add one blank line in second line if not exists
firstline=`head -n1 $COMMIT_MSG`
secondline=`head -n2 $COMMIT_MSG | tail -1`
if [ -n "$firstline" ] && [ -n "$secondline" ] && ! [ "$firstline" == "$secondline" ]; then
echo adding new line
sed -i '1 a\\' $COMMIT_MSG
fi
fi
@jatubio
Copy link
Author

jatubio commented Jun 15, 2015

Not use: BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) because returns 'Head' during a rebase and will add [head] to your new edited rebased commits.

@jatubio
Copy link
Author

jatubio commented Jun 16, 2015

On sed -i.bak -e "1s:^:[$BRANCH_NAME] :" $1 i'm using : as separator because some branch names can have / characters. You can replace : by another character.

@jatubio
Copy link
Author

jatubio commented Jun 21, 2015

Add check to do not modify message if have BRACKETS_IN_MESSAGE
Add Converts '_' to '/' in BRANCH_NAME. By sample: 'feature/Users_Mail' to 'feature/Users/Mail'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment