╭ 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 target_ref: []const u8, 11 message: []const u8, 12 13 pub const Record = struct { 14 event: Self, 15 removed: bool = false, 16 author_email: ?[]const u8 = null, 17 created_order: u64 = 0, 18 event_oid: []const u8, 19 base_tree_oid: []const u8, 20 head_tree_oid: []const u8, 21 patch_oid: []const u8, 22 }; 23 24 const Self = @This(); 25 26 pub const merge_policy: evt.MergePolicy = .target_wins; 27 pub const record_map_key = "event-id->patchrev"; 28 pub const id_set_key = "patchrev-id-set"; 29 30 // create the mergeable squash commit represented by this patch revision 31 pub fn writeSquashCommit( 32 comptime repo_kind: rp.RepoKind, 33 comptime repo_opts: rp.RepoOpts(repo_kind), 34 state: rp.Repo(repo_kind, repo_opts).State(.read_write), 35 io: std.Io, 36 allocator: std.mem.Allocator, 37 event: Self, 38 head_tree_oid: *const [hash.hexLen(repo_opts.hash)]u8, 39 author: []const u8, 40 committer: []const u8, 41 timestamp: u64, 42 ) ![hash.hexLen(repo_opts.hash)]u8 { 43 var base_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 44 if (event.base_oid.len != base_oid.len) return error.InvalidPatch; 45 @memcpy(&base_oid, event.base_oid); 46 try validateOid(repo_opts.hash, &base_oid); 47 const parent_oids = [_][hash.hexLen(repo_opts.hash)]u8{base_oid}; 48 return try obj.writeCommitWithoutRef(repo_kind, repo_opts, state, io, allocator, .{ 49 .author = author, 50 .committer = committer, 51 .message = event.message, 52 .parent_oids = &parent_oids, 53 .timestamp = timestamp, 54 }, head_tree_oid); 55 } 56 57 pub fn readTrees( 58 comptime repo_kind: rp.RepoKind, 59 comptime repo_opts: rp.RepoOpts(repo_kind), 60 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 61 io: std.Io, 62 allocator: std.mem.Allocator, 63 tree_oid: *const [hash.hexLen(repo_opts.hash)]u8, 64 ) !struct { 65 base: [hash.hexLen(repo_opts.hash)]u8, 66 head: [hash.hexLen(repo_opts.hash)]u8, 67 } { 68 var tree_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, tree_oid); 69 defer tree_object.deinit(); 70 71 const entries = switch (tree_object.content) { 72 .tree => |tree| tree.entries, 73 else => return error.InvalidPatch, 74 }; 75 if (entries.count() != 2) return error.InvalidPatch; 76 const base = entries.get("base") orelse return error.InvalidPatch; 77 const head = entries.get("head") orelse return error.InvalidPatch; 78 if (!base.isTree() or !head.isTree()) return error.InvalidPatch; 79 80 const base_oid = std.fmt.bytesToHex(base.oid, .lower); 81 const head_oid = std.fmt.bytesToHex(head.oid, .lower); 82 var base_reader = try obj.ObjectReader(repo_kind, repo_opts).init(state, io, allocator, &base_oid); 83 defer base_reader.deinit(); 84 var head_reader = try obj.ObjectReader(repo_kind, repo_opts).init(state, io, allocator, &head_oid); 85 defer head_reader.deinit(); 86 if (base_reader.header().kind != .tree or head_reader.header().kind != .tree) return error.InvalidPatch; 87 88 return .{ .base = base_oid, .head = head_oid }; 89 } 90 91 pub fn consume( 92 comptime DB: type, 93 comptime hash_kind: hash.HashKind, 94 haxy_moment: DB.HashMap(.read_write), 95 event_id: *const [evt.event_id_size]u8, 96 record_maybe: ?Record, 97 arena: *std.heap.ArenaAllocator, 98 _: ?[]const u8, 99 ) !void { 100 const record_key = hash.hashInt(hash_kind, event_id); 101 const records = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, record_map_key))); 102 103 var existing_maybe: ?Record = null; 104 const existing_cursor_maybe = try records.getCursor(record_key); 105 if (existing_cursor_maybe) |cursor| { 106 existing_maybe = try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)); 107 } 108 109 var record = if (record_maybe) |value| 110 value 111 else blk: { 112 var value = existing_maybe orelse return error.EventNotFound; 113 value.removed = true; 114 break :blk value; 115 }; 116 117 try validateOid(hash_kind, record.event.base_oid); 118 try validateOid(hash_kind, record.event.source_oid); 119 try validateTarget(record.event.target_ref); 120 try validateOid(hash_kind, record.event_oid); 121 try validateOid(hash_kind, record.base_tree_oid); 122 try validateOid(hash_kind, record.head_tree_oid); 123 try validateOid(hash_kind, record.patch_oid); 124 125 if (existing_maybe) |existing| { 126 if (!std.mem.eql(u8, existing.event.base_oid, record.event.base_oid) or 127 !std.mem.eql(u8, existing.event.source_oid, record.event.source_oid) or 128 !std.mem.eql(u8, existing.event.target_ref, record.event.target_ref) or 129 !std.mem.eql(u8, existing.event.message, record.event.message) or 130 !std.mem.eql(u8, existing.base_tree_oid, record.base_tree_oid) or 131 !std.mem.eql(u8, existing.head_tree_oid, record.head_tree_oid) or 132 !std.mem.eql(u8, existing.patch_oid, record.patch_oid)) 133 { 134 return error.PatchRevChanged; 135 } 136 record.created_order = existing.created_order; 137 record.author_email = existing.author_email; 138 record.event_oid = existing.event_oid; 139 } 140 141 const cursor = try records.putCursor(record_key); 142 try evt.upsert(Record, DB, hash_kind, try DB.HashMap(.read_write).init(cursor), record); 143 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .patchrev, record.created_order, record.removed); 144 145 if (existing_cursor_maybe == null) { 146 const ids = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key))); 147 try ids.put(&evt.orderKeyDesc(record.created_order, event_id)); 148 } 149 } 150 151 pub fn validateOid(comptime hash_kind: hash.HashKind, oid: []const u8) !void { 152 var bytes: [hash.byteLen(hash_kind)]u8 = undefined; 153 _ = try std.fmt.hexToBytes(&bytes, oid); 154 if (!std.mem.eql(u8, oid, &std.fmt.bytesToHex(bytes, .lower))) return error.InvalidOid; 155 } 156 157 pub fn validateTarget(target_ref: []const u8) !void { 158 if (!std.mem.startsWith(u8, target_ref, "refs/heads/") or 159 !xit.ref.validateName(target_ref["refs/".len..])) return error.InvalidTarget; 160 } 161 162 pub fn readById( 163 comptime DB: type, 164 comptime hash_kind: hash.HashKind, 165 haxy_moment: DB.HashMap(.read_only), 166 arena: *std.heap.ArenaAllocator, 167 id: *const [evt.event_id_size]u8, 168 ) !?Record { 169 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 170 const records = try DB.HashMap(.read_only).init(records_cursor); 171 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 172 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 173 } 174 175 pub const WithId = struct { 176 id: [evt.event_id_size]u8, 177 record: Record, 178 }; 179 180 pub fn readNewest( 181 comptime DB: type, 182 comptime hash_kind: hash.HashKind, 183 haxy_moment: DB.HashMap(.read_only), 184 arena: *std.heap.ArenaAllocator, 185 ) !?WithId { 186 const ids_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, id_set_key)) orelse return null; 187 const ids = try DB.SortedSet(.read_only).init(ids_cursor); 188 var iter = try ids.iteratorFromIndex(0); 189 while (try iter.next()) |cursor| { 190 const id = try evt.readOrderKeyId(DB, cursor); 191 const record = (try readById(DB, hash_kind, haxy_moment, arena, &id)) orelse continue; 192 if (!record.removed) return .{ .id = id, .record = record }; 193 } 194 return null; 195 } 196 197 // squash commits referenced only by patch revision records 198 pub fn gcRoots( 199 comptime DB: type, 200 comptime hash_kind: hash.HashKind, 201 allocator: std.mem.Allocator, 202 haxy_moment: DB.HashMap(.read_only), 203 ) ![][hash.hexLen(hash_kind)]u8 { 204 var roots: std.ArrayList([hash.hexLen(hash_kind)]u8) = .empty; 205 errdefer roots.deinit(allocator); 206 var arena = std.heap.ArenaAllocator.init(allocator); 207 defer arena.deinit(); 208 209 const ids_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, id_set_key)) orelse return try roots.toOwnedSlice(allocator); 210 const ids = try DB.SortedSet(.read_only).init(ids_cursor); 211 var iter = try ids.iteratorFromIndex(0); 212 while (try iter.next()) |cursor| { 213 _ = arena.reset(.retain_capacity); 214 const id = try evt.readOrderKeyId(DB, cursor); 215 const record = (try readById(DB, hash_kind, haxy_moment, &arena, &id)) orelse continue; 216 var oid: [hash.hexLen(hash_kind)]u8 = undefined; 217 if (record.patch_oid.len != oid.len) return error.InvalidOid; 218 @memcpy(&oid, record.patch_oid); 219 try roots.append(allocator, oid); 220 } 221 222 return try roots.toOwnedSlice(allocator); 223 } 224