╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issue.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
239 // live ones, and a conflicted description is reassembled from the hunk 240 // resolutions against the conflict entry. the chunking is deterministic: the 241 // entry holds immutable slots and any event since would have removed it, so 242 // the hunks match what the resolve form was built from. a settled or 243 // unreadable entry keeps the live description. 244 fn resolveFields( 245 comptime repo_kind: rp.RepoKind, 246 comptime repo_opts: rp.RepoOpts(repo_kind), 247 io: std.Io, 248 allocator: std.mem.Allocator, 249 arena: *std.heap.ArenaAllocator, 250 repo: *rp.Repo(repo_kind, repo_opts), 251 id_bytes: *const [evt.event_id_size]u8, 252 live: Record, 253 resolve: Resolve, 254 ) !Self { 255 const DB = evt.EventDB(repo_opts.hash); 256 const aa = arena.allocator(); 257 258 var updated = live.event; 259 if (resolve.title) |title| updated.title = title; 260 if (resolve.labels) |labels| updated.labels = labels; 261 262 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; 263 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 264 const moment = (if (event_db_maybe) |*event_db| 265 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 266 else if (repo_kind == .git) 267 return updated 268 else 269 evt.currentMoment(repo_opts, repo)) catch return updated; 270 271 const conflicts_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, conflicts_key))) orelse return updated; 272 const conflicts = try DB.SortedMap(.read_only).init(conflicts_cursor); 273 const order_key = evt.orderKeyDesc(live.created_order, id_bytes); 274 const conflict_cursor = (try conflicts.getCursor(&order_key)) orelse return updated; 275 const entry = try DB.HashMap(.read_only).init(conflict_cursor); 276 277 const fields_cursor = (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key))) orelse return updated; 278 const conflicted_fields = try fields_cursor.readBytesAlloc(aa, null); 279 if (std.mem.indexOf(u8, conflicted_fields, "description") == null) return updated; 280 281 const their_cursor = (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key))) orelse return updated; 282 const theirs = try evt.read(Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(their_cursor)); 283 284 // absent when both sides created the issue independently 285 var base_description: []const u8 = ""; 286 if (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.base_record_key))) |base_cursor| { 287 const base = try evt.read(Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(base_cursor)); 288 base_description = base.event.description; 289 } 290 291 const chunks = try diff3.chunks(allocator, arena, base_description, live.event.description, theirs.event.description); 292 var resolutions: std.ArrayList([]const u8) = .empty; 293 var hunk_index: usize = 0; 294 for (chunks) |chunk| { 295 if (chunk != .conflict) continue; 296 try resolutions.append(aa, if (hunk_index < resolve.hunks.len) resolve.hunks[hunk_index] else (chunk.conflict.ours orelse "")); 297 hunk_index += 1; 298 } 299 updated.description = try diff3.assemble(aa, chunks, resolutions.items); 300 return updated; 301 } 302 303 // the issue `id_bytes` names in the repo's consumed event db (the repo's own 304 // db for xit; the side db next to a git repo), or null 305 fn readById( 306 comptime repo_kind: rp.RepoKind, 307 comptime repo_opts: rp.RepoOpts(repo_kind), 308 io: std.Io, 309 allocator: std.mem.Allocator, 310 arena: *std.heap.ArenaAllocator, 311 repo: *rp.Repo(repo_kind, repo_opts), 312 id_bytes: *const [evt.event_id_size]u8, 313 ) !?Record { 314 const DB = evt.EventDB(repo_opts.hash); 315 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; 316 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 317 const moment = (if (event_db_maybe) |*event_db| 318 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 319 else if (repo_kind == .git) 320 return null 321 else 322 evt.currentMoment(repo_opts, repo)) catch return null; 323 const event_id_to_issue_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, record_map_key))) orelse return null; 324 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 325 const issue_cursor = (try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) orelse return null; 326 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 327 return try evt.read(Record, DB, repo_opts.hash, arena, issue_map); 328 } 329 330 // remove an issue's order key from its labels' `status` sets, pruning entries 331 // whose set becomes empty 332 fn removeFromLabelSets( 333 comptime DB: type, 334 label_to_issues: DB.SortedMap(.read_write), 335 labels: []const u8, 336 status: Status, 337 order_key: []const u8, 338 ) !void { 339 var label_iter = labelIterator(labels); 340 while (label_iter.next()) |label| { 341 // skipped the same way consume skipped it, so it was never indexed 342 if (label.len == 0 or label.len > label_max_len) continue; 343 var key_buffer: LabelStatusKey = undefined; 344 const key = try labelStatusKey(&key_buffer, label, status); 345 const label_set_cursor = try label_to_issues.putCursor(key); 346 const label_set = try DB.SortedSet(.read_write).init(label_set_cursor); 347 _ = try label_set.remove(order_key); 348 if (0 == try label_set.count()) _ = try label_to_issues.remove(key); 349 } 350 } 351