Last active
April 19, 2024 19:19
-
-
Save bkiahstroud/c1a42f6ecc81cfc2d8a6c3f52f5c2680 to your computer and use it in GitHub Desktop.
Run rubocop via Docker Compose as a git 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 | |
# Get a list of all staged Ruby (.rb) files | |
RUBY_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.rb$') | |
# If there are no Ruby files, skip the check | |
if [ -z "$RUBY_FILES" ]; then | |
exit 0 | |
fi | |
# Run Rubocop on the staged Ruby files | |
echo "Running Rubocop on staged Ruby files..." | |
docker compose exec -T web bundle exec rubocop -P $RUBY_FILES | |
# Check if Rubocop passed | |
if [ $? -ne 0 ]; then | |
echo "Rubocop detected problems, aborting commit." | |
exit 1 | |
fi | |
# If we got here, everything is OK | |
echo "Rubocop checks passed." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions
Setup
cd your/repo
touch .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
pre-commit
file below into.git/hooks/pre-commit
Test
docker compose up web
.rb
file that causes a rubocop warninggit add
the filegit commit -m "test"
You should see the output of
rubocop
and it should prevent the commit from going through