When multiple developers work on one project it is also essential that they all commit source code that matches some formatting rules. Otherwise reviewing changes and merging source code can be a great challenge.

When using git repositories you can install hooks. Hooks are any kind of executables that can be run before something happens in a git repository like creating a commit or updating the refs during a push command.

I developed a small bash script that can be used to execute checkstyle on every push and that rejects changes that have checkstyle errors. The script is simple and easy to configure so I want to share it here.

Just install the following script in the remote repository as a file named pre-receive in the hooks folder. Make sure that the script is executable, e.g. chmod +x pre-receive.

Now you just have to configure the checkstyle.cmd git configuration property. This property must contains the full executable checkstyle command. E.g.

This is all you need to get it running. Now if someone tries to push changes to the remote repository that do not conform to the checkstyle checks you  configured, the remote repository will reject the push. E.g.

checkstyle_rejected_push

The script will reject the push and print out the path of the file that violates the checkstyle checks and the violation count in front of the file path.