The Hackerlab at regexps.com

Multi-Branch Merging -- The reconcile Command

up: arch
next: Synchronizing Two Branches
prev: Arbitrary Patching with delta-patch

History sensative merging with replay can avoid some avoidable merge conflicts, but not all. One example is a class of merge problems that we'll name "Repeated Multi-Branch Merging": the problem of merging several branches when each of the branches have previously merged with some of the others. Although this kind of merging seems arcane, it can, in fact, easily arise in quite realistic situations (for example, when simultaneously supporting multiple releases of a single project).

Below is an example to illustrate the problem. The set-up in this example is a bit long, but each step along the way is perfectly reasonable, and the end result is quite a tangled knot. The pay-off will be seeing how to cope with the resulting mess.

The Repeated Multi-Branch Merge Problem

Imagine that we start with a particular version of a particular branch, call it X . We'll begin at a particular revision in that branch version: X-1 (for the purposes of this explanation, calling the revision X-1 is much less cumbersome than using a real revision name like foo--mumble--3.5--patch-24 ).

Three programmers each form their own branch from X-1 : call them A , B , and C :

                 ---> A-0
                |
        X-1 ----+---> B-0
                |
                 ---> C-0

The plan here is develop on each branch, then merge the changes together to create a new revision of X .

Programmer B starts off, and creates a series of revisions. Simultaneously, A creates a project tree and starts making local changes for his first revision:

                 ---> A-0
                |      \
                |       A's project tree with local changes
                |
        X-1 ----+---> B-0 -> B-1 -> B-2 -> B-3
                |
                 ---> C-0

Programmer A wants to develop on top of those three patches from B , and so forms a merge. At this stage, A can do a simple update or replay to create a merged project tree:

                 ---> A-0
                |     \
                |      A's merged project tree
                |                 ^
                |      -----------|---------
                |     /                     \
        X-1 ----+---> B-0 -> B-1 -> B-2 -> B-3
                |
                 ---> C-0

Let's assume that A 's merge involved some conflicts: B 's code has been slightly rearranged in the merged tree. Now A can check in that revision. Meanwhile, C starts work:

                 ---> A-0 -> A-1
                |             ^
                |             |
                |      -------|-------------
                |     /                     \
        X-1 ----+---> B-0 -> B-1 -> B-2 -> B-3
                |
                |       C's project tree with local changes
                |      /
                 ---> C-0

C decides it would be a good idea to merge with the feature's found in A-1 . In doing so, he'll also be picking up B-0..3 . Once again, a simple update (or replay ) is sufficient at this point, though to keep things interesting, we'll again assume that there are conflicts to resolve during the update. And meanwhile, by the way, B works on his next patch, and A commits a new revision:

                 ---> B-0 -> B-1 -> B-2 -> B-3
                |     \                     /\
                |      --------------------- |
                |             |              V
                |             |         B's project tree
                |             V
        X-1 ----+---> A-0 -> A-1 -> A-2
                |             |
                |             V
                |       C's merged project tree
                |       ^     |
                |      /      V
                 ---> C-0 -> C-1

