I’ve for the last week or so been playing around with Overcommit, a hook manager for git. It’s easy to use and you can configure it to work well with the workflow you desire.
I use it both at work and at home and I’m happy with the results. The readme is extensive and will get you going in no time.
My current setup for Ruby projects is as follows:
PreCommit:
RuboCop:
enabled: true
on_warn: fail # Treat all warnings as failures
problem_on_unmodified_line: ignore
TrailingWhitespace:
enabled: true
Reek:
enabled: true
problem_on_unmodified_line: ignore
YamlSyntax:
enabled: true
PrePush:
RSpec:
enabled: true
As you can see I run both Reek and RuboCop when I type git commit
. I’ve also found that using problem_on_unmodified_line: ignore
to be invaluable when working with an old code base. Especially since you’re running the risk of getting swamped with errors that’s not related to the change you’re working on.
I recommend that you run the checks on the whole file you’ve modified from time to time. If you do that, and change the things that you and your team deem relevant, you’ll soon have a more consistent code base. I find that a code base with a consistent style easier and more pleasant to work with.
As you can glean from my configuration file Overcommit will run RSpec whenever I try to push my branch. This is a nice way to catch that failing test case that you forgot to fix.
Below you can see how it looks when I commit code. Overcommit’s pre-commit hooks also give you some nice pointers on how to write better commit message subjects.
Running pre-commit hooks
Analyzing with Reek............................................[Reek] OK
Analyzing with RuboCop......................................[RuboCop] OK
Checking for trailing whitespace.................[TrailingWhitespace] OK
✓ All pre-commit hooks passed
Running commit-msg hooks
Checking subject capitalization..................[CapitalizedSubject] OK
Checking subject line.............................[SingleLineSubject] OK
Checking text width.......................................[TextWidth] OK
Checking for trailing periods in subject.............[TrailingPeriod] OK
✓ All commit-msg hooks passed
Overcommit has a plethora of built-in git hooks and support custom hooks. I highly recommend that you take a closer look at the project, read the readme and try it out for yourself.
Feel free to post you own configuration in the comments and come with suggestions of what I can improve with mine.