如何在 SVN 中分支单个文件?
分支的颠覆概念似乎专注于创建整个存储库的[不稳定]分支,并在其上进行开发。 是否有一种机制可以创建单个文件的分支?
对于用例,请考虑具有多个特定于平台的源代码 (*.c) 实现的通用头文件 (*.h)。 这种类型的分支是永久性的。 所有这些分支都将不断发展,偶尔会进行跨分支合并。 这与不稳定的开发/稳定的发布分支形成鲜明对比,后者通常具有有限的生命周期。
我不想想要对整个存储库进行分支(无论便宜与否),因为它会产生不合理的维护量以在主干和所有分支之间不断合并。 目前我正在使用 ClearCase,它具有不同的分支概念,这使得这变得很容易。 我被要求考虑过渡到 SVN,但这种范式差异很重要。 我更关心的是能够轻松地为单个文件创建替代版本,而不是诸如剪切稳定发布分支之类的事情。
The subversion concept of branching appears to be focused on creating an [un]stable fork of the entire repository on which to do development. Is there a mechanism for creating branches of individual files?
For a use case, think of a common header (*.h) file that has multiple platform-specific source (*.c) implementations. This type of branch is a permanent one. All of these branches would see ongoing development with occasional cross-branch merging. This is in sharp contrast to unstable development/stable release branches which generally have a finite lifespan.
I do not want to branch the entire repository (cheap or not) as it would create an unreasonable amount of maintenance to continuously merge between the trunk and all the branches. At present I'm using ClearCase, which has a different concept of branching that makes this easy. I've been asked to consider transitioning to SVN but this paradigm difference is important. I'm much more concerned about being able to easily create alternate versions for individual files than about things like cutting a stable release branch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
SVN 中的分支只是一个副本。 我相信,要按照您希望的方式执行此操作,您必须将文件的每个版本放在存储库中的单独目录中,并将其签入源文件夹中。 IE 将该文件视为一个单独的项目。
A branch in SVN is just a copy. I believe that to do it the way you are hoping to, you'd have to have each version of the file in a separate directory in the repository, and check it out into your source folder. I.E. treat that file like a separate project.
Subversion 中的分支正是您所说的。 除了您更改的文件外,所有文件都是主干的精确副本。 这就是SVN书中谈到的“廉价复制”方法论。 唯一需要注意的是需要不时地将主干合并到分支中,以确保在那里所做的更改反映在分支中。 当然,如果不需要这些更改,则不需要发生主干->分支合并。
允许自动合并主干更改(模拟 Clear Case 范例)的一种简单方法是使用预提交挂钩脚本在提交之前合并主干更改。(事实上,这始终是一个防止代码漂移的好策略)。
A branch in Subversion is exactly what you are talking about. All of the files are an exact copy of the trunk, with the exception of the ones you change. This is the "cheap copy" methodology talked about in the SVN Book. The only caveat is the need to merge the trunk into the branch from time to time to insure that the changes made there are reflected in the branch. Of course, if those changes are not desired, no trunk->branch merges need to happen.
One easy way to allow for trunk changes to be merged in automatically(which simulates the Clear Case paradigm) would be to use a pre-commit hook script to merge the trunk changes in prior to the commit.(in fact, this is always a good strategy to prevent code drift).
我认为分支单个文件没有多大意义? 有没有办法用trunk代码测试一下?
如果您想取消更改并稍后应用它们,您可以打一个补丁。
I don't think there is much point in branching a single file? There is no way to test it with the trunk code?
You could take a patch instead if you want to back out changes and apply them later on.
您确定您的 VCS 中确实需要此功能吗??
为什么不使用 C 预处理器和 #ifdef 删除不需要的代码? 或者任何类似的工具。
例如:
有时,如果它不合适,那么它就不是正确的解决方案。
Are you sure you really need this feature in your VCS ?
Why not use the C preprocessor and #ifdef away the code you don't need ? Or any similar tool.
something like:
Sometimes if it doesn't fit right, then it's not the right solution.
Subversion“分支”只是存储库中某些内容的副本。 因此,如果您想对文件进行分支,只需执行以下操作:
A Subversion "branch" is just a copy of something in your repository. So if you wanted to branch a file you'd just do:
这是我对你的问题的理解。 您有以下树:
并且您需要针对多个架构拒绝它:
此外,在当前的 VCS 中,您可以通过根据需要从 time.c 创建尽可能多的分支来实现此目的,并且当您从 VCS 中签出文件时,您会自动检查最新的文件来自公共主干的 time.h 和来自您正在处理的分支的最新 time.c。
您担心的问题是,如果您在签出分支时使用 SVN,您将不得不经常从主干合并 time.h,或者冒着处理旧文件(与主干相比)的风险,这种开销是不可接受的给你。
根据源代码的结构,可能有一个解决方案。 想象一下,
你可以分支 /,并使用 svn:externals 功能可将您的标头链接到行李箱的标头。 它仅适用于目录,并且在提交回 test.h 方面存在一些限制(您必须进入头目录才能使其工作),但它可以工作。
Here is how I understand your problem. You have the following tree:
and you need to decline it for multiple architectures :
Also in your current VCS you can do this by creating as many branches from time.c as needed and when you checkout the files from the VCS you automatically check the latest time.h from the common trunk and the latest time.c from the branch you are working on.
The problem you are concerned about is that if you use SVN when checking out a branch you will have to merge time.h from trunk very often or risk working on an older file (as compared to the trunk) that amount of overhead is not acceptable to you.
Depending on the structure of your source code, there might be a solution though. imagine that you have
Then you could branch /, and use the svn:externals feature to link your headers to the trunk's head. It only works on directories and bears some limitations with regard to committing back to test.h (you have to go in the header directory for it to work) but it could work.
遗憾的是,我认为真正的答案是 ClearCase 比 Subversion 更好地处理这种情况。 使用 subversion,您必须对所有内容进行分支,但是 ClearCase 允许一种“惰性分支”想法,这意味着只有特定组的文件被分支,其余文件仍然遵循主干(或任何分支)您指定)。
此处提供的其他解决方案并不能真正按照您的预期工作,它们只是将文件复制到不同的路径。 现在你必须做一些奇怪的事情才能实际使用该文件。
呃,抱歉。 这确实不是一个很好的答案。 但 Subversion 没有一个好的解决方案。 它的模型是分支和合并。
编辑:好的,所以扩展一下 crashmstr 所说的内容。 你可以这样做:
但是哇!,这很容易出错。 每当你执行 svn st 时,你都会看到这个:
有点吵。 当您想要在大型源存储库中分支几个文件或模块时,它会开始变得非常混乱。
实际上,这里可能有一个不错的项目,用于模拟 ClearCase 的带有 svn 属性和切换的分支文件之类的东西,围绕 bog 标准 svn 客户端编写一个包装器来处理所有混乱。
Sadly, I think the real answer here is that ClearCase handles this situation a lot better than Subversion. With subversion, you have to branch everything, but ClearCase allows a kind of "lazy branch" idea that means only a certain group of files are branched, the rest of them still follow the trunk (or whichever branch you specify).
The other solutions provided here don't really work as you intend, they are just copying the file to a different path. Now you have to do odd things to actually use that file.
Erm, sorry. That wasn't really a very good answer. But there isn't a good solution to this with Subversion. Its model is branch and merge.
Edit: OK, so expanding on what crashmstr said. You could do this:
But wow!, is that prone to errors. Whenever you do a svn st you will see this:
A bit noisy that. And when you want to branch a few files or modules within a large source repository it will start to get very messy.
Actually, there's probably a decent project in here for simulating something like ClearCase's branched files with svn properties and switching, writing a wrapper around the bog standard svn client to deal with all the mess.
您不必对整个存储库进行分支。 您可以在项目中创建文件夹分支(例如包含文件夹)。 正如其他人所指出的,您还可以仅对单个文件进行“复制”。 一旦您拥有文件或文件夹的副本,您就可以“切换”到分支文件或文件夹以处理分支版本。
如果您在存储库中创建单独的分支文件夹,则可以通过服务器端命令将分支文件复制到那里:
然后您可以切换该文件以使用
branches_files
存储库路径You don't have to branch the entire repository. You could make branches of folders in your project (such as an include folder). As others have noted, you can also do a "copy" of just a single file. Once you have a copy of a file or folder, you "switch" to the branched file or folder to work on the branch version.
If you create a separate branches folder in the repository, you could copy your branched files there via server side commands:
Then you could switch that file to use the
branches_files
repository path