Last active
November 16, 2023 10:58
-
-
Save davidecaruso/fa3aa55742590aa177886b9a7d853f49 to your computer and use it in GitHub Desktop.
Git hook to prefix commit with Jira ticket
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 | |
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) | |
if [ -n "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "HEAD" ] && [ "$SKIP_PREPARE_COMMIT_MSG" != 1 ]; then | |
PREFIX_PATTERN='[A-Z]{2,}\-[0-9]{2,}' | |
[[ $BRANCH_NAME =~ $PREFIX_PATTERN ]] | |
PREFIX=${BASH_REMATCH[0]} | |
PREFIX_IN_COMMIT=$(grep -c "^$PREFIX\:" "$1") | |
if [[ -n "$PREFIX" ]] && ! [[ $PREFIX_IN_COMMIT -ge 1 ]]; then | |
sed -i.bak -e "1s~^~$PREFIX: ~" "$1" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An alternative may be: