utopia-ia/elffuss-braid public
Changesets · Branches · Tags · Compare
| Changeset | 2a0e4dd55cbbced02d4142b3f39715bcf20f9f99c8b503ade4c88fbdc3aa2f4b |
|---|---|
| Author | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-16 12:04 +02:00 |
| Committer | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-16 12:04 +02:00 |
| Parents | 0f2ebd52224c |
1 file changed
modified crates/repo/tests/history_vs_git.rs +42 −16
| @@ -29,8 +29,11 @@ | ||
| 29 | 29 | //! chain: replaying git's own diffs (`git diff -U0`) must give git's blame |
| 30 | 30 | //! line for line; braid's blame at each version must be braid's own diff |
| 31 | 31 | //! applied to its blame at the version before, a diff that keeps only |
| 32 | -//! equal lines; and that diff must never insert fewer lines than the | |
| 33 | -//! shortest one (`git diff --minimal -U0`), which would be a bug. Files | |
| 32 | +//! equal lines; and that diff must never insert fewer lines than a | |
| 33 | +//! shortest diff does, which would be a bug. That bound is computed here | |
| 34 | +//! exactly, from a longest common subsequence: `git diff --minimal` is not | |
| 35 | +//! one, since git treats lines that repeat very often specially even then. | |
| 36 | +//! Files | |
| 34 | 37 | //! where the two diffs insert as many lines at every version count as |
| 35 | 38 | //! realigned, the others as heuristic divergences. |
| 36 | 39 | |
| @@ -538,15 +541,9 @@ | ||
| 538 | 541 | } |
| 539 | 542 | } |
| 540 | 543 | |
| 541 | -/// The hunks of `git diff -U0` between two blobs, or of `git diff --minimal | |
| 542 | -/// -U0`: for each, the lines it replaces and the lines it puts there, | |
| 543 | -/// counted from 0. | |
| 544 | -fn git_diff_hunks( | |
| 545 | - git: &Path, | |
| 546 | - old: &GitOid, | |
| 547 | - new: &GitOid, | |
| 548 | - minimal: bool, | |
| 549 | -) -> Vec<(Range<usize>, Range<usize>)> { | |
| 544 | +/// The hunks of `git diff -U0` between two blobs: for each, the lines it | |
| 545 | +/// replaces and the lines it puts there, counted from 0. | |
| 546 | +fn git_diff_hunks(git: &Path, old: &GitOid, new: &GitOid) -> Vec<(Range<usize>, Range<usize>)> { | |
| 550 | 547 | if old == new { |
| 551 | 548 | return Vec::new(); |
| 552 | 549 | } |
| @@ -559,9 +556,6 @@ | ||
| 559 | 556 | "--no-ext-diff", |
| 560 | 557 | "--indent-heuristic", |
| 561 | 558 | ]); |
| 562 | - if minimal { | |
| 563 | - command.arg("--minimal"); | |
| 564 | - } | |
| 565 | 559 | command.args([old.to_hex(), new.to_hex()]); |
| 566 | 560 | let range = |spec: &str| -> Range<usize> { |
| 567 | 561 | let (start, len) = match spec.split_once(',') { |
| @@ -692,6 +686,38 @@ | ||
| 692 | 686 | hunks.iter().map(|(_, after)| after.len()).sum() |
| 693 | 687 | } |
| 694 | 688 | |
| 689 | +/// Lines a shortest diff from `old` to `new` inserts: the new lines outside a | |
| 690 | +/// longest common subsequence. For a given pair, insertions minus deletions | |
| 691 | +/// is fixed, so a diff with the fewest edits also has the fewest insertions. | |
| 692 | +/// Exact, unlike any heuristic diff: the common prefix and suffix are cut, | |
| 693 | +/// then the middle is solved by dynamic programming. | |
| 694 | +fn fewest_inserts(old: &[u8], new: &[u8]) -> usize { | |
| 695 | + let (old, new) = (split_lines(old), split_lines(new)); | |
| 696 | + let prefix = old.iter().zip(&new).take_while(|(a, b)| a == b).count(); | |
| 697 | + let (old, new) = (&old[prefix..], &new[prefix..]); | |
| 698 | + let suffix = old | |
| 699 | + .iter() | |
| 700 | + .rev() | |
| 701 | + .zip(new.iter().rev()) | |
| 702 | + .take_while(|(a, b)| a == b) | |
| 703 | + .count(); | |
| 704 | + let (old, new) = (&old[..old.len() - suffix], &new[..new.len() - suffix]); | |
| 705 | + let mut row = vec![0u32; new.len() + 1]; | |
| 706 | + for a in old { | |
| 707 | + let mut diagonal = 0u32; | |
| 708 | + for (j, b) in new.iter().enumerate() { | |
| 709 | + let above = row[j + 1]; | |
| 710 | + row[j + 1] = if a == b { | |
| 711 | + diagonal + 1 | |
| 712 | + } else { | |
| 713 | + above.max(row[j]) | |
| 714 | + }; | |
| 715 | + diagonal = above; | |
| 716 | + } | |
| 717 | + } | |
| 718 | + new.len() - row[new.len()] as usize | |
| 719 | +} | |
| 720 | + | |
| 695 | 721 | /// Blame of each file at `revision`, against git, line by line. |
| 696 | 722 | /// |
| 697 | 723 | /// Braid's blame of every version of the file is checked against braid's |
| @@ -780,12 +806,12 @@ | ||
| 780 | 806 | .iter() |
| 781 | 807 | .map(|change| change.new.len()) |
| 782 | 808 | .sum::<usize>(); |
| 783 | - let fewest = inserts(&git_diff_hunks(git, &old, &new, true)); | |
| 809 | + let fewest = fewest_inserts(&checked[i - 1].0, &checked[i].0); | |
| 784 | 810 | assert!( |
| 785 | 811 | inserted >= fewest, |
| 786 | 812 | "{what}: braid's diff inserts {inserted} lines, the shortest diff {fewest}" |
| 787 | 813 | ); |
| 788 | - let git_hunks = git_diff_hunks(git, &old, &new, false); | |
| 814 | + let git_hunks = git_diff_hunks(git, &old, &new); | |
| 789 | 815 | same_lengths &= inserted == inserts(&git_hunks); |
| 790 | 816 | replayed = replay(&replayed, lines, &git_hunks, *changeset); |
| 791 | 817 | } |