Conventional Commitsを試してみる

  • 目次

    Conventional Commitsというのを知ったのでメモしておきます。

    コミットメッセージのフォーマット

    <型>[任意 スコープ]: <タイトル>
    
    [任意 本文]
    
    [任意 フッター]
    

    例:タイトルおよび破壊的変更のフッターを持つコミットメッセージ

    feat: allow provided config object to extend other configs
    
    BREAKING CHANGE: `extends` key in config file is now used for extending other config files
    

    型

    No型役割
    1fixバグの修正(これは Semantic Versioning における PATCH に相当する)
    2feat新機能の追加(これは Semantic Versioning における MINOR に相当する)
    3BREAKING CHANGE後方互換がない変更(これはSemantic Versioning における MAJOR に相当する)
    4ciCI設定ファイルやスクリプトの変更
    5styleコードの意味に影響しない変更(空白、フォーマット、セミコロンの追加など)
    6refactorバグの修正でも新機能の追加でもないコード変更
    7perfパフォーマンスを改善するコード変更
    8test不足しているテストの追加や既存テストの修正
    9docsドキュメントのみ変更
    10buildビルドシステムや外部依存関係に影響を与える変更
    11choreその他(元にしているangularにはない分類)

    導入方法

    Coventional Commitsを開発プロセスに取り入れるために、コミットメッセージの書式をチェックするツールを導入する。

    1. コミットメッセージをチェックするツールをインストールする。
    $ npm install --save-dev @commitlint/config-conventional @commitlint/cli
    
    1. 設定ファイル(commitlint.config.js)を用意する
    module.exports = {
      // インストール済みのパッケージを参照する
      extends: ['@commitlint/config-conventional'],
    };
    

    config-conventionalのドキュメントだと以下のコマンドを実行してくださいとあったが、私の環境だとエラーだった。

    echo “export default {extends: [’@commitlint/config-conventional’]};” > commitlint.config.js

    1. .git/hooks以下にcommit-msgというファイルを作成する
    #!/bin/bash
    
    echo -e "\033[37;1m🪝 Running Git Hooks: commit-msg\033[0m"
    
    if ! npx --no -- commitlint --edit $1; then
      exit 1
    fi
    
    exit 0
    
    1. 実行権限を与える
    $ chmod +x .git/hooks/commit-msg
    
    1. 動作確認をする
    $ touch test
    $ git add test
    # 間違ったコミットメッセージだとエラーが出る。
    $ git commit -m "add new file"
    🪝 Running Git Hooks: commit-msg
    ⧗   input: add new file
    ✖   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
    
    # 正しいコミットメッセージだと問題なし。
    $ git commit -m "feat: add new file"
    🪝 Running Git Hooks: commit-msg
    [main 7c330a5] feat: add new file
     1 file changed, 1 insertion(+)
     create mode 100644 tests/test.txt
    

    CHANGELOGの生成

    1. ツールをインストールする
    $ npm install --save-dev conventional-changelog-cli
    
    1. CHANGELOGを生成する
    $ npx -- conventional-changelog -p angular -i CHANGELOG.md -s -r 0
    

    -p 使用するconventional commitsのpreset
    -i CHANGELOGを読み込むファイル
    -s 同じファイルに書き出しを行う
    -r 0 最新からいくつのリリースを生成するか。0の場合、全部を生成する

    1. 以下のようなCHANGELOG.mdが生成される。
    ##  (2024-11-23)
    
    
    
    
    ## 1.1.0 (2024-11-23)
    
    * build: add new package ([541a720](https://github.com/zEttOn86/practice-conventional-commits/commit/541a720))
    * chore: update .gitignore ([73765f5](https://github.com/zEttOn86/practice-conventional-commits/commit/73765f5))
    
    
    
    ## 1.0.0 (2024-11-23)
    
    * feat: add new file ([07593ed](https://github.com/zEttOn86/practice-conventional-commits/commit/07593ed))
    * first commit ([3e07588](https://github.com/zEttOn86/practice-conventional-commits/commit/3e07588))
    

    tagは2回打っており、コミットログは以下の通り。

    $ git log --oneline
    541a720 (HEAD -> main, tag: v1.1.0) build: add new package
    73765f5 chore: update .gitignore
    07593ed (tag: 1.0.0) feat: add new file
    3e07588 (origin/main) first commit
    

    参考

    comments powered by Disqus