Ensure coding-style during a git commit [closed]
Asked Answered
N

3

10

Im my company i set-up a continuous integration test and i run the tests when someone push the code on the server.

Now i want to check that the code match with the our basic coding rules, the first rule is "run mogrify on your code!"

There is something to do this check "out the shelf"? the output of this analisys can be stored on a file or something else.

thanks

Nephrosis answered 2/12, 2013 at 22:39 Comment(2)
I'm sure you can use git hooks to do something but I don't know enough to provide an answer. Alternatively you could enforce a coding style with a utility like EditorConfig, which plugs into a lot of code editors and is pretty simple to use: editorconfig.orgAureole
phpcs may help you check 'basic coding rules', if you use standard it support, eg: MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and ZendTenth
L
1

I would suggest using a lint like tool, e.g. for ObjectC you could use oclint, but basically any coding standard verification tool that can output to either text files or to stdout - you can then use a python script, (since python is one of the default languages for hooks), or almost any thing that can parse that output, and compare it to a given benchmark then return 0 if the code is no worse than before and 1 if it is.

This can then be used as a hook, either pre-commit locally or pre-receive on the server, (or even both).

Alternatively if you are concerned about the developer having actually run a given tool you can always put a wrapper around the tool that saves, as a part of the committed code, something like an MD5 of the code at the point that the tool was last run and you can write a pre-commit/receive hook that checks that the MD5/whatever of that file matches that of the committed code.

Levy answered 4/12, 2013 at 7:41 Comment(2)
WOW! OCLint is awesome! how I worked until now wihtout this tool? :) it's excatly what i was looking for. thanks!Nephrosis
@Nephrosis How were you able to run OCLint from git hook? I now know how a git pre-commit hook works. I even know what does OCLint do. But can you please help me on how do we run OCLint from git pre-commit hook? Any help will be appreciated.Stegodon
M
6

During a git commit, you could ask your users to setup a pre-commit hook which could run the test, and block the commit.

But you don't have a guarantee that your users actually follow that policy (or bypass it with a git commit --no-verify).

So you should rather put a pre-receive hook in your central repository (the one where all developers are pushing), in order to reject the push if you detect that your tool hasn't been properly run.

See Git Hooks.

Regarding tools, uncrustify can be set as a pre-commit hook, but that can be a bit slow. But it has been used that way before.
Alternatives are listed in this thread but seem quite obsolete.

Your idea to apply on the server side could work, as a pre-receive hook (meaning you would reject the push if you detect differences between the code pushed and the same code received and "uncrustified").

Mochun answered 3/12, 2013 at 4:5 Comment(2)
thanks, i know git-hooks i use a hook to build&run my unit tests. I'm looking for a toot that verify if the code respect our rules. My first idea is to run uncrustify on source code and compare the result with the original files, if there are some difference it means that the developer didn't run uncrustify before commit.Nephrosis
@Nephrosis which language do you want to check? ObjectiveC?Mochun
L
1

I would suggest using a lint like tool, e.g. for ObjectC you could use oclint, but basically any coding standard verification tool that can output to either text files or to stdout - you can then use a python script, (since python is one of the default languages for hooks), or almost any thing that can parse that output, and compare it to a given benchmark then return 0 if the code is no worse than before and 1 if it is.

This can then be used as a hook, either pre-commit locally or pre-receive on the server, (or even both).

Alternatively if you are concerned about the developer having actually run a given tool you can always put a wrapper around the tool that saves, as a part of the committed code, something like an MD5 of the code at the point that the tool was last run and you can write a pre-commit/receive hook that checks that the MD5/whatever of that file matches that of the committed code.

Levy answered 4/12, 2013 at 7:41 Comment(2)
WOW! OCLint is awesome! how I worked until now wihtout this tool? :) it's excatly what i was looking for. thanks!Nephrosis
@Nephrosis How were you able to run OCLint from git hook? I now know how a git pre-commit hook works. I even know what does OCLint do. But can you please help me on how do we run OCLint from git pre-commit hook? Any help will be appreciated.Stegodon
G
0

I would recommend setting up a jenkins server with appropiate plug-ins. This CI server would then run what ever test you like on all commits to the git; module tests, coding style enforcers, system tests etc

For code style checker you can use: For C# - http://joel.fjorden.se/static.php?page=CodeStyleEnforcer

Glosso answered 5/12, 2013 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.