Skip to content

Instantly share code, notes, and snippets.

@edisonywh
Last active April 15, 2024 22:58
Show Gist options
  • Save edisonywh/552cdf75166bfd84abb42d06982da999 to your computer and use it in GitHub Desktop.
Save edisonywh/552cdf75166bfd84abb42d06982da999 to your computer and use it in GitHub Desktop.
Run Rubocop in Git's pre-commit hook
```
#!/bin/sh
echo "\nRunning rubocop 🚓 💨 💨 💨\n"
declare -a ERRORS=()
for file in $(git diff --cached --name-only | grep -E '.rb')
do
ERRORS+=("$(rubocop $file | grep -e 'C:' -e 'E:')")
done
if [[ "$ERRORS" != "" ]]; then
echo "\n 🚨 BEE-BOP! There are some things that you need to fix before commiting!\n"
history -p "${ERRORS[@]}"
exit 1
fi
echo "Done! Rubocop has no complains! ✅\n"
exit 0
```
#####
Two steps to get it working:
1) Run this command in your root directory: `mv .git/hooks/pre-commit.sample .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit`
2) Replace the content of the file with the bash script above.
NOTE:
- The first line tells it what intepreter to use, here it's using bash. You can do #!/bin/sh ruby if you want to write a Ruby script.
- `mv` command is to rename the `.githook` from a sample to a legit one. Then `chmod` makes it executable so that it'll actually run.
@myanch200
Copy link

myanch200 commented May 13, 2023

@edisonywh thank you so much for this it really helped me. The only thing that I had to add is instead of using
$(git diff --cached --name-only | grep -E '.rb') to use $(git diff --cached --name-only | grep -E '\.rb$') as in my case it was catching .html.erb files which rubocop can't really format for me

@vlreshtanenko
Copy link

@edisonywh thanks for the script!

we ran into an issue on our project: the script was catching some files even though they were exlucded in .rubocop.yml, since running rubocop $file overrides all exclusions. Adding --force-exclusion helped us:

ERRORS+=("$(rubocop --force-exclusion $file | grep -e 'C:' -e 'E:')")

@denis-kos
Copy link

denis-kos commented Sep 15, 2023

When there are a lot of files, it would faster to run rubocop once for all changed files:

if git diff HEAD --name-only | grep -q -E '\.rb$'; then
  ERRORS+=("$(git diff HEAD --name-only | grep -E '.rb' | xargs bundle exec rubocop | grep -e 'C:' -e 'E:')")
fi

@mckeed
Copy link

mckeed commented Oct 6, 2023

Here's another version that runs on all the files in parallel and also checks that you don't have unstaged changes to files you are committing, as that can lead to rubocop passing when it really shouldn't (like if you fix a violation but forget to stage the change):

https://gist.github.com/mckeed/baa6ce5d85b28ef5690bfa025890fb0d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment