Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
Learn how to automate code formatting using Laravel Pint during Git Commit with a pre-commit hook. By following this method, you can ensure that your code is always formatted according to Laravel Pint standards, leading to better code quality and readability.
To automatically format code using Laravel Pint during Git Commit, you can set up a pre-commit git hook in your Git repository that runs the Laravel Pint command to format your code before it is committed. This simple snippet ensures that your code is consistently formatted and adheres to the Laravel Pint coding standards, which can improve code readability and maintainability.
Also Read: Useful Github Repositories for Developers
composer require laravel/pint --devThen, go to your root directory, find .git/hooks and create a copy of the pre-commit.sample hook file and make it executable using the following commands:
cp ./git/hooks/pre-commit.sample ./git/hooks/pre-commitchmod +x ./git/hooks/pre-commitOpen that pre-commit file in your editor and paste the following bash script
Also Read: Laravel Livewire Comments
#!/bin/sh
files=$(git diff --cached --name-only --diff-filter=ACMR -- '*.php');
vendor/bin/pint $filesfiles variable stores the list of PHP files that have been changed and are ready to be committed. git diff command is used here to retrieve a list of changed files that are staged for commit. --cached option specifies that only staged changes should be considered. --name-only option is used to output only the file names, without any additional details. --diff-filter=ACMR option limits the output to files that are Added, Copied, Renamed or Modified. *.php, which specifies that only PHP files should be considered.pint command on thees changed files on every Git commit.Also Read: Laravel Responsables
In summary, this script retrieves the list of changed PHP files that are staged for commit and runs Laravel Pint on each file to automatically format it according to Laravel Pint standards before the changes are committed.
Also Read: Cool projects on Github