Skip to content

Instantly share code, notes, and snippets.

@ZackMattor
Created April 18, 2013 16:18
Show Gist options
  • Select an option

  • Save ZackMattor/5414046 to your computer and use it in GitHub Desktop.

Select an option

Save ZackMattor/5414046 to your computer and use it in GitHub Desktop.
This pre-commit hook searches all of your staged files for debug statements (defined in the grep_search variable), and fails to commit if they are present. It also asks you if you want to have them automatically removed for your commit.
#!/bin/sh
txtrst=$(tput sgr0) # Text reset
txtred=$(tput setaf 1) # Red
exit_status=0
staging_modified=0
grep_search="binding.pry\\|console.log\\|debugger"
echo ""
for FILE in `git diff --cached --name-only --diff-filter=ACM` ; do
grep -q $grep_search $FILE
if [[ $? -eq 0 ]] ; then
exit_status=1
echo " In File: '"$FILE"'"
arr=($(grep -n $grep_search $FILE |cut -f1 -d:))
grep -on $grep_search $FILE
delete_string=""
for i in ${arr[@]}
do
delete_string="${delete_string}${i}d;"
done
if [[ ${#arr[@]} -gt 0 ]] ; then
exec < /dev/tty
while true; do
read -p "Do you want me to remove these(y/n)?" yn
case $yn in
[Yy]* ) sed $delete_string $FILE > SED_TEMP_FILE.tmp;
mv SED_TEMP_FILE.tmp $FILE;
staging_modified=1
break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
fi
fi
done
if [[ $staging_modified -eq 1 ]] ; then
exit_status=0
git add .
#git commit -m "Auto remove of debug files"
fi
if [[ $exit_status -eq 1 ]] ; then
echo "${txtred}Commit failed, see above${txtrst}"
fi
exit $exit_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment