기록

Git - 커밋 메시지 규격 자동화

안녕하세요 청이 입니다.

오늘은 작업한 내용을 깃에 올릴때 암묵적으로 정의하여 왔던 커밋메시지를 처음에는 지켜지는듯 하지만, 진행될수록 잘 지켜지지 않을때가 많습니다.

이를 도와주기위한 라이브러리 2개를 소개 하도록 하겠습니다.


Commitizen
장점 : 템플릿이 제공되어 보다 편리하게 작성 할수 있습니다.
단점 : 기존의 커밋 명령어와 상반 됩니다.

commitizen : commit 메시지 규격을 정의 함
cz-conventional-changelog : commitizen 의 기본 설정 및 규칙을 설정 합니다.
cz-customizable : 위 두가지를 합친것으로 생각하면 편리합니다.

설치

yarn add -D commitizen cz-customizable

공식 문서에 나와 있는데로 설치 하면 설치가 끝납니다. ( 설치 방법이 3가지 정도 존재합니다. 하단 참고 사이트 참조)

하지만 이렇게하고 커밋을 하면 기존과 다를바 없이 동작 합니다.
-> TEST를 위해 커밋을 하였고, 커밋을 취소하는 명령어는 git reset HEAD^ 입니다.

앞으로는 커밋을 스크립트를 통해 진행 하도록 하여야 합니다.

아래와 같이 package.json에 코드를 추가 합니다.

//package.json
...
  "scripts": {
         ...
    "commit": "cz"
  },	
    "config": {
    "commitizen": {
      "path": "cz-customizable"
    }
    }
...

.cz-config.js 파일도 생성해 줍니다. (공식 문서 사용)

module.exports = {
  types: [
    { value: '✨ Feat', name: '✨ Feat:\tAdd a new feature' },
    { value: '🐛 Modify', name: '🐛 Fix:\tModify production, UI,UX code' },
    { value: '📝 Docs', name: '📝 Docs:\tAdd or update documentation' },
    {
      value: '💄 Style',
      name: '💄 Style:\tAdd or update code format (not updation production, UI,UX code)',
    },
    {
      value: '🤖 Refactor',
      name: '🤖 Refactor:\tCode change that neither fixes a bug nor adds a feature',
    },
    {
      value: '✅ Test',
      name: '✅ Test:\tCode change related with tests cases',
    },
    {
      value: '🚚 Chore',
      name: '🚚 Chore:\tChanges to the build process or auxiliary tools\n\t\tand libraries such as documentation generation',
    },
  ],
  allowCustomScopes: false,
  allowBreakingChanges: ['feat', 'fix'],
  skipQuestions: ['body'],
  subjectLimit: 100,
};

이후, yarn commit 을 실행하면 아래와 같이 실행 됩니다.

실행 결과

! 추가

yarn berry 사용사 아래와 같은 에러가 발생 할수 있습니다. ( 해결 방안 확인 불가..)


CommitLint

위 commitizen 을 사용하려 하였으나 몇가지 문제에 봉착하여 사용하지 않았습니다.

  1. yarn berry 지원 관련 문제 ( 해결 못함 )
  2. yarn commit 시 husky 미 동작
  3. 흔히 사용되는 git commit 이 아닌, script 명령어 사용

설치 (https://commitlint.js.org/#/guides-local-setup?id=install-commitlint) 공식 문서 참조

// commit lint install
yarn add -D @commitlint/config-conventional @commitlint/cli

// setting file make (cli 에 입력 시 자동 생성)
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js

// husky install & set up
yarn add -D husky
yarn husky init

// husky commit-msg file make
echo "yarn commitlint --edit \$1" > .husky/commit-msg

// commmit lint setup
yarn add -D @commitlint/cli @commitlint/config-conventional

위 명령어들을 순서대로 입력하고 커밋을 한번 해보면?

메시지 규칙 미 준수 시

메시지 규칙 준수시

단순하지만, 비교적 간단하게 적용 할수 있습니다. ( 규칙과 상세한 정보는 공식 홈페이지 에 정리 되어 있습니다 )

컨피그 파일 작성 규칙 예

module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'header-max-length': [0, 'always', 72], // 0 : no error  1: warning, 2 : error
        'type-enum': [
      0,
      'always',
      [
        'build',
        'chore',
        'ci',
        'docs',
        'feat',
        'fix',
        'perf',
        'refactor',
        'revert',
        'style',
        'test',
      ],
    ],
  },
  },
};

댓글

이름만 적고 남길 수 있어요