Skip to content

Instantly share code, notes, and snippets.

@aaronlab
Last active October 7, 2021 12:13
Show Gist options
  • Save aaronlab/c9bf0021d7e44cd06963b586bcf3ce3f to your computer and use it in GitHub Desktop.
Save aaronlab/c9bf0021d7e44cd06963b586bcf3ce3f to your computer and use it in GitHub Desktop.
prepare-commit-msg
#!/usr/bin/env bash
# .git/hooks/prepare-commit-msg
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop release hotfix)
fi
PROJECT_ID=RPL
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
JIRA_ID=`echo $BRANCH_NAME | egrep -o "$PROJECT_ID-[0-9]+"`
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
COMMIT_MSG_HEAD=$(head $1 -n 1)
BRANCH_IN_COMMIT=$(grep -c $COMMIT_MSG_HEAD)
if [ -n $JIRA_ID ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/$JIRA_ID /" $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment