Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVN automatic code formatting on check in?

Tags:

svn

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

like image 265
SuperSuperDev1234 Avatar asked Jun 19 '09 13:06

SuperSuperDev1234


4 Answers

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.

like image 167
Michael Wiles Avatar answered Sep 27 '22 23:09

Michael Wiles


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.

like image 22
mog Avatar answered Nov 15 '22 19:11

mog


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.

like image 27
Stefan Avatar answered Nov 15 '22 18:11

Stefan


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.

like image 15
Yoopergeek Avatar answered Nov 15 '22 19:11

Yoopergeek