Skip to content

Instantly share code, notes, and snippets.

@mobeigi
Created Jul 25, 2020
Embed
What would you like to do?
Git Commit Message Hook for JIRA Issue Keys
#!/bin/bash
# The script below adds the branch name automatically to
# every one of your commit messages. The regular expression
# below searches for JIRA issue key's. The issue key will
# be extracted out of your branch name
#
# A variant of grep support the -P flag for PCRE is required
#
REGEX_ISSUE_ID="((?!([A-Z0-9a-z]{1,10})-?$)[A-Z]{1}[A-Z0-9]+-\d+)"
# Find current branch name
BRANCH_NAME=$(git symbolic-ref --short HEAD)
if [[ -z "$BRANCH_NAME" ]]; then
echo "No branch name... "; exit 1
fi
# Extract issue id from branch name
ISSUE_ID=$(echo "$BRANCH_NAME" | LC_ALL=en_US.utf8 grep -o -P "$REGEX_ISSUE_ID")
# Prepend issue id if it was found
if [[ -z "$ISSUE_ID" ]]; then
echo $(cat "$1") > "$1"
else
echo "$ISSUE_ID"': '$(cat "$1") > "$1"
fi
@mobeigi

This comment has been minimized.

Copy link
Owner Author

@mobeigi mobeigi commented Jul 25, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment