1
0

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 3 years have passed since last update.

Gitフックでgit commit時にRubocopを走らせる方法

Last updated at Posted at 2021-09-23

Gitに備わっているGitフックのpre-commitという機能を使って、
git commit時にRubocopを走らせる方法を紹介します。
- Gitフックとは
- appbrew Tech Blog (pre-commitとは) 

手順1.

chmodコマンドで、設定を記述する.git/hooks/pre-commitの実行権限を与える

ターミナル
chmod a+x .git/hooks/pre-commit

手順2.

.git/hooks/pre-commitを開く

ターミナル
open .git/hooks/pre-commit

VScodeなら

ターミナル
code .git/hooks/pre-commit

手順3.

.git/hooks/pre-commitのファイル末行にRubocopを走らせるコマンドを記述する

.git/hooks/pre-commit
~省略~
bundle exec rubocop

Docker-composeを使っていたら

.git/hooks/pre-commit
~省略~
docker-compose -f docker-compose.yml exec -T サービス名 bundle exec rubocop

-T オプションをつけないと、うまくいかない。

[ -T ] : 擬似 TTY への割り当てを無効にします。 デフォルトにおいてdocker-compose exec には TTY が割り当てられます。
https://docs.docker.jp/compose/reference/exec.html?highlight=exec

手順4.

.git/hooks/pre-commitを保存して完了。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?