Skip to content

Instantly share code, notes, and snippets.

@Lycisca
Created July 12, 2017 09:50
Show Gist options
  • Save Lycisca/ae510f97d496acefd58d93fe8cf337ce to your computer and use it in GitHub Desktop.
Save Lycisca/ae510f97d496acefd58d93fe8cf337ce to your computer and use it in GitHub Desktop.
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
@danielRomero
Copy link

Instructions

Download the file to git hooks folder and set execution permission.

curl https://gist.githubusercontent.com/Lycisca/ae510f97d496acefd58d93fe8cf337ce/raw/72c8bdb732058f99b3008b7b2615a3fefd621aea/pre-commit > .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit

When you try to commit a file with any word that match the regexp consoleregexp this program warning you and ask for continue or abort the commit.

Example

$ git commit -m "testing script"
File: lib/testfile.rb:17
17:      binding.pry

There are some occurrences of console.log|debugger|binding.pry at your modification.
Are you sure want to continue? (y/n)n

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