-
-
Save mcchrish/f2775cd9a5e4f9123f66 to your computer and use it in GitHub Desktop.
Git pre-commit hook to check for debugging codes and Standard JS violations in a Javascript project
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/bash | |
FILES_PATTERN='\.(css|less|scss|sass|html|ejs|js|jsx)(\..+)?$' | |
FORBIDDEN=( ">>>>>>" "<<<<<<" "======" "console.log" "debugger" ) | |
# the exit code from `grep -E $FILES_PATTERN` gets swallowed unless the pipefail option is set | |
set -o pipefail | |
FORBIDDEN_FOUND=false | |
# grep for forbidden patterns | |
for i in "${FORBIDDEN[@]}" | |
do | |
git diff --cached --diff-filter=ACMR --name-only | grep -E "$FILES_PATTERN" | \ | |
xargs grep --color --with-filename -n "$i" \ | |
&& echo 'COMMIT FAILED: Found' "$i" 'references. Commit with \033[0;32m--no-verify \033[0mto bypass this check.' \ | |
&& FORBIDDEN_FOUND=true | |
done | |
$FORBIDDEN_FOUND && exit 1 | |
# just ignore pipefail exit code | |
set +o pipefail | |
# check for Standard JS violations | |
standard $(git diff --name-only --cached --relative | grep -E '\.(js$|jsx)(\..+)?$') | |
exit "$?" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
xargs -r
on Linux.