Skip to content

Instantly share code, notes, and snippets.

@berniechiu
Forked from tomohung/prepare-commit-msg
Created August 25, 2019 09:50
Show Gist options
  • Save berniechiu/5e9a249f5cd28a3957f4da6eb305452b to your computer and use it in GitHub Desktop.
Save berniechiu/5e9a249f5cd28a3957f4da6eb305452b to your computer and use it in GitHub Desktop.
git/hooks/prepare-commit-msg add branch name
#!/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".
# .git/hooks/prepare-commit-msg
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test)
fi
BRANCH_NAME=`git branch | grep '^\*' | cut -b3-`
BRANCH_IN_COMMIT=$(grep -c "$BRANCH_NAME:" $1)
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
REBASING=$(echo $BRANCH_NAME | grep 'rebasing')
RELEASE=$(echo $BRANCH_NAME | grep 'release/')
FILE=`cat "$1"`
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]] && [ -z "$REBASING" ] && [ -z "$RELEASE" ]; then
echo "$BRANCH_NAME: $FILE" > "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment