Advertisement
slovacus

precommit

Mar 22nd, 2012
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.79 KB | None | 0 0
  1. #!/bin/bash
  2. # Author: Boris Guéry <guery.b@gmail.com>
  3. # https://github.com/borisguery/git-keywords-checker
  4.  
  5. # Add or remove keywords here
  6. KEYWORDS_REGEX="var_dump\(|die\(|Zend_Debug::|print_r\(|console\.(debug|info|log|warn)\("
  7. # Add extensions to check here
  8. EXTENSIONS_REGEX="(.php$|.phtml$|.js$)"
  9.  
  10. ERRORS_BUFFER=""
  11. TEXT_DEFAULT="\\033[0;39m"
  12. TEXT_INFO="\\033[1;32m"
  13. TEXT_ERROR="\\033[1;31m"
  14. TEXT_UNDERLINE="\\0033[4m"
  15. TEXT_BOLD="\\0033[1m"
  16.  
  17. FILES=$(git diff-index --cached --name-only --diff-filter=ACMR HEAD)
  18.  
  19. echo -e "\\033[1;33m""Keywords checker - pre-commit hook" "$TEXT_DEFAULT"
  20. echo
  21.  
  22. for FILE in $FILES; do
  23. if [[ $FILE =~ $EXTENSIONS_REGEX ]]; then
  24. echo -e "$TEXT_INFO" "Checking file: $FILE" "$TEXT_DEFAULT"
  25.         ERRORS=""
  26.         while IFS=: read -ra RESULT; do
  27. if [ "$RESULT" != "" ]; then
  28. ERRORS="$ERRORS\n\tline $TEXT_BOLD${RESULT[1]}$TEXT_DEFAULT: "
  29.                 ERRORS="$ERRORS"$(sed -n ${RESULT[1]}p $FILE | sed -E "s/($KEYWORDS_REGEX)/\\$TEXT_UNDERLINE\1\\$TEXT_DEFAULT/g")
  30.                 if [ "$ERRORS_BUFFER" != "" ]; then
  31. ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
  32.                 else
  33. ERRORS_BUFFER="$ERRORS"
  34.                 fi
  35. fi
  36. done < <(grep -sEnH $KEYWORDS_REGEX $FILE)
  37.         if [ "$ERRORS" != "" ]; then
  38. ERRORS="$TEXT_ERROR Errors found in $TEXT_BOLD$FILE$TEXT_DEFAULT$ERRORS"
  39.             echo -e "$ERRORS"
  40.         else
  41. echo -e "$TEXT_INFO No errors found in $TEXT_BOLD$FILE$TEXT_DEFAULT\n"
  42.         fi
  43. fi
  44. done
  45.  
  46. if [ "$ERRORS_BUFFER" != "" ]; then
  47. echo
  48. echo -e "$TEXT_ERROR" "There were errors or warnings, commit aborted." "$TEXT_DEFAULT"
  49.     echo -e "$TEXT_INFO" "If you are sure you want to commit those files, use --no-verify option" "$TEXT_DEFAULT"
  50.  
  51.     exit 1
  52. else
  53. echo -e "$TEXT_INFO" "All files are clean." "$TEXT_DEFAULT"
  54.     exit 0
  55. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement