Skip to content

Instantly share code, notes, and snippets.

@felipepodesta
Forked from guilherme/gist:9604324
Last active January 20, 2022 02:01
Show Gist options
  • Save felipepodesta/ba64e91e0673d3ea81271e0ebef36810 to your computer and use it in GitHub Desktop.
Save felipepodesta/ba64e91e0673d3ea81271e0ebef36810 to your computer and use it in GitHub Desktop.
Git pre-commit hook that detects if the developer forget to remove all the javascript console.log before commit.
#!/bin/bash
# Redirect output to stderr.
exec 1>&2
# enable user input
exec < /dev/tty
consoleRegexp='^[^-].*.*console.log'
filenameRegexp='\(^[^-].*.*console.log(\|^+++\)'
if test "$(git diff --cached | grep -c "$consoleRegexp")" != 0
then
exec git diff --cached | grep -ne "$filenameRegexp" | grep -B 1 "$consoleRegexp"
read -rp "There are some occurrences of console.log at your modification. Are you sure want to continue? (y/n) " yn
if echo "$yn" | grep "^[Yy]$"
then
exit 0; #THE USER WANTS TO CONTINUE
else
exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment