Created
January 30, 2016 18:01
-
-
Save lzilioli/e813a649621209141754 to your computer and use it in GitHub Desktop.
git `prepare-commit-msg` - prepend branch name when branch matches `PROJECT-[0-9]+`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BRANCH_NAME=$(git symbolic-ref --short HEAD) | |
BRANCH_NAME="${BRANCH_NAME##*/}" | |
BRANCH_MATCHES_PATTERN=0 | |
if [[ $BRANCH_NAME == PROJECT-* ]] | |
then | |
BRANCH_MATCHES_PATTERN=1 | |
fi | |
PREPEND_VAL=$(echo $BRANCH_NAME | grep -Eow 'PROJECT-[0-9]+') | |
MSG_ALREADY_THERE=0 | |
if [[ -z "$PREPEND_VAL" ]] | |
then | |
# This is the case where the branch doesn't match the pattern | |
MSG_ALREADY_THERE=1 | |
else | |
head -1 $1 | grep $PREPEND_VAL > /dev/null | |
if [[ $? -eq 0 ]] | |
then | |
MSG_ALREADY_THERE=1 | |
fi | |
fi | |
if [ -n "$BRANCH_NAME" ] && [[ $BRANCH_MATCHES_PATTERN -eq 1 ]] && [[ $MSG_ALREADY_THERE -eq 0 ]]; then | |
sed -i.bak -e "1s/^/[$PREPEND_VAL] /" $1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment