Search code examples
gitvisual-studio-2012stylecop

version control over visual studio formatting style


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?


Solution

  • 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