╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const xit = @import("xit"); 4 const rp = xit.repo; 5 const obj = xit.object; 6 const hash = xit.hash; 7 const rf = xit.ref; 8 const fork = @import("../../fork.zig"); 9 const pch = @import("../../patch.zig"); 10 11 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 12 const Repo = rp.Repo(.xit, repo_opts); 13 const author = evt.CommitAuthor{ .name = "alice", .email = "alice@example.test" }; 14 15 fn commitTree( 16 io: std.Io, 17 allocator: std.mem.Allocator, 18 repo: *Repo, 19 oid: *const [hash.hexLen(repo_opts.hash)]u8, 20 ) ![hash.hexLen(repo_opts.hash)]u8 { 21 var moment = try repo.core.latestMoment(); 22 const state = Repo.State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 23 var object = try obj.Object(.xit, repo_opts).init(state, io, allocator, oid); 24 defer object.deinit(); 25 return switch (object.content) { 26 .commit => |commit| commit.tree, 27 else => error.InvalidObject, 28 }; 29 } 30 31 fn consumePatchWithRevision( 32 comptime role: evt.RepoRole, 33 io: std.Io, 34 allocator: std.mem.Allocator, 35 repo: *Repo, 36 ref: rf.Ref, 37 revision_id: [evt.event_id_size]u8, 38 revision: evt.PatchRev, 39 tree_entries: []const evt.EventTreeEntry, 40 revision_timestamp: u64, 41 patch_id: [evt.event_id_size]u8, 42 patch_value: evt.Patch, 43 patch_timestamp: u64, 44 ) !void { 45 try evt.consume(role, .xit, repo_opts, io, allocator, repo, ref, &.{.{ 46 .id = std.fmt.bytesToHex(revision_id, .lower), 47 .timestamp = revision_timestamp, 48 .author = author, 49 .tree_entries = tree_entries, 50 .event = .{ .patchrev = revision }, 51 }}); 52 53 var arena = std.heap.ArenaAllocator.init(allocator); 54 defer arena.deinit(); 55 const moment = try evt.currentMoment(repo_opts, repo); 56 const record = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &revision_id)) orelse return error.NotFound; 57 var patch = patch_value; 58 patch.revision = evt.Patch.Revision.fromRecord(revision_id, record); 59 try evt.consume(role, .xit, repo_opts, io, allocator, repo, ref, &.{.{ 60 .id = std.fmt.bytesToHex(patch_id, .lower), 61 .timestamp = patch_timestamp, 62 .author = author, 63 .event = .{ .patch = patch }, 64 }}); 65 } 66 67 test "patch event conflicts, stacking, and gc" { 68 const io = std.testing.io; 69 const allocator = std.testing.allocator; 70 const temp_dir_name = "temp-event-patch"; 71 const cwd = std.Io.Dir.cwd(); 72 73 // create the temp dir 74 if (cwd.openDir(io, temp_dir_name, .{})) |dir_value| { 75 var dir = dir_value; 76 dir.close(io); 77 try cwd.deleteTree(io, temp_dir_name); 78 } else |_| {} 79 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 80 defer cwd.deleteTree(io, temp_dir_name) catch {}; 81 defer temp_dir.close(io); 82 83 const cwd_path = try std.process.currentPathAlloc(io, allocator); 84 defer allocator.free(cwd_path); 85 const upstream_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "upstream" }); 86 defer allocator.free(upstream_path); 87 const target_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "target" }); 88 defer allocator.free(target_path); 89 const source_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "source" }); 90 defer allocator.free(source_path); 91 92 // 93 // create the upstream repo and its base commit 94 // 95 96 const base_oid = blk: { 97 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 98 defer upstream.deinit(io, allocator); 99 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 100 defer file.close(io); 101 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 102 try upstream.add(io, allocator, &.{"main.zig"}); 103 break :blk try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 104 }; 105 106 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 107 defer target.deinit(io, allocator); 108 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 109 110 // 111 // fork the target and commit the proposed change 112 // 113 114 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 115 defer source.deinit(io, allocator); 116 117 const source_oid = blk: { 118 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 119 defer file.close(io); 120 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 121 try source.add(io, allocator, &.{"feature.zig"}); 122 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 123 }; 124 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 125 126 // 127 // copy the proposed objects without creating a target ref 128 // 129 130 { 131 var source_moment = try source.core.latestMoment(); 132 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 133 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 134 defer objects.deinit(); 135 try objects.include(&source_oid); 136 try target.copyObjects(.xit, repo_opts, &objects, io, null); 137 } 138 139 // 140 // define the patch revision and patch 141 // 142 143 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 144 const patchrev_id = evt.EventWithId.randomId(prng.random()); 145 const patch_id = evt.EventWithId.randomId(prng.random()); 146 const patch = evt.Patch{ 147 .title = "add the answer", 148 .description = "adds a reusable answer constant", 149 .tags = "enhancement", 150 }; 151 const tree_entries = [_]evt.EventTreeEntry{ 152 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 153 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 154 }; 155 156 // 157 // consume the patch revision and patch 158 // 159 160 { 161 try consumePatchWithRevision( 162 .repo, 163 io, 164 allocator, 165 &target, 166 evt.events_ref, 167 patchrev_id, 168 .{ .base_oid = &base_oid, .source_oid = &source_oid, .target_ref = "refs/heads/master", .message = patch.title }, 169 &tree_entries, 170 3, 171 patch_id, 172 patch, 173 4, 174 ); 175 } 176 177 // 178 // check the records and squash commit 179 // 180 181 var arena = std.heap.ArenaAllocator.init(allocator); 182 defer arena.deinit(); 183 const moment = try evt.currentMoment(repo_opts, &target); 184 const patch_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 185 const patchrev_record = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &patchrev_id)) orelse return error.NotFound; 186 const records_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse return error.NotFound; 187 const records = try Repo.DB.HashMap(.read_only).init(records_cursor); 188 const record_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 189 const record_map = try Repo.DB.HashMap(.read_only).init(record_cursor); 190 const event_cursor = try record_map.getCursor(hash.hashInt(repo_opts.hash, "event")) orelse return error.NotFound; 191 const event_map = try Repo.DB.HashMap(.read_only).init(event_cursor); 192 const revision_cursor = try event_map.getCursor(hash.hashInt(repo_opts.hash, "revision")) orelse return error.NotFound; 193 const revision_map = try Repo.DB.HashMap(.read_only).init(revision_cursor); 194 try std.testing.expect(null != try revision_map.getCursor(hash.hashInt(repo_opts.hash, "id"))); 195 try std.testing.expectEqual(null, try record_map.getCursor(hash.hashInt(repo_opts.hash, "title"))); 196 try std.testing.expectEqualStrings(&base_tree_oid, patchrev_record.base_tree_oid); 197 try std.testing.expectEqualStrings(&head_tree_oid, patchrev_record.head_tree_oid); 198 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_id, .lower), &(patch_record.event.revision orelse return error.NotFound).id); 199 200 var initial_patch_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 201 @memcpy(&initial_patch_oid, patchrev_record.patch_oid); 202 { 203 var repo_moment = try target.core.latestMoment(); 204 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &repo_moment } }; 205 var patch_object = try obj.Object(.xit, repo_opts).init(state, io, allocator, &initial_patch_oid); 206 defer patch_object.deinit(); 207 switch (patch_object.content) { 208 .commit => |commit| { 209 try std.testing.expectEqualStrings(&head_tree_oid, &commit.tree); 210 const parents = commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 211 try std.testing.expectEqual(1, parents.len); 212 try std.testing.expectEqualStrings(&base_oid, &parents[0]); 213 }, 214 else => return error.InvalidObject, 215 } 216 } 217 218 // 219 // update metadata without creating another patch revision 220 // 221 222 var updated_patch = patch; 223 updated_patch.revision = .{ 224 .id = std.fmt.bytesToHex(patchrev_id, .lower), 225 .squash_oid = &initial_patch_oid, 226 .source_oid = &source_oid, 227 .target_ref = "refs/heads/master", 228 }; 229 updated_patch.title = "add a reusable answer"; 230 { 231 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 232 .id = std.fmt.bytesToHex(patch_id, .lower), 233 .timestamp = 5, 234 .author = author, 235 .event = .{ .patch = updated_patch }, 236 }}); 237 } 238 239 _ = arena.reset(.retain_capacity); 240 const updated_moment = try evt.currentMoment(repo_opts, &target); 241 const updated = (try evt.Patch.readById(Repo.DB, repo_opts.hash, updated_moment, &arena, &patch_id)) orelse return error.NotFound; 242 try std.testing.expectEqualStrings("add a reusable answer", updated.event.title); 243 244 // 245 // branch the events history before creating competing revisions 246 // 247 248 const side_events_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-side" }; 249 { 250 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 251 defer result.deinit(); 252 try target.addBranch(io, .{ .name = side_events_ref.name }); 253 } 254 255 // 256 // create two competing code revisions 257 // 258 259 const source_a_oid = blk: { 260 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 261 defer file.close(io); 262 try file.writeStreamingAll(io, "pub const answer = 43;\n"); 263 try source.add(io, allocator, &.{"feature.zig"}); 264 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature one way", .timestamp = 6 }); 265 }; 266 const source_a_tree = try commitTree(io, allocator, &source, &source_a_oid); 267 const source_b_oid = blk: { 268 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 269 defer file.close(io); 270 try file.writeStreamingAll(io, "pub const answer = 44;\n"); 271 try source.add(io, allocator, &.{"feature.zig"}); 272 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature another way", .timestamp = 7 }); 273 }; 274 const source_b_tree = try commitTree(io, allocator, &source, &source_b_oid); 275 { 276 var source_moment = try source.core.latestMoment(); 277 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 278 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 279 defer objects.deinit(); 280 try objects.include(&source_b_oid); 281 try target.copyObjects(.xit, repo_opts, &objects, io, null); 282 } 283 284 // 285 // select the first revision on the events branch 286 // 287 288 const patchrev_a_id = evt.EventWithId.randomId(prng.random()); 289 const target_entries = [_]evt.EventTreeEntry{ 290 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 291 .{ .tree = .{ .name = "head", .oid = &source_a_tree } }, 292 }; 293 var target_patch = updated_patch; 294 target_patch.status = .closed; 295 { 296 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, patchrev_a_id, .{ 297 .base_oid = &base_oid, 298 .source_oid = &source_a_oid, 299 .target_ref = "refs/heads/master", 300 .message = updated_patch.title, 301 }, &target_entries, 8, patch_id, target_patch, 9); 302 } 303 304 // 305 // select the second revision on the side branch 306 // 307 308 const patchrev_b_id = evt.EventWithId.randomId(prng.random()); 309 const parent_entries = [_]evt.EventTreeEntry{ 310 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 311 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 312 }; 313 var parent_patch = updated_patch; 314 parent_patch.status = .merged; 315 { 316 try consumePatchWithRevision(.repo, io, allocator, &target, side_events_ref, patchrev_b_id, .{ 317 .base_oid = &base_oid, 318 .source_oid = &source_b_oid, 319 .target_ref = "refs/heads/master", 320 .message = updated_patch.title, 321 }, &parent_entries, 10, patch_id, parent_patch, 11); 322 } 323 324 // 325 // merge the competing patch revisions 326 // 327 328 { 329 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, side_events_ref); 330 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 331 } 332 333 // 334 // check the selected revision and conflict 335 // 336 337 _ = arena.reset(.retain_capacity); 338 const merged_moment = try evt.currentMoment(repo_opts, &target); 339 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 340 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_a_id, .lower), &(merged.event.revision orelse return error.NotFound).id); 341 const merged_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patchrev_a_id)) orelse return error.NotFound; 342 try std.testing.expectEqualStrings(&source_a_oid, merged_patchrev.event.source_oid); 343 try std.testing.expectEqualStrings(&source_a_tree, merged_patchrev.head_tree_oid); 344 345 const conflicts_cursor = try merged_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 346 const conflicts = try Repo.DB.SortedMap(.read_only).init(conflicts_cursor); 347 const conflict_cursor = try conflicts.getCursor(&evt.orderKeyDesc(merged.created_order, &patch_id)) orelse return error.NotFound; 348 const conflict = try Repo.DB.HashMap(.read_only).init(conflict_cursor); 349 const fields_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key)) orelse return error.NotFound; 350 try std.testing.expectEqualStrings("revision status", try fields_cursor.readBytesAlloc(arena.allocator(), null)); 351 const their_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key)) orelse return error.NotFound; 352 const theirs = try evt.read(evt.Patch.Record, Repo.DB, repo_opts.hash, &arena, try Repo.DB.HashMap(.read_only).init(their_cursor)); 353 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_b_id, .lower), &(theirs.event.revision orelse return error.NotFound).id); 354 355 // 356 // create a patch stacked on the selected revision 357 // 358 359 const child_patchrev_id = evt.EventWithId.randomId(prng.random()); 360 const child_id = evt.EventWithId.randomId(prng.random()); 361 const child_entries = [_]evt.EventTreeEntry{ 362 .{ .tree = .{ .name = "base", .oid = merged_patchrev.head_tree_oid } }, 363 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 364 }; 365 { 366 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, child_patchrev_id, .{ 367 .base_oid = merged_patchrev.patch_oid, 368 .source_oid = &source_b_oid, 369 .target_ref = "refs/heads/master", 370 .message = "stack another answer change", 371 }, &child_entries, 12, child_id, .{ 372 .title = "stack another answer change", 373 .description = "depends on the first patch", 374 .tags = "enhancement", 375 .target_patch_id = std.fmt.bytesToHex(patch_id, .lower), 376 }, 13); 377 } 378 379 // 380 // check the squash commit and reverse index 381 // 382 383 _ = arena.reset(.retain_capacity); 384 const stacked_moment = try evt.currentMoment(repo_opts, &target); 385 const child = (try evt.Patch.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &child_id)) orelse return error.NotFound; 386 var selected_patchrev_id: [evt.event_id_size]u8 = undefined; 387 _ = try std.fmt.hexToBytes(&selected_patchrev_id, &(child.event.revision orelse return error.NotFound).id); 388 const child_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &selected_patchrev_id)) orelse return error.NotFound; 389 var child_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 390 @memcpy(&child_oid, child_patchrev.patch_oid); 391 { 392 var child_repo_moment = try target.core.latestMoment(); 393 const child_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &child_repo_moment } }; 394 var child_object = try obj.Object(.xit, repo_opts).init(child_state, io, allocator, &child_oid); 395 defer child_object.deinit(); 396 const child_commit = switch (child_object.content) { 397 .commit => |commit| commit, 398 else => return error.InvalidObject, 399 }; 400 const child_parents = child_commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 401 try std.testing.expectEqualStrings(child_patchrev.event.base_oid, &child_parents[0]); 402 } 403 404 const targets_cursor = try stacked_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.target_patch_id_to_patch_id_set_key)) orelse return error.NotFound; 405 const targets = try Repo.DB.HashMap(.read_only).init(targets_cursor); 406 const children_cursor = try targets.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 407 const children = try Repo.DB.SortedSet(.read_only).init(children_cursor); 408 try std.testing.expectEqual(1, try children.count()); 409 410 // 411 // close and merge the same revision concurrently 412 // 413 414 const status_side_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-status-side" }; 415 { 416 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 417 defer result.deinit(); 418 try target.addBranch(io, .{ .name = status_side_ref.name }); 419 } 420 var closed_child = child.event; 421 closed_child.status = .closed; 422 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 423 .id = std.fmt.bytesToHex(child_id, .lower), 424 .timestamp = 14, 425 .author = author, 426 .event = .{ .patch = closed_child }, 427 }}); 428 var merged_child = child.event; 429 merged_child.status = .merged; 430 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, status_side_ref, &.{.{ 431 .id = std.fmt.bytesToHex(child_id, .lower), 432 .timestamp = 15, 433 .author = author, 434 .event = .{ .patch = merged_child }, 435 }}); 436 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, status_side_ref); 437 try evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 438 439 _ = arena.reset(.retain_capacity); 440 const status_moment = try evt.currentMoment(repo_opts, &target); 441 const merged_child_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, status_moment, &arena, &child_id)) orelse return error.NotFound; 442 try std.testing.expectEqual(.merged, merged_child_record.event.status); 443 try std.testing.expectEqual(0, try patchStatusCount(status_moment, .open)); 444 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .closed)); 445 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .merged)); 446 try std.testing.expectEqual(1, try patchTagStatusCount(status_moment, "enhancement", .merged)); 447 const status_conflicts_cursor = try status_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 448 const status_conflicts = try Repo.DB.SortedMap(.read_only).init(status_conflicts_cursor); 449 const child_conflict = try status_conflicts.getCursor(&evt.orderKeyDesc(merged_child_record.created_order, &child_id)); 450 try std.testing.expectEqual(null, child_conflict); 451 452 var reopened_child = merged_child_record.event; 453 reopened_child.status = .open; 454 try std.testing.expectError(error.PatchAlreadyMerged, evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 455 .id = std.fmt.bytesToHex(child_id, .lower), 456 .timestamp = 16, 457 .author = author, 458 .event = .{ .patch = reopened_child }, 459 }})); 460 461 const invalid_merged_id = evt.EventWithId.randomId(prng.random()); 462 try std.testing.expectError(error.InvalidPatch, evt.consume(.repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 463 .id = std.fmt.bytesToHex(invalid_merged_id, .lower), 464 .timestamp = 16, 465 .author = author, 466 .event = .{ .patch = .{ 467 .title = "invalid merged patch", 468 .description = "has no revision", 469 .tags = "enhancement", 470 .status = .merged, 471 } }, 472 }})); 473 474 // 475 // retain the ref-free commits during garbage collection 476 // 477 478 { 479 const roots = try evt.PatchRev.gcRoots(Repo.DB, repo_opts.hash, allocator, stacked_moment); 480 defer allocator.free(roots); 481 try std.testing.expectEqual(4, roots.len); 482 _ = try target.garbageCollect(io, allocator, roots); 483 484 var after_gc_moment = try target.core.latestMoment(); 485 const after_gc_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &after_gc_moment } }; 486 var retained = try obj.Object(.xit, repo_opts).init(after_gc_state, io, allocator, &roots[0]); 487 retained.deinit(); 488 } 489 } 490 491 test "patch lifecycle" { 492 try patchLifecycle("temp-patch-lifecycle-squash", .squash); 493 try patchLifecycle("temp-patch-lifecycle-source", .source); 494 } 495 496 fn patchLifecycle(temp_dir_name: []const u8, merge_revision: pch.MergeRevision) !void { 497 const io = std.testing.io; 498 const allocator = std.testing.allocator; 499 const cwd = std.Io.Dir.cwd(); 500 501 if (cwd.openDir(io, temp_dir_name, .{})) |dir_value| { 502 var dir = dir_value; 503 dir.close(io); 504 try cwd.deleteTree(io, temp_dir_name); 505 } else |_| {} 506 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 507 defer cwd.deleteTree(io, temp_dir_name) catch {}; 508 defer temp_dir.close(io); 509 510 const cwd_path = try std.process.currentPathAlloc(io, allocator); 511 defer allocator.free(cwd_path); 512 const root = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 513 defer allocator.free(root); 514 const repos_dir = try std.fs.path.join(allocator, &.{ root, "repos" }); 515 defer allocator.free(repos_dir); 516 const admin_path = try std.fs.path.join(allocator, &.{ root, "admin" }); 517 defer allocator.free(admin_path); 518 const upstream_path = try std.fs.path.join(allocator, &.{ root, "upstream" }); 519 defer allocator.free(upstream_path); 520 const source_path = try std.fs.path.join(allocator, &.{ root, "source" }); 521 defer allocator.free(source_path); 522 523 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 524 const user_id = evt.EventWithId.randomId(prng.random()); 525 const repo_id = evt.EventWithId.randomId(prng.random()); 526 const repo_id_hex = std.fmt.bytesToHex(repo_id, .lower); 527 const target_path = try std.fs.path.join(allocator, &.{ repos_dir, &repo_id_hex }); 528 defer allocator.free(target_path); 529 530 var admin = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 531 defer admin.deinit(io, allocator); 532 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 533 .{ 534 .id = std.fmt.bytesToHex(user_id, .lower), 535 .timestamp = 1, 536 .author = author, 537 .event = .{ .user = .{ 538 .name = author.name, 539 .email = author.email, 540 .password_hash = "", 541 } }, 542 }, 543 .{ 544 .id = repo_id_hex, 545 .timestamp = 1, 546 .author = author, 547 .event = .{ .repo = .{ 548 .user_id = &user_id, 549 .name = "repo", 550 .description = "", 551 } }, 552 }, 553 }); 554 555 // 556 // create the target repo 557 // 558 559 { 560 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 561 defer upstream.deinit(io, allocator); 562 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 563 defer file.close(io); 564 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 565 try upstream.add(io, allocator, &.{"main.zig"}); 566 _ = try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 567 } 568 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 569 defer target.deinit(io, allocator); 570 571 // 572 // create the patch draft in the fork 573 // 574 575 const patch_id = evt.EventWithId.randomId(prng.random()); 576 const patch_id_hex = std.fmt.bytesToHex(patch_id, .lower); 577 const draft_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 578 .id = patch_id_hex, 579 .user_id = user_id, 580 .repo_id = repo_id, 581 .title = "add the answer", 582 .tags = "enhancement", 583 .description = "adds a reusable answer constant", 584 .author = author, 585 .timestamp = 2, 586 }); 587 defer allocator.free(draft_path); 588 589 // 590 // create and record the proposed revision in the fork 591 // 592 593 const base_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 594 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 595 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 596 defer source.deinit(io, allocator); 597 const source_oid = blk: { 598 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 599 defer file.close(io); 600 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 601 try source.add(io, allocator, &.{"feature.zig"}); 602 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 603 }; 604 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 605 const first_patchrev_id = evt.EventWithId.randomId(prng.random()); 606 const first_patchrev_hex = std.fmt.bytesToHex(first_patchrev_id, .lower); 607 const first_entries = [_]evt.EventTreeEntry{ 608 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 609 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 610 }; 611 { 612 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 613 defer draft.deinit(io, allocator); 614 var arena = std.heap.ArenaAllocator.init(allocator); 615 defer arena.deinit(); 616 617 const initial_moment = try evt.currentMoment(repo_opts, &draft); 618 const initial_patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, initial_moment, &arena, &patch_id)) orelse return error.NotFound; 619 try std.testing.expect(!initial_patch.removed); 620 try std.testing.expectEqualStrings("add the answer", initial_patch.event.title); 621 try std.testing.expectEqual(null, initial_patch.event.revision); 622 try std.testing.expectEqual(null, try evt.PatchRev.readNewest(Repo.DB, repo_opts.hash, initial_moment, &arena)); 623 624 var source_moment = try source.core.latestMoment(); 625 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 626 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 627 defer objects.deinit(); 628 try objects.include(&source_oid); 629 try draft.copyObjects(.xit, repo_opts, &objects, io, null); 630 try consumePatchWithRevision(.fork, io, allocator, &draft, evt.events_ref, first_patchrev_id, .{ 631 .base_oid = &base_oid, 632 .source_oid = &source_oid, 633 .target_ref = "refs/heads/master", 634 .message = "add the answer", 635 }, &first_entries, 3, patch_id, .{ 636 .title = "add the answer", 637 .tags = "enhancement", 638 .description = "adds a reusable answer constant", 639 }, 3); 640 641 _ = arena.reset(.retain_capacity); 642 const moment = try evt.currentMoment(repo_opts, &draft); 643 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 644 try std.testing.expect(!patch.removed); 645 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 646 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 647 } 648 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 649 650 // 651 // publish another draft before its first push 652 // 653 654 const unpushed_id = evt.EventWithId.randomId(prng.random()); 655 const unpushed_id_hex = std.fmt.bytesToHex(unpushed_id, .lower); 656 const unpushed_path = try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 657 .id = unpushed_id_hex, 658 .user_id = user_id, 659 .repo_id = repo_id, 660 .title = "explain the answer", 661 .tags = "documentation", 662 .description = "describes the existing answer", 663 .author = author, 664 .timestamp = 3, 665 }); 666 defer allocator.free(unpushed_path); 667 try pch.publish(repo_opts, io, allocator, &admin, &target, unpushed_path, .{ 668 .id = unpushed_id_hex, 669 .user_id = user_id, 670 .repo_id = repo_id, 671 .author = author, 672 .timestamp = 4, 673 }); 674 { 675 var verify_arena = std.heap.ArenaAllocator.init(allocator); 676 defer verify_arena.deinit(); 677 const target_moment = try evt.currentMoment(repo_opts, &target); 678 const unpushed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, target_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 679 try std.testing.expectEqual(null, unpushed.event.revision); 680 681 var unpushed_repo = try Repo.open(io, allocator, .{ .path = unpushed_path }); 682 defer unpushed_repo.deinit(io, allocator); 683 const fork_moment = try evt.currentMoment(repo_opts, &unpushed_repo); 684 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, fork_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 685 try std.testing.expect(removed.removed); 686 } 687 688 // 689 // edit the patch metadata in the fork 690 // 691 692 try std.testing.expect(try pch.editDraft(repo_opts, io, allocator, &admin, draft_path, .{ 693 .id = patch_id_hex, 694 .user_id = user_id, 695 .repo_id = repo_id, 696 .title = "explain the answer", 697 .tags = "enhancement documentation", 698 .description = "adds and explains a reusable answer constant", 699 .author = author, 700 .timestamp = 4, 701 })); 702 703 // 704 // publish the metadata and revision pointer 705 // 706 707 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 708 .id = patch_id_hex, 709 .user_id = user_id, 710 .repo_id = repo_id, 711 .author = author, 712 .timestamp = 5, 713 }); 714 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 715 .id = patch_id_hex, 716 .user_id = user_id, 717 .repo_id = repo_id, 718 .author = author, 719 .timestamp = 6, 720 }); 721 722 var arena = std.heap.ArenaAllocator.init(allocator); 723 defer arena.deinit(); 724 const moment = try evt.currentMoment(repo_opts, &target); 725 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 726 try std.testing.expectEqualStrings("explain the answer", patch.event.title); 727 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 728 try std.testing.expectEqual(.open, patch.event.status); 729 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 730 731 _ = arena.reset(.retain_capacity); 732 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 733 const published_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.NotFound; 734 try std.testing.expect(!published_fork.removed); 735 try std.testing.expectEqual(.publish, published_fork.event.stage); 736 const draft_key = evt.Fork.draftKey(&repo_id, &user_id); 737 try std.testing.expectEqual(0, try indexedForkCount(admin_moment, evt.Fork.repo_user_to_draft_id_set_key, &draft_key)); 738 try std.testing.expectEqual(2, try indexedForkCount(admin_moment, evt.Fork.user_id_to_fork_id_set_key, &user_id)); 739 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 740 { 741 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 742 defer draft.deinit(io, allocator); 743 _ = arena.reset(.retain_capacity); 744 const draft_moment = try evt.currentMoment(repo_opts, &draft); 745 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 746 try std.testing.expect(removed.removed); 747 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 748 @memcpy(&squash_oid, revision.patch_oid); 749 try std.testing.expectEqualStrings("refs/heads/master", revision.event.target_ref); 750 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 751 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 752 @memcpy(&event_oid, revision.event_oid); 753 var repo_moment = try draft.core.latestMoment(); 754 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 755 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 756 defer event_object.deinit(); 757 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 758 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 759 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 760 } 761 762 // 763 // merge the selected revision into the target 764 // 765 766 try evt.Patch.update(.xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 767 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, repos_dir, &target, .{ 768 .id = patch_id_hex, 769 .revision = merge_revision, 770 .author = author, 771 .timestamp = 5, 772 })); 773 try evt.Patch.update(.xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 774 775 const selected_oid = switch (merge_revision) { 776 .squash => squash_oid, 777 .source => source_oid, 778 }; 779 try pch.mergeAndRemoveFork(repo_opts, io, allocator, repos_dir, &admin, &target, .{ 780 .id = patch_id_hex, 781 .revision = merge_revision, 782 .author = author, 783 .timestamp = 5, 784 }); 785 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 786 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 787 788 _ = arena.reset(.retain_capacity); 789 const merged_moment = try evt.currentMoment(repo_opts, &target); 790 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 791 try std.testing.expectEqual(.merged, merged.event.status); 792 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 793 794 _ = arena.reset(.retain_capacity); 795 const removed_fork = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, try evt.currentMoment(evt.admin_repo_opts, &admin), &arena, &patch_id)) orelse return error.NotFound; 796 try std.testing.expect(removed_fork.removed); 797 } 798 799 fn indexedForkCount( 800 moment: evt.AdminDB.HashMap(.read_only), 801 index_key: []const u8, 802 parent_id: []const u8, 803 ) !u64 { 804 const index_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, index_key)) orelse return 0; 805 const index = try evt.AdminDB.HashMap(.read_only).init(index_cursor); 806 const ids_cursor = try index.getCursor(hash.hashInt(evt.admin_repo_opts.hash, parent_id)) orelse return 0; 807 const ids = try evt.AdminDB.SortedSet(.read_only).init(ids_cursor); 808 return try ids.count(); 809 } 810 811 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.Status) !u64 { 812 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 813 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 814 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 815 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 816 return try ids.count(); 817 } 818 819 fn patchTagStatusCount(moment: Repo.DB.HashMap(.read_only), tag: []const u8, status: evt.Patch.Status) !u64 { 820 const tags_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return 0; 821 const tags = try Repo.DB.SortedMap(.read_only).init(tags_cursor); 822 var key_buffer: evt.Patch.TagStatusKey = undefined; 823 const ids_cursor = try tags.getCursor(try evt.Patch.tagStatusKey(&key_buffer, tag, status)) orelse return 0; 824 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 825 return try ids.count(); 826 } 827