╭ Patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const diff3 = @import("../diff3.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const hash = xit.hash; 7 8 title: []const u8, 9 description: []const u8, 10 tags: []const u8, // space-separated 11 target_patch_id: ?[evt.event_id_size * 2]u8 = null, 12 revision: ?Revision = null, 13 status: Status = .open, 14 15 pub const Revision = struct { 16 id: [evt.event_id_size * 2]u8, 17 squash_oid: []const u8, 18 source_oid: []const u8, 19 target_ref: []const u8, 20 21 pub fn fromRecord(id: [evt.event_id_size]u8, record: evt.PatchRev.Record) Revision { 22 return .{ 23 .id = std.fmt.bytesToHex(id, .lower), 24 .squash_oid = record.patch_oid, 25 .source_oid = record.event.source_oid, 26 .target_ref = record.event.target_ref, 27 }; 28 } 29 30 pub fn matches(self: Revision, record: evt.PatchRev.Record) bool { 31 return !record.removed and 32 std.mem.eql(u8, record.patch_oid, self.squash_oid) and 33 std.mem.eql(u8, record.event.source_oid, self.source_oid) and 34 std.mem.eql(u8, record.event.target_ref, self.target_ref); 35 } 36 }; 37 38 pub const Record = struct { 39 event: Self, 40 removed: bool = false, 41 author_email: ?[]const u8 = null, 42 created_order: u64 = 0, 43 }; 44 45 const Self = @This(); 46 47 pub const Status = enum { 48 open, 49 closed, 50 merged, 51 52 const longest_len = blk: { 53 var len: usize = 0; 54 for (@typeInfo(Status).@"enum".fields) |field| len = @max(len, field.name.len); 55 break :blk len; 56 }; 57 }; 58 59 pub const Resolve = struct { 60 title: ?[]const u8 = null, 61 tags: ?[]const u8 = null, 62 hunks: []const []const u8 = &.{}, 63 theirs: []const u8 = "", 64 }; 65 66 pub const Update = union(enum) { 67 status: Status, 68 fields: struct { title: []const u8, tags: []const u8, description: []const u8 }, 69 resolve: Resolve, 70 }; 71 72 pub const tag_max_len = 64; 73 pub const merge_policy: evt.MergePolicy = .field_conflicts; 74 pub const record_map_key = "event-id->patch"; 75 pub const id_set_key = "patch-id-set"; 76 pub const conflicts_key = "conflicted-patch-id->conflict"; 77 pub const id_to_field_to_oid_key = "patch-id->field->oid"; 78 pub const target_patch_id_to_patch_id_set_key = "target-patch-id->patch-id-set"; 79 pub const status_to_id_set_key = "status->patch-id-set"; 80 pub const tag_status_to_id_set_key = "tag+status->patch-id-set"; 81 pub const revision_to_id_set_key = "target-ref+oid->patch-id-set"; 82 83 pub const TagStatusKey = [tag_max_len + 1 + Status.longest_len]u8; 84 85 pub fn tagStatusKey(buffer: *TagStatusKey, tag: []const u8, status: Status) ![]const u8 { 86 return std.fmt.bufPrint(buffer, "{s} {s}", .{ tag, @tagName(status) }); 87 } 88 89 pub fn tagIterator(tags: []const u8) std.mem.SplitIterator(u8, .scalar) { 90 return std.mem.splitScalar(u8, tags, ' '); 91 } 92 93 pub fn fieldsValid(title: []const u8, tags: []const u8) bool { 94 if (title.len == 0) return false; 95 var tag_iter = tagIterator(tags); 96 while (tag_iter.next()) |tag| { 97 if (tag.len > tag_max_len) return false; 98 } 99 return true; 100 } 101 102 pub fn consume( 103 comptime DB: type, 104 comptime hash_kind: hash.HashKind, 105 haxy_moment: DB.HashMap(.read_write), 106 event_id: *const [evt.event_id_size]u8, 107 record_maybe: ?Record, 108 arena: *std.heap.ArenaAllocator, 109 event_oid: ?[]const u8, 110 ) !void { 111 const record_key = hash.hashInt(hash_kind, event_id); 112 const records = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, record_map_key))); 113 const statuses = try DB.SortedMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, status_to_id_set_key))); 114 const tag_statuses = try DB.SortedMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, tag_status_to_id_set_key))); 115 const conflicts = try DB.SortedMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, conflicts_key))); 116 const field_oid_map = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, id_to_field_to_oid_key))); 117 118 var existing_maybe: ?Record = null; 119 const existing_cursor_maybe = try records.getCursor(record_key); 120 if (existing_cursor_maybe) |cursor| { 121 existing_maybe = try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)); 122 } 123 124 var record = if (record_maybe) |value| 125 value 126 else blk: { 127 var value = existing_maybe orelse return error.EventNotFound; 128 value.removed = true; 129 break :blk value; 130 }; 131 132 if (!fieldsValid(record.event.title, record.event.tags)) return error.InvalidPatch; 133 const revision_id = if (record.event.revision) |*revision| blk: { 134 try evt.PatchRev.validateOid(hash_kind, revision.squash_oid); 135 try evt.PatchRev.validateOid(hash_kind, revision.source_oid); 136 try evt.PatchRev.validateTarget(revision.target_ref); 137 break :blk try evt.parseEventId(&revision.id); 138 } else null; 139 const target_patch_id = if (record.event.target_patch_id) |*id| try evt.parseEventId(id) else null; 140 if (target_patch_id) |*id| { 141 if (std.mem.eql(u8, id, event_id)) return error.InvalidPatch; 142 } 143 144 if (existing_maybe) |existing| { 145 record.created_order = existing.created_order; 146 record.author_email = existing.author_email; 147 if (existing.event.status == .merged and record.event.status != .merged) return error.PatchAlreadyMerged; 148 if (event_oid != null) { 149 if (!std.meta.eql(existing.event.target_patch_id, record.event.target_patch_id)) { 150 return error.TargetPatchChanged; 151 } 152 } 153 } 154 155 if (record.event.status == .merged) { 156 const id = revision_id orelse return error.InvalidPatch; 157 const selected = record.event.revision orelse return error.InvalidPatch; 158 const revision = (try evt.PatchRev.readById(DB, hash_kind, haxy_moment.readOnly(), arena, &id)) orelse return error.InvalidPatch; 159 if (!selected.matches(revision)) return error.InvalidPatch; 160 } 161 162 const order_key = evt.orderKeyDesc(record.created_order, event_id); 163 if (existing_maybe) |existing| { 164 if (event_oid != null or record.removed) _ = try conflicts.remove(&order_key); 165 if (!existing.removed) { 166 const old_status = try statusSet(DB, statuses, existing.event.status); 167 _ = try old_status.remove(&order_key); 168 try removeFromTagSets(DB, tag_statuses, existing.event.tags, existing.event.status, &order_key); 169 if (existing.event.status != .merged) { 170 if (existing.event.revision) |revision| { 171 const revisions = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, revision_to_id_set_key))); 172 for ([_][]const u8{ revision.squash_oid, revision.source_oid }, 0..) |oid, i| { 173 if (i != 0 and std.mem.eql(u8, revision.squash_oid, revision.source_oid)) continue; 174 const key_hash = hash.hashInt(hash_kind, try revisionKey(arena.allocator(), revision.target_ref, oid)); 175 const ids = try DB.CountedHashSet(.read_write).init(try revisions.putCursor(key_hash)); 176 _ = try ids.remove(record_key); 177 if (try ids.count() == 0) _ = try revisions.remove(key_hash); 178 } 179 } 180 } 181 } 182 } 183 184 try evt.writeOid(Self, DB, hash_kind, field_oid_map, record_key, if (existing_maybe) |existing| existing.event else null, record.event, event_oid); 185 186 const cursor = try records.putCursor(record_key); 187 try evt.upsert(Record, DB, hash_kind, try DB.HashMap(.read_write).init(cursor), record); 188 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .patch, record.created_order, record.removed); 189 190 if (existing_cursor_maybe == null) { 191 const ids = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key))); 192 try ids.put(&order_key); 193 if (target_patch_id) |*target_id| { 194 const targets = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, target_patch_id_to_patch_id_set_key))); 195 const children = try DB.SortedSet(.read_write).init(try targets.putCursor(hash.hashInt(hash_kind, target_id))); 196 try children.put(&order_key); 197 } 198 } 199 200 if (!record.removed) { 201 const status = try statusSet(DB, statuses, record.event.status); 202 try status.put(&order_key); 203 204 var tag_iter = tagIterator(record.event.tags); 205 while (tag_iter.next()) |tag| { 206 if (tag.len == 0 or tag.len > tag_max_len) continue; 207 var key_buffer: TagStatusKey = undefined; 208 const set = try DB.SortedSet(.read_write).init(try tag_statuses.putCursor(try tagStatusKey(&key_buffer, tag, record.event.status))); 209 try set.put(&order_key); 210 } 211 212 if (record.event.status != .merged) { 213 if (record.event.revision) |revision| { 214 const revisions = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, revision_to_id_set_key))); 215 for ([_][]const u8{ revision.squash_oid, revision.source_oid }, 0..) |oid, i| { 216 if (i != 0 and std.mem.eql(u8, revision.squash_oid, revision.source_oid)) continue; 217 const key = try revisionKey(arena.allocator(), revision.target_ref, oid); 218 const ids = try DB.CountedHashSet(.read_write).init(try revisions.putCursor(hash.hashInt(hash_kind, key))); 219 try ids.put(record_key, .{ .bytes = event_id }); 220 } 221 } 222 } 223 } 224 } 225 226 pub fn resolveMerge( 227 target: Self, 228 parent: Self, 229 merged: *Self, 230 outcome: *[std.meta.fields(Self).len]evt.FieldMerge, 231 ) void { 232 const status_index = std.meta.fieldIndex(Self, "status") orelse @compileError("Patch.status not found"); 233 const revision_index = std.meta.fieldIndex(Self, "revision") orelse @compileError("Patch.revision not found"); 234 if (outcome[status_index] != .conflicted or outcome[revision_index] == .conflicted) return; 235 236 if (target.status == .merged and evt.fieldEqual(?Revision, target.revision, merged.revision)) { 237 merged.status = .merged; 238 outcome[status_index] = .kept; 239 } else if (parent.status == .merged and evt.fieldEqual(?Revision, parent.revision, merged.revision)) { 240 merged.status = .merged; 241 outcome[status_index] = .parent; 242 } 243 } 244 245 pub fn update( 246 comptime repo_kind: rp.RepoKind, 247 comptime repo_opts: rp.RepoOpts(repo_kind), 248 io: std.Io, 249 allocator: std.mem.Allocator, 250 repo: *rp.Repo(repo_kind, repo_opts), 251 id: *const [evt.event_id_size]u8, 252 change: Update, 253 author: evt.CommitAuthor, 254 ) !void { 255 var arena = std.heap.ArenaAllocator.init(allocator); 256 defer arena.deinit(); 257 const record = (try readFromRepo(repo_kind, repo_opts, io, allocator, &arena, repo, id)) orelse return error.NotFound; 258 if (record.removed) return error.NotFound; 259 260 var updated = record.event; 261 switch (change) { 262 .status => |status| { 263 if (updated.status == status) return; 264 updated.status = status; 265 }, 266 .fields => |fields| { 267 updated.title = fields.title; 268 updated.tags = fields.tags; 269 updated.description = fields.description; 270 }, 271 .resolve => |resolve| { 272 updated = try resolveFields(repo_kind, repo_opts, io, allocator, &arena, repo, id, record, resolve); 273 }, 274 } 275 if (!fieldsValid(updated.title, updated.tags)) return error.InvalidFields; 276 277 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{.{ 278 .id = std.fmt.bytesToHex(id.*, .lower), 279 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 280 .author = author, 281 .event = .{ .patch = updated }, 282 }}); 283 } 284 285 fn resolveFields( 286 comptime repo_kind: rp.RepoKind, 287 comptime repo_opts: rp.RepoOpts(repo_kind), 288 io: std.Io, 289 allocator: std.mem.Allocator, 290 arena: *std.heap.ArenaAllocator, 291 repo: *rp.Repo(repo_kind, repo_opts), 292 id: *const [evt.event_id_size]u8, 293 live: Record, 294 resolve: Resolve, 295 ) !Self { 296 const DB = evt.EventDB(repo_opts.hash); 297 var updated = live.event; 298 if (resolve.title) |title| updated.title = title; 299 if (resolve.tags) |tags| updated.tags = tags; 300 301 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 302 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 303 const moment = (if (event_db_maybe) |*event_db| 304 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 305 else if (repo_kind == .git) 306 return updated 307 else 308 evt.currentMoment(repo_opts, repo)) catch return updated; 309 const conflicts_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, conflicts_key))) orelse return updated; 310 const conflicts = try DB.SortedMap(.read_only).init(conflicts_cursor); 311 const conflict_cursor = (try conflicts.getCursor(&evt.orderKeyDesc(live.created_order, id))) orelse return updated; 312 const entry = try DB.HashMap(.read_only).init(conflict_cursor); 313 const fields_cursor = (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key))) orelse return updated; 314 const fields = try fields_cursor.readBytesAlloc(arena.allocator(), null); 315 const their_cursor = (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key))) orelse return updated; 316 const theirs = try evt.read(Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(their_cursor)); 317 318 var field_iter = std.mem.splitScalar(u8, fields, ' '); 319 while (field_iter.next()) |field| { 320 if (std.mem.eql(u8, field, "status") and fieldListed(resolve.theirs, ',', field)) updated.status = theirs.event.status; 321 if (std.mem.eql(u8, field, "revision") and fieldListed(resolve.theirs, ',', field)) updated.revision = theirs.event.revision; 322 } 323 324 if (!fieldListed(fields, ' ', "description")) return updated; 325 var base_description: []const u8 = ""; 326 if (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.base_record_key))) |base_cursor| { 327 const base = try evt.read(Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(base_cursor)); 328 base_description = base.event.description; 329 } 330 const chunks = try diff3.chunks(io, allocator, arena, base_description, live.event.description, theirs.event.description); 331 var resolutions: std.ArrayList([]const u8) = .empty; 332 var hunk_index: usize = 0; 333 for (chunks) |chunk| { 334 if (chunk != .conflict) continue; 335 try resolutions.append(arena.allocator(), if (hunk_index < resolve.hunks.len) resolve.hunks[hunk_index] else (chunk.conflict.ours orelse "")); 336 hunk_index += 1; 337 } 338 updated.description = try diff3.assemble(arena.allocator(), chunks, resolutions.items); 339 return updated; 340 } 341 342 fn fieldListed(fields: []const u8, delimiter: u8, name: []const u8) bool { 343 var iter = std.mem.splitScalar(u8, fields, delimiter); 344 while (iter.next()) |field| { 345 if (std.mem.eql(u8, field, name)) return true; 346 } 347 return false; 348 } 349 350 fn readFromRepo( 351 comptime repo_kind: rp.RepoKind, 352 comptime repo_opts: rp.RepoOpts(repo_kind), 353 io: std.Io, 354 allocator: std.mem.Allocator, 355 arena: *std.heap.ArenaAllocator, 356 repo: *rp.Repo(repo_kind, repo_opts), 357 id: *const [evt.event_id_size]u8, 358 ) !?Record { 359 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 360 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 361 const moment = (if (event_db_maybe) |*event_db| 362 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 363 else if (repo_kind == .git) 364 return null 365 else 366 evt.currentMoment(repo_opts, repo)) catch return null; 367 return readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, arena, id); 368 } 369 370 fn revisionKey(allocator: std.mem.Allocator, target_ref: []const u8, oid: []const u8) ![]u8 { 371 return std.mem.concat(allocator, u8, &.{ target_ref, "\x00", oid }); 372 } 373 374 fn statusSet( 375 comptime DB: type, 376 statuses: DB.SortedMap(.read_write), 377 status: Status, 378 ) !DB.SortedSet(.read_write) { 379 return DB.SortedSet(.read_write).init(try statuses.putCursor(@tagName(status))); 380 } 381 382 fn removeFromTagSets( 383 comptime DB: type, 384 tag_statuses: DB.SortedMap(.read_write), 385 tags: []const u8, 386 status: Status, 387 order_key: []const u8, 388 ) !void { 389 var tag_iter = tagIterator(tags); 390 while (tag_iter.next()) |tag| { 391 if (tag.len == 0 or tag.len > tag_max_len) continue; 392 var key_buffer: TagStatusKey = undefined; 393 const key = try tagStatusKey(&key_buffer, tag, status); 394 const set = try DB.SortedSet(.read_write).init(try tag_statuses.putCursor(key)); 395 _ = try set.remove(order_key); 396 if (0 == try set.count()) _ = try tag_statuses.remove(key); 397 } 398 } 399 400 pub fn readById( 401 comptime DB: type, 402 comptime hash_kind: hash.HashKind, 403 haxy_moment: DB.HashMap(.read_only), 404 arena: *std.heap.ArenaAllocator, 405 id: *const [evt.event_id_size]u8, 406 ) !?Record { 407 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 408 const records = try DB.HashMap(.read_only).init(records_cursor); 409 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 410 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 411 } 412