How to integrate PHP_CodeSniffer with Git repositories?

At work, we used a SVN server and enforced our project coding standard with a pre-commit hook on the server that ran PHP_CodeSniffer.

Whenever a developer tried to commit some code that does not match the standard, he got it rejected. To be able to check in legacy code, our commit hook recognized special commit message tags like "SKIP_CS". Developers using that were logged and beaten at the end of the week when they used it too often :)

Problems with Git

Now we switched to Git with a self-hosted Gitorious instance as frontend but unfortunately lost the Coding Standard enforcement. Let me explain that:

With Git, you commit locally - thus your pre-commit hooks need to run locally on the developer's machine. In our case, this might be a Linux, Mac or Windows machine - which means that I can't use a bash script. Also, the developer needs to set it up himself because when you clone a Git repository, you do not get hooks from the server. That's not even possible.

Apart from the setup issue there is no way to enforce the check because one can pass the --no-verify commit parameter and the pre-commit hook is not executed.

Server-side hook

The only way to enforce the standard is a pre-receive hook on our central Git repository server that all devs push to. Just installing the SVN hook on it isn't the solution, though:

I think the solution is to create a list of files that were changed in all the commits the developer tries to push and look at their current state. This way, the developer can commit a fix and push it together with the rest of the history to the server.

Several issues remain:

What now?

Do you have an idea how to solve that problem? Please comment on the question on StackOverflow or send me a mail.

Written by Christian Weiske.

Comments? Please send an e-mail. Or Reply or Like.