Last active
February 14, 2021 12:50
-
-
Save plamentotev/87ce0110bb7b438d91d27e6c5e0b006c to your computer and use it in GitHub Desktop.
Git hook that checks if an issue was linked to the commit. If not, adds default issue reference and marks the commit as draft. To use - save as .git\hooks\commit-msg and make it executable
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
#!/usr/bin/bash | |
# Git hook that checks if an issue was linked to the commit. | |
# If not, adds default issue reference and marks the commit as draft. | |
ISSUE_KEYWORD='JIRA' | |
DEFAULT_ISSUE='NA' | |
DRAFT_KEYWORD='DRAFT' | |
# File containing the commit message is passed as first argument | |
COMMIT_MESSGAE_FILE="$1" | |
COMMIT_TITLE=$(cat "$COMMIT_MESSGAE_FILE" | head --lines 1) | |
LINKED_ISSUE=$(git interpret-trailers --parse --trim-empty "$COMMIT_MESSGAE_FILE" | grep "$ISSUE_KEYWORD") | |
# If there is no issue referenced | |
if [ -z "$LINKED_ISSUE" ]; then | |
# Add default issue reference | |
git interpret-trailers --in-place --if-exists replace --trailer "$ISSUE_KEYWORD: $DEFAULT_ISSUE" "$COMMIT_MESSGAE_FILE" | |
# Insert the draft keyword if it is not added already | |
if [[ ! "$COMMIT_TITLE" =~ ^\[$DRAFT_KEYWORD\] ]]; then | |
sed --in-place "1 s/^/[$DRAFT_KEYWORD] /" "$COMMIT_MESSGAE_FILE" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment