Git - prevent to commit code containing debugging functions

Public

It's not a snippet, but very useful git hook which is made by my friend and adapted for Drupal and php by me ;)
To use it you have to add file called 'pre-commit' to .git/hooks, copy and past the following code and make it executable.
Also You can copy or clone this hook directly from github:
https://github.com/fadehelix/git-hooks/blob/master/hooks/pre-commit

Get raw version
bash
  1. #!/bin/bash
  2.  
  3. if git rev-parse --verify HEAD >/dev/null 2>&1
  4. then
  5. against=HEAD
  6. else
  7. against=f737485baee8b0edc5ef4d00c498305578d630fd
  8. fi
  9.  
  10. # Redirect output to stderr.
  11. exec 1>&2
  12.  
  13. keywords=(dpm dvm print_r var_dump throw console.log debugger)
  14. keywords_for_grep=$(printf "|%s" "${keywords[@]}")
  15. keywords_for_grep=${keywords_for_grep:1}
  16. files_changed=`git diff-index --name-only HEAD --`
  17. `git grep -q -E "$keywords_for_grep" $files_changed`
  18. number_of_results=$?
  19.  
  20. if [ $number_of_results -eq 0 ]
  21. then
  22. echo "# Check following lines:"
  23. for FILE in ${files_changed[*]} ; do
  24. for keyword in ${keywords[*]} ; do
  25. `git grep -q $keyword $FILE`
  26. result_for_file=$?
  27. if [ $result_for_file -eq 0 ]
  28. then
  29. LINE=`git grep -n $keyword $FILE | awk -F ":" '{print $2}'`
  30. echo "$FILE contains $keyword at line $LINE."
  31. fi
  32. done
  33. done
  34. exit 1
  35. fi

Comments

Anonymous's picture

Thank you