Skip to content

Instantly share code, notes, and snippets.

@lzilioli
Created January 30, 2016 18:01
Show Gist options
  • Save lzilioli/e813a649621209141754 to your computer and use it in GitHub Desktop.
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]+`
#!/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