Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: pre-receive hook with PHP_CodeSniffer

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 :)

like image 948
cweiske Avatar asked May 25 '11 06:05

cweiske


People also ask

How do you trigger a pre-commit hook?

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.

What is pre receive hook in git?

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.

Can you commit pre-commit hooks?

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> .

How do you run pre-commit hook without committing?

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 .


1 Answers

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.

like image 106
VonC Avatar answered Oct 04 '22 15:10

VonC