Skip to content
zit Artifact [f44afed99e]
Login

Artifact [f44afed99e]

Artifact f44afed99ea6615a0dab6d25607972355baa38a70cf4fa0beb4804e1f007716f:


const std = @import("std");
const support = @import("support.zig");
const appendArtifact = support.appendArtifact;
const expectExit = support.expectExit;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runZit = support.runZit;
const runZitStdinEnv = support.runZitStdinEnv;
const withZCard = support.withZCard;
const writeFile = support.writeFile;

/// `wiki commit` with its D card pinned to `epoch`; returns the new hash.
fn pageAt(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, epoch: []const u8, body_file: []const u8, args: []const []const u8) ![64]u8 {
    var res = try runZitStdinEnv(gpa, io, dir, &.{.{ "SOURCE_DATE_EPOCH", epoch }}, body_file, args);
    defer freeResult(gpa, &res);
    try expectExit(res.term, 0);
    try std.testing.expectEqual(@as(usize, 65), res.stdout.len);
    return res.stdout[0..64].*;
}

fn rawOf(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, hash: []const u8) ![]u8 {
    const res = try runZit(gpa, io, dir, &.{ "artifact", "--raw", hash });
    defer gpa.free(res.stderr);
    errdefer gpa.free(res.stdout);
    try expectExit(res.term, 0);
    return res.stdout;
}

/// The bytes fossil's wiki_cmd_commit writes for these inputs (wiki.c):
/// D, L, P when there is a parent, U, W with its trailing newline, Z.
fn fossilPage(gpa: std.mem.Allocator, comment: ?[]const u8, date: []const u8, title: []const u8, parent: ?[]const u8, user: []const u8, body: []const u8) ![]u8 {
    var cards: std.ArrayList(u8) = .empty;
    defer cards.deinit(gpa);
    if (comment) |c| try cards.print(gpa, "C {s}\n", .{c});
    try cards.print(gpa, "D {s}\nL {s}\n", .{ date, title });
    if (parent) |p| try cards.print(gpa, "P {s}\n", .{p});
    try cards.print(gpa, "U {s}\nW {d}\n{s}\n", .{ user, body.len, body });
    return withZCard(gpa, cards.items);
}

test "cli wiki commit: each version after the first names its predecessor in P, a deletion too, byte for byte as fossil writes them" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "one.txt", "one\n");
    try writeFile(io, tmp.dir, "two.txt", "two\n");
    try writeFile(io, tmp.dir, "empty.txt", "");
    const args: []const []const u8 = &.{ "wiki", "commit", "Page", "--user", "pat" };

    const v1 = try pageAt(gpa, io, tmp.dir, "1000000000", "one.txt", args);
    const v2 = try pageAt(gpa, io, tmp.dir, "1000000060", "two.txt", args);
    const v3 = try pageAt(gpa, io, tmp.dir, "1000000120", "empty.txt", args);

    const Want = struct { hash: *const [64]u8, date: []const u8, parent: ?[]const u8, body: []const u8 };
    const wants = [_]Want{
        .{ .hash = &v1, .date = "2001-09-09T01:46:40.000", .parent = null, .body = "one\n" },
        .{ .hash = &v2, .date = "2001-09-09T01:47:40.000", .parent = &v1, .body = "two\n" },
        .{ .hash = &v3, .date = "2001-09-09T01:48:40.000", .parent = &v2, .body = "" },
    };
    for (wants) |w| {
        const got = try rawOf(gpa, io, tmp.dir, w.hash);
        defer gpa.free(got);
        const want = try fossilPage(gpa, null, w.date, "Page", w.parent, "pat", w.body);
        defer gpa.free(want);
        try std.testing.expectEqualStrings(want, got);
    }

    // Another title's history is its own: its first version has no parent.
    const other = try pageAt(gpa, io, tmp.dir, "1000000180", "one.txt", &.{ "wiki", "commit", "Other", "--user", "pat" });
    const got = try rawOf(gpa, io, tmp.dir, &other);
    defer gpa.free(got);
    try std.testing.expect(std.mem.indexOf(u8, got, "\nP ") == null);
}

test "cli wiki commit: a page with two heads gets one parent, the later-dated head, as fossil's does" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "body.txt", "body\n");
    const args: []const []const u8 = &.{ "wiki", "commit", "Fork", "--user", "pat" };

    const base = try pageAt(gpa, io, tmp.dir, "1000000000", "body.txt", args);
    // Two clones each edited the base and synced: two versions naming it.
    const early_bytes = try fossilPage(gpa, null, "2001-09-09T01:50:00.000", "Fork", &base, "a", "from a\n");
    defer gpa.free(early_bytes);
    const early = try appendArtifact(gpa, io, tmp.dir, early_bytes);
    defer gpa.free(early);
    const late_bytes = try fossilPage(gpa, null, "2001-09-09T01:51:00.000", "Fork", &base, "b", "from b\n");
    defer gpa.free(late_bytes);
    const late = try appendArtifact(gpa, io, tmp.dir, late_bytes);
    defer gpa.free(late);

    const next = try pageAt(gpa, io, tmp.dir, "1000001000", "body.txt", args);
    {
        const got = try rawOf(gpa, io, tmp.dir, &next);
        defer gpa.free(got);
        const want = try fossilPage(gpa, null, "2001-09-09T02:03:20.000", "Fork", late, "pat", "body\n");
        defer gpa.free(want);
        try std.testing.expectEqualStrings(want, got);
    }

    // A child whose clock ran behind its parent does not become current:
    // fossil weighs every version by date, replaced or not.
    const skewed_bytes = try fossilPage(gpa, null, "2001-09-09T01:00:00.000", "Fork", &next, "c", "skewed\n");
    defer gpa.free(skewed_bytes);
    const skewed = try appendArtifact(gpa, io, tmp.dir, skewed_bytes);
    defer gpa.free(skewed);
    const after = try pageAt(gpa, io, tmp.dir, "1000002000", "body.txt", args);
    const got = try rawOf(gpa, io, tmp.dir, &after);
    defer gpa.free(got);
    const want_p = try std.fmt.allocPrint(gpa, "\nP {s}\n", .{&next});
    defer gpa.free(want_p);
    try std.testing.expect(std.mem.indexOf(u8, got, want_p) != null);
}

