Skip to content

Instantly share code, notes, and snippets.

@Bradshaw
Forked from jatubio/prepare-commit-msg
Last active January 20, 2022 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Bradshaw/3874d38bde8f93d76ee4821685bd7bc5 to your computer and use it in GitHub Desktop.
Save Bradshaw/3874d38bde8f93d76ee4821685bd7bc5 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, put this inside `.git/hooks/prepare-commit-msg`
# Make this script executable by Git with `chmod +x .git/hooks/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 main release 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
printf "\n\nbranch: %s" "$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
echo $firstline
echo $secondline
cat $COMMIT_MSG
sed -i '1 a\\' $COMMIT_MSG
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment