Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Directory Restructuring

I have a source code of about 500 files in about 10 directories. I need to refactor the directory structure - this includes changing the directory hierarchy or renaming some directories.

I am using svn version control. There are two ways to refactor: one preserving svn history (using svn move command) and the other without preserving. I think refactoring preserving svn history is a lot easier using eclipse CDT and SVN plugin (visual studio does not fit at all for directory restructuring).

But right now since the code is not released, we have the option to not preserve history.

Still there remains the task of changing the include directives of header files wherever they are included. I am thinking of writing a small script using python - receives a map from current filename to new filename, and makes the rename wherever needed (using something like sed). Has anyone done this kind of directory refactoring? Do you know of good related tools?

like image 389
amit kumar Avatar asked Feb 27 '09 06:02

amit kumar


2 Answers

If you're having to rewrite the #includes to do this, you did it wrong. Change all your #includes to use a very simple directory structure, at mot two levels deep and only using a second level to organize around architecture or OS dependencies (like sys/types.h).

Then change your make files to use -I include paths.

Voila. You'll never have to hack the code again for this, and compiles will blow up instantly if something goes wrong.

As far as the history part, I personally find it easier to make a clean start when doing this sort of thing; archive the old one, make a new repository v2, go from there. The counterargument is when there is a whole lot of history of changes, or lots of open issues against the existing code.

Oh, and you do have good tests, and you're not doing this with a release coming right up, right?

like image 130
Charlie Martin Avatar answered Oct 15 '22 03:10

Charlie Martin


I would preserve the history, even if it takes a small amount of extra time. There's a lot of value in being able to read through commit logs and understand why function X is written in a weird way, or that this really is an off-by-one error because it was written by Oliver, who always gets that wrong.

The argument against preserving the history can be made for the following users:

  • your code might have embarrassing things, like profanity and fighting among developers
  • you don't care about the commit history of your code, because it's not going to change or be maintained in the future

I did some directory refactoring like this last year on our code base. If your code is reasonable structured at the beginning, you can do about 75-90% of the work using scripts written in your language of choice (I used Perl). In my case, we were moving from set of files all in one big directory, to a series of nested directories depending on namespaces. So, a file that declared the class protocols::serialization::SerializerBase was located in src/protocols/serialization/SerializerBase. The mapping from the old name to the new name was trivial, so that doing a find and replace on #includes in every source file in the tree was trivial, although it was a big change. There were a couple of weird edge cases that we had to fix by hand, but that seemed a lot better than either having to do everything by hand or having to write our own C++ parser.

like image 45
James Thompson Avatar answered Oct 15 '22 02:10

James Thompson