开发者

Using Subversion, how can I cut from one file and paste to another preserving history

The situation is that I've spent some time messing around with some experimental code. I now want to move part of that code - about 500 lines - into another file, but I don't want to lose the history, as I would if I do a simple text-editor cut and paste.

As close as I know how to get is separating the code out of the original file - svn copy, then delete unwanted stuff from both copies. But I don't know how to then append that partial copy onto an existing file, keeping the history from both.

The reason this is important is basically that the code is just pretty specialised stuff, to help implement some higher level functions. I don'开发者_开发问答t want it polluting global namespaces, so I want it all in the one file where it will be used and wrapped in an anonymous namespace.

I realise this sounds like merging a branch back into the trunk. The thing is, there is no branch. The experimental code didn't start as a copy of anything - it's just a bunch of started-from-scratch code. The file I want to cut from and the one I want to paste into are completely independent files.

I mostly use TortoiseSVN, but have command-line subversion installed too.


You can merge all revisions (or specific revisions) of one file into another like this

svn merge sourcefile targetfile -r 0:HEAD

At first I thought one would have to use the --ignore-ancestry option (since both files don't share any common history) but apparently this is not necessary. I tested with svn 1.6.3.

You are of course very likely to get alot of conflicts markers in the merge result. It may be easier to do the merge by hand (a copy and paste merge as you say), and then run the above merge command with --record-only to tell subversion about it.

After the merge, the targetfile will have a svn:mergeinfo property which indicates which commits from sourcefile where merged into it. When you examine the log of targetfile, you can see the history of both files by using the --use-merge-history option. TortoiseSVN has the same feature in the form of a checkbox in the log form.


I don't think you can preserve history in the way you are describing. SVN keeps track of history on a file-by-file basis, and it won't keep track of two separate files that are combined together on the same code line.

If you started out with two separate files and then combine them together into a third, then the history of both will be preserved. If you combine one into the other, then the history of one of them will be "lost" in the sense that you won't be able to link back to the history of the "deleted" file just from looking at the history.

I guess what you could do is in the commit message just note that the content from the other file was combined and then commit the delete in the same commit.


If you want to copy in a new file and delete the old file :

svn mv

If you want to duplicate in a new file :

svn copy

If both file already exists :

# copy/paste with a text editor

You can delete a file an keep its history with :

svn del

With SVN you cannot keep trace of the history of a merge of two file. You can merge it by hand and keep a trace in the commit message.


Can't you leave the code in its own file (after trimming the parts you don't want to keep) and include this file into your 'real' source file?

// file foo.cpp:
...
namespace {
#  include "util_code.inc"
}

Not really orthodox, but should work...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