Since switching from SVN to Git, we lost the ability to enforce our coding standards through a pre-commit
hook on the subversion server.
With Git, you only have pre-commit hooks on the client which cannot be enforced in any way. What makes it worse is that we have developers working with all three main operating systems, thus a pre-commit hook that works on Linux or OS X does not automatically work on Windows.
The way to go is implementing a pre-receive
hook on the server, but the solution is not as easy as it seems:
Imagine the developer did 20 commits and wants to push them. All pre-commit and pre-receive hooks I know of (1, 2) just check the single commits, which will ultimately fail and prevent the push. Now the developer fixes the issues and does another commit, and tries to push again. Since the hooks check the single commits, it will fail again.
So we need a pre-receive
hook that generates a list of all changed files in all commits that are going to be pushed and runs phpcs on the current state only.
Does such a hook script exist already? Where?
Edit: There seems to be a script that creates that list of files - unfortunately in Python, but that can be ported. I'm still interested in pre-made solutions with PHPCS :)
Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable. Note 📝 – If you do not have your terminal setup in GitKraken Client, please review the Start Here Tips for setup details.
Pre-receive hooks enforce rules for contributions before commits may be pushed to a repository. Pre-receive hooks run tests on code pushed to a repository to ensure contributions meet repository or organization policy. If the commit contents pass the tests, the push will be accepted into the repository.
pre-commit will now run on every commit. Every time you clone a project using pre-commit running pre-commit install should always be the first thing you do. If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> .
Just run git commit . You don't have to add anything before doing this, hence in the end you get the message no changes added to commit .
I would rather not wait for a server-side hook to control a push.
You could setup an intermediate repo which would fetch each developer's branches very regularly and audit each new commit, sending an email if a commit fails to meet some pre-defined criteria.
You can also have pre-receive hook on the central repo, but at least the developer would be aware of a potential issue sooner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With