Skip to content

Instantly share code, notes, and snippets.

@azimin
Created November 8, 2016 15:20
Show Gist options
  • Save azimin/4aa78ce4beb332da0b48f7c36a57db6e to your computer and use it in GitHub Desktop.
Save azimin/4aa78ce4beb332da0b48f7c36a57db6e to your computer and use it in GitHub Desktop.
prepare-commit-msg that take part of 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.
#
# Converts branch name 'bugfix/APP-1234-example' and commit message 'fixed index problem' into a commit message '[APP-123] fixed index problem'
#
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
# Seems this one is working :)
[[ $BRANCH_NAME =~ ^"APP".[0-9]* ]] && BASH_REMATCH
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
if [ -n "$BRANCH_NAME" ] && [ -n "$BASH_REMATCH" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/[$BASH_REMATCH] /" $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment