LoginSignup
13
9

More than 5 years have passed since last update.

あなたのプロジェクトに秩序をもたらす!git commit 支援ツールを作りました。

Last updated at Posted at 2018-03-19

前置き

上記のような記事でコミットメッセージにprefixを使うことのメリットが語られています。
実際、自分も今やっている仕事ではコミットにprefixをつけています。

が、これが結構面倒くさい。
「どんなprefixがあったっけ?」「docだっけ、docsだっけ?」
みたいな。

prefixを楽にするツールにcommitizenというのがあるけど、これが結構使いにくい。
なぜならprefixの種類が強制されていて自由に変更できないから。

ということで自分好みのツールを作りました。
それがgit-consistentです。

git-consistent01.gif

↑ こんなやつです。

インストール

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つのファイルができます。

.gitcommit_template
<type>: <subject>

<body>
.git_consistent
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を自由に編集することでかんたんに使うことができます。

詳しくはREADMEおよびサンプルリストを参照ください。

あなたのプロジェクトに秩序があらんことを!

13
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
9