Skip to content

Instantly share code, notes, and snippets.

@frandieguez
Last active September 3, 2019 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frandieguez/4714f0809ca7e9257a0b058e9b941d64 to your computer and use it in GitHub Desktop.
Save frandieguez/4714f0809ca7e9257a0b058e9b941d64 to your computer and use it in GitHub Desktop.
Prepend ticket id to commits with husky and git hooks
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"husky" : {
"hooks" : {
"prepare-commit-msg" : "bash prepare-commit-msg.sh"
}
},
"author": "",
"license": "ISC"
}
#!/bin/bash
#
# Automatically adds branch name and branch description to every commit message.
# Modified from the gist here https://gist.github.com/bartoszmajsak/1396344
#
# This way you can customize which branches should be skipped when
# prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop)
fi
# Get branch name and description
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
# Branch name should be excluded from the prepend
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
TICKET_ID=$(echo "$BRANCH_NAME"| egrep -o '[A-Z]{,3}-[0-9]{,5}')
# A developer has already prepended the commit in the format [BRANCH_NAME]
BRANCH_IN_COMMIT=$(grep -c "$TICKET_ID" $1)
if [ -n "$TICKET_ID" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/$TICKET_ID: /" "$GIT_DIR/COMMIT_EDITMSG"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment