epergo
7/12/2017 - 9:50 AM

Git pre-commit hook to detect some words like binding.pry, debugger...

Git pre-commit hook to detect some words like binding.pry, debugger...

#!/bin/sh
# Redirect output to stderr.
exec 1>&2
# enable user input
exec < /dev/tty

consoleregexp='console.log|debugger|binding.pry'
# CHECK
if test $(git diff --cached | grep -E $consoleregexp | wc -l) != 0
then
  files_changed=$(git diff --cached --name-only --)
  for file in $files_changed
  do
    if test $(grep -E $consoleregexp $file | wc -l) != 0
      then
        echo 'File:' $file':'$(grep -E -n $consoleregexp $file | cut -f1 -d:)
        grep -E -ne $consoleregexp $file
        echo
      fi
  done
  echo "There are some occurrences of $consoleregexp at your modification."
  read -p "Are you sure want to continue? (y/n)" yn
  echo $yn | grep ^[Yy]$
  if [ $? -eq 0 ]
  then
    exit 0; # Continue
  else
    exit 1; # Not continue
  fi
fi