From 6b9258536aeec670f579d48758a4cece7054a5de Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Fri, 6 Nov 2026 08:27:52 -0600 Subject: [PATCH 1/2] diff-tree: under -m, the last parent that searched decides the rename warning git diff-tree -m compares a merge with each parent in turn, and a search for renames that comes within the limit forgets what an earlier parent's search needed: the warning is written only if the last parent that searched for renames went past the limit (a parent with nothing to search leaves it as it was). Across commits the most any needed is still kept. Seen on an octopus whose second parent, but not its third, needed more than -l1. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P9FTtTrkEbuvbZnRyJCBir --- tools/diff_tree.zig | 2 ++ tools/diff_tree_tests.zig | 61 +++++++++++++++++++++++++++++++++++++++ tools/repo_diff.zig | 23 +++++++++++++-- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/tools/diff_tree.zig b/tools/diff_tree.zig index 59abe89..21063e4 100644 --- a/tools/diff_tree.zig +++ b/tools/diff_tree.zig @@ -676,6 +676,8 @@ pub const Comparer = struct { .combined, .dense => return t.combined(id, parents, header), }; var differs = false; + if (parents.len > 1) t.warnings.beginParents(); + defer t.warnings.endParents(); for (parents) |p| { var h = header; if (parents.len > 1) h.from = p; diff --git a/tools/diff_tree_tests.zig b/tools/diff_tree_tests.zig index 8ca8757..0ff62c4 100644 --- a/tools/diff_tree_tests.zig +++ b/tools/diff_tree_tests.zig @@ -547,3 +547,64 @@ test "diff-tree: the empty line before a combined patch after an empty --summary try same(&repo, &.{ "diff-tree", "--pretty=bogus", "bogus" }); try same(&repo, &.{ "diff-tree", "-v", "--date=bogus", "bogus" }); } + +test "diff-tree: under -m the rename limit is warned of by the last parent that searched" { + const gpa = testing.allocator; + var repo = try Repo.init(gpa, testing.io); + defer repo.deinit(); + try write(&repo, "base", "base\n"); + try write(&repo, "gone", "a file that goes, long enough to be compared\n"); + try repo.git(&.{ "add", "." }); + try repo.commit("root"); + // Against the root and `wide`, one deletion and more than one + // addition: more than -l1 allows. Against `narrow`, one of each: a + // search within it. + try repo.git(&.{ "checkout", "-q", "-f", "-b", "wide", "main" }); + try write(&repo, "x", "x\n"); + try repo.git(&.{ "add", "." }); + try repo.commit("wide"); + try repo.git(&.{ "checkout", "-q", "-f", "-b", "narrow", "main" }); + try remove(&repo, "gone"); + try write(&repo, "one", "one\n"); + try write(&repo, "two", "two\n"); + try write(&repo, "y", "x\nand more\n"); + try repo.git(&.{ "add", "-A" }); + try repo.commit("narrow"); + try repo.git(&.{ "checkout", "-q", "-f", "main" }); + try remove(&repo, "gone"); + try write(&repo, "one", "one\n"); + try write(&repo, "two", "two\n"); + try write(&repo, "x", "x\n"); + try repo.git(&.{ "add", "-A" }); + try repo.commit("result"); + const result = try revParse(&repo, "HEAD"); + defer gpa.free(result); + const root = try revParse(&repo, "HEAD~"); + defer gpa.free(root); + const wide = try revParse(&repo, "wide"); + defer gpa.free(wide); + const narrow = try revParse(&repo, "narrow"); + defer gpa.free(narrow); + const orders = [_][]const []const u8{ + &.{ root, narrow }, &.{ narrow, root }, &.{ root, wide }, + &.{ wide, root }, &.{ root, wide, narrow }, &.{ root, narrow, wide }, + &.{ narrow, wide, narrow }, + }; + for (orders) |parents| { + var line: std.ArrayList(u8) = .empty; + defer line.deinit(gpa); + try line.appendSlice(gpa, result); + for (parents) |p| { + try line.append(gpa, ' '); + try line.appendSlice(gpa, p); + } + try line.append(gpa, '\n'); + for ([_][]const []const u8{ &.{ "-m", "-l1", "-M" }, &.{ "-m", "-l1", "-M", "-s", "--format=%s%n%b%e", "--first-parent" } }) |opts| { + var args: std.ArrayList([]const u8) = .empty; + defer args.deinit(gpa); + try args.appendSlice(gpa, &.{ "diff-tree", "--stdin" }); + try args.appendSlice(gpa, opts); + try tool.expectSameAsGitHow(&repo, args.items, .{ .input = line.items }); + } + } +} diff --git a/tools/repo_diff.zig b/tools/repo_diff.zig index e43a54e..5c1a284 100644 --- a/tools/repo_diff.zig +++ b/tools/repo_diff.zig @@ -1330,15 +1330,34 @@ pub const RenameWarnings = struct { walk_within: bool = false, /// Set while a combined diff is worked out, and under `--follow`. quiet: bool = false, + /// Set while `diff-tree -m` compares a merge with each parent + /// (`beginParents`): then a search that comes within the limit + /// forgets what an earlier parent's needed, as git was seen to + /// forget it, where across commits the most any needed is kept. + parents: ?usize = null, fn note(w: *RenameWarnings, l: git.diff.renames.Limited) void { if (w.quiet) return; if (l.copies_from_modified_only) w.modified_only = true; - w.walk_needed = @max(w.walk_needed, l.needed); + if (w.parents) |*p| p.* = l.needed else w.walk_needed = @max(w.walk_needed, l.needed); } fn noteWithin(w: *RenameWarnings) void { - if (!w.quiet) w.walk_within = true; + if (w.quiet) return; + w.walk_within = true; + if (w.parents) |*p| p.* = 0; + } + + /// Begins comparing a merge with each of its parents in turn. + pub fn beginParents(w: *RenameWarnings) void { + w.parents = 0; + } + + /// Ends `beginParents`: what the last parent searched needed counts. + pub fn endParents(w: *RenameWarnings) void { + const p = w.parents orelse return; + w.parents = null; + w.walk_needed = @max(w.walk_needed, p); } /// Ends a walk and writes the warnings it leaves. -- knotmirror 0.0.1 From b702cef29a7bdda53ad16de1bc7381c24d756970 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Fri, 6 Nov 2026 11:07:31 -0600 Subject: [PATCH 2/2] diff-tree: --check goes into subtrees without -r As the patch does, --check reads the files' contents, and git diff-tree was seen to look into subdirectories for it whether or not -r is given. Found on Ghostty under --stdin. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P9FTtTrkEbuvbZnRyJCBir --- tools/diff_tree.zig | 2 +- tools/diff_tree_tests.zig | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tools/diff_tree.zig b/tools/diff_tree.zig index 21063e4..e25cbc9 100644 --- a/tools/diff_tree.zig +++ b/tools/diff_tree.zig @@ -223,7 +223,7 @@ pub fn run(ctx: Context, args: []const []const u8, stdin: *Io.Reader, stdout: *I // The formats that read contents go into subtrees, unless the name // formats replace them. const fmt = s.out.format; - if (!fmt.name_only and !fmt.name_status and (fmt.patch or fmt.stat or fmt.numstat or fmt.shortstat or fmt.summary or fmt.dirstat)) o.recursive = true; + if (!fmt.name_only and !fmt.name_status and (fmt.patch or fmt.stat or fmt.numstat or fmt.shortstat or fmt.summary or fmt.dirstat or fmt.check)) o.recursive = true; if (s.quiet) s.exit_code = true; // With -t, git was seen to count the trees' own entries in the // directories' shares, by a rule not found. diff --git a/tools/diff_tree_tests.zig b/tools/diff_tree_tests.zig index 0ff62c4..6a80b8d 100644 --- a/tools/diff_tree_tests.zig +++ b/tools/diff_tree_tests.zig @@ -536,6 +536,13 @@ test "diff-tree: the empty line before a combined patch after an empty --summary try tool.expectSameAsGitHow(&repo, args.items, .{ .input = input }); } } + // --check goes into subtrees without -r, as the patch does. + try write(&repo, "dir/sub/f", "trailing \n"); + try repo.git(&.{ "add", "-A" }); + try repo.commit("whitespace in a subdirectory"); + try same(&repo, &.{ "diff-tree", "--check", "HEAD" }); + try same(&repo, &.{ "diff-tree", "-v", "--check", "HEAD" }); + try repo.git(&.{ "reset", "-q", "--hard", "HEAD~" }); // --check makes the separator an empty line, not ---. try same(&repo, &.{ "diff-tree", "-v", "--patch-with-stat", "--check", "HEAD^2" }); try same(&repo, &.{ "diff-tree", "--pretty=fuller", "-p", "--stat", "--check", "-m", "HEAD" }); -- knotmirror 0.0.1