gitを使っているの開発の途中であったり、複数の開発をまとめてコミット
したりする際に適切ではないコミットメッセージをいれてしまって、あとで、
コミットメッセージを辿ったときにいったい何を開発したのかわからないといった
ことになってしまうときがあります。
Git Copを使えば、あらかじめ設定した形式でコミットメッセージを入れているかをチェックできるので、チームで開発している場合でもコミットメッセージに一貫性を保つことができます。
インストール方法
rubyのGemなので以下のコマンドでインストールを行います。
ruby 2.4.1が必要なので、環境に入っていなければ、rbenv等でruby 2.4.1を
インストールします。
gem cert --add <(curl --location --silent https://www.alchemists.io/gem-public.pem)
gem install git-cop --trust-policy MediumSecurity
Git Copがバリデーションに使用するための設定ファイルを
~/.config/git-cop/configuration.ymlに作成します。
設定ファイルがなかったり空ファイルの場合はデフォルト値が使用されますが
最終的にカスタマイズして使用すると思うので、とりあえずGit Copのページにあるデフォルトの
設定をコピーして設定ファイルを作成します。
:commit_body_issue_tracker_link:の項目はドキュメントでは:blacklist:
の項目の末尾にカンマが入っていてconfigurationが全てデフォルト値で
動作するようになってしまうので、以下に修正します。
:commit_body_issue_tracker_link:
:enabled: true,
:severity: :error,
:blacklist:
- "(f|F)ix(es|ed)?\\s\\#\\d+"
- "(c|C)lose(s|d)?\\s\\#\\d+"
- "(r|R)esolve(s|d)?\\s\\#\\d+"
- "github\\.com\\/.+\\/issues\\/\\d+"
git hooksの設定
コミットメッセージを入力した後にメッセージのチェックをしたいので、
commit-msgのhookを作成します。
対象のgitプロジェクトの.git/hooks/commit-msgを実行可能な権限で作成
して以下の内容にすることで、git-copが使用できます。
#! /usr/bin/env bash
set -o nounset
set -o errexit
set -o pipefail
IFS=$'\n\t'
if ! command -v git-cop > /dev/null; then
printf "%s\n" "[git]: Git Cop not found. To install, run: gem install git-cop --trust-policy MediumSecurity."
exit 1
fi
git-cop --hook --commit-message "${BASH_ARGV[0]}"
Git Copのバリデーション項目とデフォルト値
git-copでは以下の項目のチェックをすることが出来ます。
必要に応じて~/.config/git-cop/configuration.ymlの
値を設定します。
ymlのキー | 監視項目 | 有効/無効(デフォルト値) | エラーレベル | 備考 |
---|---|---|---|---|
:commit_author_email | Commit Author Email | true | error | git configのuser.emailの値をチェック |
:commit_author_name_capitalization | Commit Author Name Capitalization | true | error | git configのuser.nameの値がCapitalizationされているか |
:commit_author_name_parts | Commit Author Name Parts | true | error | git configのuser.nameの値を姓名に分かれているか。 |
:commit_body_bullet | Commit Body Bullet | true | error | コミットメッセージ本文の箇条書きにMarkdown構文を使用するにするかどうか |
:commit_body_bullet_capitalization | Commit Body Bullet Capitalization | true | error | コミットメッセージ本文の行をCapitalizationしているか |
:commit_body_issue_tracker_link | Commit Body Issue Tracker Link | true | error | コミットメッセージ本文にgithub用のissueへのリンクがあるかどうか |
:commit_body_leading_line | Commit Body Leading Line | true | error | コミットメッセージの題名と本文の間に空白行があるかどうか |
:commit_body_leading_space | Commit Body Leading Space | false | warn | version 2.0.0で削除されるので説明は割愛します。 |
:commit_body_line_length | Commit Body Line Length | true | error | コミットメッセージの本文の行の長さをチェックします。 |
:commit_body_paragraph_capitalization | Commit Author Name Capitalization | true | error | コミットメッセージの本文の各段落でCapitalizationしているかどうか |
:commit_body_phrase | Commit Body Phrase | true | error | コミットメッセージの本文に適切でない単語が含まれているかどうか |
:commit_body_presence | Commit Body Presence | true | warn | コミットメッセージの本文が存在しているかどうか |
:commit_body_single_bullet | Commit Body Single Bullet | true | error | コミットメッセージの本文で単一の箇条書き表現を許可するかどうか |
:commit_subject_length | Commit Subject Length | true | error | コミットメッセージの題名の長さに制限を使えるか |
:commit_subject_prefix | Commit Subject Prefix | true | error | コミットメッセージの題名に特定のprefixを含むかどうか |
:commit_subject_suffix | Commit Subject Suffix | true | error | コミットメッセージの題名に特定のsuffixを含むかどうか |
設定のカスタマイズ
~/.config/git-cop/configuration.ymlを整えたいルールの通りに修正します。
自分の環境ではCommit Author Nameに使う名前が他のシステムの名前
と統一したい都合もあったため、:commit_author_name_capitalizationと:commit_author_name_partsの項目をfalseにしています。また、:commit_subject_suffixのsuffixの設定も感覚的にフィットしなかった
のでfalseにしています。
git commit -mでメッセージを入れてコミットした場合には:commit_body_*系のチェックは動作しないようです。
:commit_author_name_capitalization:
:enabled: false
:severity: :error
:commit_author_name_parts:
:enabled: false
:severity: :error
:minimum: 2
:commit_subject_suffix:
:enabled: false
:severity: :error
:whitelist:
- "\\."
以上になります。