Synchronizing code with two subversion repositorie

2019-02-02 15:46发布

问题:

A bit of background first:

  • I am using "base" code from a remote SVN repository, not under my control. The code is not tagged (yet), so I always need to keep up with the trunk.

  • For a number of reasons (the most important being that our local extensions to the code are of a "niche" nature, and intended to solve a specific problem with the project in which the code is used) I can't use the remote repository to do version control of any modifications I make locally.

  • I have a local SVN repository in which I am currently doing the "local" versioning.

The problem I'm faced with: I can't figure out if there's a good way to have the code simultaneously synchronized with both repositories. That is, I would like to keep the "remote" version information (so that I can merge in future changes), but I would also like to have "local" version information at the same time (i.e., within the same directory structure).

At the moment I am doing this using two different directories, both containing identical code, but each containing different versioning information. Obviously this is quite a bit of overhead, especially since the code in the two directories needs to be synchronized independently.

Is there a way to do this in subversion? Or do you have suggestions about alternative ways of approaching this?

回答1:

I did this using git svn, with my development done in a git repository. The remote development is done in subversion. I made a git svn clone of the subversion repository, which I push to a real git repository. A cronjob runs "git svn rebase && git push" every now and again to create a git mirror of the subversion repo.

In order to merge the subversion changes, I have 2 remotes in my local git copy - the "local development" origin and the "from subversion mirror" origin. Whenever I feel the need, I can merge changes from the subversion mirror into our development tree. Local changes are not affected, they live apart and don't mess up the svn mirror.

I used gitosis to set up and administer the git repositories. The steps would be something like this (from memory, may be wrong):

# set up the mirror
git svn clone -s $SVN
git remote add origin git@$MACHINE:svnmirror.git
git push
# + cron job to do git svn rebase && git push every N hours/minutes

# set up the local working copy for development
git clone git://$MACHINE/svnmirror.git
# that's an anonymous, read only clone 
# no push to the svn mirror for developers - only cronjob user can push there
git remote add newproject git@$MACHINE:myproject.git
git push newproject
# now do the real deal
git clone git://$MACHINE/myproject.git
# hack hack hack
git push # origin master not needed
git remote add svnmirror git://$MACHINE/svnmirror.git
git merge svnmirror/master
git push


回答2:

You can have your local repository where you commit your changes, as you already have done. Further you would do a periodic merge from the base repository in order to merge the changes done in the base trunk into your local repository.

The only difficult thing is that you need to keep track of the revisions from which you've already merged from the base repository.

This could look like this:

svn merge -r X:Y baseRepositoryURL // merge from base repo
svn commit                         // commit the changes to local repo
svn merge -r Y:Z baseRepositoryURL // merge from base repo
svn commit                         // commit the changes to local repo

Where X is the revision of your initial checkout, Y is the head revision a the time of the first merge and Z is the head revision a the time of the second merge. You see the pattern. Furthermore it is important that you are in the base directory of your local checkout when issueing the commands.