views:

384

answers:

6

We have a web application framework that we want to use with Subversion. We've tried a couple of times before to set it up, but the code generation part of our application is causing problems.

The problem is that the generated code from one developer can be in a newer file than from another developer, but the content of the newer file can be older, because the code and xml files that are the basis for the code generation has been update by developer nr2.

One solution we've looked at is to exclude the code generated files, but we often get new files that are generated, and they are automatically added to the repository unless we remember to exclude it, and we have to manually check in the latest generated files. And how do you know if you have the latest generated file?

Any suggestion on how to solve this in Subversion?

+5  A: 

You should use svn:ignore, because you really don't want to commit the auto-generated stuff to subversion at all.

krosenvold
@krosenvold: not sure I'd agree with that. If you want to rollback to a point in time, not having the generated files in SCC makes things very difficult. I've always put generated code in SCC.
Mitch Wheat
@Mitch, why can't you regenerate files after rollback?
Juha Syrjälä
I suppose it depnds what you are generating them off....
Mitch Wheat
I think some platforms may have some toolchain issues that can make reproducing a given state harder. If you're working on those I'd think it may change behaviour somewhat.
krosenvold
+2  A: 

Maybe I'm simple, but I wouldn't store generated files in version control. Since it's generated it doesn't make sence to keep a version history of it.

With the code and xml to do the generating in version control you can always also generate the code for every version.

Onots
+10  A: 

One solution we've looked at is to exclude the code generated files

Yes! A thousand times yes! Never do version control of files you can generate from other files. Instead, add the generation rules to your makefile (or other build script).

they are automatically added to the repository

What is automatically adding them? As far as I can recall, I've always had to add files manually. If the tool that automatically adds is wrong, then maybe fixing that tool is the right way to go?

unless we remember to exclude it

Have you generation tool add a comment saying "file autogenerated by $TOOL", and add a subversion hook which greps the files for that comment and rejects them. Make suitable provisions for the code generation tools; such as: if the file contains the "autogenerated" comment, add it anyways if it also has "svn rejection except" in a comment on the same line(s).

And how do you know if you have the latest generated file?

By generating it from the latest recipe, which is in the latest subversion commit.

Jonas Kölker
+1. Using grep to automatically check for autogenerated-ness is a good idea.
j_random_hacker
+23  A: 

The best way to decide to whether something should be in Subversion is to remember that it's a version control system. If you don't need to remember how it changed over time, it doesn't need to be in svn. Use svn:ignore to exclude files from such consideration.

That's the case here: you don't care how the generated files changed over time, only the original code used to produce them. That means they should be excluded from Subversion. You can do this with a pre-commit hook -- enforcing, for example, that all code-generated files shouldn't ever be committed. I have a favorite phrase for this tactic: "Version the recipe, not the cake."

And how do you know if you have the latest generated file?

Exactly; you don't, because the generated files were dependent on the source used to create them. Your next step is to make sure that your build process automatically generates these files given the initial source code, but that's probably a separate StackOverflow question if you don't know how to do that already.

John Feminella
+1. "Version the recipe, not the cake" -- very nice!
j_random_hacker
A: 

Another good stratergy is not to write code in generated files. Keep them seperate. Then no matter who generates them, it won't matter.

Keep your logic out of a generated file.

Aditya
I don't think the author was talking about **manually editing autogenerated source code**, which is something to avoid like the plague.
j_random_hacker
A: 

Name generated file something like "className.autoGenerated.cs" and add recursive svn:ignore attribute with the corresponding name pattern "*.autoGenerated.cs".

Konstantin Spirin