Debian Codegraph @

cli/docs/design/jj-converge-command.md

raw
1 # jj converge (aka resolve-divergence) Command Design
2
3 Authors: [David Rieber](mailto:[email protected]),
4 [Martin von Zweigbergk](mailto:[email protected])
5
6 **Summary:** This document is a proposal for a new `jj converge` command to help
7 users resolve (or reduce) divergence. The command will use heuristics -- and
8 sometimes will prompt the user for input -- to rewrite the N visible commits for
9 a given change with a single new commit, without introducing new divergence in
10 the process. `jj resolve-divergence` will be an alias for `jj converge`.
11
12 ## Objective
13
14 A [divergent change] occurs when multiple [visible commits] have the same change
15 ID. Divergence is not a desirable state, but is not a bad state either. In this
16 regard divergence is similar to conflicts: the user can choose when and how to
17 deal with divergence. The [Handling divergent commits] guide has some useful
18 tips, but nevertheless divergence is confusing to our users. We can do better
19 than that. It should be possible to solve divergence (after the fact) in many
20 scenarios with the help of this command. Solving divergence means rewriting the
21 commit graph to end up with a single visible commit for the given change id. For
22 the purposes of this design doc we call this commit the *"solution"*.
23
24 The command should produce informative messages to summarize any changes made,
25 and will prompt for user input in some situations. The user may of course not
26 like the solution. `jj undo` can be used in that case.
27
28 [divergent change]: ../glossary.md#divergent-change
29 [visible commits]: ../glossary.md#visible-commits
30 [Handling divergent commits]: ../guides/divergence.md
31
32 ## Divergent changes
33
34 Divergent commits (for the same change-id) can differ:
35
36 * In their commit description (including tags)
37 * In their commit trees
38 * In the parent(s) of the commits (commits *B/0* and *B/1* for change *B* have
39 different parents)
40 * In the commit author
41 * It is also possible divergence involves two commits with different
42 timestamps that are otherwise identical
43
44 As you read this design doc it is important to not confuse the
45 *predecessor / successor* relationship versus the *ancestor / descendant*
46 relationship.
47
48 ### Some divergence scenarios
49
50 Divergence can be introduced in many ways. This document does not aim to explain
51 any / all of those scenarios accurately, this section is only meant to be rough
52 background material. Here are some examples:
53
54 * In one terminal you type `jj describe` to edit a commit description and
55 while the editor is open you take a coffee break, when you come back you
56 open another terminal and do something that rewrites the commit (for example
57 you modify a file and run `jj log`, causing a snapshot). When you save the
58 new description `jj describe` completes and you end up with 2 visible
59 commits with the same change id.
60
61 * In general any interactive jj command (`jj split -i`, `jj squash -i`, etc.)
62 can lead to divergence in a similar way.
63
64 * You can introduce divergence by making some hidden predecessor of your
65 change visible again. There are many ways this could happen.
66
67 * Divergence can happen when mutating two workspaces. For example, assume you
68 have workspaces w1 and w2 with working copy commits *A* and *B*
69 respectively, where *B* is a child of *A*. In w2 you run `jj git fetch` and
70 then rebase the whole branch onto main. Go back to w1 (which is now stale),
71 modify some file on disk and take a snapshot (e.g. run `jj log`). This
72 introduces divergence.
73
74 * When using the Git backend jj propagates change-id. The change-id is stored
75 in the commit header, so after jj git fetch you can end up with a second
76 commit with the same change-id.
77
78 * There is a Google-specific jj upload command to upload a commit to Google's
79 review / test / submit system, and there is an associated Google-specific
80 command to "download" a change from that system back to your jj repo. This
81 can introduce divergence very much like in the Git scenario.
82
83 * At Google, snapshotting operations happen concurrently on different machines
84 (e.g. two terminals, or more commonly, a terminal and an IDE). Often times
85 they end up snapshotting the same content. Google's backend does not hold
86 locks while snapshotting because it's a distributed filesystem, so locking
87 would be slow.
88
89 ## Strawman proposal
90
91 We look at some examples to illustrate what the command should do, starting with
92 simple cases and moving on to more complex ones.
93
94 ### Examples and expected behavior (with basic evolution graph)
95
96 The first few examples assume commits *B/0* and *B/1* are visible commits for
97 change *B*. First we assume *B/0* and *B/1* evolve directly from a common
98 predecessor commit *P*, which is now hidden (no longer visible). Later we look
99 at more complex evolution graphs. Note that *P*'s change id is also *B*.
100
101 ```
102 Evolution graph for examples 1, 2, 3 and 4.
103 B/0 and B/1 may have other predecessors for unrelated change-ids, P may have
104 predecessors (even for change-id B):
105
106 B/0
107 |
108 | B/1
109 |/
110 P
111 ```
112
113 We will write `A⁻` to denote the parent trees of commit `A`.
114
115 #### Example 1: two commits for change *B*, same parent
116
117 ```console
118 $ jj log
119 B/0
120 |
121 | B/1
122 |/
123 A
124 ```
125
126 In this simple case it is clear the solution should be a child of *A*:
127
128 ```console
129 $ jj log
130 B (solution)
131 |
132 | B/0 (not visible)
133 |/
134 | B/1 (not visible)
135 |/
136 A
137 ```
138
139 Let's now consider two cases: when *P*'s parent is *A*, and when *P* has some
140 other parent. First, if *P*'s parent is *A* we have:
141
142 ```console
143 $ jj log
144 B/0
145 |
146 | B/1
147 |/
148 | P (not visible)
149 |/
150 A
151 ```
152
153 Here *P*, *B/0* and *B/1* are siblings. The command needs to determine the
154 description, parents, tree and author of the solution. It uses a simple data
155 structure for this purpose:
156
157 ```rust
158 struct MergedState {
159 author: Merge<Signature>,
160 description: Merge<String>,
161 parents: Merge<Vec<CommitId>>,
162 tree: Merge<MergedTree>,
163 }
164 ```
165
166 Each of the fields of `MergedState` are populated by doing a merge of the
167 corresponding fields of *P*, *B/0* and *B/1*. Loosely speaking each merge can be
168 expressed as `P + (B/0 - P) + (B/1 - P)` for each of the fields. The command
169 attempts to resolve the various Merge objects trivially, using `same_change:
170 SameChange::Accept` (later on in this design doc we will tweak the merge
171 algorithm a bit).
172
173 The description is merged as a String value. If the description does not
174 trivially resolve, the user's merge tool will be invoked, with conflict markers.
175 If author does not trivially resolve, the user will be presented with the
176 options to choose from. Once that's all done we have our solution commit *B*.
177 All descendants of *B/0* and *B/1* are rebased onto *B*. The command records the
178 operation in the operation log with a new View where *B* is a visible commit
179 with *{B/0, B/1}* as predecessors. *B/0* and *B/1* become hidden commits.
180
181 Note that in some cases the solution may be identical to either *B/0* or *B/1*
182 (in all regards except the commit timestamp): we choose to create a new commit
183 *B* to make the evolution graph and op log more clearly show that jj converge
184 was invoked. Alternatively we could keep the matching commit instead of creating
185 a new commit (this could result in cycles in the evolog).
186
187 #### Example 2: two commits for change *B* with same parent (predecessor has a different parent)
188
189 Now lets consider the case where *P* has a different parent:
190
191 ```console
192 $ jj log
193 B/0
194 |
195 | B/1
196 |/
197 A
198 | P (not visible)
199 |/
200 X
201 ```
202
203 In this case we first rebase *P* onto *A* (in-memory) to produce `P' = A + (P -
204 P⁻)`. This essentially reduces the problem to the previous case. We now produce
205 the solution as before: `B = P' + (B/0 - P') + (B/1 - P')`. Note that again the
206 parent of the solution is *A*.
207
208 #### Example 3: divergent commits with different parents
209
210 ```console
211 $ jj log
212 B/0
213 |
214 | B/1
215 | /
216 | C
217 |/
218 A
219 ```
220
221 In this case it is not immediately obvious which commit should be the parent of
222 the solution. Let's first consider the case where *P* is a child of *A*.
223
224 ```console
225 $ jj log
226 B/0
227 |
228 | B/1
229 | /
230 | C
231 |/
232 | P (not visible)
233 |/
234 A
235 ```
236
237 We determine the parent(s) of the solution as follows:
238
239 ```
240 parents = P⁻ + (B/0⁻ - P⁻) + (B/1⁻ - P⁻)
241 ```
242
243 In this example the expression evaluates to `{A} + ({A} - {A}) + ({C} - {A}) =
244 {C}`. Since this expression resolves trivially to *{C}*, we use that as the
245 parents of the solution.
246
247 Note that this simple algorithm produces the desired output in example 1 and
248 example 2. In example 2, the expression looks like this:
249
250 ```
251 parents = P⁻ + (B/0⁻ - P⁻) + (B/1⁻ - P⁻)
252 = {X} + ({A} - {X}) + ({A} - {X})
253 = {A} + ({A} - {X})
254 ```
255
256 That expression resolves trivially to *{A}* when using SameChange::Accept.
257
258 #### Example 4: divergent commits with different parents, must prompt user to choose parents
259
260 If instead *P* is a child of some other commit *X*, the story is a bit
261 different:
262
263 ```console
264 $ jj log
265 B/0
266 |
267 | B/1
268 | |
269 | C
270 |/
271 A
272 | P (not visible)
273 |/
274 X
275 ```
276
277 In this case parents will be
278
279 ```
280 {X} + ({A} - {X}) + ({C} - {X}) = {A} + ({C} - {X})
281 ```
282
283 Since this does not trivially resolve, the command prompts the user to select
284 the desired parents for the solution: either *{A}* or *{C}*.
285
286 Assume the user chooses *{C}*. The command then rebases (in memory) *B/0*, *B/1*
287 and *P* onto the chosen parents:
288
289 ```
290 In-memory commits after rebasing B/0, B/1 and P on top of C (edges represent
291 parent/child relationship):
292
293 # B/0' = C + (B/0 - A)
294 # B/1' = C + (B/1 - C) = B/1
295 # P' = C + (P - X)
296
297 B/0'
298 |
299 | B/1'
300 |/
301 | P'
302 |/
303 C
304 ```
305
306 As a result we obtain *B/0'*, *B/1'* and *P'*, and these are sibling commits. At
307 this point the command does a 3-way merge of `MergedTree` objects to produce
308 `MergedState::tree` (in reality it is enough to rebase the commit *trees*).
309
310 #### Example 5: more than 2 divergent commits
311
312 There can be more than 2 visible commits for a given change-id. We are assuming
313 here *B/0*, *B/1* and *B/2* are all direct successors of commit *P* (which is
314 invisible).
315
316 ```console
317 $ jj log
318 B/0
319 |
320 | B/1
321 | |
322 | | B/2
323 | |/
324 |/
325 A
326 ```
327
328 This is completely analogous to the first example, we simply have more terms on
329 all merges. The same thing applies to all previous examples, in all cases we can
330 deal with any number of divergent commits for change *B*.
331
332 ### Examples and expected behavior (with arbitrary evolution graph)
333
334 So far we only considered simple cases where all divergent commits are direct
335 successors of a common predecessor *P*. Now we extend the ideas to arbitrary
336 evolution history. To that end we introduce the *"truncated evolution graph for
337 B/0, B/1, ... , B/n"*, where *B/0, B/1, ... , B/n* are two or more commits with
338 the same change-id *B*. This is a directed graph. Its nodes are commits for
339 change-id *B* and the edges are from a commit to its (immediate) predecessor(s),
340 ignoring predecessors with unrelated change-ids. The graph is built by
341 traversing the operation log and associated View objects, adding nodes and
342 predecessor edges as needed. Nodes are added this way until a single most-recent
343 common predecessor commit is found. We call the most-recent common predecessor
344 the *"evolution fork point of B/0, ... , B/n"*. The traversal keeps track of
345 visited commits to avoid infinite loops [^footnote-about-evolog-cycles]
346 [^virtual-evolution-fork-point].
347
348 #### Example 6: a two-level evolution graph
349
350 We continue by looking at a truncated evolution graph that is slightly more
351 complex than the basic 3-commit case. This will serve as motivation for the
352 general case. Here is our truncated evolution graph (remember the edges here
353 represent change evolution, not parent-child relations):
354
355 ```
356 Truncated evolution graph. B/0, B/1 and Q may have other predecessors for
357 unrelated change-ids. P is the evolution fork point (it may have predecessors,
358 even for change-id B):
359
360 B/0 (description: "v3")
361 |
362 | B/1 (description: "v2")
363 Q / (description: "v2")
364 |/
365 P (description: "v1")
366 ```
367
368 Commit *P* evolved into *Q* and *B/1*, and *Q* evolved into *B/0*. As before
369 *B/0* and *B/1* are visible, *P* and *Q* are not. Since both sides of the
370 evolution transitioned from "v1" to "v2", and then one side further transitioned
371 to "v3", it seems a good heuristic to take "v3" as the description of the
372 solution. Note that this observation would not be possible if the algorithm only
373 considered the leafs (*B/0*, *B/1*) and their evolution fork point (*P*).
374
375 Note: Why do we care about divergence producing two commits with the exact same
376 change? It may seem this would be a very uncommon scenario, however, as
377 mentioned in the last bullet point in the "Some divergence scenarios" section,
378 this is in fact fairly common at Google due to the distributed nature of
379 Google's backend filesystem.
380
381 To implement the heuristic we outlined above in example 6 (i.e. to produce
382 "v3"), we propose introducing a new *try_resolve_deduplicating_same_diffs*
383 method to `Merge<T>`, and using that in calls to attempt to resolve the
384 MergedState::description, MergedState::parent, MergedState::author and
385 MergedState::trees. try_resolve_deduplicate_same_diffs is similar to
386 resolve_trivial, but it counts multiple identical *(X - Y)* terms exactly once,
387 otherwise it follows the same flow as resolve_trivial with SameChange::Accept.
388
389 We illustrate what *try_resolve_deduplicating_same_diffs* does when resolving
390 the description merge for the case in example 6. We build a `Merge<String>`,
391 starting with the description of the evolution fork point *P*, then adding
392 *desc(Y) - desc(X)* terms for each *X->Y* edge in the truncated evolution graph.
393 Then we call *try_resolve_deduplicating_same_diffs* to get:
394
395 ```
396 P + (Q - P) + (B/1 - P) + (B/0 - Q) =
397 = v1 + (v2 - v1) + (v2 - v1) + (v3 - v2) <== collapse duplicate
398 (v2 - v1) edge/term
399 = v1 + (v2 - v1) + (v3 - v2)
400 = v3
401 ```
402
403 *try_resolve_deduplicating_same_diffs* returns our desired value "v3". Note that
404 resolve_trivial (with either SameChange value) would return none. Here is
405 another example:
406
407 ```
408 Truncated evolution graph:
409
410 B/0 ( foo.txt contents: "v3" )
411 |
412 | B/1 ( foo.txt contents: "v2" )
413 Q / ( foo.txt contents: "v1" )
414 |/
415 P ( foo.txt contents: "v1" )
416 ```
417
418 In this case *try_resolve_deduplicating_same_diffs* produces none. jj converge
419 cannot automatically resolve this merge so the user has to merge the
420 description: the command invokes the user's merge-tool with base "v1" and sides
421 "v2"/"v3".
422
423 ### Edge cases when choosing the parents of the solution
424
425 When attempting to produce the solution parents, the command applies
426 *try_resolve_deduplicating_same_diffs* to MergedState::parents (of type
427 `Merge<Vec<CommitId>>`). If the result is `Some<Vec<CommitId>>` we have a set of
428 possible parents for the solution. If these candidate parents are all visible
429 commits with change-ids other than *B*, and none of those are descendants of
430 *B/0, B/1, ... B/n*, then we have the desired parents for the solution commit.
431
432 On the other hand,
433
434 * if any candidate parent is a descendant of one of the divergent commits we
435 are trying to solve, or
436 * if any of the candidate parents are hidden and the chain starting at visible
437 roots and leading up to and including the parent has any commit for the
438 divergent change we are trying to solve (*B*), or for any other visible
439 commit (divergent or not), or the chain itself has two or more commits with
440 the same change-id,
441
442 then we would introduce new divergence or cycles in the commit graph if we based
443 on the solution on such candidate parents. In these edge cases the command will
444 simply discard the candidate parents and will instead ask the user to choose
445 which parents to use (or quit without making any changes). Care must be
446 taken when picking the options to present to the user for choosing parents:
447 essentially the user will choose between the parents of *B/0*, the parents of
448 *B/1*, and so on, but we will skip any *B/i* if any of the parents of *B/i*
449 descends from any *B/j*. Since the commit graph is a DAG, at least one option is
450 viable.
451
452 ## Multiple divergent change-ids
453
454 If there are multiple divergent change-ids, the command could prompt the user to
455 choose one, or apply heuristics to choose one programmatically. In the first
456 version it is OK to prompt the user.
457
458 If the command successfully resolves divergence in the first divergent
459 change-id, it could continue to process the next divergent change-id, and so on.
460 To avoid complexity the first implementation will only deal with one divergent
461 change per invocation.
462
463 ### Rebasing descendants and persisting
464
465 The last step is to rebase all descendants of the divergent commits on top of
466 the new solution commit, persist the changes and record the operation in the op
467 log. The command will move local bookmarks pointing to any of the rewritten
468 divergent commits to point to the solution commit.
469
470 ## Other edge cases
471
472 When the command starts it needs to find the divergent change-ids and their
473 corresponding visible commits. If the portion of the visible commit graph
474 leading up to immutable heads is too big, the command should error out.
475
476 There could be pathological cases where the evolution history is too long. When
477 building the truncated evolution graph, if we have traversed too many nodes (say
478 50) and we have not yet completed the traversal, the algorithm will not traverse
479 any more commits. We could simply error out, or we could use an incomplete
480 truncated evolution graph by adding a virtual evolution fork point. It is
481 probably best to error out.
482
483 ## Open questions
484
485 * Do we ever have divergence of committer? Is it safe to mess with committer?
486
487 ## Alternatives considered
488
489 ### Automatically resolving divergence
490
491 It would be nice if divergence was avoided in the first place, at least in some
492 cases, at the point where jj is about to introduce the second (or third or
493 fourth, etc.) visible commit for a given change id. This should be investigated
494 separately.
495
496 ### Resolve divergence two commits at a time
497
498 The algorithm in this proposal should work when there are any number of
499 divergent commits (for a given change id). In practice we expect most often
500 there will be just 2 or perhaps a few divergent commits. We could design an
501 algorithm for just 2 commits, but we chose to think about the more general case.
502
503 ### Only considering the evolution fork point and visible commits
504
505 As explained in example 6 this proposal uses the truncated evolution graph and
506 *try_resolve_deduplicating_same_diffs* to produce the solution. That example
507 shows why we think this leads to a better heuristic. We could instead only
508 consider *P* and *B/0, B/1, ... , B/n*. That would be slightly simpler.
509
510 [^footnote-about-evolog-cycles]: It is unclear if the evolution history can
511 contain cycles today, but there has been some
512 discussion about `jj undo` possibly producing
513 cycles. In any case, it is very easy to deal
514 with that possibility, so we may as well handle
515 it?
516 [^virtual-evolution-fork-point]: Today there should always be a single evolution
517 fork point. However, we could handle cases
518 where a change-id "emanates from multiple
519 initial commits" by adding a single *virtual
520 evolution fork point* commit with empty state:
521 empty description, empty tree and empty author,
522 and having the root commit as its parent, and
523 treating it as a predecessor of all initial
524 commits. Again, we probably don't need to worry
525 about this, but it is good to know we could
526 handle it.