如何在SVN中分支单个文件?

时间:2022-08-26 23:23:24

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?

分支的颠覆概念似乎专注于创建整个存储库的[un]稳定分支,以进行开发。是否有创建单个文件分支的机制?

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.

对于用例,请考虑具有多个特定于平台的源(* .c)实现的公共头(* .h)文件。这种类型的分支是永久性的。所有这些分支都会看到正在进行的开发,偶尔会进行跨分支合并。这与通常具有有限寿命的不稳定的发育/稳定释放分支形成鲜明对比。

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.

我不想分支整个存储库(便宜或不便宜),因为它会在主干和所有分支之间不断合并,从而产生不合理的维护量。目前我正在使用ClearCase,它具有不同的分支概念,这使得这很容易。我被要求考虑过渡到SVN,但这种范式差异很重要。我更关心的是能够为单个文件轻松创建备用版本,而不是像剪切稳定版本分*样。

8 个解决方案

#1


3  

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

遗憾的是,我认为这里真正的答案是ClearCase比Subversion更好地处理这种情况。使用subversion,你必须分支所有东西,但ClearCase允许一种“懒分支”的想法,这意味着只有一组文件被分支,其余的仍然跟随主干(或你指定的任何分支)。

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.

呃,对不起那不是一个非常好的答案。但Subversion没有一个很好的解决方案。它的模型是分支和合并。

Edit: OK, so expanding on what crashmstr said. You could do this:

编辑:好的,所以扩展了crashmstr所说的内容。你可以这样做:

svn cp $REP/trunk/file.h $REP/branched_files/file.h
svn co $REP/trunk
svn switch $REP/branched_files/file.h file.h

But wow!, is that prone to errors. Whenever you do a svn st you will see this:

但是,哇!是容易出错的。每当你做一个svn st你会看到这个:

svn st
    S  file.h

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.

实际上,这里可能有一个不错的项目用于模拟像ClearCase的svn属性和切换的分支文件,在bog标准svn客户端周围编写一个包装来处理所有混乱。

#2


9  

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:

如果在存储库中创建单独的分支文件夹,则可以通过服务器端命令复制分支文件:

svn copy svn://server/project/header.h svn://server/branched_files/header.h

Then you could switch that file to use the branches_files repository path

然后,您可以切换该文件以使用branches_files存储库路径

#3


3  

Here is how I understand your problem. You have the following tree:

这是我如何理解你的问题。您有以下树:

time.h
time.c

and you need to decline it for multiple architectures :

你需要为多种架构拒绝它:

time.h is comon
time.c (for x386), time.c (for ia64), time.c (for alpha),...

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.

同样在您当前的VCS中,您可以根据需要从time.c创建多个分支,并在从VCS签出文件时自动检查公共中继的最新time.h和分支中的最新time.c你正在努力。

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.

您担心的问题是,如果您在签出分支时使用SVN,则必须经常合并来自主干的time.h,或者冒险处理较旧的文件(与主干相比),这些开销是不可接受的给你。

Depending on the structure of your source code, there might be a solution though. imagine that you have

根据源代码的结构,可能会有一个解决方案。想象你有

 
/
/headers/
/headers/test.h
/source/
/source/test.c

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.

然后你可以分支/,并使用svn:externals功能将你的标题链接到主干的头部。它只适用于目录,并且在提交回test.h方面有一些限制(你必须进入头目录才能使它工作)但它可以工作。

#4


1  

A Subversion "branch" is just a copy of something in your repository. So if you wanted to branch a file you'd just do:

Subversion“分支”只是存储库中某些内容的副本。因此,如果您想要分支文件,您只需执行以下操作:

svn copy myfile.c myfile_branch.c

#5


1  

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.

如果您想要退出更改并稍后应用它们,则可以改为使用补丁。

#6


1  

Are you sure you really need this feature in your VCS ?

您确定VCS中确实需要此功能吗?

Why not use the C preprocessor and #ifdef away the code you don't need ? Or any similar tool.

为什么不使用C预处理器和#ifdef远离你不需要的代码?或任何类似的工具。

something like:

// foo.h:
void Foo();

// foo_win32.c
#ifdef _WIN32
void Foo()
{
   ...
}
#endif

// foo_linux.c
#ifdef _GNUC
void Foo()
{
   ...
}
#endif

Sometimes if it doesn't fit right, then it's not the right solution.

有时如果它不合适,那么它不是正确的解决方案。

#7


0  

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.

SVN中的分支只是一个副本。我相信,按照您希望的方式执行此操作,您必须将文件的每个版本放在存储库中的单独目录中,并将其签出到源文件夹中。 I.E.将该文件视为一个单独的项目。

#8


0  

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.

