Rails:“过量提交-运行”通过但预提交钩失败

时间:2019-04-17 09:30:06

标签: ruby-on-rails git-commit pre-commit-hook rubocop ruby-overcommit

我遇到overcommit中的预提交挂钩问题,该挂钩已配置为运行rubocoprails_best_practices

简而言之,下面列出的所有三个命令都通过了,但是overcommit不允许我使用git。对于为什么/如何规避此问题的任何建议,将不胜感激。

# 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

现在,我读到here时,overcommit --run并没有像您实际尝试提交时那样触发钩子(整个项目vs仅提交更改)。但是,我不确定这将如何影响我的情况,尤其是因为rubocoprails_best_practices都分别通过了。顺便说一下,这里提出的错误都是虚假警报。 rails_best_practices不应首先检查未使用的方法,因为该检查已在config/rails_best_practices.yml中关闭。

1 个答案:

答案 0 :(得分:1)

在使用这些宝石几周后,我想我已经解决了。对于那些遇到类似问题的人,我会在这里留下一些提示。

1。在进行overcommit --run

之前先进行更改

正如commentdocs所说,--run命令不会检查未跟踪的文件。

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

我遇到的一个问题是overcommit无法读取我的config/rails_best_practices.yml(最初不应该检查unused methods,因为该检查已关闭在配置文件中。)

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

  

如果您正在使用Bundler管理Ruby gem依赖项,则可能需要使用gemfile选项来控制钩子运行期间可用的gem版本。

我的.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)。