Skip to content

Instantly share code, notes, and snippets.

@unikitty37
Last active January 25, 2023 08:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unikitty37/e6a9687a1462803f46c8fa4f107af0f2 to your computer and use it in GitHub Desktop.
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
#!/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
@Hesamedin
Copy link

Can it be somehow hooked to https://github.com/pre-commit/pre-commit-hooks?

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