4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

OvercommitでGit hookを簡単に設定する

Posted at

Overcommit!クリーンコード警察ツール

そもそもLintなどの静的コード解析を通過しない状態でコミットしたくない・させたくないですね。
Git Hookで色々と組み込めますが一からやるのがかなり辛いです。

そこで、Overcommit! Git Hookを簡単に設定・管理ができるツールです。

今回nodejs用に簡単に設定してみました。

インストール

事前準備

  • gemコマンドが使える環境
  • nodejsやJavascriptのある既存Gitレポジトリ
  • EsLintのパッケージがインストールされ、任意のルールで設定されている

Overcommitのgemをインストールする

> gem install overcommit
Fetching: iniparse-1.4.4.gem (100%)
Successfully installed iniparse-1.4.4
Fetching: overcommit-0.46.0.gem (100%)
Install hooks by running `overcommit --install` in your Git repository
Successfully installed overcommit-0.46.0
Parsing documentation for iniparse-1.4.4
Installing ri documentation for iniparse-1.4.4
Parsing documentation for overcommit-0.46.0
Installing ri documentation for overcommit-0.46.0
Done installing documentation for iniparse, overcommit after 1 seconds
2 gems installed

Overcommitを初期設定する

.overcommit.yml
PreCommit:
  EsLint:
    enabled: true
    description: 'Analyze with ESLint'
    required_executable: 'node_modules/.bin/eslint'
    flags: ['--format=compact']
    install_command: 'npm install -g eslint'
    include: '**/*.js'

そして設定を反映します。

> overcommit --sign
Updating signature for configuration file...

実験

EsLintのエラーを起こす

適当にソース内に行の終わりに;;を入れてコミットしてみます。

> git commit -m "ESLint prehook should fail"
Running pre-commit hooks
Analyze with ESLint..........................................[EsLint] FAILED
Errors on modified lines:
/Users/jb/Documents/nyan/co/app.js: line 10, col 20, Error - Extra semicolon. (semi)
/Users/jb/Documents/nyan/co/app.js: line 10, col 20, Error - Missing whitespace after semicolon. (semi-spacing)
/Users/jb/Documents/nyan/co/app.js: line 10, col 21, Error - Unnecessary semicolon. (no-extra-semi)


✗ One or more pre-commit hooks failed

コミットメッセージの警告を出す

デフォルトで入るチェックがいくつかあります。コミットメッセージのフォーマットはその一つです。

詳しくは: https://github.com/brigade/overcommit/blob/master/config/default.yml

> git commit -m "elisnt wil succeed"
Running pre-commit hooks

✓ All pre-commit hooks passed

Running commit-msg hooks
Check text width..........................................[TextWidth] OK
Check subject capitalization.....................[CapitalizedSubject] WARNING
Subject should start with a capital letter
Check subject line................................[SingleLineSubject] OK
Check for trailing periods in subject................[TrailingPeriod] OK

⚠ All commit-msg hooks passed, but with warnings

[master fb50ca4] elisnt wil succeed
 1 file changed, 1 insertion(+), 1 deletion(-)

感想

最高です。プロジェクト初期段階(スプリントゼロ)に必ず入れたいものです。

PreCommitの他、PrePush、PostMergeなども設定できます。

すぐに利用できるHookは非常に豊富です。Ruby, PHP, Javascript, Python, yaml, Typescript, Golang, CSS, SCSS, HTML, nginx, shellなど、必要以上にあります。

コミット前に、RubyLintとRailsBestPracticesでクリーンコードをチェックし、プッシュ前にRspecでテスト通過確認させるフローはこれで簡単に実現できます♥

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?