| 1 |
# First-class conflicts |
| 2 |
|
| 3 |
## Introduction |
| 4 |
|
| 5 |
Conflicts can happen when two changes are applied to some state. This document |
| 6 |
is about conflicts between changes to files (not about [conflicts between |
| 7 |
changes to bookmark targets](concurrency.md), for example). |
| 8 |
|
| 9 |
For example, if you merge two branches in a repo, there may be conflicting |
| 10 |
changes between the two branches. Most DVCSs require you to resolve those |
| 11 |
conflicts before you can finish the merge operation. Jujutsu instead records |
| 12 |
the conflicts in the commit and lets you resolve the conflict when you feel like |
| 13 |
it. |
| 14 |
|
| 15 |
## Data model |
| 16 |
|
| 17 |
When a merge conflict happens, it is recorded as an ordered list of tree objects |
| 18 |
linked from the commit (instead of the usual single tree per commit). There will |
| 19 |
always be an odd number of trees linked from the commit. You can think of the |
| 20 |
first tree as a start tree, and the subsequent pairs of trees to apply the diff |
| 21 |
between onto the start. Examples: |
| 22 |
|
| 23 |
* If the commit has trees A, B, C, D, and E it means that the contents should be |
| 24 |
calculated as A+(C-B)+(E-D). |
| 25 |
* A three-way merge between A and C with B as base can be represented as a |
| 26 |
commit with trees A, B, and C, also known as A+(C-B). |
| 27 |
|
| 28 |
The resulting tree contents is calculated on demand. Note that we often don't |
| 29 |
need to merge the entire tree. For example, when checking out a commit in the |
| 30 |
working copy, we only need to merge parts of the tree that differs from the |
| 31 |
tree that was previously checked out in the working copy. As another example, |
| 32 |
when listing paths with conflicts, we only need to traverse parts of the tree |
| 33 |
that cannot be trivially resolved; if only one side modified `lib/`, then we |
| 34 |
don't need to look for conflicts in that sub-tree. |
| 35 |
|
| 36 |
When merging trees, if we can't resolve a sub-tree conflict trivially by looking |
| 37 |
at just the tree id, we recurse into the sub-tree. Similarly, if we can't |
| 38 |
resolve a file conflict trivially by looking at just the id, we recurse into |
| 39 |
the hunks within the file. |
| 40 |
|
| 41 |
See [here](../git-compatibility.md#format-mapping-details) for how conflicts are |
| 42 |
stored when using the Git commit backend. |
| 43 |
|
| 44 |
## Conflict simplification |
| 45 |
|
| 46 |
Remember that a 3-way merge can be written `A+(C-B)`. If one of those states is |
| 47 |
itself a conflict, then we simply insert the conflict expression there. Then we |
| 48 |
simplify by removing canceling terms. These two steps are implemented in |
| 49 |
`Merge::flatten()` and `Merge::simplify()` in [`merge.rs`][merge-rs]. |
| 50 |
|
| 51 |
For example, let's say commit B is based on A and is rebased to C, where it |
| 52 |
results in conflicts (`C+(B-A)`), which the user leaves unresolved. If the |
| 53 |
commit is then rebased to D, the result will be `D+((C+(B-A))-C)`. That expression |
| 54 |
can be simplified to `D+(B-A)`, which is a regular 3-way merge between D and B |
| 55 |
with A as base (no trace of C). This is what lets the user keep old commits |
| 56 |
rebased to head without resolving conflicts and still not get messy recursive |
| 57 |
conflicts. |
| 58 |
|
| 59 |
As another example, let's go through what happens when you back out a conflicted |
| 60 |
commit. Let's say we have the usual `E = C+(B-A)` conflict on top of |
| 61 |
non-conflict state `C`. We then revert that change. Reverting a change means |
| 62 |
applying its reverse diff `-(E-C)`, so the result is `E+(C-E) = |
| 63 |
(C+(B-A))+(C-(C+(B-A)))`, which we can simplify to just `C` (i.e. no conflict). |
| 64 |
|
| 65 |
## Same-change rule |
| 66 |
|
| 67 |
When all sides of a conflict make the same change, [we automatically consider it |
| 68 |
resolved to that value][resolve] by default. We call this "the same-change |
| 69 |
rule". This behavior matches what Git and Mercurial do. Darcs, on the other |
| 70 |
hand, considers it a conflict. The automatic conflict resolution we do is lossy |
| 71 |
in terms of conflict algebra; it means that rebasing a commit onto a commit that |
| 72 |
has the same changes (or a subset thereof) and then rebasing it back will lose |
| 73 |
changes (for a real-life example see [bug #6369]). We do it because it is more |
| 74 |
user-friendly in the vast majority of cases. |
| 75 |
|
| 76 |
[bug #6369]: https://github.com/jj-vcs/jj/issues/6369 |
| 77 |
[merge-rs]: https://github.com/jj-vcs/jj/blob/main/lib/src/merge.rs |
| 78 |
[resolve]: https://github.com/jj-vcs/jj/blob/53272510bf879086d83bb5eea1406f75ba31f138/lib/src/merge.rs#L85-L99 |