@need 3200

@node Development Branches -- The star-merge Style of Cooperation
@chapter Development Branches -- The star-merge Style of Cooperation

In earlier chapters, we developed an extended example out of the
@code{hello-world} project.


Alice and Bob, the primary programmers on the project, started one
archive and created some revisions there.


Candice, a user of the project, created her own archive, started a
branch of the @code{hello-world} project, and began maintaining her own
local modifications.


In this chapter, we'll begin to consider a situation that is more
typical of free software projects in the real world.   Here, we'll
consider Alice and Bob to be the maintainers of a public project, and
Candice as a major remote contributor to the project.  We'll identify
the new revision control needs that arise from that arrangement, and
look at some @code{arch} commands that help to satisfy those needs.

@need 3200

@section Promoting an Elementary Branch to a Development Branch

So far, if you've been following the examples, Candice has an
elementary branch.  She made a branch from the mainline, made some
local changes, and has kept her branch up-to-date with Alice and Bob's
mainline.


We're supposing, at this point, that Alice and Bob want to merge
Candice's changes into the mainline.


Well, that merging work has already been done.  Candice's latest
revision is exactly the tree that Alice and Bob want.   They can 
incorporate that merge into their mainline very simply, by committing
Candice's latest revision to their own mainline:

@example
@group

@end group
@group
        % tla get -A candice@@candice.net--2003-candice \
                    hello-world--candice--0.1 \
                    hw-C
        [...]
@end group
@group


@end group
@group
        % cd hw-C
@end group
@group

@end group
@group
        % tla set-tree-version -A lord@@emf.net--2003-example \
                    hello-world--mainline--0.1
@end group
@group

@end group
@group
        % tla make-log
        ++log.hello-world--mainline--0.1--lord@@emf.net--2003-example
@end group
@group

@end group
@group
        [... edit log file (consider `tla log-for-merge') ... ]
@end group
@group

@end group
@group
        % cat ++log.hello-world--mainline--0.1--lord@@emf.net--2003-example
        Summary: merge from Candice's Branch
        Keywords: 
@end group
@group

@end group
@group
        Patches applied:
@end group
@group

@end group
@group
          * candice@@candice.net--2003-candice/hello-world--candice--0.1--patch-2
             merge from mainline sources
@end group
@group

@end group
@group
          * candice@@candice.net--2003-candice/hello-world--candice--0.1--patch-1
             Punctuated the output correctly
@end group
@group

@end group
@group
          * candice@@candice.net--2003-candice/hello-world--candice--0.1--base-0
             tag of 
              lord@@emf.net--2003-example/hello-world--mainline--0.1--patch-1
@end group
@group

@end group
@group
        % tla commit
        [....]
@end group
@group

@end group
@end example

@strong{Read Carefully Note:}  Note carefully the @geindex trick
@dfn{trick} we just used.
Candice's latest revision was exactly what Alice and Bob wanted --
they combined @code{get} with @code{set-tree-version} to turn Candice's tree
into one they could easily commit to their own mainline.




@need 3200

@section Simple Development Branches

Let's consider what happens as development proceeds on both branches.
For this purpose, we'll introduce something new: a way of diagraming
branches and the merges between them.


After the examples so far, we have this situation:

@example
@group

@end group
@group
      mainline--0.1                    candice--0.1
      -------------                    ------------
        base-0             -----------> base-0 (a tag)
        patch-1  ---------'             patch-1
        patch-2             ----------> patch-2
        patch-3  ----------'  --------'
        patch-4  <-----------'
@end group
@group

@end group
@end example

@noindent
which tells us that the @code{candice} branch is a tag 
of @code{patch-1} from the mainline;  that at @code{patch-2} of the @code{candice}
branch, there was a merge of everything up to @code{patch-3} of the
@code{mainline}; and finally that @code{patch-4} of the mainline merges
in everything up to @code{patch-2} from the @code{candice} branch.


Whenever we have a such a diagram in which none of the merge lines
cross, that is a @geindex simple development branch
@dfn{simple development branch}.


The significance of a simple development branch is that it's a model
for how two development efforts can work asynchronously on one
project.  Within each effort -- on each branch -- programmer's use the
"update/commit" style of cooperation (see @ref{The update/commit Style of Cooperation}).   However, changes on one branch have no
effect on the other until the two branches are merged.




@need 3200

@section Introducing The Development Branch Merging Problem

Let's suppose that more work happens on both the @code{mainline} and
@code{candice} branches, leaving us with:

@example
@group

@end group
@group
      mainline--0.1                    candice--0.1
      -------------                    ------------
        base-0             -----------> base-0 (a tag)
        patch-1  ---------'             patch-1
        patch-2             ----------> patch-2
        patch-3  ----------'  --------' patch-3
        patch-4  <-----------'          patch-4
        patch-5
        patch-6
@end group
@group


@end group
@group
        % tla revisions --summary -A candice@@candice.net--2003-candice \
                          hello-world--candice--0.1
        base-0
            tag of 
            lord@@emf.net--2003-example/hello-world--mainline--0.1--patch-1
@end group
@group
        patch-1
            Punctuated the output correctly
        patch-2
@end group
@group
            merge from mainline sources
        patch-3
            added a period to output string
        patch-4
            capitalized the output string
@end group
@group



@end group
@group
        % tla revisions --summary -A lord@@emf.net--2003-example \
                          hello-world--mainline--0.1
        base-0
            initial import
        patch-1
@end group
@group
            Fix bugs in the "hello world" string
        patch-2
            commented return from main
        patch-3
            added copywrong statements
@end group
@group
        patch-4
@end group
@group
            merge from Candice's Branch
        patch-5
            fixed the copyrwrong for hw.c
        patch-6
            fixed the copyrwrong for main.c
@end group
@group




@end group
@end example

Let's consider a scenario in which our goal is to merge the new work
on the @code{mainline} branch into the @code{candice} branch.  In other words,
we want to wind up with:

@example
@group

@end group
@group
      mainline--0.1                    candice--0.1
      -------------                    ------------
        base-0             -----------> base-0 (a tag)
        patch-1  ---------'             patch-1
        patch-2             ----------> patch-2
        patch-3  ----------'  --------' patch-3
        patch-4  <-----------'          patch-4
        patch-5               --------> patch-5
        patch-6  ------------'
@end group
@group

@end group
@end example

How can we perform that merge?  Let's start with the latest pre-merge
candice revision (@code{patch-4}):

@example
@group

@end group
@group
        % tla get -A candice@@candice.net--2003-candice \
                       hello-world--candice--0.1--patch-4 \
                       hw-C-4
        [....]
@end group
@group

@end group
@group
        % cd hw-C-4
@end group
@group

@end group
@end example

Here are two techniques that @emph{@strong{don't}} work:

@need 3200

@subsection replay Does Not Solve the Development Branch Merge Problem

@code{replay} will try to apply all "missing" changes from the @code{mainline}
into the @code{candice} tree.   The list of changeset it will apply is
given by:

@example
@group

@end group
@group
        % tla missing --summary \
                      -A candice@@candice.net--2003-example \
                      hello-world--mainline--0.1
        patch-4
            merge from Candice's Branch
        patch-5
            fixed the copyrwrong for hw.c
        patch-6
            fixed the copyrwrong for main.c
@end group
@group

@end group
@end example

Problematic in that list is @code{patch-4}.   It's a merge that includes
all of the changes from the @code{candice} branch up to its @code{patch-2}
level.   Yet those changes are already present in the @code{patch-4}
revision of the @code{candice} branch -- so @code{replay} will be applying them
redundantly (cause patch conflicts).


@strong{Note of Warning:} The @code{replay} command will not prevent you from running
further replays even though the source tree is not in a consistant state. 
TLA in its current incarnation does not merge reject files. This leaves
open the possibility that patch rejects will be lost if a second @code{replay}
is performed before the rejects from the first replay are resolved. (Some day
TLA may be able to merge multiple rejects into a combined reject.)


@strong{Advanced User Note:}  The @code{replay} command has options that would
allows us to skip the @code{patch-4} revision from the mainline.   That
@emph{sort of} solves the problem, but it has some drawbacks.  First, it
means that @code{patch-4} will continue to appear in the @code{missing} output
of the @code{candice} branch.  Second, there is nothing that guarantees us
that the @code{patch-4} changeset contains @emph{only} merges from the @code{candice}
branch.  If Alice and Bob made other changes in @code{patch-4}, and we skip
that changeset, those other changes will be lost.




@need 3200

@subsection update Does Not Solve the Development Branch Merge Problem

Suppose we try to @code{update} from the @code{mainline} branch.  Recall that
@code{update} will compute a changeset from the youngest @code{mainline}
ancestor of the project tree to the tree itself, then apply that
changeset to the latest @code{mainline} revision.


We have a notation for this.  A changeset from @code{X} to @code{Y} is written:

@example
@group
        delta(X, Y)
@end group
@group
@end group
@end example

In this case, @code{update} will start by computing a changeset from the
@code{mainline} @code{patch-3} revision to our project tree:

@example
@group
        delta(mainline--0.1--patch-3, hw-C-4) 
@end group
@group
@end group
@end example

The tree that results for applying a changeset from @code{X} to @code{Y} to a
tree @code{Z} is written:

@example
@group
        delta(X, Y) [ Z ]
@end group
@group
@end group
@end example

In other words, the result of @code{update} in our example can be described
as:

@example
@group
        delta(mainline--0.1--patch-3, hw-C-4) [mainline--0.1--patch-6]
@end group
@group
@end group
@end example

Here's the problem, though.   The @code{patch-3} revision of @code{mainline} was
not previously merged with the @code{candice} branch.   Thus, the changeset

@example
@group
        delta(mainline--0.1--patch-3, hw-C-4)
@end group
@group
@end group
@end example

@noindent
will include, among other changes, the changes from @code{patch-1} and
@code{patch-2} of the candice branch.


Unfortunately, the tree we'll be applying that changeset to,
@code{mainline--0.1--patch-6}, has already been merged with
@code{base-0...patch-2} of the @code{candice} branch.


As with @code{replay}, @code{update} will cause merge conflicts by making
zredundant changes.







@need 3200

@section Solving One Instance of the Development Branch Merging Problem

Using just our @code{delta} notation and merge diagrams, let's look at
solving this merge problem cleanly.


Remember that we currently have:

@example
@group
      mainline--0.1                    candice--0.1
      -------------                    ------------
        base-0             -----------> base-0 (a tag)
        patch-1  ---------'             patch-1
        patch-2             ----------> patch-2
        patch-3  ----------'  --------' patch-3
        patch-4  <-----------'          patch-4
        patch-5
        patch-6
@end group
@group
@end group
@end example

@noindent
and our goal is to create a new merge, for patch-5 of Candice's
branch:

@example
@group
                              --------> patch-5
        patch-6  ------------'
@end group
@group
@end group
@end example

We might decide to start with a @code{mainline} branch and merge in missing
@code{candice} changes, or start with a @code{candice} tree and merge in missing
@code{mainline} changes.  Let's assume the latter (merging into a @code{candice}
tree).


In this case, @code{mainline-0.1} revision @code{patch-6} is "up to date" with 
@code{candice-0.1} revision @code{patch-2}.   We want too apply all changes
since then to the latest @code{candice} revision:

@example
@group
        with:
                ancestor := candice--0.1--patch-2
                merge_in := mainline--0.1--patch-6
                target   := canidice--0.1--patch-4
@end group
@group
@end group
@end example
@example
@group
        answer := delta(ancestor, merge_in)[target]
@end group
@group
@end group
@end example

The arrows in the merge diagram are critical to figuring out the right
answer.  For example, suppose that the arrow from Candice's @code{patch-2}
to the @code{mainline} revision @code{patch-4} wasn't there.   Then the answer
would be:

@example
@group
        with:
                ancestor := mainline--0.1--patch-3
                merge_in := mainline--0.1--patch-6
                target   := canidice--0.1--patch-4
@end group
@group
@end group
@end example
@example
@group
        answer := delta(ancestor, merge_in)[target]
@end group
@group
@end group
@end example

Tracing out the arrows for a given merge is a tedious process.   
It's automated by the @code{star-merge} command:




@need 3200

@section star-merge -- Solving the Development Branch Merging Problem in General

It's a bit beyond the scope of this tutorial to explain the complete
solution to the development branch merging problem in general.  The
two solutions shown above illustrate two cases, but slightly different
solutions are sometimes necessary.


What you should know is that when you have simple development
branches (see @strong{Simple Development Branches} in @ref{Development Branches -- The star-merge Style of Cooperation}), the command
@code{star-merge} knows how to merge between them without causing spurious
merge conflicts.


In ordinary use, you invoke @code{star-merge} in the tree you want to merge
info, providing as an argument the tree you want to merge from:

@example
@group
        
        
@end group
@group
        % tla get -A candice@@candice.net--2003-candice \
                hello-world--candice--0.1--patch-4 \
                merge-temp
@end group
@group

@end group
@group
        % tla star-merge lord@@emf.net--2003/hello-world--mainline--0.1
@end group
@group

@end group
@end example