Subversion中的一个分支正是你所说的。所有文件都是主干的精确副本,但您更改的文件除外。这是SVN Book中谈到的“廉价拷贝”方法。唯一需要注意的是需要不时将主干合并到分支中,以确保在那里进行的更改反映在分支中。当然,如果不希望这些更改,则不需要发生trunk-> branch合并。

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

允许自动合并主干更改(模拟Clear Case范例)的一种简单方法是使用预提交钩子脚本在提交之前合并主干更改。(事实上,这总是一个防止代码漂移的好策略。

#1


3  

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

遗憾的是,我认为这里真正的答案是ClearCase比Subversion更好地处理这种情况。使用subversion,你必须分支所有东西,但ClearCase允许一种“懒分支”的想法,这意味着只有一组文件被分支,其余的仍然跟随主干(或你指定的任何分支)。

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.

呃,对不起那不是一个非常好的答案。但Subversion没有一个很好的解决方案。它的模型是分支和合并。

Edit: OK, so expanding on what crashmstr said. You could do this:

编辑:好的,所以扩展了crashmstr所说的内容。你可以这样做:

svn cp $REP/trunk/file.h $REP/branched_files/file.h
svn co $REP/trunk
svn switch $REP/branched_files/file.h file.h

But wow!, is that prone to errors. Whenever you do a svn st you will see this:

但是,哇!是容易出错的。每当你做一个svn st你会看到这个:

svn st
    S  file.h

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.

实际上,这里可能有一个不错的项目用于模拟像ClearCase的svn属性和切换的分支文件,在bog标准svn客户端周围编写一个包装来处理所有混乱。

#2


9  

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:

如果在存储库中创建单独的分支文件夹,则可以通过服务器端命令复制分支文件:

svn copy svn://server/project/header.h svn://server/branched_files/header.h

Then you could switch that file to use the branches_files repository path

然后,您可以切换该文件以使用branches_files存储库路径

#3


3  

Here is how I understand your problem. You have the following tree:

这是我如何理解你的问题。您有以下树:

time.h
time.c

and you need to decline it for multiple architectures :

你需要为多种架构拒绝它:

time.h is comon
time.c (for x386), time.c (for ia64), time.c (for alpha),...

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.

同样在您当前的VCS中,您可以根据需要从time.c创建多个分支,并在从VCS签出文件时自动检查公共中继的最新time.h和分支中的最新time.c你正在努力。

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.

您担心的问题是,如果您在签出分支时使用SVN,则必须经常合并来自主干的time.h,或者冒险处理较旧的文件(与主干相比),这些开销是不可接受的给你。

Depending on the structure of your source code, there might be a solution though. imagine that you have

根据源代码的结构,可能会有一个解决方案。想象你有

 
/
/headers/
/headers/test.h
/source/
/source/test.c

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.

然后你可以分支/,并使用svn:externals功能将你的标题链接到主干的头部。它只适用于目录,并且在提交回test.h方面有一些限制(你必须进入头目录才能使它工作)但它可以工作。

#4


1  

A Subversion "branch" is just a copy of something in your repository. So if you wanted to branch a file you'd just do:

Subversion“分支”只是存储库中某些内容的副本。因此,如果您想要分支文件,您只需执行以下操作:

svn copy myfile.c myfile_branch.c

#5


1  

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.

如果您想要退出更改并稍后应用它们,则可以改为使用补丁。

#6


1  

Are you sure you really need this feature in your VCS ?

您确定VCS中确实需要此功能吗?

Why not use the C preprocessor and #ifdef away the code you don't need ? Or any similar tool.

为什么不使用C预处理器和#ifdef远离你不需要的代码?或任何类似的工具。

something like:

// foo.h:
void Foo();

// foo_win32.c
#ifdef _WIN32
void Foo()
{
   ...
}
#endif

// foo_linux.c
#ifdef _GNUC
void Foo()
{
   ...
}
#endif

Sometimes if it doesn't fit right, then it's not the right solution.

有时如果它不合适,那么它不是正确的解决方案。

#7


0  

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.

SVN中的分支只是一个副本。我相信,按照您希望的方式执行此操作,您必须将文件的每个版本放在存储库中的单独目录中,并将其签出到源文件夹中。 I.E.将该文件视为一个单独的项目。

#8


0  

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.

Subversion中的一个分支正是你所说的。所有文件都是主干的精确副本,但您更改的文件除外。这是SVN Book中谈到的“廉价拷贝”方法。唯一需要注意的是需要不时将主干合并到分支中,以确保在那里进行的更改反映在分支中。当然,如果不希望这些更改,则不需要发生trunk-> branch合并。

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

允许自动合并主干更改(模拟Clear Case范例)的一种简单方法是使用预提交钩子脚本在提交之前合并主干更改。(事实上,这总是一个防止代码漂移的好策略。