Last active
September 3, 2019 10:07
-
-
Save frandieguez/4714f0809ca7e9257a0b058e9b941d64 to your computer and use it in GitHub Desktop.
Prepend ticket id to commits with husky and git hooks
This file contains 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
{ | |
"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" | |
} |
This file contains 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 | |
# | |
# 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