Skip to content

Instantly share code, notes, and snippets.

@mcchrish
Forked from dy-dx/pre-commit
Last active May 27, 2016 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcchrish/f2775cd9a5e4f9123f66 to your computer and use it in GitHub Desktop.
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
#!/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 "$?"
@mcchrish
Copy link
Author

Use xargs -r on Linux.

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