Skip to content

Instantly share code, notes, and snippets.

@skylock
Last active February 3, 2021 08:48
Show Gist options
  • Save skylock/3bc4a63b9fcec6be405b0144ccb97394 to your computer and use it in GitHub Desktop.
Save skylock/3bc4a63b9fcec6be405b0144ccb97394 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# An example hook script to prepare the commit log message.
# Called by "git commit" with the name of the branch you're in,
# 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.
# Ex: you're on branch feature/MIT-123, and you commit your changes adding
# a message "Released 2.7 version" the actual commit message should look
# like "MIT-123 - Released 2.7 version"
#
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master development)
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