Last active
January 25, 2023 08:03
-
-
Save unikitty37/e6a9687a1462803f46c8fa4f107af0f2 to your computer and use it in GitHub Desktop.
Git pre-commit hook to reject RSpec files with 'focus: true' in them
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 | |
function check_diffs () { | |
FOLDER_PATTERN="$1" | |
FILES_PATTERN="$2" | |
FORBIDDEN="$3" | |
FORBIDDEN_PRINTABLE="${4:-$3}" | |
CHANGED_FILES=$(git diff --cached --name-only | egrep $FOLDER_PATTERN | egrep $FILES_PATTERN) | |
OK=1 | |
for FILE in $CHANGED_FILES ; do | |
git diff --cached --no-color -U0 $FILE | egrep '^\+[^\+]' | egrep -q "$FORBIDDEN" | |
if [[ "$?" -eq 0 ]]; then | |
OK=0 | |
echo "$FILE" | |
fi | |
done | |
if [[ "$OK" -eq 0 ]]; then | |
echo "COMMIT REJECTED: found '$FORBIDDEN_PRINTABLE' references. Please remove them before committing." | |
exit 1 | |
fi | |
} | |
check_diffs '^spec/' '\.rb(\..+)?$' 'focus:\s*true' 'focus: true' | |
check_diffs '^(app|spec)/' '\.e?rb(\..+)?$' 'byebug' | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can it be somehow hooked to https://github.com/pre-commit/pre-commit-hooks?