Setup Laravel Pint in Git Pre-Commit Hook without using any dependencies
Without even needing to depend upon Husky and npm to setup

Former Full-stack Engineer at Nexlabs
Undergrad SE @ CAMT - Chiang Mai University
Laravel Pint is an opinionated automated code styling fixer. It is based on PHP-CS Fixer, but you don't need to worry about anything about what is going underneath.
You just need to install Pint, run ./vendor/bin/pint, and boom; your project is now fixed with Laravel's code styling. It's that simple.
But what is not simple is that we need to ensure every developer in the project team uses Pint. However, it is not an easy job to enforce that every developer runs it manually each time they commit a change.
So what about using a node package like Husky to add Pint to the pre-commit?
It might work, but it needs to run npm install on each developer's machine to setup the hook.
So what if the project doesn't use npm or even javascript at all?
That's when we have to think about alternatives. In this tutorial, I will use the bare-bone pre-commit hook to setup Pint.
Setup
Install Laravel Pint
Laravel Pint now comes out of the box with all new Laravel Applications.
If you are working on an old project, run the following:
composer require laravel/pint --dev
Create a folder called
.hooksin the project rootThen create a file called pre-commit and give appropriate execute permission by running the below command.
cd .hooks/ sudo chmod 754 pre-commitAdd the following bash script to the pre-commit file. This script will run whenever you and anyone try to commit this project with git.
#!/bin/sh # Run Laravel Pint # This script will run Laravel Pint on newly staged PHP Files. files=$(git diff --cached --name-only --diff-filter=AMCR | grep "\.php$") if echo "$files" | grep --quiet "\.php$"; then echo "Running Laravel Pint..." ./vendor/bin/pint $files && git add $files fiThen, we will register with the composer's
post-autoload-dumphook to register this git hook when someone setup the project. Add this line to thecomposer.jsonfile'sscripts.post-autoload-dumpof your project.git config core.hooksPath .hooksIt should look something like this in your
composer.jsonfile:
Just run the
composer installcommand again, and the pre-commit hook will be registered. This should also automatically install this git hook in your teammates' machines whenever they runcomposer installor anything that triggers thepost-autoload-dumphook such ascomposer dump-autoloadcommand.Now, you have successfully configured
pre-commithook when you see Laravel Pint running when committing something. :)



