Tired of create commits like “Fix rubocop violations” after push your code?
How about run rubocop before each git push
operation?
Setup the pre-push hook
Create the file below and save it as pre-push
in your project’s hook directory:
your-project/.git/hooks/
#!/bin/sh
rubocop
Then make it executable:
$ chmod +x pre-push
From now on Rubocop will run before git push
operation.
Testing the hook
Make sure you have rubocop installed before proceed.
The push action will be cancelled once a violation is detected, as shown in the example below:
$ git push origin test-branch
Inspecting 41 files
.....C...................................
Offenses:
app/models/user.rb:9:3: C: Layout/LeadingCommentSpace: Missing space after #.
#another
^^^^^^^^
app/models/user.rb:10:1: C: Layout/TrailingWhitespace: Trailing whitespace detected.
41 files inspected, 2 offenses detected
error: failed to push some refs to '[email protected]:hacker-sessions/remember-the-bills.git'
After fix the violations you will be able to push the code without problems.
$ git push origin test-branch
Inspecting 41 files
.........................................
41 files inspected, no offenses detected
Counting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 956 bytes | 0 bytes/s, done.
Total 11 (delta 8), reused 0 (delta 0)
remote: Resolving deltas: 100% (8/8), completed with 4 local objects.
To [email protected]:hacker-sessions/remember-the-bills.git
* [new branch] test-branch -> test-branch