CVS Merging Details

OK, so you've just read the CVS FAQ and are wondering how in heck it works. Here's a detailed explanation of why the merge command can be used to replace branch A with branch B.

When you specify two -j options to merge, as in

cvs update -jR1 -jR2

CVS does two things:

  1. it creates a delta from revision R1 to revision R2
  2. it applies that delta to the current working directory

A delta is a set of changes which, when applied to R1, results in R2.

For example, suppose you have a file which contains two lines:

line 1
this is the second line

This is rev 1.1. If you then modify it, so rev 1.2 looks like this:

changed line 1
this is the second line
and this is a new, third line

The delta from rev 1.1 to rev 1.2 will be:

  • change line 1 from "line 1" to "changed line 1" (this can be thought of as "delete line 1, then insert a new line 1")
  • add line 3

This can be expressed mathematically.

rev 1.2 = rev 1.1 + delta

which can be rearranged as

delta = rev 1.2 - rev 1.1

This concept can be extended to any two arbitrary revisions R1 and R2:

delta = R2 - R1

That covers step 1. Now, in step 2, we get:

cwd = cwd + delta

If cwd (current working directory) and R1 are the same, then you get:

cwd = R1 + delta
    = R1 + R2 - R1
    = R2

In the end, the current working directory will be exactly identical to revision R2, including all files that have been added or removed from either branch.

In summary, the general procedure is:

  • make TARGET branch the current branch (cvs update -r TARGET, or cvs update -A if TARGET is HEAD)
  • cvs update -jTARGET -jSOURCE
  • cvs commit
  • done

Pretty powerful stuff!

--Jhyslop 16:42, 28 Oct 2005 (EDT)