Elffuss BraidHub

utopia-ia/elffuss-braid public

Blame test: the shortest-diff bound comes from an exact LCS

The test held braid's line diff to never insert fewer lines than
`git diff --minimal`. That is not a true lower bound: git treats lines
that repeat very often specially even with --minimal. On a version of
crates/wc/src/status.rs braid's diff inserts 50 lines where git's
"minimal" one inserts 51.

The bound is now computed exactly in the test, from a longest common
subsequence of the two versions, after cutting their common prefix and
suffix: for a given pair, the fewest edits also mean the fewest
insertions. Braid's diff must never go below it.
Changeset2a0e4dd55cbbced02d4142b3f39715bcf20f9f99c8b503ade4c88fbdc3aa2f4b
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-16 12:04 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-16 12:04 +02:00
Parents0f2ebd52224c

Browse the files at this changeset

1 file changed

modified crates/repo/tests/history_vs_git.rs +42 −16
@@ -29,8 +29,11 @@
2929 //! chain: replaying git's own diffs (`git diff -U0`) must give git's blame
3030 //! line for line; braid's blame at each version must be braid's own diff
3131 //! 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
3437 //! where the two diffs insert as many lines at every version count as
3538 //! realigned, the others as heuristic divergences.
3639
@@ -538,15 +541,9 @@
538541 }
539542 }
540543
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>)> {
550547 if old == new {
551548 return Vec::new();
552549 }
@@ -559,9 +556,6 @@
559556 "--no-ext-diff",
560557 "--indent-heuristic",
561558 ]);
562- if minimal {
563- command.arg("--minimal");
564- }
565559 command.args([old.to_hex(), new.to_hex()]);
566560 let range = |spec: &str| -> Range<usize> {
567561 let (start, len) = match spec.split_once(',') {
@@ -692,6 +686,38 @@
692686 hunks.iter().map(|(_, after)| after.len()).sum()
693687 }
694688
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+
695721 /// Blame of each file at `revision`, against git, line by line.
696722 ///
697723 /// Braid's blame of every version of the file is checked against braid's
@@ -780,12 +806,12 @@
780806 .iter()
781807 .map(|change| change.new.len())
782808 .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);
784810 assert!(
785811 inserted >= fewest,
786812 "{what}: braid's diff inserts {inserted} lines, the shortest diff {fewest}"
787813 );
788- let git_hunks = git_diff_hunks(git, &old, &new, false);
814+ let git_hunks = git_diff_hunks(git, &old, &new);
789815 same_lengths &= inserted == inserts(&git_hunks);
790816 replayed = replay(&replayed, lines, &git_hunks, *changeset);
791817 }