-
-
Save myanch200/d6c0b8ad926a6e6d0e522bba39a70a98 to your computer and use it in GitHub Desktop.
Run Rubocop in Git's pre-commit hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
#!/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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment