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