╭ changed ───────────────╮ │src/test/event/patch.zig│ ╰────────────────────────╯ ╭───────────╮ │← all files│ ╰───────────╯ ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │757 })); │ │758 try evt.Patch.update(.xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author);│ │759 │ │760 + // run the cache scenario once; both lifecycle runs still perform a merge │ │761 + if (merge_revision == .source) { │ │762 + try testMergeability(&target, io, allocator, repos_dir, &patch_id, &base_oid, &source_oid); │ │763 + } │ │764 + │ │765 const selected_oid = switch (merge_revision) { │ │766 .squash => squash_oid, │ │767 .source => source_oid, │ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │818 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; │ │819 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); │ │820 return try ids.count(); │ │821 +} │ │822 + │ │823 +fn readMergeability(repo: *Repo, io: std.Io, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8, patch: evt.Patch) !pch.Mergeability { │ │824 + var arena = std.heap.ArenaAllocator.init(allocator); │ │825 + defer arena.deinit(); │ │826 + var moment = try repo.core.latestMoment(); │ │827 + return pch.readMergeability(repo_opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, id, patch); │ │828 +} │ │829 + │ │830 +fn expectMergeabilityShortBytes(moment: Repo.DB.HashMap(.read_only), id: *const [evt.event_id_size]u8) !void { │ │831 + const checks = try Repo.DB.HashMap(.read_only).init((try moment.getCursor(hash.hashInt(repo_opts.hash, "patch-id->mergeability"))) orelse return error.NotFound);│ │832 + const entry = try Repo.DB.HashMap(.read_only).init((try checks.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return error.NotFound); │ │833 + const result = try Repo.DB.HashMap(.read_only).init((try entry.getCursor(hash.hashInt(repo_opts.hash, "result"))) orelse return error.NotFound); │ │834 + for ([_][]const u8{ "source", "squash" }) |field| { │ │835 + const cursor = (try result.getCursor(hash.hashInt(repo_opts.hash, field))) orelse return error.NotFound; │ │836 + try std.testing.expectEqual(.short_bytes, cursor.slot().tag); │ │837 + } │ │838 +} │ │839 + │ │840 +fn testMergeability( │ │841 + target: *Repo, │ │842 + io: std.Io, │ │843 + allocator: std.mem.Allocator, │ │844 + repos_dir: []const u8, │ │845 + id: *const [evt.event_id_size]u8, │ │846 + base: *const [hash.hexLen(repo_opts.hash)]u8, │ │847 + source: *const [hash.hexLen(repo_opts.hash)]u8, │ │848 +) !void { │ │849 + var arena = std.heap.ArenaAllocator.init(allocator); │ │850 + defer arena.deinit(); │ │851 + const events = try evt.currentMoment(repo_opts, target); │ │852 + const record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, events, &arena, id)) orelse return error.NotFound; │ │853 + const patch = record.event; │ │854 + │ │855 + // publication checks both methods, and reads and cache hits do no work │ │856 + const clean = pch.Mergeability{ .source = .clean, .squash = .clean }; │ │857 + try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); │ │858 + try expectMergeabilityShortBytes(try target.core.latestMoment(), id); │ │859 + const before = (try target.core.latestMoment()).cursor.slot(); │ │860 + pch.refreshMergeability(repo_opts, io, allocator, target, id.*); │ │861 + try std.testing.expectEqualDeep(before, (try target.core.latestMoment()).cursor.slot()); │ │862 + { │ │863 + var moment = try target.core.latestMoment(); │ │864 + const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &moment } }; │ │865 + try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, repo_opts).initCommit(state, io, allocator, source)); │ │866 + } │ │867 + const events_before = try target.readRef(io, evt.events_ref); │ │868 + │ │869 + // a target update invalidates the old result before refresh │ │870 + { │ │871 + const file = try target.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); │ │872 + defer file.close(io); │ │873 + try file.writeStreamingAll(io, "pub const answer = 99;\n"); │ │874 + } │ │875 + try target.add(io, allocator, &.{"feature.zig"}); │ │876 + const conflicting_target = try target.commit(io, allocator, .{ .message = "different answer", .timestamp = 20 }); │ │877 + const input = pch.MergeInput{ .id = std.fmt.bytesToHex(id.*, .lower), .revision = .source, .author = author, .timestamp = 21 }; │ │878 + var squash_input = input; │ │879 + squash_input.revision = .squash; │ │880 + try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); │ │881 + try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); │ │882 + try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); │ │883 + pch.refreshMergeability(repo_opts, io, allocator, target, null); │ │884 + try std.testing.expectEqualDeep(pch.Mergeability{ .source = .conflict, .squash = .conflict }, try readMergeability(target, io, allocator, id, patch)); │ │885 + try expectMergeabilityShortBytes(try target.core.latestMoment(), id); │ │886 + try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, input)); │ │887 + try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, repos_dir, target, squash_input)); │ │888 + try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); │ │889 + try std.testing.expectEqual(conflicting_target, (try target.readRef(io, .{ .kind = .head, .name = "master" })).?); │ │890 + try std.testing.expectEqual(null, try target.readRef(io, .{ .kind = .none, .name = "MERGE_HEAD" })); │ │891 + │ │892 + // deleting and restoring the target cannot reuse the conflict result │ │893 + { │ │894 + var detached = try target.switchDir(io, allocator, .{ .target = .{ .oid = base } }); │ │895 + defer detached.deinit(); │ │896 + } │ │897 + try target.removeBranch(io, .{ .name = "master" }); │ │898 + pch.refreshMergeability(repo_opts, io, allocator, target, null); │ │899 + try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); │ │900 + try expectMergeabilityShortBytes(try target.core.latestMoment(), id); │ │901 + try target.addBranch(io, .{ .name = "master" }); │ │902 + { │ │903 + var restored = try target.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); │ │904 + defer restored.deinit(); │ │905 + } │ │906 + pch.refreshMergeability(repo_opts, io, allocator, target, null); │ │907 + try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); │ │908 + │ │909 + // a missing fork disables merging without changing the accepted events │ │910 + const draft_path = try fork.forkPath(allocator, repos_dir, &std.fmt.bytesToHex(id.*, .lower)); │ │911 + defer allocator.free(draft_path); │ │912 + const away_path = try std.fmt.allocPrint(allocator, "{s}.away", .{draft_path}); │ │913 + defer allocator.free(away_path); │ │914 + { │ │915 + try std.Io.Dir.renameAbsolute(draft_path, away_path, io); │ │916 + defer std.Io.Dir.renameAbsolute(away_path, draft_path, io) catch {}; │ │917 + pch.refreshMergeability(repo_opts, io, allocator, target, id.*); │ │918 + try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); │ │919 + try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); │ │920 + } │ │921 + pch.refreshMergeability(repo_opts, io, allocator, target, id.*); │ │922 + try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); │ │923 + │ │924 + // a changed revision is unknown even if its target has not moved │ │925 + var changed = patch; │ │926 + changed.revision.?.id[0] = if (changed.revision.?.id[0] == 'a') 'b' else 'a'; │ │927 + try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, changed)); │ │928 } │ │929 │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