We're nearly done with the set-up: B decides to merge in the changes in A-2 . This is a slightly interesting merge (although not the primary topic of this chapter). The common ancestor of A-2 and B 's project tree is B-3 . We previously assumed that when A updated against B-3 there were conflicts that had to be resolved by hand. B has a choice. He can use update against A-2 to create a new tree:

        delta (B-3, B's project tree) [A-2]

giving priority to A 's resolution of those merge conflicts. Or, he can commit his project tree, get revision A-2 , and update against the committed project tree (or do an equivalent thing by hand, without comitting, using mkpatch and dopatch ):

        delta(B-3, A-2) [B's project tree]

giving priority to B 's code, and reconsidering the merge conflicts that A handled. The choice is arbitrary and the best answer depends on the particular changes made. B might want to experimentally try both merges (perhaps in a scratch repository) before picking one. Either way, after a commit, we'll have something like:

                 ---> B-0 -> B-1 -> B-2 -> B-3 -> B-4 -> B-5
                |                           /            ^ 
                |                          /            /
                |                         /            /
                |               ----------            /
                |              /       ---------------
                |             /       /
                |            V       /
        X-1 ----+---> A-0 -> A-1 -> A-2
                |             |
                |             V
                 ---> C-0 -> C-1

        or in English:

        A-1 is up-to-date with respect to B-3
        B-5 is up-to-date with respect to A-2
        C-1 is up-to-date with respect to A-1 and (therefore) B-3

Finally, let's assume that the main development path, X , has evolved independently of these three branches, and that A has added a few more revisions:

                 ---> B-0 -> B-1 -> B-2 -> B-3 -> B-4 -> B-5
                |                           /            ^ 
                |                          /            /
                |                         /            /
                |               ----------            /
                |              /       ---------------
                |             /       /
                |            V       /
        X-1 ----+---> A-0 -> A-1 -> A-2 -> A-3 -> A-4
         |      |             |
         V      |             V
        X-2      ---> C-0 -> C-1

The Challenge

Whew. What an (unfortunately plausible) mess. Now for the challenge:

        Create X-3, which is up-to-date with A-4, B-5, and C-1

There is no one right answer to the challenge: no elegant solution that is guaranteed to avoid merge conflicts. Indeed, there are many ways to perform the merge which differ in terms of what conflicts they'll produce. The goal of arch is to arm programmers with plenty of tools to understand the situation, explore, generate and apply patches effectively, and find a reasonable solution with the greatest degree possible of automated assistance.

The Simple update Solutions

Simple update gives us a whole collection of simplistic solutions. For example, X could update against A , then B , then C or:

        intermediate-1 := delta (A-4, X-1) [X-2]
        intermediate-2 := delta (B-5, X-1) [intermediate-1]
        X-3-candidate  := delta (C-1, X-1) [intermediate-2]

That update path has some problems, though. delta (A-4, X-1) includes the changes in delta (A-1, X-1) , and so does delta (B-5, X-1) . So creating intermediate-2 will involve redundant patching and plenty of opportunities for conflicts. Similar problems occur when creating X-3-candidate .

X could try doing the update s in a different order, but similar problems will still occur.

The Simple replay Solutions

X could replay the branches in some order. Suppose he replays A , then B , then C :

    intermediate-1 := A-4 [ A-3 [ A-2 [ A-1 [ A-0 [ X-2 ]]]]]
    intermediate-2 := B-5 [ B-4 [ intermediate-1 ]]
    X-3-candidate  := C-1 [ C-0 [ intermediate-2 ]]

History sensitivity helped a bit there: replay knows better than to apply B-0..3 -- eliminating one source of needless conflicts. Still, when we replay B-5 and C-1 , there will be plenty of conflicts to make up for that.

It's also worth mentioning that that this solution involves applying nine different patches: we can do better. By differently ordering the replay solution, we get by with fewer patches (replay C first, then A , then B , for example). Figuring out the best order in which to apply patches is, ultimately, the subject of this chapter:

The reconcile Solution

Suppose that X asks, of the tree X-2 :

        % larch whats-missing A B C

the answer is:

        A-0
        A-1
        A-2
        A-3
        A-4
        B-0
        B-1
        B-2
        B-3
        B-4
        B-5
        C-0
        C-1

X can also ask the more interesting question:

        % larch whats-missing --merges A B C

which will answer not only what patches are missing, but what patches include other patches:

        A-0 A-0
        A-1 A-1
        A-1 B-0
        A-1 B-1
        A-1 B-2
        A-1 B-3
        A-2 A-2
        A-3 A-3
        A-4 A-4
        B-0 B-0
        B-1 B-1
        B-2 B-2
        B-3 B-3
        B-4 B-4
        B-5 B-5
        B-5 A-0
        B-5 A-1
        B-5 A-2
        C-0 C-0
        C-1 C-1
        C-1 A-0
        C-1 A-1
        C-1 B-0
        C-1 B-1
        C-1 B-2
        C-1 B-3

X can pipe that list into a filtering command, larch reconcile , which does some magic (the trick is revealed below):

      % larch whats-missing --merges A B C \
        | larch reconcile
        C-0
        C-1
        B-4
        B-5
        A-3
        A-4

which means that X can perform the merge with just:

        A-4 [ A-3 [ B-5 [ B-4 [ C-1 [ C-0 [ X-2 ]]]]]]

There is still a potential source of conflicts -- when applying B-5 in this case -- but the patch set is as small as possible (six patches instead of our earlier nine), and the sources of conflicts are as few as possible.

How did reconcile find that solution? What's the magic? Conceptually, reconcile works in two steps.

First, reconcile computes a subset of all the patches: the necessary patches . The set of necessary patches is the smallest set of patches which, applied in some order, is sufficient to bring the tree up to date. (Proof that there is a unique smallest set of patches with that property is left as an exercise for the interested reader.)

Second, reconcile repeatedly selects the next "necessary" patch to apply, until none are left. At each step of this loop, candidates for the next patch to apply are the patches all of whose prerequisites are in place. Of those, the next patch is the one that comes first in the first column of the input to reconcile .

So, you don't believe this obscure command is useful in real life? See Even/Odd Versions.

arch: The arch Revision Control System
The Hackerlab at regexps.com