Skip to content

Instantly share code, notes, and snippets.

@gmoraleda
Created May 23, 2024 11:29
Show Gist options
  • Save gmoraleda/db05a502af609864db25003b3326df88 to your computer and use it in GitHub Desktop.
Save gmoraleda/db05a502af609864db25003b3326df88 to your computer and use it in GitHub Desktop.
Prepare commit message using conventional commits
#!/bin/bash
# Format commits using JIRA issue ID and conventional commits.
# E.g.
# Branch name: fix/ABC-123/bug-fixing
# Commit message: Bug fixed
# Formatted commit message: fix: [ABC-123] Bug fixed
REGEX_ISSUE_ID="[a-zA-Z0-9,\.\_\-]+-[0-9]+"
REGEX_ISSUE_TYPE="[a-z]+"
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
if [ "$BRANCH_NAME" != 'HEAD' ]; then
ISSUE_TYPE=$(echo "$BRANCH_NAME" | grep -o -E "$REGEX_ISSUE_TYPE" | head -1)
ISSUE_ID=$(echo "$BRANCH_NAME" | grep -o -E "$REGEX_ISSUE_ID")
if [[ -z "$ISSUE_ID" ]]; then
echo "$ISSUE_TYPE: $(cat $1)" >"$1"
else
echo "$ISSUE_TYPE: [$ISSUE_ID] $(cat $1)" >"$1"
fi
echo " " >>"$1"
echo "$TAG" >>"$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment