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