Rails:"overcommit --run" 正在通过,但预提交挂钩失败

Rails: "overcommit --run" is passing but pre-commit hooks fail

我在 overcommit 中遇到预提交挂钩问题,它配置为 运行 rubocoprails_best_practices.

简而言之,下面列出的所有三个命令都通过了,但是 overcommit 不让我提交 git。任何关于 why/how 来规避此问题的建议都将不胜感激。

# These passed
rubocop -a
rails_best_practices .
overcommit --run
# Git commit failed
$ overcommit --run
Running pre-commit hooks
Analyze with RailsBestPractices..................[RailsBestPractices] OK
Analyze with RuboCop........................................[RuboCop] OK

✓ All pre-commit hooks passed


$ git commit -m 'Ensure lower case with Attr API'
Running pre-commit hooks
Analyze with RailsBestPractices..................[RailsBestPractices] FAILED
Errors on modified lines:
/Users/USER_NAME/projects/APP_NAME/app/models/lower_case_string.rb:2 - remove unused methods (LowerCaseString#cast)

Analyze with RuboCop........................................[RuboCop] FAILED
Errors on modified lines:
/Users/USER_NAME/projects/APP_NAME/app/models/lower_case_string.rb:3:3: C: Layout/IndentationWidth: Use 2 (not 0) spaces for indentation.

✗ One or more pre-commit hooks failed

现在,我读到 overcommit --run 不会以与实际尝试提交时相同的方式触发挂钩(整个项目与仅提交更改)。但是,我不确定这会如何影响我的情况,尤其是因为 rubocoprails_best_practices 也都单独通过了。对了,这里抛出的错误都是误报。 rails_best_practices 一开始就不应该检查未使用的方法,因为该检查已在 config/rails_best_practices.yml.

中关闭

在使用这些 gems 几周之后,我想我已经弄明白了。我会在这里为遇到类似问题的任何人留下一些指示。

1.在使用 git add <file>

执行 overcommit --run 之前暂存您的更改

正如这个 and docs 所说,--run 命令不检查未跟踪的文件。

2。将 gemfile 选项添加到 .overcommit.yml

我遇到的一个问题是 overcommit 未能读取我的 config/rails_best_practices.yml(它不应该首先检查 unused methods,因为该检查是在配置文件中关闭)。

似乎当我尝试 git commit 时,overcommit 正在使用我系统中安装的 gems(而不是 Gemfile),结果以某种方式无法读取我的配置文件。所以我按照文档中的建议添加了 gemfile 选项,并确保 overcommit 使用 Gemfile/bundler 版本。从那以后我就再也没有遇到过主题错误。正如文档所说:

If you are using Bundler to manage your Ruby gem dependencies, you'll likely want to use the gemfile option to control which gem versions are available during your hook runs.

我的.overcommit.yml文件供参考:

gemfile: Gemfile

PreCommit:
 RuboCop:
   enabled: true
   on_warn: fail

 RailsBestPractices:
   enabled: true
   on_warn: fail
   command: ['bundle', 'exec', 'rails_best_practices', '-c', 'config/rails_best_practices.yml']

作为旁注 - 我还没有尝试过这个,但显然你也可以创建一个单独的 Gemfile 只是为了 overcommit 目的,如果你觉得加载你的原始 Gemfile 减慢挂钩执行 (docs).