前置き
上記のような記事でコミットメッセージにprefixを使うことのメリットが語られています。
実際、自分も今やっている仕事ではコミットにprefixをつけています。
が、これが結構面倒くさい。
「どんなprefixがあったっけ?」「doc
だっけ、docs
だっけ?」
みたいな。
prefixを楽にするツールにcommitizenというのがあるけど、これが結構使いにくい。
なぜならprefixの種類が強制されていて自由に変更できないから。
ということで自分好みのツールを作りました。
それがgit-consistentです。
↑ こんなやつです。
インストール
npmかyarnでインストールするだけです。
$ npm install -g git-consistent
# or
$ yarn global add git-consistent
使い方
git consistent --gen-config
と打つと色々聞かれるので答えましょう。
$ cd my-project/
$ git consistent --gen-config
? Use Type? Yes
? Use Emoji? No
? choice types feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert, ui, remove, sec, up, down, tada
? Does the subject start with? lower case
? Does the subject put dot (.) at end? No
Generated '.gitcommit_template' and '.git_consistent'.
You can edit them freely.
Enjoy!
そうするとこんな2つのファイルができます。
<type>: <subject>
<body>
type:
type: enum
required: true
description: 'commit type'
values:
-
name: feat
description: 'when implementing function'
-
name: fix
description: 'when fixing a bug'
-
name: tada
description: 'when celebrating'
subject:
type: string
required: true
description: 'The subject contains succinct description of the change'
rules:
firstLatter: lower
dotAtEnd: false
nonAscii: false
body:
type: text
default: ''
required: false
description: 'The body contains details of the change'
rules:
firstLatter: upper
dotAtEnd: true
nonAscii: false
あとはgit commit
の代わりにgit consistent -i
と打つだけです。
$ git consistent -i
? Select type:
feat when implementing function
fix when fixing a bug
❯ tada when celebrating
(Move up and down to reveal more choices)
? Write subject: my first commit
こんな風にインタラクティブにコミットメッセージを生成することができます。
optionで入力することもできます。option名は.gitcommit_template
の<>
で囲まれた名前です。
optionを入力した場合、入力しなかったものだけがインタラクティブに入力できます。
$ git consistent -i --subject="my first commit"
? Select type:
feat when implementing function
fix when fixing a bug
❯ tada when celebrating
(Move up and down to reveal more choices)
# subjectは聞かれない
またsubjectは特別に-m
のaliasがついているのでgit aliasと組み合わせるとこんなふうになります。
$ git config --global alias.con "consistent -i"
$ git con -m "this is amazing commit"
? Select type:
❯ feat when implementing function
fix when fixing a bug
tada when celebrating
(Move up and down to reveal more choices)
普段コミットしているときとほぼ同じ使い方ですね。
応用
応用として以下のような機能があります。
- 入力を装飾する 例)
test
と入力すると【test】
になる - フォーマットチェック
- はじめの文字の大文字/小文字
- 末尾のdot (
.
) - 日本語許可
- コミットメッセージにブランチ名の一部を含める 例)
issue-123
というブランチでコミットしたときにclose #123
を自動的にコミットに入れる - スペルチェック
- 絵文字サポート
- git-duetサポート
これらは.gitcommit_template
と.git_consistent
を自由に編集することでかんたんに使うことができます。
あなたのプロジェクトに秩序があらんことを!