pre-commit 788 B

123456789101112131415161718192021222324252627282930313233
  1. #!/bin/bash
  2. pass=true
  3. files=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(php|phtml)$')
  4. if [ "$files" != "" ]; then
  5. # Run php syntax check before commit
  6. while read -r file; do
  7. php -l "$file"
  8. if [ $? -ne 0 ]; then
  9. pass=false
  10. fi
  11. done <<< "$files"
  12. # Run php-cs-fixer validation before commit
  13. echo "$files" | xargs ./vendor/bin/php-cs-fixer fix --diff --config .php_cs.dist
  14. if [ $? -ne 0 ]; then
  15. pass=false
  16. fi
  17. # Automatically add files that may have been fixed by php-cs-fixer
  18. echo "$files" | xargs git add
  19. fi
  20. if $pass; then
  21. exit 0
  22. else
  23. echo ""
  24. echo "PRE-COMMIT HOOK FAILED:"
  25. echo "Code style validation failed. Please fix errors and try committing again."
  26. exit 1
  27. fi