Saturday, February 28, 2009

Merge Bazaar Repositories With No Common Ancestor


Distributed version control is great for merging. Things like bazaar, git, mercurial, etc. all have sophisticated tools and good support for merging branches of the same code that have diverged even if files have been moved or renamed. But what about merging branches with no common ancestor? I needed to do just that today. I had two bazaar repositories that were separate parts of the same project. One repository was the client and one held the server. I decided that it was pretty inconvenient and a bad idea to have them in separate repositories. I wanted to merge the repositories so I wouldn't lose the history for either project.

Turns out this is pretty easy... In bazaar you can simply do the following:
  • Change directory to the destination working copy.
  • Execute the following command
    bzr merge /path/to/source-working-copy -r0..-1
  • Note the -r0..-1 part of the command. This tells bazaar to merge a range of revisions from the first revision to the most current.
  • Now review and commit the changes. Notice that bazaar gives you an indication that all history is preserved by displaying a list of checkin comments. Nice.
Now the destination working copy will contain all the code and history from the source working copy. Cool. I used this to merge the server codebase into the client codebase for the shellsink project. Of course I tagged and annotated this command in shellsink. On my list of things to do is hook into the blogger api from shellsink so that you can have annotated commands from shellsink automatically show up on your blog, but that is for another post...

3 comments:

maphew said...

thank you!

dieck said...

Very simple (and therefore good) howto :)

I have a problem with subdirectories:
I have multiple directories containing plain branches, let's call them C:\bazaar\A, C:\bazaar\B and so on.
Creating a new branch to merge them all into, at
C:\combined, I want to have C:\combined\A, C:\combined\B and so on. But when I use bzr merge, from a freshly created directory C:\combined\A, the content of C:\bazaar\A is merged into C:\combined, not C:\combined\A. Any ideas? Thanks!

Jon said...

This is awesome! I have several repositories that I want to merge as sub directories of a larger repository, without losing any history.

Thanks!