diff --git a/tools/describe.zig b/tools/describe.zig index d7c2a48..6252fdd 100644 --- a/tools/describe.zig +++ b/tools/describe.zig @@ -277,6 +277,39 @@ pub fn isUnsupported(arg: []const u8) bool { return false; } +/// The format `text` names, `pretty.` resolved, or git's message. +fn parseFormat(gpa: Allocator, config: *const git.Config, text: []const u8, stderr: *Io.Writer) !pretty.Format { + const Lookup = struct { + fn get(context: *const anyopaque, name: []const u8) ?[]const u8 { + const c: *const git.Config = @ptrCast(@alignCast(context)); + var buffer: [256]u8 = undefined; + const key = std.fmt.bufPrint(&buffer, "pretty.{s}", .{name}) catch return null; + return c.getString(key) catch null; + } + }; + const resolved = pretty.resolve(text, .{ .context = config, .get = Lookup.get }); + var diag: Diagnostics = .{}; + return pretty.parse(gpa, resolved, &diag) catch |err| switch (err) { + error.UnknownFormat => return tool.fatalMessage(stderr, "invalid --pretty format: {s}", .{text}), + error.UnsupportedPlaceholder => return tool.fatalMessage(stderr, "zig-git: the placeholder '{s}' is not supported", .{diag.detail()}), + else => |e| return e, + }; +} + +/// Stops as git stops at a format or a date mode it cannot read, which +/// git does while reading the options, before anything else is looked +/// at (`diff-tree`'s revisions, say). +pub fn checkOptions(gpa: Allocator, config: *const git.Config, options: *const Options, stderr: *Io.Writer) !void { + if (options.pretty) |text| { + var f = try parseFormat(gpa, config, text, stderr); + f.deinit(); + } + if (options.date) |t| { + var diag: Diagnostics = .{}; + _ = git.date.parseMode(t, .{ .terminal = false }, &diag) catch return tool.fatalMessage(stderr, "{s}", .{diag.detail()}); + } +} + /// Commits described: the format, its settings and everything they read. pub const Describer = struct { ctx: Context, @@ -348,21 +381,7 @@ pub const Describer = struct { if (header) { format = .{ .builtin = .raw }; } else if (options.pretty orelse default_text) |text| { - const Lookup = struct { - fn get(context: *const anyopaque, name: []const u8) ?[]const u8 { - const c: *const git.Config = @ptrCast(@alignCast(context)); - var buffer: [256]u8 = undefined; - const key = std.fmt.bufPrint(&buffer, "pretty.{s}", .{name}) catch return null; - return c.getString(key) catch null; - } - }; - const resolved = pretty.resolve(text, .{ .context = config, .get = Lookup.get }); - var diag: Diagnostics = .{}; - format = pretty.parse(gpa, resolved, &diag) catch |err| switch (err) { - error.UnknownFormat => return tool.fatalMessage(stderr, "invalid --pretty format: {s}", .{text}), - error.UnsupportedPlaceholder => return tool.fatalMessage(stderr, "zig-git: the placeholder '{s}' is not supported", .{diag.detail()}), - else => |e| return e, - }; + format = try parseFormat(gpa, config, text, stderr); } errdefer if (format) |*f| f.deinit(); diff --git a/tools/diff_tree.zig b/tools/diff_tree.zig index 02ab9ab..59abe89 100644 --- a/tools/diff_tree.zig +++ b/tools/diff_tree.zig @@ -196,6 +196,22 @@ pub fn run(ctx: Context, args: []const []const u8, stdin: *Io.Reader, stdout: *I } } } + try describe.checkOptions(gpa, &repo.config, &described, stderr); + const settings = try object_names.settings(&repo.config, stderr); + var names: object_names.Names = .init(ctx, &repo, settings); + defer names.deinit(); + var revs = try revisions.Revisions.init(ctx, &repo, settings, .rev_parse, stderr); + defer revs.deinit(); + revs.resolver.options.sets = false; + // The revisions and paths are read before the options are checked + // against each other, as git was seen to read them; after + // --find-object, which git resolves while reading the options. + var trees: std.ArrayList(ObjectId) = .empty; + defer trees.deinit(gpa); + var paths: std.ArrayList([]const u8) = .empty; + defer paths.deinit(gpa); + var range_given = false; + if (s.find_objects.items.len == 0) try resolveWords(ctx, &repo, &revs, words.items, &trees, &paths, &range_given, stderr); try repo_diff.check(&s, stderr); if (o.all_paths and o.merges != .combined and o.merges != .dense) return tool.fatalMessage(stderr, "--combined-all-paths makes no sense without -c or --cc", .{}); @@ -213,42 +229,11 @@ pub fn run(ctx: Context, args: []const []const u8, stdin: *Io.Reader, stdout: *I // directories' shares, by a rule not found. if (fmt.dirstat and o.show_trees) return repo_diff.unsupported(stderr, .diff_tree, "--dirstat with -t"); - const settings = try object_names.settings(&repo.config, stderr); - var names: object_names.Names = .init(ctx, &repo, settings); - defer names.deinit(); - var revs = try revisions.Revisions.init(ctx, &repo, settings, .rev_parse, stderr); - defer revs.deinit(); - revs.resolver.options.sets = false; try repo_diff.setUp(ctx, &repo, &s, &revs, stderr); + if (s.find_objects.items.len != 0) try resolveWords(ctx, &repo, &revs, words.items, &trees, &paths, &range_given, stderr); // The pickaxe looks into subtrees. if (s.pickaxe_kind != null) o.recursive = true; - // The revisions, then paths: a word that names no revision begins the - // paths, each of which must then be in the working tree. - var trees: std.ArrayList(ObjectId) = .empty; - defer trees.deinit(gpa); - var paths: std.ArrayList([]const u8) = .empty; - defer paths.deinit(gpa); - var in_paths = false; - var range_given = false; - for (words.items) |w| { - if (!in_paths) { - if (try resolveWord(&revs, &repo, w, &trees, &range_given)) continue; - in_paths = true; - if (!try pathspecs.takenForPath(ctx, &repo, w)) { - try stderr.print( - \\fatal: ambiguous argument '{s}': unknown revision or path not in the working tree. - \\Use '--' to separate paths from revisions, like this: - \\'git [...] -- [...]' - \\ - , .{w}); - return error.Fatal; - } - } else if (!try pathspecs.takenForPath(ctx, &repo, w)) { - return tool.fatalMessage(stderr, "{s}: no such path in the working tree.\nUse 'git -- ...' to specify paths that do not exist locally.", .{w}); - } - try paths.append(gpa, w); - } try paths.appendSlice(gpa, after_dashdash.items); if (o.stdin) { if (trees.items.len != 0) return repo_diff.unsupported(stderr, .diff_tree, "--stdin with revisions"); @@ -371,6 +356,30 @@ pub fn run(ctx: Context, args: []const []const u8, stdin: *Io.Reader, stdout: *I if (s.exit_code and t.has_changes and !repo_diff.exitCodeSeesNothing(&s, false)) return error.No; } +/// The revisions, then paths: a word that names no revision begins the +/// paths, each of which must then be in the working tree. +fn resolveWords(ctx: Context, repo: *git.Repository, revs: *revisions.Revisions, words: []const []const u8, trees: *std.ArrayList(ObjectId), paths: *std.ArrayList([]const u8), range_given: *bool, stderr: *Io.Writer) !void { + var in_paths = false; + for (words) |w| { + if (!in_paths) { + if (try resolveWord(revs, repo, w, trees, range_given)) continue; + in_paths = true; + if (!try pathspecs.takenForPath(ctx, repo, w)) { + try stderr.print( + \\fatal: ambiguous argument '{s}': unknown revision or path not in the working tree. + \\Use '--' to separate paths from revisions, like this: + \\'git [...] -- [...]' + \\ + , .{w}); + return error.Fatal; + } + } else if (!try pathspecs.takenForPath(ctx, repo, w)) { + return tool.fatalMessage(stderr, "{s}: no such path in the working tree.\nUse 'git -- ...' to specify paths that do not exist locally.", .{w}); + } + try paths.append(ctx.gpa, w); + } +} + /// Resolves `w` as a revision or a range, appending its objects; false /// when it names none. A symmetric difference `A...B` stands for three /// objects, as git was seen to count it: its ends and their merge base. @@ -565,7 +574,8 @@ pub const Comparer = struct { if (t.describer) |d| if (all_pairs.len != 0 and s.out.format.any()) switch (d.framing()) { .full => { const f = s.out.format; - try t.w.writeAll(if (f.patch and f.stat and !f.name_only and !f.name_status) "---\n" else "\n"); + // Under --check, an empty line, as git was seen to write it. + try t.w.writeAll(if (f.patch and f.stat and !f.name_only and !f.name_status and !f.check) "---\n" else "\n"); }, .oneline, .empty => {}, }; diff --git a/tools/diff_tree_tests.zig b/tools/diff_tree_tests.zig index 70b23eb..8ca8757 100644 --- a/tools/diff_tree_tests.zig +++ b/tools/diff_tree_tests.zig @@ -501,3 +501,49 @@ test "diff-tree: --merge-base, and what git refuses with combined diffs" { try same(&repo, &.{ "diff-tree", "--format", "HEAD" }); try same(&repo, &.{ "diff-tree", "--decorate", "-v", "HEAD" }); } + +test "diff-tree: the empty line before a combined patch after an empty --summary, --check's separator, and the order of errors" { + const gpa = testing.allocator; + var repo = try Repo.init(gpa, testing.io); + defer repo.deinit(); + try makeHistory(&repo); + // A merge changing only what both sides changed: its --summary + // against the first parent is empty, and --cc leaves its one change + // out. + try repo.git(&.{ "checkout", "-q", "-f", "-b", "a", "main" }); + try write(&repo, "mod", "one\na\nthree\nfour\nfive\nsix\nseven\neight\nnine\n10\neleven\n"); + try repo.git(&.{ "add", "-A" }); + try repo.commit("a"); + try repo.git(&.{ "checkout", "-q", "-f", "-b", "b", "main" }); + try write(&repo, "mod", "one\nb\nthree\nfour\nfive\nsix\nseven\neight\nnine\n10\neleven\n"); + try repo.git(&.{ "add", "-A" }); + try repo.commit("b"); + (try repo.runIn(".", &.{ "merge", "-q", "--no-commit", "a" }, "")).deinit(gpa); + try write(&repo, "mod", "one\na\nthree\nfour\nfive\nsix\nseven\neight\nnine\n10\neleven\n"); + try repo.git(&.{ "add", "-A" }); + try repo.commit("take a"); + const merge = try revParse(&repo, "HEAD"); + defer gpa.free(merge); + const input = try std.fmt.allocPrint(gpa, "{s}\n{s}\n", .{ merge, merge }); + defer gpa.free(input); + for ([_][]const []const u8{ &.{}, &.{"--oneline"}, &.{"-v"} }) |described| { + for ([_][]const []const u8{ &.{ "--cc", "-p", "--summary" }, &.{ "-c", "-p", "--summary" }, &.{ "--cc", "--summary" }, &.{ "--cc", "-p", "--dirstat" } }) |combined| { + var args: std.ArrayList([]const u8) = .empty; + defer args.deinit(gpa); + try args.appendSlice(gpa, &.{ "diff-tree", "--stdin" }); + try args.appendSlice(gpa, described); + try args.appendSlice(gpa, combined); + try tool.expectSameAsGitHow(&repo, args.items, .{ .input = input }); + } + } + // --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" }); + // The revisions are read before the options are checked against each + // other, and after the format and the date. + try same(&repo, &.{ "diff-tree", "--format=%h", "%s", "-s", "--check", "HEAD" }); + try same(&repo, &.{ "diff-tree", "-s", "--check", "bogus", "HEAD" }); + try same(&repo, &.{ "diff-tree", "--combined-all-paths", "bogus" }); + try same(&repo, &.{ "diff-tree", "--pretty=bogus", "bogus" }); + try same(&repo, &.{ "diff-tree", "-v", "--date=bogus", "bogus" }); +} diff --git a/tools/log_diff.zig b/tools/log_diff.zig index 940fe90..f119689 100644 --- a/tools/log_diff.zig +++ b/tools/log_diff.zig @@ -603,9 +603,6 @@ pub const Writer = struct { return; } var written = false; - // An empty --summary calls for the empty line only before a file - // the patch shows, as git was seen to write it. - var summary_pending = false; if (f.numstat or f.stat or f.shortstat or f.summary or f.dirstat) { const saved = w.settings.out.format; defer w.settings.out.format = saved; @@ -621,11 +618,11 @@ pub const Writer = struct { error.ContentsUnreadable => return tool.fatalMessage(w.stderr, "zig-git {s}: cannot read a blob", .{w.command.name()}), else => |e| return repo_diff.refuseComparison(w.stderr, w.command, e), }; - // The directories' shares call for the empty line before the - // patch whatever they measure, and even when none is shown, - // as git was seen to write it before a combined patch. - written = outcome.written or (f.dirstat and shown.len != 0); - summary_pending = !written and f.summary and shown.len != 0; + // The directories' shares and the summary call for the empty + // line before the patch even when they show nothing, and even + // when --cc then leaves every file out, as git was seen to + // write it before a combined patch. + written = outcome.written or ((f.dirstat or f.summary) and shown.len != 0); } if (f.raw) for (paths.items) |row| { // A colon for each parent. @@ -647,7 +644,7 @@ pub const Writer = struct { }; if (f.patch and paths.items.len != 0) { if (written) try stdout.writeByte(end); - for (paths.items) |row| try w.writeCombinedPatch(stdout, row, &summary_pending); + for (paths.items) |row| try w.writeCombinedPatch(stdout, row); } } @@ -693,9 +690,7 @@ pub const Writer = struct { return gpa.dupe(u8, o.data); } - /// Writes one file's combined patch, if it shows anything, after an - /// empty line if `pending` says one is owed. - fn writeCombinedPatch(w: *Writer, out: *Io.Writer, row: []const output.Pair, pending: *bool) !void { + fn writeCombinedPatch(w: *Writer, out: *Io.Writer, row: []const output.Pair) !void { const gpa = w.ctx.gpa; const result = row[0].new; var texts: std.ArrayList([]u8) = .empty; @@ -749,8 +744,6 @@ pub const Writer = struct { }; if (hunk_text.written().len == 0 and !absent_everywhere and result.present() and !modes_differ) return; } - if (pending.*) try out.writeByte(if (w.settings.out.nul) 0 else '\n'); - pending.* = false; try out.writeAll(if (dense) "diff --cc " else "diff --combined "); try quotePath(out, row[0].path, &w.settings); try out.writeAll("\nindex "); @@ -902,7 +895,7 @@ pub const Writer = struct { const text = w.buffer.written(); const f = w.settings.out.format; if (framing == .full) { - if (f.patch and f.stat and !f.name_only and !f.name_status) try stdout.writeAll("---\n") else try stdout.writeByte('\n'); + if (f.patch and f.stat and !f.name_only and !f.name_status and !f.check) try stdout.writeAll("---\n") else try stdout.writeByte('\n'); } try stdout.writeAll(text); }