2

What's the way to go if I want to have a consistent style standard versioned? I want to achieve two things:

  1. Pressing ctrl-k,ctrl-f (auto indenting) formats the code based on a shared standard
  2. Stylecop runs against shared rules

What files do I have to add in my git repository to achieve this?

Giacomo Tagliabue
  • 1,679
  • 2
  • 15
  • 31

1 Answers1

1

You can add a pre-commit hook.

You can version in your git repo a hook script (say pre-commit.sh) at the root of your project and include the installation instructions in your README/documentation to encourage all developers use it.

Installation is nothing more than:

ln -s ../../pre-commit.sh .git/hooks/pre-commit

As the article "Tips for using a git pre-commit hook", you need, in that hook, to ensure that code that isn't part of the prospective commit isn't tested within your pre-commit script:

# pre-commit.sh
git stash -q --keep-index

# Test prospective commit
# call format, stylecop...

git stash pop -q
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • That's all very interesting, I always promised myself to learn more about pre-commits, but I don't see how it addresses poinst **1** and **2** – Giacomo Tagliabue Oct 11 '14 at 16:51
  • @GiacomoTagliabue because the committed version would auto-indent, and run stylecop: if stylecop fails, the commit won't be completed. – VonC Oct 11 '14 at 16:52
  • Do you know what's the best way to auto indent c# code? – Giacomo Tagliabue Oct 11 '14 at 23:01
  • @GiacomoTagliabue you can try calling stylecop with http://sourceforge.net/projects/stylecopcli/. And you can call your format tool (like Resharper) from a decicated command: http://stackoverflow.com/q/15580580/6309. You also can have a look at http://www.codemaid.net/ – VonC Oct 12 '14 at 04:36
  • Thanks, I resolved not using the StyleCop cli but the Stylecop msbuild target. as per the autoformatting, I'll look into codemaid. Thnanks also for the idea about the hooks, I might implement one – Giacomo Tagliabue Oct 12 '14 at 04:47