Git Hook을 이용해 자동으로 커밋메세지에 JIRA 이슈키 넣어주기
맥북 숨김폴더 확인 : shift + command + .
vscode 숨김폴더 설정
- 프로젝트 루트 경로((레포 클론 뜨면 있는 폴더)에 있는 숨김 폴더로 되어있는
.git
폴더 열기
.git/hooks/prepare-commit-msg.sample
파일 열기
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2F980718df-dec2-4ed6-86a7-8523376d1275%2F3acaf2f1-b53d-4876-b6f5-531ca43f132c%2FUntitled.png%3Fid%3De52caa0b-e06f-4e48-9b01-b39f3771a917%26table%3Dblock%26spaceId%3D980718df-dec2-4ed6-86a7-8523376d1275%26expirationTimestamp%3D1720368000000%26signature%3DcKB3Gi3N5-bwdxPtHo7NAuvfSDowMI_EWe-azk_GFL8?table=block&id=e52caa0b-e06f-4e48-9b01-b39f3771a917&cache=v2)
- 아래 코드가 초기에 작성되어 있지만 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
- 아래 코드로 수정하기
(어차피 이전 코드는
.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
prepare-commit-msg.sample
이었던 파일 이름에서.sample
확장자 빼기
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2F980718df-dec2-4ed6-86a7-8523376d1275%2F9cc098c0-dd6e-4a3d-b032-a497ee7f6955%2FUntitled.png%3Fid%3Df83cca95-2c0d-49e9-aae3-2d04e9c57a8b%26table%3Dblock%26spaceId%3D980718df-dec2-4ed6-86a7-8523376d1275%26expirationTimestamp%3D1720368000000%26signature%3DWi_CG5bswfnhwlSr0DuzsR5di_d__dpJuOCQYATBxh8?table=block&id=f83cca95-2c0d-49e9-aae3-2d04e9c57a8b&cache=v2)
- 잘 동작하는지 확인
5번까지 하시면 아마 잘 설정이 됐을 텐데 혹시 모르니 이를 테스트 해야 해요. 왜냐면 이때 적용이 안 되면 아파 파일권한을 만져야 하는 경우도 있기 때문에 테스트 필요합니다.
그 후에 푸시하지 마시고,
git log
를 사용해서 커밋 메세지 앞에 지라티켓(이슈키)가 잘 붙었는지 확인합니다.![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2F980718df-dec2-4ed6-86a7-8523376d1275%2F63896d3e-539b-4786-b2a5-7908bbb5cc13%2FUntitled.png%3Fid%3D49b862f2-f5a5-4278-8a9b-4d44da017939%26table%3Dblock%26spaceId%3D980718df-dec2-4ed6-86a7-8523376d1275%26expirationTimestamp%3D1720368000000%26signature%3DPRBPan08N4IKXA3UuSgUGa5_CQaVitDN_PNgPo8EKwk?table=block&id=49b862f2-f5a5-4278-8a9b-4d44da017939&cache=v2)
작업한 브랜치 -
LAS-5
라는 지라티켓이 붙어 있습니다.![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2F980718df-dec2-4ed6-86a7-8523376d1275%2F2e6e5a0c-5342-4048-a7ab-abb396d24c5d%2FUntitled.png%3Fid%3D42f03e79-74b6-4605-a0bb-122293ae0c3c%26table%3Dblock%26spaceId%3D980718df-dec2-4ed6-86a7-8523376d1275%26expirationTimestamp%3D1720368000000%26signature%3DTVg21QCHYtvy9AiItIGv4OfA8M7xPpQ441poE56yu6o?table=block&id=42f03e79-74b6-4605-a0bb-122293ae0c3c&cache=v2)
커밋 메세지를 쓸 때 지라 티켓을 수작업으로 작성하지 않습니다.
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2F980718df-dec2-4ed6-86a7-8523376d1275%2Fea9747e3-3e57-425e-8e5b-2cb71d9f59eb%2FUntitled.png%3Fid%3D1d8aba64-9de7-416e-bf6d-daeba58ccf1e%26table%3Dblock%26spaceId%3D980718df-dec2-4ed6-86a7-8523376d1275%26expirationTimestamp%3D1720368000000%26signature%3DvRaD6gcG-Wr9e5a3Z66r25fPtOSThGOOocQgftEua7M?table=block&id=1d8aba64-9de7-416e-bf6d-daeba58ccf1e&cache=v2)
푸시를 하지 않고
git log
명령어를 통해 지라 티켓이 커밋 메세지에 잘 생성됐는지 확인합니다.![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2F980718df-dec2-4ed6-86a7-8523376d1275%2F920972a0-888e-46f7-8ee7-5cd7c5d11ea0%2FUntitled.png%3Fid%3D09240ce6-12cc-475f-9e53-8952b03f3e7d%26table%3Dblock%26spaceId%3D980718df-dec2-4ed6-86a7-8523376d1275%26expirationTimestamp%3D1720368000000%26signature%3DbX0LgjRl6K8FR34niDf-PRIppZCVj2UjZTymWjnQxO4?table=block&id=09240ce6-12cc-475f-9e53-8952b03f3e7d&cache=v2)
커밋 메세지에 지라 티켓이 잘 붙어있는 것을 확인했으니 테스트를 종료합니다.
해당 커밋은 테스트용으로 아무렇게나 만든 커밋이기에 푸시하지 않고 삭제합니다.(
git reset HEAD^ —soft
명령어를 사용해 staging area
에 올린 뒤 각자 알아서 판단해 수정하거나 삭제합니다.)