Skip to content

Instantly share code, notes, and snippets.

@plamentotev
Last active February 14, 2021 12:50
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 plamentotev/87ce0110bb7b438d91d27e6c5e0b006c to your computer and use it in GitHub Desktop.
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
#!/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