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

女生的网名这么多〃 提交于 2019-12-03 09:04:19

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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...



来源:https://stackoverflow.com/questions/1557658/using-subversion-how-can-i-cut-from-one-file-and-paste-to-another-preserving-hi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!