| 1 |
# Copy Tracking and Tracing Design |
| 2 |
|
| 3 |
Authors: [Daniel Ploch](mailto:[email protected]), [Martin von Zweigbergk](mailto:[email protected]) |
| 4 |
|
| 5 |
**Summary:** This Document documents an approach to tracking and detecting copy |
| 6 |
information in jj repos, in a way that is compatible with both Git's detection |
| 7 |
model and with custom backends that have more complicated tracking of copy |
| 8 |
information. This design affects the output of diff commands as well as the |
| 9 |
results of rebasing across remote copies. |
| 10 |
|
| 11 |
## Objective |
| 12 |
|
| 13 |
Add support for copy information that is sufficient for at least the following |
| 14 |
use cases: |
| 15 |
|
| 16 |
* Diffing: If a file has been copied, show a diff compared to the source version |
| 17 |
instead of showing a full addition. |
| 18 |
* Merging: When one side of a merge (or rebase) has renamed a file and the other |
| 19 |
side has modified it, propagate the changes to the other side. (There are many |
| 20 |
other cases to handle too.) |
| 21 |
* Log: It should be possible to run something like `jj log -p <file>` and follow |
| 22 |
the file backwards when it had been created by copying. |
| 23 |
* Annotate (blame): Similar to the log use case, we should follow the file |
| 24 |
backwards when it had been created by copying. |
| 25 |
|
| 26 |
The solution should support recording and retrieving copy info in a way that |
| 27 |
is performant both for Git, which synthesizes copy info on the fly between |
| 28 |
arbitrary trees, and for custom backends which may explicitly record and |
| 29 |
re-serve copy info over arbitrarily large commit ranges. |
| 30 |
|
| 31 |
The APIs should be defined in a way that makes it easy for custom backends to |
| 32 |
ignore copy info entirely until they are ready to implement it. |
| 33 |
|
| 34 |
### Desired UX |
| 35 |
|
| 36 |
The following sections describe some scenarios and how we would ideally handle |
| 37 |
them. |
| 38 |
|
| 39 |
We have not seen much reason to distinguish copies from renames, so a rename |
| 40 |
is simply the same thing as a copy plus a deletion. This means that we cannot |
| 41 |
distinguish "copy `foo` to `bar` and rename `foo` to `baz`" from "copy `foo` |
| 42 |
to `baz` and rename `foo` to `bar`". |
| 43 |
|
| 44 |
#### Restoring from a commit should preserve copies |
| 45 |
|
| 46 |
For example, `jj new X--; jj restore --from X` should restore any copies |
| 47 |
made in `X-` and `X` into the new working copy. Transitive copies should |
| 48 |
be "flattened". For example, if `X-` renamed `foo` to `bar` and `X` renamed |
| 49 |
`bar` to `baz`, then the restored commit should rename `foo` to `baz`. |
| 50 |
|
| 51 |
This also applies to reparenting in general, such as for |
| 52 |
["verbatim rebase"](https://github.com/martinvonz/jj/issues/1027). |
| 53 |
|
| 54 |
#### Diff after restore |
| 55 |
|
| 56 |
`jj restore --from X; jj diff --from X` should be empty, at least when it comes |
| 57 |
to file contents. It may indicate that renamed file have different history. |
| 58 |
|
| 59 |
#### Lossless round-trip of rebase |
| 60 |
|
| 61 |
Except for the [`A+(A-B)=A` rule][same_change_rule], rebasing is currently never |
| 62 |
lossy; rebasing a commit and then rebasing it back yields the same content. We |
| 63 |
should ideally preserve this property when possible. |
| 64 |
|
| 65 |
For example: |
| 66 |
|
| 67 |
```console |
| 68 |
$ jj log |
| 69 |
C rename bar->baz |
| 70 |
| |
| 71 |
B rename foo->bar |
| 72 |
| |
| 73 |
A add foo |
| 74 |
|
| 75 |
$ jj rebase -r C -o A |
| 76 |
$ jj rebase -r C -o B # Takes us back to the state above |
| 77 |
``` |
| 78 |
|
| 79 |
#### Reverting the parent commit should be a no-op |
| 80 |
|
| 81 |
Patches should be reversible so you can make a change and then revert it, and |
| 82 |
end up with an empty diff across both commits. |
| 83 |
|
| 84 |
For example: |
| 85 |
|
| 86 |
```console |
| 87 |
$ jj log |
| 88 |
B rename foo->bar |
| 89 |
| |
| 90 |
A add foo |
| 91 |
|
| 92 |
$ jj revert -r B -o B |
| 93 |
$ jj diff --from B- --to B+ # Should be empty |
| 94 |
``` |
| 95 |
|
| 96 |
#### Parallelize/serialize |
| 97 |
|
| 98 |
This is a special case of the lossless rebase. |
| 99 |
|
| 100 |
```console |
| 101 |
$ jj log |
| 102 |
E edit qux |
| 103 |
| |
| 104 |
D rename baz->qux |
| 105 |
| |
| 106 |
C rename bar->baz |
| 107 |
| |
| 108 |
B rename foo->bar |
| 109 |
| |
| 110 |
A add foo |
| 111 |
|
| 112 |
$ jj parallelize B::D |
| 113 |
# There should be no conflict in E and it should look like a |
| 114 |
# regular edit just like before |
| 115 |
$ jj rebase -r C -A B |
| 116 |
$ jj rebase -r D -A C |
| 117 |
# Now we're back to the same graph as before. |
| 118 |
``` |
| 119 |
|
| 120 |
#### Copies inside merge commit |
| 121 |
|
| 122 |
We should be able to resolve a naming conflict: |
| 123 |
|
| 124 |
```console |
| 125 |
$ jj log |
| 126 |
D resolve naming conflict by choosing `foo` as the source |
| 127 |
|\ |
| 128 |
C | rename bar->baz |
| 129 |
| | |
| 130 |
| B rename foo->baz |
| 131 |
|/ |
| 132 |
A add foo and bar |
| 133 |
|
| 134 |
$ jj file annotate baz # Should not include changes from C |
| 135 |
``` |
| 136 |
|
| 137 |
We should also be able to back out that resolution and get back into the |
| 138 |
name-conflicted state. |
| 139 |
|
| 140 |
We should be able to rename files that exist on only one side: |
| 141 |
|
| 142 |
```console |
| 143 |
$ jj log |
| 144 |
D rename foo2->foo3 and bar2->bar3 |
| 145 |
|\ |
| 146 |
C | rename bar->bar2 |
| 147 |
| | |
| 148 |
| B rename foo->foo2 |
| 149 |
|/ |
| 150 |
A add foo and bar |
| 151 |
``` |
| 152 |
|
| 153 |
#### Copies across merge commit |
| 154 |
|
| 155 |
```console |
| 156 |
$ jj log |
| 157 |
D delete baz |
| 158 |
|\ |
| 159 |
C | rename foo->baz |
| 160 |
| | |
| 161 |
| B rename foo->bar |
| 162 |
|/ |
| 163 |
A add foo |
| 164 |
``` |
| 165 |
|
| 166 |
`jj diff --from C --to D` should now show a baz->bar rename (just like |
| 167 |
`jj diff --from C --to B` would). `jj diff --from B --to D` should show |
| 168 |
no renames. That's despite there being a rename in C. |
| 169 |
|
| 170 |
## High-level Design |
| 171 |
|
| 172 |
Jujutsu uses a snapshot-based model similar to Git's. The algebra for our |
| 173 |
first-class conflicts is also based on snapshots and being able to calculate |
| 174 |
patches as differences between states. That means that we have to fit copy |
| 175 |
information into that snapshot-based model too [^martinvonz_slow]. |
| 176 |
|
| 177 |
The proposal is to update tree objects to also contain information about a |
| 178 |
file's past names. For example, if file `foo` gets renamed to `bar` in one |
| 179 |
commit and then to `baz` in another commit, we will record that `baz` previously |
| 180 |
had names `bar` and `foo`. |
| 181 |
|
| 182 |
To support merging two files into one, the list of past names is actually a DAG. |
| 183 |
Merging can happen in a merge commit when two sides copy/rename different |
| 184 |
source files to the same target file. By having support for it in the model, we |
| 185 |
can also support merging multiple files into one in a regular non-merge commit. |
| 186 |
|
| 187 |
To avoid having to store all past paths in the tree object entry, we will write |
| 188 |
the copy history as an object and the tree will refer to the object by ID. Each |
| 189 |
ID refers to a node in the copy history DAG, similar to how commit IDs refer to |
| 190 |
a node in the commit DAG. |
| 191 |
|
| 192 |
Each node in the copy history DAG stores the path. Having the path in the copy |
| 193 |
graph can be useful for finding copy sources without having to scan the whole |
| 194 |
tree or having to ask the backend. |
| 195 |
|
| 196 |
If we use only the file name as only input to the ID, then we get deterministic |
| 197 |
tree IDs. On the other hand, if we add a salt to the copy graph node, then we |
| 198 |
can represent that a file was rewritten from scratch. For example, a `foo` might |
| 199 |
have copy ID `123` in the previous commit and when the file gets rewritten in |
| 200 |
the current commit, it gets copy ID `456` even though there was no copy from an |
| 201 |
existing file involved. That makes logical sense, but I'm not sure how useful |
| 202 |
it will be. |
| 203 |
|
| 204 |
The data structure might look like this: |
| 205 |
|
| 206 |
```rust |
| 207 |
// Current `TreeValue::File` variant: |
| 208 |
File { id: FileId, executable: bool }, |
| 209 |
// New `TreeValue::File` variant: |
| 210 |
File { id: FileId, executable: bool, copy_id: CopyId }, |
| 211 |
|
| 212 |
// A CopyId is a hash of this struct: |
| 213 |
struct CopyHistory { |
| 214 |
path: RepoPath, |
| 215 |
parents: Vec<CopyId> |
| 216 |
} |
| 217 |
``` |
| 218 |
|
| 219 |
Should we support copy tracking for symlinks? Their history is not very useful |
| 220 |
for annotation purposes, but knowing the history may at least be useful for |
| 221 |
detecting directory renames (if all files and symlinks in a directory were |
| 222 |
renamed). |
| 223 |
|
| 224 |
We probably should not support tracking copied directories because it seems |
| 225 |
complicated. I haven't spent much thinking about it, so it's also possible that |
| 226 |
it's not that complicated. |
| 227 |
|
| 228 |
### Diffing |
| 229 |
|
| 230 |
When diffing two trees, we first diff the trees without considering copy info. |
| 231 |
For any copy IDs that changed in that diff, we walk all of their copy graphs to |
| 232 |
figure out how they're related and which source file to associate with which |
| 233 |
destination file. |
| 234 |
|
| 235 |
The details of the algorithm is left for the implementation. The following |
| 236 |
sections provide some examples to hopefully show that it's feasible. |
| 237 |
|
| 238 |
#### Example: Divergent copy and rename |
| 239 |
|
| 240 |
Let's look at an example of how this model would look in this scenario: |
| 241 |
|
| 242 |
```console |
| 243 |
M rename foo->baz, create bar |
| 244 |
| |
| 245 |
| L copy foo->bar, create baz |
| 246 |
|/ |
| 247 |
K add foo |
| 248 |
``` |
| 249 |
|
| 250 |
Assuming the new files are different in each commit, we get the following trees. |
| 251 |
Notation: |
| 252 |
* `id`is the hash of the contents (the `FileId`) |
| 253 |
* The `2:bar->1:foo` means that copy ID 2 (i.e. hash of the `CopyHistory` |
| 254 |
struct) has file `bar`, which was copied from copy ID `1`, where it was |
| 255 |
called `foo`. |
| 256 |
|
| 257 |
```console |
| 258 |
Commit K: |
| 259 |
name: foo, id: K, copy_id: 1:foo |
| 260 |
|
| 261 |
Commit L: |
| 262 |
name: bar, id: L, copy_id: 2:bar->1:foo |
| 263 |
name: baz, id: L, copy_id: 3:baz |
| 264 |
name: foo, id: K, copy_id: 1:foo |
| 265 |
|
| 266 |
Commit M: |
| 267 |
name: bar, id: M, copy_id: 4:bar |
| 268 |
name: baz, id: M, copy_id: 5:baz->1:foo |
| 269 |
``` |
| 270 |
|
| 271 |
This graph also shows the relationship between the copy IDs and which commits |
| 272 |
they appear in: |
| 273 |
|
| 274 |
```mermaid |
| 275 |
graph LR |
| 276 |
|
| 277 |
subgraph L["Commit L"] |
| 278 |
2["2:bar"] |
| 279 |
3["3:baz"] |
| 280 |
subgraph K["Commit K"] |
| 281 |
1["1:foo"] |
| 282 |
end |
| 283 |
end |
| 284 |
|
| 285 |
subgraph M["Commit M"] |
| 286 |
4["4:bar"] |
| 287 |
5["5:baz"] |
| 288 |
end |
| 289 |
|
| 290 |
2 --> 1 |
| 291 |
5 --> 1 |
| 292 |
``` |
| 293 |
|
| 294 |
Let's first consider the diff from `K` to `M`. Looking at just the trees, that |
| 295 |
diff finds that copy IDs 1,4,5 were affected. By walking their graphs, we find |
| 296 |
1 and 5 are related, while 4 is not. Considering that copy graph (involving IDs |
| 297 |
1 and 5), since `foo` doesn't exist in the destination and `baz` doesn't exist |
| 298 |
in the source, we consider it a rename. |
| 299 |
|
| 300 |
Let's now consider the diff from `L` to `M`. This is the same as the diff of the |
| 301 |
commit `M2` we'd get by running |
| 302 |
`jj new L; jj bookmark create M2; jj restore --from M --to M2` (which would |
| 303 |
result in the commit `M2` having the same tree as `M`). Diffing from `L` to |
| 304 |
`M` (or `M2`) finds 1,2,3,4,5 as changed copy IDs. By walking their graphs, we |
| 305 |
find that 1,2, and 5 are related, while 3 and 4 are not. |
| 306 |
|
| 307 |
The `bar` and `baz` files have unrelated copy graphs, i.e. the copy graphs for |
| 308 |
the `bar` file in commit `L` and the `bar` file in commit `M` are disjoint, and |
| 309 |
the same is true for the `baz` file. Therefore, we break up their diffs into two |
| 310 |
separate diffs for each file. |
| 311 |
|
| 312 |
Among the remaining copy IDs, the shortest path in the copy graph is between |
| 313 |
`foo` on the source side and `baz` on the destination side, so we start with these. |
| 314 |
Since `foo` doesn't exist on the destination side and `baz` doesn't exist on the |
| 315 |
source side (with a related copy ID), we consider it a rename. |
| 316 |
|
| 317 |
The remaining file is `bar` on the source side. Its closest relative on the |
| 318 |
destination side is `baz`. Since we already used `baz` as a rename target for |
| 319 |
`foo`, we won't consider `bar` renamed to it. So we consider `bar` as copied |
| 320 |
into `baz`. |
| 321 |
|
| 322 |
So we get these diffs: |
| 323 |
|
| 324 |
* `baz` is deleted (deleting content `L`) |
| 325 |
* `bar` is created (with content `M`) |
| 326 |
* `foo` is renamed to `baz` (showing diff from `K` to `M`) |
| 327 |
* `bar` is merged into `baz` (showing diff from `L` to `M`) |
| 328 |
|
| 329 |
#### Example: Divergent copy and rename (best rename target) |
| 330 |
|
| 331 |
```console |
| 332 |
N copy baz->qux |
| 333 |
| |
| 334 |
M rename foo->baz |
| 335 |
| |
| 336 |
| L rename foo->bar |
| 337 |
|/ |
| 338 |
K add foo |
| 339 |
``` |
| 340 |
|
| 341 |
When diffing `L` to `N`, we find that all files are related. Since `bar` does |
| 342 |
not exist in the destination, we should find a rename target to match it with. |
| 343 |
We pick `baz` because it's closer in the graph than `qux` is. So the diff is: |
| 344 |
|
| 345 |
* `bar` is renamed to `baz` |
| 346 |
* `bar` is copied to `qux` |
| 347 |
|
| 348 |
#### Example: Copy onto deleted file |
| 349 |
|
| 350 |
```console |
| 351 |
M copy foo->bar |
| 352 |
| |
| 353 |
L delete bar |
| 354 |
| |
| 355 |
K add foo, bar |
| 356 |
``` |
| 357 |
|
| 358 |
When diffing from `K` to `M`, we notice that `bar` has different and unrelated |
| 359 |
copy IDs. We present one record saying that `bar` was deleted, and one record |
| 360 |
saying that `bar` was copied from `foo`. |
| 361 |
|
| 362 |
When diffing from `M` to `K`, we will instead present one record that says that |
| 363 |
`bar` was created, and one record that says that `bar` was merged into `foo`. |
| 364 |
|
| 365 |
### Merging |
| 366 |
|
| 367 |
When merging, we need to add a phase before the content-level merging where we |
| 368 |
handle copies. As before, we start by creating the completely unresolved merged |
| 369 |
tree based on the input trees. To find the relevant copy information, we look at |
| 370 |
the files changed in each diff and then look up the full copy graph for each. |
| 371 |
For each copy graph, we can then walk the copy graph to find possible target |
| 372 |
paths, which we then look up in the other side of the merge. If the path exists |
| 373 |
in the tree and has the right copy ID, then we know that the files are related. |
| 374 |
|
| 375 |
We assume that the differences between the bases and the first term in the |
| 376 |
conflict can be very large, so we don't look at that diff. Assuming that the |
| 377 |
commit backend can look up the full copy graph based on a given copy ID, we |
| 378 |
don't need that diff for correctness. |
| 379 |
|
| 380 |
Once we have found all copies involved in the merge, we analyze them to find |
| 381 |
conflicts, such as when two sides of the merge rename a file to the same |
| 382 |
target. If there are conflicts, we leave the trees unchanged. The user can then |
| 383 |
resolve the name conflicts using `jj resolve` (once we've added support for |
| 384 |
that). Depending on how slow the naming conflict phase turns out to be, we may |
| 385 |
want to write a flag to commits indicating that they have unresolved naming |
| 386 |
conflicts, so subsequent calls can avoid that phase. |
| 387 |
|
| 388 |
When merging trees, we start by rewriting each diff to match any different names |
| 389 |
in the destination tree. For example, if the tree conflict is `A+(B-C)+(D-E)`, |
| 390 |
then we will rewrite the `(B-C)` diff and the `(D-E)` diff to the paths in `A`. |
| 391 |
To translate the `(B-C)` diff, we calculate renames from `C` to `A` and then we |
| 392 |
apply those renames to both `C` and `B`. This may result in conflicts. |
| 393 |
|
| 394 |
If a file has a conflict in the copy ID, it will appear as if it doesn't exist |
| 395 |
when materialized. It will therefore not show up in the working copy until the |
| 396 |
user has resolved the conflict. |
| 397 |
|
| 398 |
For example: |
| 399 |
|
| 400 |
```console |
| 401 |
M set foo="bye" |
| 402 |
| |
| 403 |
| L rename foo->bar |
| 404 |
|/ |
| 405 |
K add foo="hello" |
| 406 |
``` |
| 407 |
|
| 408 |
When rebasing `M` onto `L`, we apply the `foo->bar` rename to the trees in `M` |
| 409 |
and its parent. |
| 410 |
|
| 411 |
Another example: |
| 412 |
|
| 413 |
```console |
| 414 |
N rename foo->bar |
| 415 |
| |
| 416 |
| M create foo="M" |
| 417 |
| | |
| 418 |
| L delete foo |
| 419 |
|/ |
| 420 |
K add foo="K" |
| 421 |
``` |
| 422 |
|
| 423 |
When rebasing `M` onto `N`, we find the `foo->bar` rename in `N`, but since it |
| 424 |
is unrelated to the `foo` file in `M` (assuming the `foo` file created in `M` |
| 425 |
used a different salt), we will not perform any renames. The new `foo` file |
| 426 |
is then simply created in the rebased `M` just like it was before the rebase. |
| 427 |
|
| 428 |
#### Propagating changes across copies? |
| 429 |
|
| 430 |
Should we propagate changes to copies? For example, if you've modified file |
| 431 |
`foo` and then rebase it onto a commit that copied `foo` to `bar`, should we |
| 432 |
apply your change to `bar` too? Mercurial does that but Git doesn't. It's |
| 433 |
particularly useful when a file has been split in two. For example, let's say |
| 434 |
you've made various changes in file `foo` and then rebase those change onto a |
| 435 |
commit that split `foo` into `foo1` and `foo2` (or `foo` and `bar`). If we |
| 436 |
propagate the changes to both files, each change will apply successfully in one |
| 437 |
file (assuming the changes do not overlap with the split boundary). Each change |
| 438 |
will have a modify/delete conflicts in the other file. Those can relatively |
| 439 |
easily be resolved in favor of the deleted hunk. If we do not propagate changes, |
| 440 |
then changes that belong in one of the files will instead only appear as |
| 441 |
modify/delete conflicts in the first file and you will have to manually copy |
| 442 |
over the changes to the copied file. |
| 443 |
|
| 444 |
Propagating changes to copies means that rebasing a commit and then rebasing it |
| 445 |
back is no longer a no-op even when ignoring the "same-change rule". For |
| 446 |
example, if your commit modifies file `foo` and you rebase that commit onto a |
| 447 |
commit that copied `foo` to `bar`, and then you rebase it back, the same change |
| 448 |
will be applied twice to `foo`. However, thanks to the same-change rule, we |
| 449 |
won't consider it a conflict, so maybe it actually works well in practice. |
| 450 |
|
| 451 |
A third option is to not leave it up to the user whether to propagate the |
| 452 |
change across the copy. We can do this by leaving the relevant paths in the |
| 453 |
input trees unchanged in the conflicted commit. Then we will redo the copy |
| 454 |
tracking process every time the commit is inspected. We can have `jj resolve` |
| 455 |
ask the user if they want to propagate the changes to the copy target with a |
| 456 |
simple yes/no question per copy target. |
| 457 |
|
| 458 |
Decision: Asking the user about propagating copies seems like the best option. |
| 459 |
It avoids surprises, and it makes the conflict algebra work in more cases. |
| 460 |
|
| 461 |
#### Example: Propagate changes to copied file, then rebase back |
| 462 |
|
| 463 |
```console |
| 464 |
M foo="M" |
| 465 |
| |
| 466 |
| L copy foo->bar |
| 467 |
|/ |
| 468 |
K add foo="K" |
| 469 |
``` |
| 470 |
|
| 471 |
Let's say we rebase `M` onto `L`. Since we decided to not automatically |
| 472 |
propagate changes to copies, we will leave the `M+(L-K)` tree unresolved (i.e. |
| 473 |
without making any changes to the three trees). If the user does not resolve |
| 474 |
the conflict, and instead rebases `M` back onto `K`, the conflict will be |
| 475 |
resolved automatically per the usual conflict simplification. |
| 476 |
|
| 477 |
#### Example: Multiple copies |
| 478 |
|
| 479 |
```console |
| 480 |
N foo="N" |
| 481 |
| |
| 482 |
| M foo="M, foo2="M2", foo3="M3" |
| 483 |
| | |
| 484 |
| L copy foo->foo2, copy foo->foo3 |
| 485 |
|/ |
| 486 |
K add foo="K" |
| 487 |
``` |
| 488 |
|
| 489 |
Let's say we rebase `M` onto `N`. The changes to `foo`, `foo2`, and` foo3` will |
| 490 |
then all apply to `foo`, which means we get a 4-sided conflict. |
| 491 |
|
| 492 |
#### Example: Convergent renames |
| 493 |
|
| 494 |
Consider this "convergent copy/rename" scenario: |
| 495 |
|
| 496 |
```console |
| 497 |
$ jj log |
| 498 |
C rename bar->baz |
| 499 |
| |
| 500 |
| B rename foo->baz |
| 501 |
|/ |
| 502 |
A add foo, add bar |
| 503 |
|
| 504 |
$ jj new B C |
| 505 |
``` |
| 506 |
|
| 507 |
It seems clear that `baz`'s copy graph should inherit from both `foo` and `bar`, |
| 508 |
producing a merge in copy graph. The trees would look like this: |
| 509 |
|
| 510 |
``` |
| 511 |
Commit A: |
| 512 |
name: foo, id: aaa111, copy_id: 1:foo |
| 513 |
name: bar, id: aaa111, copy_id: 2:bar |
| 514 |
|
| 515 |
Commit B: |
| 516 |
name: bar, id: aaa111, copy_id: 2:bar |
| 517 |
name: baz, id: aaa111, copy_id: 3:baz->1:foo |
| 518 |
|
| 519 |
Commit C: |
| 520 |
name: foo, id: aaa111, copy_id: 1:foo |
| 521 |
name: baz, id: aaa111, copy_id: 4:baz->2:bar |
| 522 |
|
| 523 |
Merge commit: |
| 524 |
name: baz, id: aaa111, copy_id: 5:baz->{3:baz->1:foo,4:baz->2:bar} |
| 525 |
``` |
| 526 |
|
| 527 |
We used the same content for both `foo` and `bar` above to simplify. If they |
| 528 |
had been different, we would have had a conflict in the contents but the copy |
| 529 |
ID would still have been clear. |
| 530 |
|
| 531 |
#### Example: Rebasing |
| 532 |
|
| 533 |
```console |
| 534 |
$ jj log |
| 535 |
C rename bar->baz |
| 536 |
| |
| 537 |
B rename foo->bar |
| 538 |
| |
| 539 |
A add foo |
| 540 |
|
| 541 |
$ jj rebase -r C -o A |
| 542 |
``` |
| 543 |
|
| 544 |
```console |
| 545 |
$ jj log |
| 546 |
C rename foo->baz |
| 547 |
| |
| 548 |
| B rename foo->bar |
| 549 |
|/ |
| 550 |
A add foo |
| 551 |
|
| 552 |
$ jj rebase -r C -o B |
| 553 |
``` |
| 554 |
|
| 555 |
#### Example: Rename added file |
| 556 |
|
| 557 |
A well-known and thorny problem in Mercurial occurs in the following scenario: |
| 558 |
|
| 559 |
```console |
| 560 |
$ jj log |
| 561 |
C rename foo->bar |
| 562 |
| |
| 563 |
| B modify foo |
| 564 |
|/ |
| 565 |
A add foo |
| 566 |
|
| 567 |
$ jj squash --from C --into A |
| 568 |
``` |
| 569 |
|
| 570 |
The problem here for Mercurial is that after squashing C into A, the new A has |
| 571 |
file `bar` but no record that it used to be called `foo`. The design proposed |
| 572 |
above handles this case because we keep the copy ID of `bar` after squashing, |
| 573 |
so we can detect that the modifications to `foo` in commit B should be |
| 574 |
propagated to `bar`. |
| 575 |
|
| 576 |
#### Example: Divergent renames |
| 577 |
|
| 578 |
Consider this "divergent rename" scenario: |
| 579 |
|
| 580 |
```console |
| 581 |
$ jj log |
| 582 |
C rename foo->baz |
| 583 |
| |
| 584 |
| B rename foo->bar |
| 585 |
|/ |
| 586 |
A add foo |
| 587 |
|
| 588 |
$ jj new B C |
| 589 |
``` |
| 590 |
|
| 591 |
In this scenario, the regular 3-way merge of the trees without considering copy |
| 592 |
info results in a tree without conflicts. However, the user might reasonably |
| 593 |
expect to have to choose between the `bar` and `baz` names. Here's what Git says |
| 594 |
in this scenario: |
| 595 |
|
| 596 |
```console |
| 597 |
$ git merge main |
| 598 |
CONFLICT (rename/rename): foo renamed to baz in HEAD and to bar in main. |
| 599 |
Automatic merge failed; fix conflicts and then commit the result. |
| 600 |
|
| 601 |
$ git st |
| 602 |
HEAD detached from ab0b8e3 |
| 603 |
You have unmerged paths. |
| 604 |
(fix conflicts and run "git commit") |
| 605 |
(use "git merge --abort" to abort the merge) |
| 606 |
|
| 607 |
Unmerged paths: |
| 608 |
(use "git add/rm <file>..." as appropriate to mark resolution) |
| 609 |
added by them: bar |
| 610 |
added by us: baz |
| 611 |
both deleted: foo |
| 612 |
``` |
| 613 |
|
| 614 |
Interestingly, Git seems to represent this state by using index states that |
| 615 |
would not normally end up in the index as a result of conflicts. |
| 616 |
|
| 617 |
Here's what Mercurial says: |
| 618 |
|
| 619 |
```console |
| 620 |
$ hg merge main |
| 621 |
note: possible conflict - foo was renamed multiple times to: |
| 622 |
bar |
| 623 |
baz |
| 624 |
1 files updated, 0 files merged, 0 files removed, 0 files unresolved |
| 625 |
(branch merge, don't forget to commit) |
| 626 |
``` |
| 627 |
|
| 628 |
Mercurial doesn't have a place to record this state, so it just prints that |
| 629 |
note and leaves it at that. |
| 630 |
|
| 631 |
The model and algorithm described in this document would result in a conflict |
| 632 |
in the copy ID at both paths after propagating the renames. |
| 633 |
|
| 634 |
#### Example: @jonathantanmy's test case: |
| 635 |
|
| 636 |
TODO: fill this out |
| 637 |
|
| 638 |
```console |
| 639 |
$ jj log |
| 640 |
E baz="baz" (resolves conflict) |
| 641 |
| |
| 642 |
D <conflict> |
| 643 |
|\ |
| 644 |
C | rename bar->baz |
| 645 |
| | |
| 646 |
| B rename foo->baz |
| 647 |
|/ |
| 648 |
A add foo="foo" and bar="bar" |
| 649 |
|
| 650 |
$ jj rebase -r E -o C |
| 651 |
$ jj new D E -m F |
| 652 |
``` |
| 653 |
|
| 654 |
If F is empty (auto-merged), it should have the same state as E before. |
| 655 |
|
| 656 |
### Log |
| 657 |
|
| 658 |
The copy graph contains all past paths and copy IDs of a file, so when doing |
| 659 |
`jj log <filename>`, we might want to translate that to a revset that's similar |
| 660 |
to `files()` but matches specific (path, copy ID) pairs instead of specific |
| 661 |
paths. |
| 662 |
|
| 663 |
### Annotate |
| 664 |
|
| 665 |
TBD |
| 666 |
|
| 667 |
### Representation in Git |
| 668 |
|
| 669 |
Do we ever want to record renames in the Git backend? If we do, we would |
| 670 |
presumably store it outside the Git object, similar to how we store the change |
| 671 |
id for commits. |
| 672 |
|
| 673 |
What do we use for trees where we *don't* have any copy graph recorded? If we |
| 674 |
simply create a new copy graph based on the current path, then the caller will |
| 675 |
never find any copies. Do we need an indexing pass to detect all renames in a |
| 676 |
repo when running `jj git init`? That can be very expensive for large repos. |
| 677 |
For reference, `git log --summary --find-copies-harder` takes about 165 seconds |
| 678 |
in the git.git repo on my computer, and about 13 hours in the Nixpkgs repo. |
| 679 |
|
| 680 |
An alternative is to do copy indexing in the background after cloning a repo. |
| 681 |
That would mean that copy information would not show up until some time later. |
| 682 |
It would also be more work to implement it this way. |
| 683 |
|
| 684 |
How to deal with two trees having the same content but different file ids? |
| 685 |
Actually store the additional data linked from the commit object? That would |
| 686 |
not work if we point to trees from somewhere that's not a commit. We point to a |
| 687 |
tree from the working-copy state. |
| 688 |
|
| 689 |
One could imagine not storing any copy info in Git and instead making the model |
| 690 |
described above an implementation detail of the backend. Then it could be used |
| 691 |
by the native backend and the Google backend, while we still use on-the-fly |
| 692 |
copy detection in the Git backend. However, if we want to be able to tell the |
| 693 |
user about details of conflicting copy IDs so they can decide how to resolve |
| 694 |
such conflicts, then we would have to somehow represent that abstractly too. |
| 695 |
|
| 696 |
### Representation in cloud repo (e.g. Google) |
| 697 |
|
| 698 |
Let's say you have a commit with some files you've modified. You now want to |
| 699 |
sync (rebase) that to an updated main branch. If some of the files you modified |
| 700 |
no longer exist on the main branch, we want to figure out if they were renamed |
| 701 |
so we should propagate your changes to the new file location. As described |
| 702 |
earlier, we can do that by finding files that have a different copy ID since the |
| 703 |
last time you synced with the main branch. However, if there are 10 million new |
| 704 |
commits on the main branch, there's perhaps tens of thousands of such files |
| 705 |
spread across the entire tree. That can therefore be very expensive to |
| 706 |
calculate. We therefore need to be able to get help from a custom backend |
| 707 |
implementation with this query. |
| 708 |
|
| 709 |
Since we are only interested in copy graphs that involve files modified in the |
| 710 |
rebased commit, it should be sufficient if the backend provides a method to |
| 711 |
fetch the whole copy graph for a given copy ID (or list of copy IDs). We would |
| 712 |
then first find all copy IDs involved in the diff of the rebased commit. Then |
| 713 |
we query the backend to get the full copy graphs. We then need to walk the copy |
| 714 |
graphs to see if a node exists in the destination tree. |
| 715 |
|
| 716 |
A weakness of this solution is that the search gets expensive if there are very |
| 717 |
many related files. That's probably not much of a problem in practice. The |
| 718 |
server might want to populate the index only for public/immutable commits. |
| 719 |
Otherwise, a user could poison the index by creating tons of copies |
| 720 |
(intentionally or by mistake), which would make all future queries about those |
| 721 |
files expensive. |
| 722 |
|
| 723 |
## Implementation plan |
| 724 |
|
| 725 |
A rough implementation plan may look like this: |
| 726 |
|
| 727 |
1. Implement support for copy-tracking in the test backend |
| 728 |
3. Implement diff algorithm and test it |
| 729 |
4. Implement merge algorithm and test it |
| 730 |
5. Implement blame algorithm and test it |
| 731 |
6. Implement file-following log algorithm and test it |
| 732 |
7. Extract some queries to the commit backend trait so cloud-based backends |
| 733 |
(like the Google backend) can provide versions implemented using database |
| 734 |
indexes |
| 735 |
8. Implement support for copy-tracking in the Git backend. This may involve |
| 736 |
backfilling, possibly lazily. Or it may involve new abstractions in the |
| 737 |
commit backend trait. |
| 738 |
9. Implement CLI for recording copies and for resolving conflicts in copies |
| 739 |
|
| 740 |
## Alternatives considered |
| 741 |
|
| 742 |
### Detect copies (like Git) |
| 743 |
|
| 744 |
Git doesn't record copy info. Instead, it infers it when comparing two trees. |
| 745 |
|
| 746 |
It's hard to make this model scale to very large repos. For example, let's say |
| 747 |
you're rebasing your local commit to a new upstream commit that's 1 million |
| 748 |
commits ahead. We would then want to find if any of the files in your local |
| 749 |
commit has been copied upstream. That's very expensive to do by comparing the |
| 750 |
old and the new base trees. However, since the query APIs defined above take |
| 751 |
commits (not trees) as input, we allow the backend to take the history into |
| 752 |
account when calculating the copies. A backend can then create an index based |
| 753 |
on the input files (in your local commit) and find if it's been copied without |
| 754 |
comparing the full trees. |
| 755 |
|
| 756 |
### Record logical file identifiers in trees (BitKeeper-like model) |
| 757 |
|
| 758 |
BitKeeper records a file ID (which identifies a logical file, unlike our `FileId` |
| 759 |
type) for each path (or maybe it's a path for each file ID). That way you can |
| 760 |
compare two arbitrary trees, find the added and deleted files and just compare |
| 761 |
the file IDs to figure out which of them are renames. |
| 762 |
|
| 763 |
This model doesn't seem to be easily extensible to support copies (it only |
| 764 |
supports renames). |
| 765 |
|
| 766 |
To perform a rebase across millions of commits, we would not want to diff the |
| 767 |
full trees because that would be too expensive (probably millions of modified |
| 768 |
files). We could perhaps instead find renames by bisecting to find commits that |
| 769 |
deleted any of the files modified in the commit we're rebasing. |
| 770 |
|
| 771 |
Another problem is how to synthesize the file IDs in the Git backend. That could |
| 772 |
perhaps be done by walking from the root commits and persisting an index. |
| 773 |
|
| 774 |
### Include copy info in the FileId (Mercurial-like model) |
| 775 |
|
| 776 |
Mercurial stores copy info in a metadata section in the file content itself |
| 777 |
[^mercurial_changeset_copies]. That means that a file will get a new file |
| 778 |
(content) ID if its copy history changes. That's quite similar to the proposal |
| 779 |
in this document. One difference is that Mercurial's model stores information |
| 780 |
only about the most recent copy. If the file is then modified, it will get a new |
| 781 |
file ID. One therefore has to walk the history of the file to find the previous |
| 782 |
name (which is usually not much of a problem because Mercurial stores a revision |
| 783 |
DAG per file in addition to the revision DAG at the commit level). |
| 784 |
|
| 785 |
### Hybrid snapshot/patch model with copy info stored in commits |
| 786 |
|
| 787 |
We considered storing copy info about the copies/renames in the commit object. |
| 788 |
That has some significant impact on the data model: |
| 789 |
|
| 790 |
* Without copy info, if there's a linear chain of commits A..D, you can find |
| 791 |
the total diff by diffing just D-A. That works because (B-A)+(C-B)+(D-C) |
| 792 |
simplifies to just D-A. However, if there is copy info, the total diff will |
| 793 |
involve copy info. If that's associated with the individual commits, we will |
| 794 |
need to aggregate it somehow. |
| 795 |
* Restoring from another tree is no longer just a matter of copying that tree; |
| 796 |
we also need to figure out copies between the old tree and the new tree. |
| 797 |
* Conflict states are represented by a series of states to add and remove. This |
| 798 |
does not work with the patch-based copy info. We spent a lot of time trying |
| 799 |
to figure out a solution that works, but it seems like the snapshot-based |
| 800 |
conflict model and the patch-based copy info model are not reconcilable. |
| 801 |
Therefore, we won't track conflicted copy info, such as between a `foo`->`baz` |
| 802 |
rename and a `bar`->`baz` rename. |
| 803 |
* Since copy records are relative to the auto-merged parents, that unfortunately |
| 804 |
means that the records will depend on the merge algorithm, so it's possible |
| 805 |
that a future change to the merge algorithm will make some copy records |
| 806 |
invalid. We will therefore need to not assume that the copy source exists. |
| 807 |
|
| 808 |
For the state in conflicted commits, we considered using a representation like |
| 809 |
this: |
| 810 |
|
| 811 |
```rust |
| 812 |
struct MergedTree { |
| 813 |
snapshot: Tree, |
| 814 |
diffs: Diff |
| 815 |
} |
| 816 |
|
| 817 |
struct Diff { |
| 818 |
before: Tree, |
| 819 |
after: Tree, |
| 820 |
/// Copies from `before` to `after` |
| 821 |
copies: Vec<CopyInfo>, |
| 822 |
/// Copies from `before` to `snapshot` |
| 823 |
copies_to_snapshot: Vec<CopyInfo>, |
| 824 |
} |
| 825 |
|
| 826 |
struct CopyInfo { |
| 827 |
source: RepoPathBuf, |
| 828 |
target: RepoPathBuf, |
| 829 |
// Maybe more fields here for e.g. "do not propagate" |
| 830 |
} |
| 831 |
``` |
| 832 |
|
| 833 |
That works for calculating the resulting tree, but it does not seem to allow for |
| 834 |
doing the conflict algebra we currently do. That means that things like |
| 835 |
parallelizing commits and then serializing them again would lose copy |
| 836 |
information. |
| 837 |
|
| 838 |
[same_change_rule]: https://github.com/martinvonz/jj/blob/560d66ecee5a9904b42dbc0b89333f0c27c683de/lib/src/merge.rs#L98-L111 |
| 839 |
|
| 840 |
[^martinvonz_slow]: This took me (@martinvonz) months to really understand. |
| 841 |
[^mercurial_changeset_copies]: From around |
| 842 |
<https://repo.mercurial-scm.org/hg/rev/49ad315b39ee>, Mercurial also |
| 843 |
supports storing copy info in commits. That made it the kind of |
| 844 |
snapshot/patch model we described above as not working well. |