| 1 |
package merkletrie |
| 2 |
|
| 3 |
// The focus of this difftree implementation is to save time by |
| 4 |
// skipping whole directories if their hash is the same in both |
| 5 |
// trees. |
| 6 |
// |
| 7 |
// The diff algorithm implemented here is based on the doubleiter |
| 8 |
// type defined in this same package; we will iterate over both |
| 9 |
// trees at the same time, while comparing the current noders in |
| 10 |
// each iterator. Depending on how they differ we will output the |
| 11 |
// corresponding changes and move the iterators further over both |
| 12 |
// trees. |
| 13 |
// |
| 14 |
// The table below shows all the possible comparison results, along |
| 15 |
// with what changes should we produce and how to advance the |
| 16 |
// iterators. |
| 17 |
// |
| 18 |
// The table is implemented by the switches in this function, |
| 19 |
// diffTwoNodes, diffTwoNodesSameName and diffTwoDirs. |
| 20 |
// |
| 21 |
// Many Bothans died to bring us this information, make sure you |
| 22 |
// understand the table before modifying this code. |
| 23 |
|
| 24 |
// # Cases |
| 25 |
// |
| 26 |
// When comparing noders in both trees you will find yourself in |
| 27 |
// one of 169 possible cases, but if we ignore moves, we can |
| 28 |
// simplify a lot the search space into the following table: |
| 29 |
// |
| 30 |
// - "-": nothing, no file or directory |
| 31 |
// - a<>: an empty file named "a". |
| 32 |
// - a<1>: a file named "a", with "1" as its contents. |
| 33 |
// - a<2>: a file named "a", with "2" as its contents. |
| 34 |
// - a(): an empty dir named "a". |
| 35 |
// - a(...): a dir named "a", with some files and/or dirs inside (possibly |
| 36 |
// empty). |
| 37 |
// - a(;;;): a dir named "a", with some other files and/or dirs inside |
| 38 |
// (possibly empty), which different from the ones in "a(...)". |
| 39 |
// |
| 40 |
// \ to - a<> a<1> a<2> a() a(...) a(;;;) |
| 41 |
// from \ |
| 42 |
// - 00 01 02 03 04 05 06 |
| 43 |
// a<> 10 11 12 13 14 15 16 |
| 44 |
// a<1> 20 21 22 23 24 25 26 |
| 45 |
// a<2> 30 31 32 33 34 35 36 |
| 46 |
// a() 40 41 42 43 44 45 46 |
| 47 |
// a(...) 50 51 52 53 54 55 56 |
| 48 |
// a(;;;) 60 61 62 63 64 65 66 |
| 49 |
// |
| 50 |
// Every (from, to) combination in the table is a special case, but |
| 51 |
// some of them can be merged into some more general cases, for |
| 52 |
// instance 11 and 22 can be merged into the general case: both |
| 53 |
// noders are equal. |
| 54 |
// |
| 55 |
// Here is a full list of all the cases that are similar and how to |
| 56 |
// merge them together into more general cases. Each general case |
| 57 |
// is labeled with an uppercase letter for further reference, and it |
| 58 |
// is followed by the pseudocode of the checks you have to perform |
| 59 |
// on both noders to see if you are in such a case, the actions to |
| 60 |
// perform (i.e. what changes to output) and how to advance the |
| 61 |
// iterators of each tree to continue the comparison process. |
| 62 |
// |
| 63 |
// ## A. Impossible: 00 |
| 64 |
// |
| 65 |
// ## B. Same thing on both sides: 11, 22, 33, 44, 55, 66 |
| 66 |
// - check: `SameName() && SameHash()` |
| 67 |
// - action: do nothing. |
| 68 |
// - advance: `FromNext(); ToNext()` |
| 69 |
// |
| 70 |
// ### C. To was created: 01, 02, 03, 04, 05, 06 |
| 71 |
// - check: `DifferentName() && ToBeforeFrom()` |
| 72 |
// - action: insertRecursively(to) |
| 73 |
// - advance: `ToNext()` |
| 74 |
// |
| 75 |
// ### D. From was deleted: 10, 20, 30, 40, 50, 60 |
| 76 |
// - check: `DifferentName() && FromBeforeTo()` |
| 77 |
// - action: `DeleteRecursively(from)` |
| 78 |
// - advance: `FromNext()` |
| 79 |
// |
| 80 |
// ### E. Empty file to file with contents: 12, 13 |
| 81 |
// - check: `SameName() && DifferentHash() && FromIsFile() && |
| 82 |
// ToIsFile() && FromIsEmpty()` |
| 83 |
// - action: `modifyFile(from, to)` |
| 84 |
// - advance: `FromNext()` or `FromStep()` |
| 85 |
// |
| 86 |
// ### E'. file with contents to empty file: 21, 31 |
| 87 |
// - check: `SameName() && DifferentHash() && FromIsFile() && |
| 88 |
// ToIsFile() && ToIsEmpty()` |
| 89 |
// - action: `modifyFile(from, to)` |
| 90 |
// - advance: `FromNext()` or `FromStep()` |
| 91 |
// |
| 92 |
// ### F. empty file to empty dir with the same name: 14 |
| 93 |
// - check: `SameName() && FromIsFile() && FromIsEmpty() && |
| 94 |
// ToIsDir() && ToIsEmpty()` |
| 95 |
// - action: `DeleteFile(from); InsertEmptyDir(to)` |
| 96 |
// - advance: `FromNext(); ToNext()` |
| 97 |
// |
| 98 |
// ### F'. empty dir to empty file of the same name: 41 |
| 99 |
// - check: `SameName() && FromIsDir() && FromIsEmpty && |
| 100 |
// ToIsFile() && ToIsEmpty()` |
| 101 |
// - action: `DeleteEmptyDir(from); InsertFile(to)` |
| 102 |
// - advance: `FromNext(); ToNext()` or step for any of them. |
| 103 |
// |
| 104 |
// ### G. empty file to non-empty dir of the same name: 15, 16 |
| 105 |
// - check: `SameName() && FromIsFile() && ToIsDir() && |
| 106 |
// FromIsEmpty() && ToIsNotEmpty()` |
| 107 |
// - action: `DeleteFile(from); InsertDirRecursively(to)` |
| 108 |
// - advance: `FromNext(); ToNext()` |
| 109 |
// |
| 110 |
// ### G'. non-empty dir to empty file of the same name: 51, 61 |
| 111 |
// - check: `SameName() && FromIsDir() && FromIsNotEmpty() && |
| 112 |
// ToIsFile() && FromIsEmpty()` |
| 113 |
// - action: `DeleteDirRecursively(from); InsertFile(to)` |
| 114 |
// - advance: `FromNext(); ToNext()` |
| 115 |
// |
| 116 |
// ### H. modify file contents: 23, 32 |
| 117 |
// - check: `SameName() && FromIsFile() && ToIsFile() && |
| 118 |
// FromIsNotEmpty() && ToIsNotEmpty()` |
| 119 |
// - action: `ModifyFile(from, to)` |
| 120 |
// - advance: `FromNext(); ToNext()` |
| 121 |
// |
| 122 |
// ### I. file with contents to empty dir: 24, 34 |
| 123 |
// - check: `SameName() && DifferentHash() && FromIsFile() && |
| 124 |
// FromIsNotEmpty() && ToIsDir() && ToIsEmpty()` |
| 125 |
// - action: `DeleteFile(from); InsertEmptyDir(to)` |
| 126 |
// - advance: `FromNext(); ToNext()` |
| 127 |
// |
| 128 |
// ### I'. empty dir to file with contents: 42, 43 |
| 129 |
// - check: `SameName() && DifferentHash() && FromIsDir() && |
| 130 |
// FromIsEmpty() && ToIsFile() && ToIsEmpty()` |
| 131 |
// - action: `DeleteDir(from); InsertFile(to)` |
| 132 |
// - advance: `FromNext(); ToNext()` |
| 133 |
// |
| 134 |
// ### J. file with contents to dir with contents: 25, 26, 35, 36 |
| 135 |
// - check: `SameName() && DifferentHash() && FromIsFile() && |
| 136 |
// FromIsNotEmpty() && ToIsDir() && ToIsNotEmpty()` |
| 137 |
// - action: `DeleteFile(from); InsertDirRecursively(to)` |
| 138 |
// - advance: `FromNext(); ToNext()` |
| 139 |
// |
| 140 |
// ### J'. dir with contents to file with contents: 52, 62, 53, 63 |
| 141 |
// - check: `SameName() && DifferentHash() && FromIsDir() && |
| 142 |
// FromIsNotEmpty() && ToIsFile() && ToIsNotEmpty()` |
| 143 |
// - action: `DeleteDirRecursively(from); InsertFile(to)` |
| 144 |
// - advance: `FromNext(); ToNext()` |
| 145 |
// |
| 146 |
// ### K. empty dir to dir with contents: 45, 46 |
| 147 |
// - check: `SameName() && DifferentHash() && FromIsDir() && |
| 148 |
// FromIsEmpty() && ToIsDir() && ToIsNotEmpty()` |
| 149 |
// - action: `InsertChildrenRecursively(to)` |
| 150 |
// - advance: `FromNext(); ToNext()` |
| 151 |
// |
| 152 |
// ### K'. dir with contents to empty dir: 54, 64 |
| 153 |
// - check: `SameName() && DifferentHash() && FromIsDir() && |
| 154 |
// FromIsEmpty() && ToIsDir() && ToIsNotEmpty()` |
| 155 |
// - action: `DeleteChildrenRecursively(from)` |
| 156 |
// - advance: `FromNext(); ToNext()` |
| 157 |
// |
| 158 |
// ### L. dir with contents to dir with different contents: 56, 65 |
| 159 |
// - check: `SameName() && DifferentHash() && FromIsDir() && |
| 160 |
// FromIsNotEmpty() && ToIsDir() && ToIsNotEmpty()` |
| 161 |
// - action: nothing |
| 162 |
// - advance: `FromStep(); ToStep()` |
| 163 |
// |
| 164 |
// |
| 165 |
|
| 166 |
// All these cases can be further simplified by a truth table |
| 167 |
// reduction process, in which we gather similar checks together to |
| 168 |
// make the final code easier to read and understand. |
| 169 |
// |
| 170 |
// The first 6 columns are the outputs of the checks to perform on |
| 171 |
// both noders. I have labeled them 1 to 6, this is what they mean: |
| 172 |
// |
| 173 |
// 1: SameName() |
| 174 |
// 2: SameHash() |
| 175 |
// 3: FromIsDir() |
| 176 |
// 4: ToIsDir() |
| 177 |
// 5: FromIsEmpty() |
| 178 |
// 6: ToIsEmpty() |
| 179 |
// |
| 180 |
// The from and to columns are a fsnoder example of the elements |
| 181 |
// that you will find on each tree under the specified comparison |
| 182 |
// results (columns 1 to 6). |
| 183 |
// |
| 184 |
// The type column identifies the case we are into, from the list above. |
| 185 |
// |
| 186 |
// The type' column identifies the new set of reduced cases, using |
| 187 |
// lowercase letters, and they are explained after the table. |
| 188 |
// |
| 189 |
// The last column is the set of actions and advances for each case. |
| 190 |
// |
| 191 |
// "---" means impossible except in case of hash collision. |
| 192 |
// |
| 193 |
// advance meaning: |
| 194 |
// - NN: from.Next(); to.Next() |
| 195 |
// - SS: from.Step(); to.Step() |
| 196 |
// |
| 197 |
// 1 2 3 4 5 6 | from | to |type|type'|action ; advance |
| 198 |
// ------------+--------+--------+----+------------------------------------ |
| 199 |
// 0 0 0 0 0 0 | | | | | if !SameName() { |
| 200 |
// . | | | | | if FromBeforeTo() { |
| 201 |
// . | | | D | d | delete(from); from.Next() |
| 202 |
// . | | | | | } else { |
| 203 |
// . | | | C | c | insert(to); to.Next() |
| 204 |
// . | | | | | } |
| 205 |
// 0 1 1 1 1 1 | | | | | } |
| 206 |
// 1 0 0 0 0 0 | a<1> | a<2> | H | e | modify(from, to); NN |
| 207 |
// 1 0 0 0 0 1 | a<1> | a<> | E' | e | modify(from, to); NN |
| 208 |
// 1 0 0 0 1 0 | a<> | a<1> | E | e | modify(from, to); NN |
| 209 |
// 1 0 0 0 1 1 | ---- | ---- | | e | |
| 210 |
// 1 0 0 1 0 0 | a<1> | a(...) | J | f | delete(from); insert(to); NN |
| 211 |
// 1 0 0 1 0 1 | a<1> | a() | I | f | delete(from); insert(to); NN |
| 212 |
// 1 0 0 1 1 0 | a<> | a(...) | G | f | delete(from); insert(to); NN |
| 213 |
// 1 0 0 1 1 1 | a<> | a() | F | f | delete(from); insert(to); NN |
| 214 |
// 1 0 1 0 0 0 | a(...) | a<1> | J' | f | delete(from); insert(to); NN |
| 215 |
// 1 0 1 0 0 1 | a(...) | a<> | G' | f | delete(from); insert(to); NN |
| 216 |
// 1 0 1 0 1 0 | a() | a<1> | I' | f | delete(from); insert(to); NN |
| 217 |
// 1 0 1 0 1 1 | a() | a<> | F' | f | delete(from); insert(to); NN |
| 218 |
// 1 0 1 1 0 0 | a(...) | a(;;;) | L | g | nothing; SS |
| 219 |
// 1 0 1 1 0 1 | a(...) | a() | K' | h | deleteChildren(from); NN |
| 220 |
// 1 0 1 1 1 0 | a() | a(...) | K | i | insertChildren(to); NN |
| 221 |
// 1 0 1 1 1 1 | ---- | ---- | | | |
| 222 |
// 1 1 0 0 0 0 | a<1> | a<1> | B | b | nothing; NN |
| 223 |
// 1 1 0 0 0 1 | ---- | ---- | | b | |
| 224 |
// 1 1 0 0 1 0 | ---- | ---- | | b | |
| 225 |
// 1 1 0 0 1 1 | a<> | a<> | B | b | nothing; NN |
| 226 |
// 1 1 0 1 0 0 | ---- | ---- | | b | |
| 227 |
// 1 1 0 1 0 1 | ---- | ---- | | b | |
| 228 |
// 1 1 0 1 1 0 | ---- | ---- | | b | |
| 229 |
// 1 1 0 1 1 1 | ---- | ---- | | b | |
| 230 |
// 1 1 1 0 0 0 | ---- | ---- | | b | |
| 231 |
// 1 1 1 0 0 1 | ---- | ---- | | b | |
| 232 |
// 1 1 1 0 1 0 | ---- | ---- | | b | |
| 233 |
// 1 1 1 0 1 1 | ---- | ---- | | b | |
| 234 |
// 1 1 1 1 0 0 | a(...) | a(...) | B | b | nothing; NN |
| 235 |
// 1 1 1 1 0 1 | ---- | ---- | | b | |
| 236 |
// 1 1 1 1 1 0 | ---- | ---- | | b | |
| 237 |
// 1 1 1 1 1 1 | a() | a() | B | b | nothing; NN |
| 238 |
// |
| 239 |
// c and d: |
| 240 |
// if !SameName() |
| 241 |
// d if FromBeforeTo() |
| 242 |
// c else |
| 243 |
// b: SameName) && sameHash() |
| 244 |
// e: SameName() && !sameHash() && BothAreFiles() |
| 245 |
// f: SameName() && !sameHash() && FileAndDir() |
| 246 |
// g: SameName() && !sameHash() && BothAreDirs() && NoneIsEmpty |
| 247 |
// i: SameName() && !sameHash() && BothAreDirs() && FromIsEmpty |
| 248 |
// h: else of i |
| 249 |
|
| 250 |
import ( |
| 251 |
"context" |
| 252 |
"errors" |
| 253 |
"fmt" |
| 254 |
|
| 255 |
"github.com/go-git/go-git/v5/utils/merkletrie/noder" |
| 256 |
) |
| 257 |
|
| 258 |
var ( |
| 259 |
// ErrCanceled is returned whenever the operation is canceled. |
| 260 |
ErrCanceled = errors.New("operation canceled") |
| 261 |
) |
| 262 |
|
| 263 |
// DiffTree calculates the list of changes between two merkletries. It |
| 264 |
// uses the provided hashEqual callback to compare noders. |
| 265 |
func DiffTree( |
| 266 |
fromTree, |
| 267 |
toTree noder.Noder, |
| 268 |
hashEqual noder.Equal, |
| 269 |
) (Changes, error) { |
| 270 |
return DiffTreeContext(context.Background(), fromTree, toTree, hashEqual) |
| 271 |
} |
| 272 |
|
| 273 |
// DiffTreeContext calculates the list of changes between two merkletries. It |
| 274 |
// uses the provided hashEqual callback to compare noders. |
| 275 |
// Error will be returned if context expires |
| 276 |
// Provided context must be non nil |
| 277 |
func DiffTreeContext(ctx context.Context, fromTree, toTree noder.Noder, |
| 278 |
hashEqual noder.Equal) (Changes, error) { |
| 279 |
ret := NewChanges() |
| 280 |
|
| 281 |
ii, err := newDoubleIter(fromTree, toTree, hashEqual) |
| 282 |
if err != nil { |
| 283 |
return nil, err |
| 284 |
} |
| 285 |
|
| 286 |
for { |
| 287 |
select { |
| 288 |
case <-ctx.Done(): |
| 289 |
return nil, ErrCanceled |
| 290 |
default: |
| 291 |
} |
| 292 |
|
| 293 |
from := ii.from.current |
| 294 |
to := ii.to.current |
| 295 |
|
| 296 |
switch r := ii.remaining(); r { |
| 297 |
case noMoreNoders: |
| 298 |
return ret, nil |
| 299 |
case onlyFromRemains: |
| 300 |
if !from.Skip() { |
| 301 |
if err = ret.AddRecursiveDelete(from); err != nil { |
| 302 |
return nil, err |
| 303 |
} |
| 304 |
} |
| 305 |
if err = ii.nextFrom(); err != nil { |
| 306 |
return nil, err |
| 307 |
} |
| 308 |
case onlyToRemains: |
| 309 |
if !to.Skip() { |
| 310 |
if err = ret.AddRecursiveInsert(to); err != nil { |
| 311 |
return nil, err |
| 312 |
} |
| 313 |
} |
| 314 |
if err = ii.nextTo(); err != nil { |
| 315 |
return nil, err |
| 316 |
} |
| 317 |
case bothHaveNodes: |
| 318 |
var err error |
| 319 |
switch { |
| 320 |
case from.Skip(): |
| 321 |
if from.Name() == to.Name() { |
| 322 |
err = ii.nextBoth() |
| 323 |
} else { |
| 324 |
err = ii.nextFrom() |
| 325 |
} |
| 326 |
case to.Skip(): |
| 327 |
if from.Name() == to.Name() { |
| 328 |
err = ii.nextBoth() |
| 329 |
} else { |
| 330 |
err = ii.nextTo() |
| 331 |
} |
| 332 |
default: |
| 333 |
err = diffNodes(&ret, ii) |
| 334 |
} |
| 335 |
|
| 336 |
if err != nil { |
| 337 |
return nil, err |
| 338 |
} |
| 339 |
default: |
| 340 |
panic(fmt.Sprintf("unknown remaining value: %d", r)) |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
func diffNodes(changes *Changes, ii *doubleIter) error { |
| 346 |
from := ii.from.current |
| 347 |
to := ii.to.current |
| 348 |
var err error |
| 349 |
|
| 350 |
// compare their full paths as strings |
| 351 |
switch from.Compare(to) { |
| 352 |
case -1: |
| 353 |
if err = changes.AddRecursiveDelete(from); err != nil { |
| 354 |
return err |
| 355 |
} |
| 356 |
if err = ii.nextFrom(); err != nil { |
| 357 |
return err |
| 358 |
} |
| 359 |
case 1: |
| 360 |
if err = changes.AddRecursiveInsert(to); err != nil { |
| 361 |
return err |
| 362 |
} |
| 363 |
if err = ii.nextTo(); err != nil { |
| 364 |
return err |
| 365 |
} |
| 366 |
default: |
| 367 |
if err := diffNodesSameName(changes, ii); err != nil { |
| 368 |
return err |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
return nil |
| 373 |
} |
| 374 |
|
| 375 |
func diffNodesSameName(changes *Changes, ii *doubleIter) error { |
| 376 |
from := ii.from.current |
| 377 |
to := ii.to.current |
| 378 |
|
| 379 |
status, err := ii.compare() |
| 380 |
if err != nil { |
| 381 |
return err |
| 382 |
} |
| 383 |
|
| 384 |
switch { |
| 385 |
case status.sameHash: |
| 386 |
// do nothing |
| 387 |
if err = ii.nextBoth(); err != nil { |
| 388 |
return err |
| 389 |
} |
| 390 |
case status.bothAreFiles: |
| 391 |
changes.Add(NewModify(from, to)) |
| 392 |
if err = ii.nextBoth(); err != nil { |
| 393 |
return err |
| 394 |
} |
| 395 |
case status.fileAndDir: |
| 396 |
if err = changes.AddRecursiveDelete(from); err != nil { |
| 397 |
return err |
| 398 |
} |
| 399 |
if err = changes.AddRecursiveInsert(to); err != nil { |
| 400 |
return err |
| 401 |
} |
| 402 |
if err = ii.nextBoth(); err != nil { |
| 403 |
return err |
| 404 |
} |
| 405 |
case status.bothAreDirs: |
| 406 |
if err = diffDirs(changes, ii); err != nil { |
| 407 |
return err |
| 408 |
} |
| 409 |
default: |
| 410 |
return fmt.Errorf("bad status from double iterator") |
| 411 |
} |
| 412 |
|
| 413 |
return nil |
| 414 |
} |
| 415 |
|
| 416 |
func diffDirs(changes *Changes, ii *doubleIter) error { |
| 417 |
from := ii.from.current |
| 418 |
to := ii.to.current |
| 419 |
|
| 420 |
status, err := ii.compare() |
| 421 |
if err != nil { |
| 422 |
return err |
| 423 |
} |
| 424 |
|
| 425 |
switch { |
| 426 |
case status.fromIsEmptyDir: |
| 427 |
if err = changes.AddRecursiveInsert(to); err != nil { |
| 428 |
return err |
| 429 |
} |
| 430 |
if err = ii.nextBoth(); err != nil { |
| 431 |
return err |
| 432 |
} |
| 433 |
case status.toIsEmptyDir: |
| 434 |
if err = changes.AddRecursiveDelete(from); err != nil { |
| 435 |
return err |
| 436 |
} |
| 437 |
if err = ii.nextBoth(); err != nil { |
| 438 |
return err |
| 439 |
} |
| 440 |
case !status.fromIsEmptyDir && !status.toIsEmptyDir: |
| 441 |
// do nothing |
| 442 |
if err = ii.stepBoth(); err != nil { |
| 443 |
return err |
| 444 |
} |
| 445 |
default: |
| 446 |
return fmt.Errorf("both dirs are empty but has different hash") |
| 447 |
} |
| 448 |
|
| 449 |
return nil |
| 450 |
} |