pre-commit 637 B

12345678910111213141516171819202122232425
  1. #!/usr/bin/env bash
  2. # get the list of changed files
  3. staged_files=$(git diff --cached --name-only)
  4. # build command to fix files
  5. cmd="$(git rev-parse --show-toplevel)/vendor/bin/php-cs-fixer"
  6. echo "Running php-cs-fixer on edited files"
  7. for staged in ${staged_files}; do
  8. # work only with existing files
  9. if [[ -f ${staged} && ${staged} == *.php ]]; then
  10. # use php-cs-fixer and get flag of correction
  11. "${cmd}" -q fix "${staged}"
  12. # if php-cs-fixer fix works, it returns 0
  13. if [[ $? -eq 0 ]]; then
  14. git add "${staged}" # execute git add directly
  15. fi
  16. fi
  17. done
  18. exit 0 # do commit