[Node.js] husky를 곁들인 효율적인 커밋 작성법

Jeerryy·2022년 12월 26일
0

Node.js

목록 보기
1/2
post-thumbnail

Conventional Commits 이란?

커밋 메시지를 작성하는 규약으로 사용자와 컴퓨터 모두에게 의미있고 이해하기 쉽도록 약속한 규악으로, 버전 관리 트레킹을 쉽게 만들어 줍니다.

커밋 메시지는 다음과 같이 구성됩니다.

<type>[optional scope]: <description>
[optional body]
[optional footer(s)]

Conventional Commits에는 의도를 전달하기 위해 다음과 같은 구조적 요소가 포함되어 있습니다.

fix: 코드베이스에서 버그를 패치하는 fix type commit

feat: 코드베이스에서 새 기능이 추가되는 feat type commit

BREAKING CHANGEBREAKING CHANGE:이라는 footer를 가지거나 타입/스코프 뒤에 !문자열을 붙인 커밋은 단절적 API 변경(breaking API change) 있다는 것을 의미합니다. 어떤 커밋 타입이라도 BREAKING CHANGE는 해당 커밋의 일부가 될 수 있습니다.

fix:와 feat: 이 외의 다른 타입도 허용됩니다.
예를 들어 build:chore:ci:docs:style:refactor:perf:test: 등의 타입을 사용할 것을 권고하고 있습니다.

Conventional Commits

적용 이유

보다 유의미한 커밋을 남기는 것에도 의미가 있지만 release 자동화 package를 통해 commit message 기반의 버전 관리 및 패키징 퍼블리싱, ChangeLog 자동화에 대한 첫걸음이 됩니다.

semantic-release

적용법

  1. huksy 설치 (이미 설치돼있으면 생략)

    • npm i -D husky
    • husky install
  2. 커밋 메시지 검사하기

    • commitlint
    • npx husky add .husky/commit-msg 'node_modules/.bin/commitlint --edit $1'
    • npm i -D @commitlint/cli @commitlint/config-conventional
    • 루트 디렉토리에 commitlint.config.js 파일 생성
module.exports = {
	extends: ['@commitlint/config-conventional'],
};
  • conventional commit 형식이 아닐 경우 commit reject (아래 이미지 참고)
  1. conventional commits helper cli (선택)
    • cz-cli
    • npx husky add .husky/prepare-commit-msg 'exec < /dev/tty && node_modules/.bin/cz --hook || true'
    • npm i -D commitizen
    • npm i -D cz-conventional-changelog
    • package.json 파일 아래에 구문 추가
"config": {
    "commitizen": {
      "path": "./node_modules/cz-conventional-changelog"
    }
  }
  • git commit 입력 시 conventional commits 을 도와주는 cli 사용 가능 (아래 이미지 참고)

git commit -m시 helper 안뜨게 하기

converional commits helper cli 적용 시 git commit -m 옵션을 사용하여 커밋을 해도 helper가 뜨는 현상이 있는데 이 현상을 없애기 위해서는 아래 스크립트를 사용하면 됩니다.
npx husky add .husky/prepare-commit-msg '[ $2 == "template" ] && exec < /dev/tty && node_modules/.bin/cz --hook || true'
기존에 추가한 사람은 prepare-commint-msg script 앞에 [ $2 == "template" ] && 를 추가해주면 된다.

원리

githooks documentation을 보면 알 수 있는데 아래는 prepare-commit-msg githook에 대한 내용입니다.

간단하게 요약하자면 git commit 시 1~3개의 parameter가 내려오는 데 첫번째 파라미터는 파일명, 두번째 파라미터는 Commit Source 즉, 커밋이 호출된 조건이 들어있습니다.
이 중에 두번째 파라미터가 git commit의 경우 template, git commit -m의 경우 message로 넘어오는 것을 이용한 방식입니다.

profile
다양한 경험을 해보고자 하는 Backend-Engineer. [2023년은 내실 다지기의 해]

0개의 댓글