Git Hook을 이용해 자동으로 커밋메세지에 JIRA 이슈키 넣어주기

category
date
thumbnail
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/920972a0-888e-46f7-8ee7-5cd7c5d11ea0/Untitled.png
slug
git-hook을-이용해-자동으로-커밋메세지에-jira-이슈키-넣어주기
author
status
Public
tags
Git
summary
type
Post
맥북 숨김폴더 확인 : shift + command + .
 
vscode 숨김폴더 설정
 

 
  1. 프로젝트 루트 경로((레포 클론 뜨면 있는 폴더)에 있는 숨김 폴더로 되어있는 .git 폴더 열기
  1. .git/hooks/prepare-commit-msg.sample 파일 열기
notion image
 
  1. 아래 코드가 초기에 작성되어 있지만 4번에 있는 코드로 수정해야 해요 (그냥 미리 작성되어 있는 게 있다는 거 알려주려고 넣은 것)
#!/bin/sh
#
# An example hook script to prepare the commit log message.
# Called by "git commit" with the name of the file that has the
# commit message, followed by the description of the commit
# message's source.  The hook's purpose is to edit the commit
# message file.  If the hook fails with a non-zero status,
# the commit is aborted.
#
# To enable this hook, rename this file to "prepare-commit-msg".

# This hook includes three examples. The first one removes the
# "# Please enter the commit message..." help message.
#
# The second includes the output of "git diff --name-status -r"
# into the message, just before the "git status" output.  It is
# commented because it doesn't cope with --amend or with squashed
# commits.
#
# The third example adds a Signed-off-by line to the message, that can
# still be edited.  This is rarely a good idea.

COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3

/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE"

# case "$COMMIT_SOURCE,$SHA1" in
#  ,|template,)
#    /usr/bin/perl -i.bak -pe '
#       print "\n" . `git diff --cached --name-status -r`
# 	 if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;;
#  *) ;;
# esac

# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE"
# if test -z "$COMMIT_SOURCE"
# then
#   /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE"
# fi
 
  1. 아래 코드로 수정하기 (어차피 이전 코드는 .sample 확장자가 있었던 거라 작동 안 하던 코드니까 싹 다 갈아 엎으시면 됩니다.)
#!/bin/bash

BRANCH_NAME=$(git symbolic-ref --short HEAD)
JIRA_ID=$(echo "$BRANCH_NAME" | egrep -o 'LAS-[0-9]+')

if [ -n "$JIRA_ID" ]; then
  COMMIT_MSG_FILE="$1"
  COMMIT_MSG_HEAD=$(head -n 1 "$COMMIT_MSG_FILE")

  if [[ "$COMMIT_MSG_HEAD" != "[$JIRA_ID]"* ]]; then
    sed -i.bak -e "1s/^/[$JIRA_ID] /" "$COMMIT_MSG_FILE"
  fi
fi
 
  1. prepare-commit-msg.sample 이었던 파일 이름에서 .sample 확장자 빼기
notion image
 
  1. 잘 동작하는지 확인
5번까지 하시면 아마 잘 설정이 됐을 텐데 혹시 모르니 이를 테스트 해야 해요. 왜냐면 이때 적용이 안 되면 아파 파일권한을 만져야 하는 경우도 있기 때문에 테스트 필요합니다.
README.md 같은 아무 파일을 수정한 다음 git add 파일이름 하시고 git commit -m “test:dfdfd” 와 같이 아무렇게나 커밋해보세요.
그 후에 푸시하지 마시고, git log를 사용해서 커밋 메세지 앞에 지라티켓(이슈키)가 잘 붙었는지 확인합니다.
 
notion image
작업한 브랜치 - LAS-5 라는 지라티켓이 붙어 있습니다.
 
notion image
커밋 메세지를 쓸 때 지라 티켓을 수작업으로 작성하지 않습니다.
 
notion image
푸시를 하지 않고 git log 명령어를 통해 지라 티켓이 커밋 메세지에 잘 생성됐는지 확인합니다.
 
notion image
커밋 메세지에 지라 티켓이 잘 붙어있는 것을 확인했으니 테스트를 종료합니다.
해당 커밋은 테스트용으로 아무렇게나 만든 커밋이기에 푸시하지 않고 삭제합니다.(git reset HEAD^ —soft 명령어를 사용해 staging area에 올린 뒤 각자 알아서 판단해 수정하거나 삭제합니다.)
 
 

참고