Skip to content

Instantly share code, notes, and snippets.

@tomohung
Last active March 28, 2023 03:57
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 tomohung/adf3e2de918dc154879040b82a9afc52 to your computer and use it in GitHub Desktop.
Save tomohung/adf3e2de918dc154879040b82a9afc52 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 deploy/production)
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
@tomohung
Copy link
Author

tomohung commented Sep 28, 2018

curl https://gist.githubusercontent.com/tomohung/adf3e2de918dc154879040b82a9afc52/raw/a73f9f84ebc9f8a9c2289d05a717b2a135cff86d/prepare-commit-msg > .git/hooks/prepare-commit-msg && chmod u+x .git/hooks/prepare-commit-msg

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