1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
|
<html>
<head>
<title>Multi-Branch Merging -- The reconcile Command</title>
</head>
<body>
<a name="Multi-Branch_Merging_--_The_reconcile_Command"></a>
<a href="http://www.regexps.com">The Hackerlab at <code>regexps.com</code></a>
<h2 align=center>Multi-Branch Merging -- The reconcile Command</h2>
<small>
<b>up: </b><a href="arch.html#arch">arch</a></br>
<b>next: </b><a href="sync.html#Synchronizing_Two_Branches">Synchronizing Two Branches</a></br>
<b>prev: </b><a href="arbitrary-patching.html#Arbitrary_Patching_with_delta-patch">Arbitrary Patching with delta-patch</a></br>
</small>
<br>
<p><a name="index-pt:0"></a>
</p><p>History sensative merging with <code>replay</code>
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).
</p><p>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.
</p>
<a name="The_Repeated_Multi-Branch_Merge_Problem"></a>
<h3 align=center>The Repeated Multi-Branch Merge Problem</h3>
<p>Imagine that we start with a particular version of a particular
branch, call it <code>X</code>
. We'll begin at a particular revision in that
branch version: <code>X-1</code>
(for the purposes of this explanation, calling
the revision <code>X-1</code>
is much less cumbersome than using a real revision
name like <code>foo--mumble--3.5--patch-24</code>
).
</p><p>Three programmers each form their own branch from <code>X-1</code>
: call them
<code>A</code>
, <code>B</code>
, and <code>C</code>
:
</p><pre>
---> A-0
|
X-1 ----+---> B-0
|
---> C-0
</pre>
<p>The plan here is develop on each branch, then merge the changes
together to create a new revision of <code>X</code>
.
</p><p>Programmer <code>B</code>
starts off, and creates a series of revisions.
Simultaneously, <code>A</code>
creates a project tree and starts making local
changes for his first revision:
</p><pre>
---> A-0
| \
| A's project tree with local changes
|
X-1 ----+---> B-0 -> B-1 -> B-2 -> B-3
|
---> C-0
</pre>
<p>Programmer <code>A</code>
wants to develop on top of those three patches from
<code>B</code>
, and so forms a merge. At this stage, <code>A</code>
can do a simple
<code>update</code>
or <code>replay</code>
to create a merged project tree:
</p><pre>
---> A-0
| \
| A's merged project tree
| ^
| -----------|---------
| / \
X-1 ----+---> B-0 -> B-1 -> B-2 -> B-3
|
---> C-0
</pre>
<p>Let's assume that <code>A</code>
's merge involved some conflicts: <code>B</code>
's code has
been slightly rearranged in the merged tree. Now <code>A</code>
can check in
that revision. Meanwhile, <code>C</code>
starts work:
</p><pre>
---> A-0 -> A-1
| ^
| |
| -------|-------------
| / \
X-1 ----+---> B-0 -> B-1 -> B-2 -> B-3
|
| C's project tree with local changes
| /
---> C-0
</pre>
<p><code>C</code>
decides it would be a good idea to merge with the feature's found
in <code>A-1</code>
. In doing so, he'll also be picking up <code>B-0..3</code>
. Once
again, a simple <code>update</code>
(or <code>replay</code>
) 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</code>
works on his next patch, and <code>A</code>
commits a new revision:
</p><pre>
---> 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
</pre>
<p>We're nearly done with the set-up: <code>B</code>
decides to merge in the changes
in <code>A-2</code>
. This is a slightly interesting merge (although not the
primary topic of this chapter). The common ancestor of <code>A-2</code>
and
<code>B</code>
's project tree is <code>B-3</code>
. We previously assumed that when <code>A</code>
updated against <code>B-3</code>
there were conflicts that had to be resolved by
hand. <code>B</code>
has a choice. He can use <code>update</code>
against <code>A-2</code>
to create
a new tree:
</p><pre>
delta (B-3, B's project tree) [A-2]
</pre>
<p>giving priority to <code>A</code>
's resolution of those merge conflicts. Or, he
can commit his project tree, <code>get</code>
revision <code>A-2</code>
, and update against
the committed project tree (or do an equivalent thing by hand, without
comitting, using <code>mkpatch</code>
and <code>dopatch</code>
):
</p><pre>
delta(B-3, A-2) [B's project tree]
</pre>
<p>giving priority to <code>B</code>
's code, and reconsidering the merge conflicts
that <code>A</code>
handled. The choice is arbitrary and the best answer depends
on the particular changes made. <code>B</code>
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:
</p><pre>
---> 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
</pre>
<pre>
or in English:
</pre>
<pre>
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
</pre>
<p>Finally, let's assume that the main development path, <code>X</code>
, has evolved
independently of these three branches, and that <code>A</code>
has added a few
more revisions:
</p><pre>
---> 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
</pre>
<a name="The_Challenge"></a>
<h3 align=center>The Challenge</h3>
<p>Whew. What an (unfortunately plausible) mess. Now for the challenge:
</p><pre>
Create X-3, which is up-to-date with A-4, B-5, and C-1
</pre>
<p>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</code>
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.
</p>
<a name="The_Simple_update_Solutions"></a>
<h3 align=center>The Simple update Solutions</h3>
<p>Simple <code>update</code>
gives us a whole collection of simplistic solutions.
For example, <code>X</code>
could update against <code>A</code>
, then <code>B</code>
, then <code>C</code>
or:
</p><pre>
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]
</pre>
<p>That <code>update</code>
path has some problems, though. <code>delta (A-4, X-1)</code>
includes the changes in <code>delta (A-1, X-1)</code>
, and so does <code>delta (B-5,
X-1)</code>
. So creating <code>intermediate-2</code>
will involve redundant patching
and plenty of opportunities for conflicts. Similar problems occur
when creating <code>X-3-candidate</code>
.
</p><p><code>X</code>
could try doing the <code>update</code>
s in a different order, but similar
problems will still occur.
</p>
<a name="The_Simple_replay_Solutions"></a>
<h3 align=center>The Simple replay Solutions</h3>
<p><code>X</code>
could <code>replay</code>
the branches in some order. Suppose he <code>replays</code>
<code>A</code>
, then <code>B</code>
, then <code>C</code>
:
</p><pre>
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 ]]
</pre>
<p>History sensitivity helped a bit there: <code>replay</code>
knows better than to
apply <code>B-0..3</code>
-- eliminating one source of needless conflicts.
Still, when we replay <code>B-5</code>
and <code>C-1</code>
, there will be plenty of
conflicts to make up for that.
</p><p>It's also worth mentioning that that this solution involves applying
nine different patches: we can do better. By differently ordering the
<code>replay</code>
solution, we get by with fewer patches (replay <code>C</code>
first,
then <code>A</code>
, then <code>B</code>
, for example). Figuring out the best order in
which to apply patches is, ultimately, the subject of this chapter:
</p>
<a name="The_reconcile_Solution"></a>
<h3 align=center>The reconcile Solution</h3>
<p>Suppose that <code>X</code>
asks, of the tree <code>X-2</code>
:
</p><pre>
% larch whats-missing A B C
</pre>
<p>the answer is:
</p><pre>
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
</pre>
<p><code>X</code>
can also ask the more interesting question:
</p><pre>
% larch whats-missing --merges A B C
</pre>
<p>which will answer not only what patches are missing, but what patches
include other patches:
</p><pre>
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
</pre>
<p><code>X</code>
can pipe that list into a filtering command, <code>larch reconcile</code>
,
which does some magic (the trick is revealed below):
</p><pre>
% larch whats-missing --merges A B C \
| larch reconcile
C-0
C-1
B-4
B-5
A-3
A-4
</pre>
<p>which means that X can perform the merge with just:
</p><pre>
A-4 [ A-3 [ B-5 [ B-4 [ C-1 [ C-0 [ X-2 ]]]]]]
</pre>
<p>There is still a potential source of conflicts -- when applying <code>B-5</code>
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.
</p><p>How did <code>reconcile</code>
find that solution? What's the magic?
Conceptually, <code>reconcile</code>
works in two steps.
</p><p>First, <code>reconcile</code>
computes a subset of all the patches: the
<em>
<a name="index-pt:1"></a>
necessary patches
</em>
. 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.)
</p><p>Second, <code>reconcile</code>
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</code>
.
</p><p>So, you don't believe this obscure command is useful in real life?
See <a href="implementing-policies.html#Even/Odd_Versions">Even/Odd Versions</a>.
</p>
<small><i>arch: The arch Revision Control System
</i></small><br>
<a href="http://www.regexps.com">The Hackerlab at <code>regexps.com</code></a>
</body>
|