@need 3200

@node Multi-Branch Merging -- The reconcile Command
@chapter Multi-Branch Merging -- The reconcile Command

@geindex reconcile



History sensative merging with @code{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.

@need 3200

@section The Repeated Multi-Branch Merge Problem

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


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

@example
@group
                 ---> A-0
                |
        X-1 ----+---> B-0
                |
                 ---> C-0
@end group
@group
@end group
@end example

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


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

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

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

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

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

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

@code{C} decides it would be a good idea to merge with the feature's found
in @code{A-1}.  In doing so, he'll also be picking up @code{B-0..3}.  Once
again, a simple @code{update} (or @code{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,
@code{B} works on his next patch, and @code{A} commits a new revision:

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

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

@example
@group
        delta (B-3, B's project tree) [A-2]
@end group
@group
@end group
@end example

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

@example
@group
        delta(B-3, A-2) [B's project tree]
@end group
@group
@end group
@end example

@noindent
giving priority to @code{B}'s code, and reconsidering the merge conflicts
that @code{A} handled.  The choice is arbitrary and the best answer depends
on the particular changes made.  @code{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:

@example
@group
                 ---> B-0 -> B-1 -> B-2 -> B-3 -> B-4 -> B-5
                |                           /            ^ 
                |                          /            /
                |                         /            /
                |               ----------            /
@end group
@group
                |              /       ---------------
                |             /       /
@end group
@group
                |            V       /
        X-1 ----+---> A-0 -> A-1 -> A-2
                |             |
                |             V
                 ---> C-0 -> C-1
@end group
@group
@end group
@end example
@example
@group
        or in English:
@end group
@group
@end group
@end example
@example
@group
        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
@end group
@group
@end group
@end example

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

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



@need 3200

@section The Challenge

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

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

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 @code{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.




@need 3200

@section The Simple update Solutions

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

@example
@group
        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]
@end group
@group
@end group
@end example

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


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




@need 3200

@section The Simple replay Solutions

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

@example
@group
    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 ]]
@end group
@group
@end group
@end example

History sensitivity helped a bit there: @code{replay} knows better than to
apply @code{B-0..3} -- eliminating one source of needless conflicts.
Still, when we replay @code{B-5} and @code{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
@code{replay} solution, we get by with fewer patches (replay @code{C} first,
then @code{A}, then @code{B}, for example).  Figuring out the best order in
which to apply patches is, ultimately, the subject of this chapter:




@need 3200

@section The reconcile Solution

Suppose that @code{X} asks, of the tree @code{X-2}:

@example
@group
        % larch whats-missing A B C
@end group
@group
@end group
@end example

@noindent
the answer is:

@example
@group
        A-0
        A-1
        A-2
        A-3
        A-4
@end group
@group
        B-0
        B-1
        B-2
@end group
@group
        B-3
        B-4
        B-5
        C-0
        C-1
@end group
@group
@end group
@end example

@code{X} can also ask the more interesting question:

@example
@group
        % larch whats-missing --merges A B C
@end group
@group
@end group
@end example

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

@example
@group
        A-0 A-0
        A-1 A-1
        A-1 B-0
        A-1 B-1
        A-1 B-2
@end group
@group
        A-1 B-3
        A-2 A-2
        A-3 A-3
        A-4 A-4
        B-0 B-0
@end group
@group
        B-1 B-1
        B-2 B-2
        B-3 B-3
        B-4 B-4
        B-5 B-5
@end group
@group
        B-5 A-0
        B-5 A-1
        B-5 A-2
        C-0 C-0
        C-1 C-1
@end group
@group
        C-1 A-0
@end group
@group
        C-1 A-1
        C-1 B-0
        C-1 B-1
        C-1 B-2
        C-1 B-3
@end group
@group
@end group
@end example

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

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

@noindent
which means that X can perform the merge with just:

@example
@group
        A-4 [ A-3 [ B-5 [ B-4 [ C-1 [ C-0 [ X-2 ]]]]]]
@end group
@group
@end group
@end example

There is still a potential source of conflicts -- when applying @code{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 @code{reconcile} find that solution?  What's the magic?
Conceptually, @code{reconcile} works in two steps.


First, @code{reconcile} computes a subset of all the patches: the
@geindex necessary patches
@dfn{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, @code{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 @code{reconcile}.


So, you don't believe this obscure command is useful in real life?
See @strong{Even/Odd Versions} in @ref{Implementing Development Policies}.







