Skip to content

Instantly share code, notes, and snippets.

@3rdp
Forked from gerad/pre-commit.sh
Last active June 6, 2019 06:52
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 3rdp/e024a008ec9e0e3df31279042b02a58f to your computer and use it in GitHub Desktop.
Save 3rdp/e024a008ec9e0e3df31279042b02a58f to your computer and use it in GitHub Desktop.
git pre-commit hook that checks for debugger / console.log
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by git-commit with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, make this file executable.
if git-rev-parse --verify HEAD 2>/dev/null
then
git-diff-index -p -M --cached HEAD --
else
# NEEDSWORK: we should produce a diff with an empty tree here
# if we want to do the same verification for the initial import.
:
fi |
perl -e '
# eslint airbnb config is pretty good at catching most of errors
# prettier is pretty good at maintaining consitent code formatting
# this script is solely needed to catch console.log and debug statements
my $found_bad = 0;
my $filename;
my $reported_filename = "";
my $lineno;
sub bad_line {
my ($why, $line) = @_;
if (!$found_bad) {
print STDERR "* [debug pre-commit] You have some suspicious patch lines:\n";
print STDERR "*\n";
$found_bad = 1;
}
if ($reported_filename ne $filename) {
print STDERR "* In $filename\n";
$reported_filename = $filename;
}
print STDERR "* $why (line $lineno)\n";
print STDERR "$filename:$lineno:$line\n";
}
$last_line = "";
while (<>) {
if (m|^diff --git a/(.*) b/\1$|) {
$filename = $1;
next;
}
if (/^@@ -\S+ \+(\d+)/) {
$lineno = $1 - 1;
next;
}
if (/^ /) {
$lineno++;
next;
}
if (s/^\+//) {
$lineno++;
chomp;
if (/debug/) {
bad_line("debug statement/import left in", $_);
}
if (/debugFn/) {
bad_line("debugFn statement left in", $_);
}
if (/log/) {
bad_line("log statement left in", $_);
}
}
}
exit($found_bad);
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment