4

I'm attempting to bring in more continuous integration to my workflow. One challenge has been finding a style checker that integrates well with Git and Github. Ideally I'd like my commits to be rejected if my style checker finds any issues with my code.

There are a number of style checkers for C# and Visual Studio like StyleCop, Resharper, FxCop but I've been unable to get any of these tools to enforce their checks on a level that rejects the commit. I've looked into pre-commit hooks as well but don't really know where to start with getting git to call these tools.

Any suggestions on systems that I could use to set this up would be great.

stenfeio
  • 249
  • 1
  • 11
  • Do you trust your committers? Then tell them to check for this before pushing. If not, they should probably fork and you can check the pull requests using the github api. – Dennis Kuypers Apr 02 '18 at 12:56
  • I've had issues with folks not checking first which is why I'm going to CI route. This adds a lot more steps to the process and kinda of contradicts my objective of building out a continuous integration workflow. – stenfeio Apr 02 '18 at 13:14
  • Hi @stenfeio, I want to run Fxcop from pre-commit, can you share your solution, please? – user8231110 Dec 11 '19 at 22:00

1 Answers1

6

Here is a suggestion to set-up such a system.

To reject a push, you'll need to set up a Git server-side hook, in particular, a pre-receive hook.

On GitHub specifically, I think you need GitHub enterprise to set-up a pre-receive hook to enforce policy. As this post explains:

For obvious reasons, GitHub won’t allow you to execute arbitrary code on their servers as part of Git hooks. The only hooking they allow is through their webhooks or the integrated third-party services. If you need to run some custom code, you will have to host that somewhere and set up a webhook to run it on your own server.

Now, you'll want to run the style-checker on the server-side hook. Ideally, to facilitate running this via script, the style-checker needs to be able to be run from command line.

For example, see ReSharper's InspectCode command line tool. You can have the server-side hook automatically run InspectCode, and process the output accordingly to determine whether the code adheres to your style settings.

(This sounds a bit tedious to set-up and requiring GitHub Enterprise may be a non-starter for you.)

Alternatively, you can set up a git pre-commit hook for this purpose. (The downside is that it'll require the pre-commit hook to be manually installed on every machine, and nothing is enforced server-side.) See this related post for details on how to set-up a pre-commit hook. For example, you could set up a pre-commit hook to run ReSharper's InspectCode, and stop the commit if any code inspections are found. Also, this related post has some similar suggestions on this topic.

As a side note, perhaps there are other practices for accomplishing what you want. You could have a coding style check as part of a pull request & code review workflow, so that code doesn't get merged in if it doesn't satisfy your team's coding standards. It may also be appropriate to discuss and (re-)align with your team on coding styles.

sonnyb
  • 3,194
  • 2
  • 32
  • 42