I'm Fxcop Analyzer style checker.
I want to run/call Fxcop from pre-commit, so if Fxcop finds any error or fields so Git Hook pre-commit should stop commit process.
I had never programmer in Bash and it is the first time I am working with git hook.
Any suggestions that I could use to set this up would be great.
For running FxCop as a
pre-commithook, you'll need to install FxCop as nuget package and then build the project withMSBuildas you would from the command-line. WhenMSBuilddetects any errors, it will exit with a non-zero exit code. Whengitdetects that thepre-commithook exited with a non-zero exit code, it will prevent the commit from going through.As per Microsoft's documentation, this is the command-line you can use:
Git uses
bashfor executing hook files even on Windows, so the command-line syntax will be a bit different than the regularcmd. You can try the following as thepre-commithook file:That's assuming
ConsoleApp1.csprojis located at the root of your local git repository folder, and I'm using Visual Studio 2019 (16.4.5), so you may need to adjust it based on your environment.It should work for either .Net or .Net Core projects.
Note that if there are just warnings, then
MSBuildwill not exit with a non-zero exit code. You can configure FxCop severity if you want some rules to fail as error, or useMSBuildwith warnAsError switch.Also, depending on how long the build takes, you may want to consider using a pull-request build pipeline, instead of the
pre-commithook.