Tutorial :SVN automatic code formatting on check in?



Question:

Is this possible? If so how do I do this?

The code is in C# and we are using TortoiseSVN.

I simply want to auto format the code on every checking.

Thanks a lot


Solution:1

You're touching on holy-war ground. Mussing with people's formatting is asking for pitchforks and torches.

My recommendation: Don't.

Not to mention, if you're talking C#, and your developers are using Visual Studio, VS has a lot of auto-formatting tools. Just by entering that closing curly-brace and VS can/will auto-format your code.

A better solution may be to get all your developers to use the same auto-formatting settings.

Tools -> Options, Text Editor -> C# ->Formatting

These settings are exportable, so if you can get the team to agree to use the same code-formatting settings in VS, you can avoid the trouble of performing it in your source control system which is better left to do what it does best.

If you're hell-bent on doing this, a pre-commit hook, like others have said, is the way to go. If you're SVN server is running on Windows, might I recommend CaptainHook for writing your hook scripts? Plugin-able hookscripts that you can write in any .NET language.


Solution:2

With a pre-commit hook script, you could do that, yes. But I'm sure that you will remove that script after the first commit because you'll get into big problems.

If you modify the data that gets committed, the client doesn't know about that. So after such a commit where your script 'fixes' the formatting of a file, the file content in the repository is different than the files in your working copy. But your working copy still thinks it's up to date with the repository (after all, it's modifications just got committed).

So on the next update, you'll get into hell - broken working copy, angry users, ...

And of course, you might break the build - auto formatting has that effect sometimes.

You can of course implement a hook script that checks for a correct formatting and returns an error if it doesn't, that's perfectly fine.

And since you're using TortoiseSVN, you can try doing the formatting in a client-side pre-commit hook.


Solution:3

I think you can do this using a pre-commit hook in your repository.

Edit To the people who think this a bad idea (I'm not saying it isn't): It's not uncommon for organizations to enforce a specific code style or code formatting. Sometimes these rules can be quite pedantic and strictly enforced, and although they are usually human actions involved in this (i.e. formatting to the correct style before you commit anything to the repository), automating the process can sometimes be useful.

An alternative approach might be to do the verification automatically before the commit, but still allow the commit even if the check fails, but then only send an e-mail or some other notification to indicate that someone didn't follow the style.


Solution:4

like most people here I agree adding a pre-commit-hook that rewrites their code is bad, however you can have a pre-commit hook that rejects code that is not formatted to your coding practices and inform the user of such error.


Solution:5

I highly recommend it.

Use a pre-commit script or better still, find a way to do it automatically in your IDE (pre-commit will push the changed file to the client). Eclipse can auto format on save.

The rationale for this is that if developers format differently you'll find files which have commits on them where the commit is only a formatting change and it will cause endless confusion.

A common formatting pattern is a very good idea. It'll be hard to introduce, but it'll be worth it. All changes will be real changes, and not just formatting changes. In my experience developers will see the benefits and accept it.

I've worked with cvs and java and used jalopy to auto format. We were using a branching system so it was mandatory, and it worked very well.


Solution:6

It is possible, but also a very, very bad idea.

No automatic code formatters are perfect, and I can almost guarantee that it will tick people off.

That said, if you want to do it, look into using pre-commit hooks.

Is the SVN server running on a Windows or Linux system? And what code-formatter do you want to use?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »