DEV Community

drevispas
drevispas

Posted on • Edited on

Add Jira ID and branch name in commit message

Create a file name .git/hook/prepare-commit-msg with following content.
.git/hook/prepare-commit-msg:

#!/bin/sh

# Not used for now
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3

if [ -z "$EXCLUDED_BRANCHES" ]; then
  # 이 템플릿을 사용하지 않을 브랜치 설정
  EXCLUDED_BRANCHES=(main release master develop)
fi

BRANCH_NAME=$(git symbolic-ref --short HEAD)

# 브랜치 이름을 분해한다.
BRANCH_PATTERN="(.*)/([A-Z0-9]{2,5}-[0-9]{1,4}){0,1}[-_/]*(.*)"
[[ $BRANCH_NAME =~ $BRANCH_PATTERN ]]
BRANCH_TYPE=${BASH_REMATCH[1]}
JIRA_ID=${BASH_REMATCH[2]}
BRANCH_DESC=`echo ${BASH_REMATCH[3]} | tr '-' ' '`

# 템플릿 미사용 조건들
IS_EXCLUDED_BRANCH=$(printf "%s\n" "${EXCLUDED_BRANCHES[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_OCCURRENCES_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)

if [ -n "$BRANCH_NAME" ] && ! [[ $IS_EXCLUDED_BRANCH -eq 1 ]] && ! [[ $BRANCH_OCCURRENCES_IN_COMMIT -ge 1 ]]; then
  sed -i.bak -e "1s|^|[$JIRA_ID] $BRANCH_TYPE: $BRANCH_DESC\nSolves [$JIRA_ID] $BRANCH_TYPE: $BRANCH_DESC\n|" $1
fi
Enter fullscreen mode Exit fullscreen mode

If you commit, the hook add two lines automatically.
Ex) A branch

feature/JIRA-123_this-is-a-description
Enter fullscreen mode Exit fullscreen mode

makes a commit message

[JIRA-123] feature: this is a description
Solves [JIRA-123] feature: this is a description
{commit message}
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (1)

Collapse
 
morjo02jm profile image
John Morales

how can you ensure the Jira ID is actually a validate Jira Issue and Jira Issue Type

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay