Skip to content

Instantly share code, notes, and snippets.

@sovetski
Last active August 31, 2023 08:58
Show Gist options
  • Save sovetski/b049bdfa27c00cb285fa4c00ad14fcb4 to your computer and use it in GitHub Desktop.
Save sovetski/b049bdfa27c00cb285fa4c00ad14fcb4 to your computer and use it in GitHub Desktop.
Run husky after any commit, it will run php cs fixer only on the staged files and let you test before committing the formatted verison
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
git diff --name-only HEAD~1..HEAD | grep '\.php$' | xargs ./tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=my_config.php_cs.dist
# You should create a file named "post-commit" in your ".husky" directory
# Change the "my_config.php_cs.dist" by your config file
# Or use this one to exlude "vendor" and "tools" directories
# git diff --name-only HEAD~1..HEAD | grep '\.php$' | grep -Ev '^vendor/|^tools/' | xargs ./tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php
# DON'T FORGET TO RUN "chmod ug+x .husky/*" to make husky files executable
# If you want, you can also directly run this command instead of creating manually the file
npx husky add .husky/post-commit "git diff --name-only HEAD~1..HEAD | grep '\.php$' | grep -Ev '^vendor/|^tools/' | xargs ./tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment