@need 3200

@node Basic Branching and Merging
@chapter Basic Branching and Merging

When a single development path splits into two paths, that's called a
@geindex branch
@dfn{branch}.  Typically, a branch is followed by a @geindex merge
@dfn{merge} -- adding the
changes made in a branch back to the branch from which it diverged.


This chapter explains how to create a branch and the simplest way to
merge changes from two branches.  Later chapters will explain fancier
techniques, useful in more complex situations.

@need 3200

@section Tagging

@geindex tag



In @code{arch}, branches are "tags".


A @geindex tag
@dfn{tag} is an alternative name for some revision.  For example, one
could use the tag @code{release-candidate} to name whatever revision people
should download for testing purposes, regardless of what branch,
version, or patch level the release candidate happens to be on.


In @code{arch}, tags are revisions on ordinary branches.  For example,
suppose we are developing @code{arch--devo--0.5} and want to create a tag
@code{release-candidate} to mark revisions which "early adopters"
should test.  First, create a development path for the tag:

@example
@group
      % larch make-branch arch--release-candidate
      % larch make-version arch--release-candidate--0.5
@end group
@group
@end group
@end example

To tag a specific revision, use the command:

@example
@group
      % larch tag SOURCE-REVISION TAG-REVISION
@end group
@group
@end group
@end example

Note that @code{source-revision} and @code{tag-revision} may be abbreviated.
For example, to tag the most recent revision of @code{arch--devo}:

@example
@group
      % larch tag arch--devo arch--release-candidate
@end group
@group
@end group
@end example

@noindent
or 

@example
@group
      % larch tag arch--devo--0.5 arch--release-candidate--0.5
@end group
@group
@end group
@end example

@noindent
or 

@example
@group
      % larch tag arch--devo--0.5--patch-37 \
                 arch--release-candidate--0.5
@end group
@group
@end group
@end example

After such a command, you can retrieve the tagged revision in the
ordinary way:

@example
@group
      % larch get arch--release-candidate--0.5
@end group
@group
@end group
@end example

Note that when you @code{get} a tag that way, the default version of the
resulting project tree is the tag's version, not the tagged version
(see @ref{Labelling Project Trees}).


You can always update a tag, making it point to a later revision,
again using the @code{tag} command:

@example
@group
      % larch tag arch--devo--0.5--patch-53 \
                 arch--release-candidate--0.5
@end group
@group
@end group
@end example

You can see the history of a tag in the usual way, too:

@example
@group
      % larch revisions --summary arch--release-candidate--0.5
      base-0
          tag of joe.hacker@@gnu.org--arch/arch--devo--0.5--patch-37
      patch-1
          tag of joe.hacker@@gnu.org--arch/arch--devo--0.5--patch-53
@end group
@group
@end group
@end example



@need 3200

@section Creating a Branch

There is another way to create a branch.  This technique is slower,
but has the side effect of leaving you with a working directory for
the new branch.  The effect on the repository is the same as when
using the @code{tag} command.


Creating a new branch from an existing branch can be accomplished by
this sequence of commands:

@example
@group
      % larch prepare-branch OLD-REVISION NEW-BRANCH-VERSION WORKING-DIR
      % cd WORKING-DIR
      [...edit log message...]
      % larch finish-branch
@end group
@group
@end group
@end example

@noindent
but it is worth deeply understanding that @code{prepare-branch} is
equivalent to:

@example
@group
      # This command gets the source tree for the revision we
      # are branching from:
      # 
      % larch get OLD-REVISION WORKING-DIR
@end group
@group
@end group
@end example
@example
@group
      # These commands switch the working directory to the
      # new branch:
      # 
      % cd WORKING-DIR
      % larch add-log NEW-BRANCH-VERSION
      % larch set-tree-version NEW-BRANCH-VERSION
@end group
@group
@end group
@end example
@example
@group
      # These commands create a "continuation" base-revision
      # for the new branch:
      # 
      % larch make-log
@end group
@group
@end group
@end example

@noindent
and that @code{finish-branch} is the same as:

@example
@group
      % larch make-branch NEW-BRANCH
      % larch make-version NEW-BRANCH-VERSION
      % larch commit --continuation OLD-REVISION
@end group
@group
@end group
@end example

For example, to create a branch @code{hello--experimental--1.0} from the
latest revision of @code{hello--devo--1.0}, use:

@example
@group
      % larch prepare-branch hello--devo--1.0 \
                            hello--experimental--1.0 \
                            wd
@end group
@group
@end group
@end example
@example
@group
      % cd wd
@end group
@group
@end group
@end example
@example
@group
      [...edit log message...]
@end group
@group
@end group
@end example
@example
@group
      % larch finish-branch
@end group
@group
@end group
@end example



@need 3200

@section Distributed Branches

There is no requirement that a branch be stored in the same archive as
the revision from which it branched.  For example, you can create a
private archive, and store some branches there -- only merging those
changes back into the shared archive when they are ready.


Here's a tip: make your private archive your default archive.  Use
fully-qualified version and revision names when getting or committing
revisions in the shared archive.  This makes it less likely that
you'll accidently make unintended changes to the shared archive.




@need 3200

@section whats-missing Revisited

@geindex whats-missing



If you have a project tree for a branch, you might want to know what
has happened in the version from which you branched.


The @code{whats-missing} is used for this.  In a working directory for a
branch, use:

@example
@group
      % larch whats-missing --summary ORIGINAL-VERSION
@end group
@group
@end group
@end example

@noindent
where @code{ORIGINAL-VERSION} is the version name of the version from which
you branched.  Actually, @code{ORIGINAL-VERSION} can be any version for
which your project tree has a patch log.


The @code{whats-missing} command is explained in greater detail in the next
chapter (see @ref{Patch Logs and ChangeLogs}).




@need 3200

@section update and replay Revisited

Similarly, @code{update} and @code{replay} work for any version for which a
project tree has a patch log, such as a version from which a branch
occurred:

@example
@group
      % larch update OLD-DIR NEW-DIR [archive/]VERSION
@end group
@group
@end group
@end example
@example
@group
      % larch replay OLD-DIR NEW-DIR [archive/]VERSION
@end group
@group
@end group
@end example



@need 3200

@section Merging After a Branch

The simplest use of branching and merging is this: you have one
development path, call it the "trunk".  You form a branch from that
development path, which we'll just call "the branch".


To make some changes, you do your work on the branch:  check out the
latest revision from the branch, make changes, commit, make more
changes, commit again, etc.


As you work, you might sometimes need to "catch up" to changes made
to the trunk.  You can do that by using @code{update} or @code{replay}.


When you're done, and the branch is fully up-to-date with the trunk,
you can check out the latest branch revision, then commit that version
to the trunk.   All of the changes that you made on the branch will be
summarized into a single patch.


There are more complicated and more realistic uses of branches.  These
are the subjects of the next several chapters.







