╭ PatchRev.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const xit = @import("xit"); 4 const hash = xit.hash; 5 const obj = xit.object; 6 const rp = xit.repo; 7 8 base_oid: []const u8, 9 source_oid: []const u8, 10 message: []const u8, 11 12 pub const Record = struct { 13 event: Self, 14 removed: bool = false, 15 author_email: ?[]const u8 = null, 16 created_order: u64 = 0, 17 base_tree_oid: []const u8, 18 head_tree_oid: []const u8, 19 patch_oid: []const u8, 20 }; 21 22 const Self = @This(); 23 24 pub const merge_policy: evt.MergePolicy = .target_wins; 25 pub const record_map_key = "event-id->patchrev"; 26 pub const id_set_key = "patchrev-id-set"; 27 28 pub fn readTrees( 29 comptime repo_kind: rp.RepoKind, 30 comptime repo_opts: rp.RepoOpts(repo_kind), 31 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 32 io: std.Io, 33 allocator: std.mem.Allocator, 34 tree_oid: *const [hash.hexLen(repo_opts.hash)]u8, 35 ) !struct { 36 base: [hash.hexLen(repo_opts.hash)]u8, 37 head: [hash.hexLen(repo_opts.hash)]u8, 38 } { 39 var tree_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, tree_oid); 40 defer tree_object.deinit(); 41 42 const entries = switch (tree_object.content) { 43 .tree => |tree| tree.entries, 44 else => return error.InvalidPatch, 45 }; 46 if (entries.count() != 2) return error.InvalidPatch; 47 const base = entries.get("base") orelse return error.InvalidPatch; 48 const head = entries.get("head") orelse return error.InvalidPatch; 49 if (!base.isTree() or !head.isTree()) return error.InvalidPatch; 50 51 const base_oid = std.fmt.bytesToHex(base.oid, .lower); 52 const head_oid = std.fmt.bytesToHex(head.oid, .lower); 53 var base_reader = try obj.ObjectReader(repo_kind, repo_opts).init(state, io, allocator, &base_oid); 54 defer base_reader.deinit(); 55 var head_reader = try obj.ObjectReader(repo_kind, repo_opts).init(state, io, allocator, &head_oid); 56 defer head_reader.deinit(); 57 if (base_reader.header().kind != .tree or head_reader.header().kind != .tree) return error.InvalidPatch; 58 59 return .{ .base = base_oid, .head = head_oid }; 60 } 61 62 pub fn consume( 63 comptime DB: type, 64 comptime hash_kind: hash.HashKind, 65 haxy_moment: DB.HashMap(.read_write), 66 event_id: *const [evt.event_id_size]u8, 67 record_maybe: ?Record, 68 arena: *std.heap.ArenaAllocator, 69 _: ?[]const u8, 70 ) !void { 71 const record_key = hash.hashInt(hash_kind, event_id); 72 const records = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, record_map_key))); 73 74 var existing_maybe: ?Record = null; 75 const existing_cursor_maybe = try records.getCursor(record_key); 76 if (existing_cursor_maybe) |cursor| { 77 existing_maybe = try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)); 78 } 79 80 var record = if (record_maybe) |value| 81 value 82 else blk: { 83 var value = existing_maybe orelse return error.EventNotFound; 84 value.removed = true; 85 break :blk value; 86 }; 87 88 try validateOid(hash_kind, record.event.base_oid); 89 try validateOid(hash_kind, record.event.source_oid); 90 try validateOid(hash_kind, record.base_tree_oid); 91 try validateOid(hash_kind, record.head_tree_oid); 92 try validateOid(hash_kind, record.patch_oid); 93 94 if (existing_maybe) |existing| { 95 if (!std.mem.eql(u8, existing.event.base_oid, record.event.base_oid) or 96 !std.mem.eql(u8, existing.event.source_oid, record.event.source_oid) or 97 !std.mem.eql(u8, existing.event.message, record.event.message) or 98 !std.mem.eql(u8, existing.base_tree_oid, record.base_tree_oid) or 99 !std.mem.eql(u8, existing.head_tree_oid, record.head_tree_oid) or 100 !std.mem.eql(u8, existing.patch_oid, record.patch_oid)) 101 { 102 return error.PatchRevChanged; 103 } 104 record.created_order = existing.created_order; 105 record.author_email = existing.author_email; 106 } 107 108 const cursor = try records.putCursor(record_key); 109 try evt.upsert(Record, DB, hash_kind, try DB.HashMap(.read_write).init(cursor), record); 110 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .patchrev, record.created_order, record.removed); 111 112 if (existing_cursor_maybe == null) { 113 const ids = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key))); 114 try ids.put(&evt.orderKeyDesc(record.created_order, event_id)); 115 } 116 } 117 118 fn validateOid(comptime hash_kind: hash.HashKind, oid: []const u8) !void { 119 var bytes: [hash.byteLen(hash_kind)]u8 = undefined; 120 _ = try std.fmt.hexToBytes(&bytes, oid); 121 if (!std.mem.eql(u8, oid, &std.fmt.bytesToHex(bytes, .lower))) return error.InvalidOid; 122 } 123 124 pub fn readById( 125 comptime DB: type, 126 comptime hash_kind: hash.HashKind, 127 haxy_moment: DB.HashMap(.read_only), 128 arena: *std.heap.ArenaAllocator, 129 id: *const [evt.event_id_size]u8, 130 ) !?Record { 131 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 132 const records = try DB.HashMap(.read_only).init(records_cursor); 133 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 134 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 135 } 136 137 // derived commits referenced only by patch revision records 138 pub fn gcRoots( 139 comptime DB: type, 140 comptime hash_kind: hash.HashKind, 141 allocator: std.mem.Allocator, 142 haxy_moment: DB.HashMap(.read_only), 143 ) ![][hash.hexLen(hash_kind)]u8 { 144 var roots: std.ArrayList([hash.hexLen(hash_kind)]u8) = .empty; 145 errdefer roots.deinit(allocator); 146 var arena = std.heap.ArenaAllocator.init(allocator); 147 defer arena.deinit(); 148 149 const ids_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, id_set_key)) orelse return try roots.toOwnedSlice(allocator); 150 const ids = try DB.SortedSet(.read_only).init(ids_cursor); 151 var iter = try ids.iteratorFromIndex(0); 152 while (try iter.next()) |cursor| { 153 _ = arena.reset(.retain_capacity); 154 const id = try evt.readOrderKeyId(DB, cursor); 155 const record = (try readById(DB, hash_kind, haxy_moment, &arena, &id)) orelse continue; 156 var oid: [hash.hexLen(hash_kind)]u8 = undefined; 157 if (record.patch_oid.len != oid.len) return error.InvalidOid; 158 @memcpy(&oid, record.patch_oid); 159 try roots.append(allocator, oid); 160 } 161 162 return try roots.toOwnedSlice(allocator); 163 } 164