╭ 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 updated_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 commit_count: ?u64 = null, 23 }; 24 25 const Self = @This(); 26 27 pub const merge_policy: evt.MergePolicy = .target_wins; 28 pub const record_map_key = "event-id->patchrev"; 29 pub const all_id_set_key = "patchrev-id-set"; 30 31 const commit_count_limit = 256; 32 33 // prepare a revision event and its patch reference in the caller's arena 34 pub fn prepare( 35 comptime repo_kind: rp.RepoKind, 36 comptime repo_opts: rp.RepoOpts(repo_kind), 37 state: rp.Repo(repo_kind, repo_opts).State(.read_write), 38 io: std.Io, 39 arena: *std.heap.ArenaAllocator, 40 base_oid: *const [hash.hexLen(repo_opts.hash)]u8, 41 source_oid: *const [hash.hexLen(repo_opts.hash)]u8, 42 message: []const u8, 43 author: evt.CommitAuthor, 44 timestamp: u64, 45 ) !struct { event: evt.EventWithId, revision: evt.Patch.Revision } { 46 const allocator = arena.allocator(); 47 var base_object = try obj.Object(repo_kind, repo_opts).initCommit(state.readOnly(), io, allocator, base_oid); 48 defer base_object.deinit(); 49 var source_object = try obj.Object(repo_kind, repo_opts).initCommit(state.readOnly(), io, allocator, source_oid); 50 defer source_object.deinit(); 51 const revision = Self{ .base_oid = try allocator.dupe(u8, base_oid), .source_oid = try allocator.dupe(u8, source_oid), .message = message }; 52 const identity = try std.fmt.allocPrint(allocator, "{s} <{s}>", .{ author.name, author.email }); 53 defer allocator.free(identity); 54 const squash = try writeSquashCommit(repo_kind, repo_opts, state, io, allocator, revision, &source_object.content.commit.tree, identity, identity, timestamp); 55 var id: [evt.event_id_size]u8 = undefined; 56 io.random(&id); 57 const id_hex = std.fmt.bytesToHex(id, .lower); 58 const entries = try allocator.alloc(evt.EventTreeEntry, 2); 59 entries[0] = .{ .tree = .{ .name = "base", .oid = try allocator.dupe(u8, &base_object.content.commit.tree) } }; 60 entries[1] = .{ .tree = .{ .name = "head", .oid = try allocator.dupe(u8, &source_object.content.commit.tree) } }; 61 return .{ 62 .event = .{ .id = id_hex, .author = author, .timestamp = timestamp, .tree_entries = entries, .event = .{ .patchrev = revision } }, 63 .revision = .{ 64 .id = id_hex, 65 .source_oid = revision.source_oid, 66 .squash_oid = try allocator.dupe(u8, &squash), 67 .commit_count = commitCount(repo_kind, repo_opts, state.readOnly(), io, allocator, base_oid, source_oid) catch null, 68 }, 69 }; 70 } 71 72 // only show the depth difference if a short first-parent walk reaches the base 73 pub fn commitCount( 74 comptime repo_kind: rp.RepoKind, 75 comptime repo_opts: rp.RepoOpts(repo_kind), 76 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 77 io: std.Io, 78 allocator: std.mem.Allocator, 79 base_oid: []const u8, 80 source_oid: []const u8, 81 ) !?u64 { 82 switch (repo_kind) { 83 .git => return null, 84 .xit => { 85 const oid_len = comptime hash.hexLen(repo_opts.hash); 86 if (base_oid.len != oid_len or source_oid.len != oid_len) return error.InvalidOid; 87 const base_count = try state.commitCount(io, allocator, .{ .oid = base_oid[0..oid_len] }); 88 const source_count = try state.commitCount(io, allocator, .{ .oid = source_oid[0..oid_len] }); 89 const count = std.math.sub(u64, source_count, base_count) catch return null; 90 if (count > commit_count_limit) return null; 91 92 // depth subtraction is only valid if the base is on the source's first-parent chain 93 var oid = source_oid[0..oid_len].*; 94 var remaining = count; 95 while (remaining > 0) : (remaining -= 1) { 96 var object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &oid); 97 defer object.deinit(); 98 const parent = object.content.commit.metadata.firstParent() orelse return null; 99 oid = parent.*; 100 } 101 return if (std.mem.eql(u8, &oid, base_oid)) count else null; 102 }, 103 } 104 } 105 106 // create the mergeable squash commit represented by this patch revision 107 pub fn writeSquashCommit( 108 comptime repo_kind: rp.RepoKind, 109 comptime repo_opts: rp.RepoOpts(repo_kind), 110 state: rp.Repo(repo_kind, repo_opts).State(.read_write), 111 io: std.Io, 112 allocator: std.mem.Allocator, 113 event: Self, 114 head_tree_oid: *const [hash.hexLen(repo_opts.hash)]u8, 115 author: []const u8, 116 committer: []const u8, 117 timestamp: u64, 118 ) ![hash.hexLen(repo_opts.hash)]u8 { 119 var base_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 120 if (event.base_oid.len != base_oid.len) return error.InvalidPatch; 121 @memcpy(&base_oid, event.base_oid); 122 try validateOid(repo_opts.hash, &base_oid); 123 const parent_oids = [_][hash.hexLen(repo_opts.hash)]u8{base_oid}; 124 return try obj.writeCommitWithoutRef(repo_kind, repo_opts, state, io, allocator, .{ 125 .author = author, 126 .committer = committer, 127 .message = event.message, 128 .parent_oids = &parent_oids, 129 .timestamp = timestamp, 130 }, head_tree_oid); 131 } 132 133 pub fn readTrees( 134 comptime repo_kind: rp.RepoKind, 135 comptime repo_opts: rp.RepoOpts(repo_kind), 136 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 137 io: std.Io, 138 allocator: std.mem.Allocator, 139 tree_oid: *const [hash.hexLen(repo_opts.hash)]u8, 140 ) !struct { 141 base: [hash.hexLen(repo_opts.hash)]u8, 142 head: [hash.hexLen(repo_opts.hash)]u8, 143 } { 144 var tree_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, tree_oid); 145 defer tree_object.deinit(); 146 147 const entries = switch (tree_object.content) { 148 .tree => |tree| tree.entries, 149 else => return error.InvalidPatch, 150 }; 151 if (entries.count() != 2) return error.InvalidPatch; 152 const base = entries.get("base") orelse return error.InvalidPatch; 153 const head = entries.get("head") orelse return error.InvalidPatch; 154 if (!base.isTree() or !head.isTree()) return error.InvalidPatch; 155 156 const base_oid = std.fmt.bytesToHex(base.oid, .lower); 157 const head_oid = std.fmt.bytesToHex(head.oid, .lower); 158 var base_reader = try obj.ObjectReader(repo_kind, repo_opts).init(state, io, allocator, &base_oid); 159 defer base_reader.deinit(); 160 var head_reader = try obj.ObjectReader(repo_kind, repo_opts).init(state, io, allocator, &head_oid); 161 defer head_reader.deinit(); 162 if (base_reader.header().kind != .tree or head_reader.header().kind != .tree) return error.InvalidPatch; 163 164 return .{ .base = base_oid, .head = head_oid }; 165 } 166 167 pub fn consume( 168 comptime DB: type, 169 comptime hash_kind: hash.HashKind, 170 haxy_moment: DB.HashMap(.read_write), 171 event_id: *const [evt.event_id_size]u8, 172 record_maybe: ?Record, 173 arena: *std.heap.ArenaAllocator, 174 _: ?[]const u8, 175 ) !void { 176 const record_key = hash.hashInt(hash_kind, event_id); 177 const records = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, record_map_key))); 178 179 var existing_maybe: ?Record = null; 180 const existing_cursor_maybe = try records.getCursor(record_key); 181 if (existing_cursor_maybe) |cursor| { 182 existing_maybe = try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)); 183 } 184 185 var record = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_maybe); 186 187 try validateOid(hash_kind, record.event.base_oid); 188 try validateOid(hash_kind, record.event.source_oid); 189 try validateOid(hash_kind, record.event_oid); 190 try validateOid(hash_kind, record.base_tree_oid); 191 try validateOid(hash_kind, record.head_tree_oid); 192 try validateOid(hash_kind, record.patch_oid); 193 194 if (existing_maybe) |existing| { 195 if (!std.mem.eql(u8, existing.event.base_oid, record.event.base_oid) or 196 !std.mem.eql(u8, existing.event.source_oid, record.event.source_oid) or 197 !std.mem.eql(u8, existing.event.message, record.event.message) or 198 !std.mem.eql(u8, existing.base_tree_oid, record.base_tree_oid) or 199 !std.mem.eql(u8, existing.head_tree_oid, record.head_tree_oid) or 200 !std.mem.eql(u8, existing.patch_oid, record.patch_oid)) 201 { 202 return error.PatchRevChanged; 203 } 204 record.created_order = existing.created_order; 205 record.author_email = existing.author_email; 206 record.event_oid = existing.event_oid; 207 } 208 209 const cursor = try records.putCursor(record_key); 210 try evt.upsert(Record, DB, hash_kind, try DB.HashMap(.read_write).init(cursor), record); 211 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .patchrev, existing_maybe, record); 212 213 if (existing_cursor_maybe == null) { 214 const ids = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key))); 215 try ids.put(&evt.orderKeyDesc(record.created_order, event_id)); 216 } 217 } 218 219 pub fn validateOid(comptime hash_kind: hash.HashKind, oid: []const u8) !void { 220 if (oid.len != hash.hexLen(hash_kind)) return error.InvalidOid; 221 var bytes: [hash.byteLen(hash_kind)]u8 = undefined; 222 _ = try std.fmt.hexToBytes(&bytes, oid); 223 if (!std.mem.eql(u8, oid, &std.fmt.bytesToHex(bytes, .lower))) return error.InvalidOid; 224 } 225 226 pub fn readById( 227 comptime DB: type, 228 comptime hash_kind: hash.HashKind, 229 haxy_moment: DB.HashMap(.read_only), 230 arena: *std.heap.ArenaAllocator, 231 id: *const [evt.event_id_size]u8, 232 ) !?Record { 233 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 234 const records = try DB.HashMap(.read_only).init(records_cursor); 235 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 236 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 237 } 238 239 pub const WithId = struct { 240 id: [evt.event_id_size]u8, 241 record: Record, 242 }; 243 244 pub fn readNewest( 245 comptime DB: type, 246 comptime hash_kind: hash.HashKind, 247 haxy_moment: DB.HashMap(.read_only), 248 arena: *std.heap.ArenaAllocator, 249 ) !?WithId { 250 const ids_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, all_id_set_key)) orelse return null; 251 const ids = try DB.SortedSet(.read_only).init(ids_cursor); 252 var iter = try ids.iteratorFromIndex(0); 253 while (try iter.next()) |cursor| { 254 const id = try evt.readOrderKeyId(DB, cursor); 255 const record = (try readById(DB, hash_kind, haxy_moment, arena, &id)) orelse continue; 256 if (!record.removed) return .{ .id = id, .record = record }; 257 } 258 return null; 259 } 260 261 // squash commits referenced only by patch revision records 262 pub fn gcRoots( 263 comptime DB: type, 264 comptime hash_kind: hash.HashKind, 265 allocator: std.mem.Allocator, 266 haxy_moment: DB.HashMap(.read_only), 267 ) ![][hash.hexLen(hash_kind)]u8 { 268 var roots: std.ArrayList([hash.hexLen(hash_kind)]u8) = .empty; 269 errdefer roots.deinit(allocator); 270 var arena = std.heap.ArenaAllocator.init(allocator); 271 defer arena.deinit(); 272 273 const ids_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, all_id_set_key)) orelse return try roots.toOwnedSlice(allocator); 274 const ids = try DB.SortedSet(.read_only).init(ids_cursor); 275 var iter = try ids.iteratorFromIndex(0); 276 while (try iter.next()) |cursor| { 277 _ = arena.reset(.retain_capacity); 278 const id = try evt.readOrderKeyId(DB, cursor); 279 const record = (try readById(DB, hash_kind, haxy_moment, &arena, &id)) orelse continue; 280 var oid: [hash.hexLen(hash_kind)]u8 = undefined; 281 if (record.patch_oid.len != oid.len) return error.InvalidOid; 282 @memcpy(&oid, record.patch_oid); 283 try roots.append(allocator, oid); 284 } 285 286 return try roots.toOwnedSlice(allocator); 287 } 288