YannBergonzat

Git pre-commit hook

Feb 13th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.71 KB | None | 0 0
  1. #!/bin/bash
  2. # Author: Nikolaos Dimopoulos
  3. # Based on code by Remigijus Jarmalavičius  
  4. # Checks the files to be committed for the presence of print_r(),
  5. # var_dump(), die()
  6. # The array below can be extended for further checks
  7.  
  8. checks[1]="var_dump("
  9. checks[2]="print_r("
  10. checks[3]="die"
  11.  
  12. element_count=${#checks[@]}
  13. let "element_count = $element_count + 1"
  14.  
  15. ROOT_DIR="$(pwd)/"
  16. LIST=$(git status | grep -e '\#.*\(modified\|added\)')
  17. ERRORS_BUFFER=""
  18. for file in $LIST
  19. do
  20.     if [ "$file" == '#' ]; then
  21.         continue
  22.     fi
  23.     if [ $(echo "$file" | grep 'modified') ]; then
  24.         FILE_ACTION="modified"
  25.     elif [ $(echo "$file" | grep 'added') ]; then
  26.         FILE_ACTION="added"
  27.     else
  28.         EXTENSION=$(echo "$file" | grep ".php$")
  29.         if [ "$EXTENSION" != "" ]; then
  30.  
  31.             index=1
  32.             while [ "$index" -lt "$element_count" ]
  33.             do
  34.                 echo "Checking $FILE_ACTION file: $file [${checks[$index]}]"
  35.                 ERRORS=$(grep "${checks[$index]}" $ROOT_DIR$file >&1)
  36.                 if [ "$ERRORS" != "" ]; then
  37.                     if [ "$ERRORS_BUFFER" != "" ]; then
  38.                         ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
  39.                     else
  40.                         ERRORS_BUFFER="$ERRORS"
  41.                     fi
  42.                     echo "${checks[$index]} found in file: $file "
  43.                 fi
  44.                 let "index = $index + 1"
  45.             done
  46.         fi
  47.     fi
  48. done
  49. if [ "$ERRORS_BUFFER" != "" ]; then
  50.     echo
  51.     echo "These errors were found in try-to-commit files: "
  52.     echo -e $ERRORS_BUFFER
  53.     echo
  54.     echo "Can't commit, fix errors first."
  55.     exit 1
  56. else
  57.     echo "Commited successfully."
  58. fi
Advertisement
Add Comment
Please, Sign In to add comment