Last active
May 22, 2023 10:21
-
-
Save quangtt-rks/ad3b9974ec85abc8a8e2ef695b99f124 to your computer and use it in GitHub Desktop.
just a pre-commit hook
This file contains hidden or 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 | |
rejected_message() | |
{ | |
echo "COMMIT REJECTED! Please fix your diff.\n\ | |
If you must, run 'git commit --no-verify' to skip this check." && \ | |
exit 1 | |
} | |
#== 1. Check forbidden keywords within modified files, except commented out sections | |
FILES_PATTERN='(rb)' | |
FORBIDDEN='\(binding.pry\|def test_\|def self.test_\|class Test\|module Test\)' | |
COMMENTS='^\s*#' | |
GREP_COLOR='4;5;37;41' | |
if [ "$(git diff --cached --name-only | grep -E $FILES_PATTERN)" ]; then | |
git diff --cached --name-only | \ | |
grep -E $FILES_PATTERN | \ | |
xargs grep --color=always --with-filename -n -v "$COMMENTS" | \ | |
grep --color=always "$FORBIDDEN" && \ | |
rejected_message | |
fi | |
if [ $? -ne 0 ]; then | |
rejected_message | |
fi | |
#== 2. Run ruboccop for sure | |
# Please adjust this rubocop command to match with your local environment | |
docker exec -t my_container bundle exec rubocop --fail-level W --display-only-fail-level-offenses | |
if [ $? -ne 0 ]; then | |
rejected_message | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment