Created
April 18, 2013 16:18
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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