Skip to content

Instantly share code, notes, and snippets.

@yn-misaki
Last active September 14, 2018 11:43
Show Gist options
  • Save yn-misaki/8c2b00326fa675d1b1ec9e3c5ab847a3 to your computer and use it in GitHub Desktop.
Save yn-misaki/8c2b00326fa675d1b1ec9e3c5ab847a3 to your computer and use it in GitHub Desktop.
コミット前に自動でRubocopを実行する(仮想開発環境編) ref: https://qiita.com/yn-misaki/items/adbc9a02be226bc10354
bundle exec rubocop -RDa app/model/query.rb
cd ${workspace_path} && bundle exec rubocop -RDa ${files}
$ vim .git/hooks/pre-commit
===========================
if [ -e ${vagrant_path} ]; then
cd ${vagrant_path} && vagrant ssh -c "cd ${workspace_path} && bundle exec rubocop -RDa ${files}"
else
bundle exec rubocop -RDa ${files}
fi
===========================
$ cd <Railsプロジェクトのディレクトリ>
$ vim .git/hooks/pre-commit
===========================
#!/bin/bash
vagrant_path="<vagrantのディレクトリ>"
workspace_path="<Vagrant上のRailsプロジェクトディレクトリ>"
files=`git diff --cached --name-only --diff-filter=AM | grep '.rb$' | tr '\n' ' '` --- (1)
# 実行するファイルがなければ、何もしない
if [ ! -n "${files}" ]; then
exit
fi
echo 変更があったファイルにrubocopを実行します。
cd ${vagrant_path} && vagrant ssh -c "cd ${workspace_path} && bundle exec rubocop -RDa ${files}" --- (2)
echo rubocopの実行が完了しました。
exit $?
===========================
yn-misaki$ git commit -m ":cop: テスト用"
変更があったファイルにrubocopを実行します。
Inspecting 1 file
.
1 file inspected, no offenses detected
rubocopの実行が完了しました。
[rubocop_test 5502ea0] :cop: テスト用
2 files changed, 9 insertions(+), 1 deletion(-)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment