Introduction

  • Conventional Commits 을 강제화하기 위한 방법에 대한 논의이다.

Motivation

  • versioning 자동화, git log 기반 release note 자동 생성을 위해서는 commit log가 규격화 되어 있어야 함. 대표적인 규격 : Conventional Commits

  • 모든 commit log에 Conventional Commits 규격을 강제화가 필요한데…

commitlint, pre-commit 이란

목표

  • commit 직전에 검사가 수행되어 Conventional Commits 규격을 준수하지 않으면 commit이 실패하도록 함.
  • 해당 규격에 비추어 무엇이 문제인지를 알려 commit log를 규격에 맞게 수정하도록 유도함.
  • pre-commit은 commit 유효성 검사의 대표적인 도구이므로, husky 등의 타 commit 유효성 검사 도구로 대치되는 일이 없도록 함

설치 방법

  1. node.js 설치 for commitlint 실행

    • node.js는 단순히 commitlint 실행을 위한 runtime으로, python, golang 등의 타 언어 환경에서도 동작 가능
    • 되도록이면 최신 버전으로 설치. download url : https://nodejs.org/en/download (commitlint 의 필요 버전은 확인되지 않음)
  2. commitlint 설치 (다음 설치 방법은 commitlint github에서 추출)

    # Install commitlint cli and conventional config (Conventional Commits)
    npm install --save-dev @commitlint/{config-conventional,cli}
    
    # For Windows:
    npm install --save-dev @commitlint/config-conventional @commitlint/cli
    
    # Configure commitlint to use conventional config (Conventional Commits)
    echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
    
    BASH

    commitlint에서 가이드하는 husky 에 대하여

    • commitlint github에서는 상기 단계 이후 husky란 commit 유효성 검사 도구 설치를 가이드하나, 목표 #3에 따라 husky 설치 단계는 건너 뜀.
    • husky를 설치할 경우 pre-commit이 동작하지 않음.
  3. pre-commit 설치

    • https://pre-commit.com/ 에 쉽게 잘 나와 있으므로 자세한 설치 방법 설명은 생략. 다음은 python 환경과 macOS - homebrew에서의 설치 방법 예.
    # 1. For python environment:
    pip install pre-commit
    
    # 2. For macOS - homebrew enviroment:
    brew install pre-commit
    
    # 3. (적용 대상 프로젝트 root에서) pre-commit 설치
    pre-commit install
    
    BASH
  4. commitlint hook for per-commit 설치 (해당 hook 공식 github link 에서 추출)

    # 1. 다음의 내용으로 적용할 프로젝트의 root에 .pre-commit-config.yaml 생성
    repos:
      - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
        rev: v9.5.0
        hooks:
          - id: commitlint
            stages: [commit-msg] # 커밋 메시지 작성 시 commitlint를 실행하도록 설정
            additional_dependencies: ["@commitlint/config-conventional"] # commit 규격으로 Conventional Commits을 사용함을 설정
    
    # 2. commit-msg hook을 해당 프로젝트에 설치
    pre-commit install --hook-type commit-msg
    
    BASH

정상 동작 확인 방법

  1. 규격에 맞지 않는 commit 수행 시 아래와 같은 오류 발생 및 commit 실패 확인

    • 해당 commit message : invalid feat: apply commitlint in pre-commit
    commitlint...............................................................Failed
    - hook id: commitlint
    - exit code: 1
    
    ⧗   input: invalid feat: apply commitlint in pre-commit
    ✖   subject may not be empty [subject-empty]
    ✖   type may not be empty [type-empty]
    
    ✖   found 2 problems, 0 warnings
    ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
    
    BASH
  2. 규격에 맞는 commit 수행 시 아래와 같은 log와 함께 commit 성공 확인

    • 해당 commit message : feat: apply commitlint in pre-commit
    commitlint...............................................................Passed
    [commitlint 0d1e3f5] feat: apply commitlint in pre-commit
     Date: Fri Jul 14 19:46:48 2023 +0900
     2 files changed, 8 insertions(+)
     create mode 100644 .pre-commit-config.yaml
     create mode 100644 commitlint.config.js
    
    BASH

Comments