test "cli wiki commit: -m and --comment record a C card fossil's way, and an empty one records none" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "body.txt", "body\n");
    try writeFile(io, tmp.dir, "empty.txt", "");

    const v1 = try pageAt(gpa, io, tmp.dir, "1000000000", "body.txt", &.{ "wiki", "commit", "Page", "--user", "pat", "-m", "first draft" });
    const v2 = try pageAt(gpa, io, tmp.dir, "1000000060", "empty.txt", &.{ "wiki", "commit", "Page", "--user", "pat", "--comment", "moved to New\\Page" });
    const v3 = try pageAt(gpa, io, tmp.dir, "1000000120", "body.txt", &.{ "wiki", "commit", "Page", "--user", "pat", "--comment", "" });

    const Want = struct { hash: *const [64]u8, comment: ?[]const u8, date: []const u8, parent: ?[]const u8, body: []const u8 };
    const wants = [_]Want{
        .{ .hash = &v1, .comment = "first\\sdraft", .date = "2001-09-09T01:46:40.000", .parent = null, .body = "body\n" },
        .{ .hash = &v2, .comment = "moved\\sto\\sNew\\\\Page", .date = "2001-09-09T01:47:40.000", .parent = &v1, .body = "" },
        .{ .hash = &v3, .comment = null, .date = "2001-09-09T01:48:40.000", .parent = &v2, .body = "body\n" },
    };
    for (wants) |w| {
        const got = try rawOf(gpa, io, tmp.dir, w.hash);
        defer gpa.free(got);
        const want = try fossilPage(gpa, w.comment, w.date, "Page", w.parent, "pat", w.body);
        defer gpa.free(want);
        try std.testing.expectEqualStrings(want, got);
    }

    // Read back decoded, beside the parent.
    var shown = try runZit(gpa, io, tmp.dir, &.{ "artifact", &v2 });
    defer freeResult(gpa, &shown);
    try expectExit(shown.term, 0);
    try std.testing.expect(std.mem.indexOf(u8, shown.stdout, "\nC moved to New\\Page\n") != null);
    const p_line = try std.fmt.allocPrint(gpa, "\nP {s}\n", .{&v1});
    defer gpa.free(p_line);
    try std.testing.expect(std.mem.indexOf(u8, shown.stdout, p_line) != null);

    // A flag with no value is a usage error, and records nothing.
    var bare = try support.runZitStdin(gpa, io, tmp.dir, "body.txt", &.{ "wiki", "commit", "Page", "--user", "pat", "-m" });
    defer freeResult(gpa, &bare);
    try expectExit(bare.term, 2);
    try std.testing.expectEqualStrings("", bare.stdout);
}

test "cli wiki commit: a version fossil could have written, with a comment and two parents, is read and becomes the parent" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try initRepo(gpa, io, tmp.dir);
    try writeFile(io, tmp.dir, "body.txt", "body\n");
    const args: []const []const u8 = &.{ "wiki", "commit", "Page", "--user", "pat" };

    const a = try pageAt(gpa, io, tmp.dir, "1000000000", "body.txt", args);
    const b_bytes = try fossilPage(gpa, null, "2001-09-09T01:47:00.000", "Page", &a, "q", "b\n");
    defer gpa.free(b_bytes);
    const b = try appendArtifact(gpa, io, tmp.dir, b_bytes);
    defer gpa.free(b);
    // The format's `P parent-artifact-id+`: a version joining both heads.
    const parents = try std.fmt.allocPrint(gpa, "{s} {s}", .{ &a, b });
    defer gpa.free(parents);
    const join_bytes = try fossilPage(gpa, "joined\\sboth", "2001-09-09T01:48:00.000", "Page", parents, "q", "joined\n");
    defer gpa.free(join_bytes);
    const join = try appendArtifact(gpa, io, tmp.dir, join_bytes);
    defer gpa.free(join);

    var shown = try runZit(gpa, io, tmp.dir, &.{ "artifact", join });
    defer freeResult(gpa, &shown);
    try expectExit(shown.term, 0);
    const want_lines = try std.fmt.allocPrint(gpa, "type: wiki\nsize: {d} bytes\nC joined both\nD 2001-09-09T01:48:00.000\nL Page\nP {s}\nU q\nW 7 bytes\nZ: verified\n", .{ join_bytes.len, parents });
    defer gpa.free(want_lines);
    try std.testing.expect(std.mem.endsWith(u8, shown.stdout, want_lines));

    const next = try pageAt(gpa, io, tmp.dir, "1000001000", "body.txt", args);
    const got = try rawOf(gpa, io, tmp.dir, &next);
    defer gpa.free(got);
    const want_p = try std.fmt.allocPrint(gpa, "\nP {s}\n", .{join});
    defer gpa.free(want_p);
    try std.testing.expect(std.mem.indexOf(u8, got, want_p) != null);
}