Skip to content

Instantly share code, notes, and snippets.

@yoeunes
Created June 1, 2020 11:19
Show Gist options
  • Save yoeunes/7763e95689ad026d209a93a6d89b8dd9 to your computer and use it in GitHub Desktop.
Save yoeunes/7763e95689ad026d209a93a6d89b8dd9 to your computer and use it in GitHub Desktop.
git hook to add Jira or Gitlab issue ID to commit message
#!/bin/bash
#
# Automatically adds Jira key to commit message
#
BRANCH_NAME=$(git symbolic-ref --short HEAD)
if [[ $BRANCH_NAME =~ feat/.* ]] || [[ $BRANCH_NAME =~ fix/.* ]]; then
BRANCH_ISSUE_ID="${BRANCH_NAME##*/}"
BRANCH_IN_COMMIT=$(grep -c "\[#[0-9]*\]" "$1")
if ! [[ $BRANCH_IN_COMMIT -eq 1 ]] && ! [[ $(cat "$1") == "$BRANCH_ISSUE_ID"* ]] && ! [ -z "$BRANCH_ISSUE_ID" ]; then
echo "[#$BRANCH_ISSUE_ID] $(cat "${1}")" > "$1"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment