╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issue.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
40 pub const merge_policy: evt.MergePolicy = .field_conflicts; 41 pub const record_map_key = "event-id->issue"; 42 pub const all_id_set_key = "issue-id-set"; 43 pub const conflicts_key = "conflicted-issue-id->conflict"; 44 pub const id_to_field_to_oid_key = "issue-id->field->oid"; 45 pub const status_to_id_set_key = "status->issue-id-set"; 46 pub const label_status_to_id_set_key = "label+status->issue-id-set"; 47 48 // a "label status" key names a label's per-status issue set (labels can't contain 49 // spaces, so the pair is unambiguous) 50 pub const LabelStatusKey = [label_max_len + 1 + Status.longest_len]u8; 51 52 pub fn labelStatusKey(buffer: *LabelStatusKey, label: []const u8, status: Status) ![]const u8 { 53 return std.fmt.bufPrint(buffer, "{s} {s}", .{ label, @tagName(status) }); 54 } 55 56 pub fn labelIterator(labels: []const u8) std.mem.SplitIterator(u8, .scalar) { 57 return std.mem.splitScalar(u8, labels, ' '); 58 } 59 60 // a title is required, and a too-long label would go unindexed once consumed 61 pub fn fieldsValid(title: []const u8, labels: []const u8) bool { 62 if (!evt.titleValid(title)) return false; 63 var label_iter = labelIterator(labels); 64 while (label_iter.next()) |label| { 65 if (label.len > label_max_len) return false; 66 } 67 return true; 68 } 69 70 pub fn consume( 71 comptime DB: type, 72 comptime hash_kind: hash.HashKind, 73 haxy_moment: DB.HashMap(.read_write), 74 event_id: *const [evt.event_id_size]u8, 75 record_maybe: ?Record, 76 arena: *std.heap.ArenaAllocator, 77 // the commit this event came from, recorded against every field it 78 // changes. null from `merge`, which sets oids and conflicts itself. 79 event_oid: ?[]const u8, 80 ) !void { 81 const issue_key = hash.hashInt(hash_kind, event_id); 82 83 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, record_map_key)); 84 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 85 86 // the per-status sets the issues views list, ordered by creation, 87 // keyed by status name 88 const status_to_issues_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, status_to_id_set_key)); 89 const status_to_issues = try DB.SortedMap(.read_write).init(status_to_issues_cursor); 90 91 // the per-label, per-status sets, keyed "label,status" 92 const label_to_issues_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, label_status_to_id_set_key)); 93 const label_to_issues = try DB.SortedMap(.read_write).init(label_to_issues_cursor); 94 95 const conflicts_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, conflicts_key)); 96 const conflicts = try DB.SortedMap(.read_write).init(conflicts_cursor); 97 98 const issue_id_to_field_to_oid_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_to_field_to_oid_key)); 99 const issue_id_to_field_to_oid = try DB.HashMap(.read_write).init(issue_id_to_field_to_oid_cursor); 100 101 var existing_record_maybe: ?Record = null; 102 const existing_cursor_maybe = try event_id_to_issue.getCursor(issue_key); 103 if (existing_cursor_maybe) |existing_cursor| { 104 const existing_issue = try DB.HashMap(.read_only).init(existing_cursor); 105 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_issue); 106 } 107 108 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 109 110 if (existing_record_maybe) |existing_record| { 111 // updates preserve the original creation order and author 112 record_to_write.created_order = existing_record.created_order; 113 record_to_write.author_email = existing_record.author_email; 114 record_to_write.created_timestamp = existing_record.created_timestamp; 115 116 // drop the old status's and labels' entries; active values are re-added below 117 const order_key = evt.orderKeyDesc(existing_record.created_order, event_id); 118 if (!existing_record.removed) { 119 const status_set = try statusSet(DB, status_to_issues, existing_record.event.status); 120 _ = try status_set.remove(&order_key); 121 try removeFromLabelSets(DB, label_to_issues, existing_record.event.labels, existing_record.event.status, &order_key); 122 } 123 124 // any event settles the conflict, since resolving in the ui may 125 // keep our own values and so change nothing to detect 126 if (event_oid != null or record_to_write.removed) _ = try conflicts.remove(&order_key); 127 } 128 129 try evt.writeOid(Self, DB, hash_kind, issue_id_to_field_to_oid, issue_key, if (existing_record_maybe) |existing| existing.event else null, record_to_write.event, event_oid); 130 131 const issue_cursor = try event_id_to_issue.putCursor(issue_key); 132 const issue = try DB.HashMap(.read_write).init(issue_cursor); 133 try evt.upsert(Record, DB, hash_kind, issue, record_to_write); 134 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .issue, existing_record_maybe, record_to_write); 135 136 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 137 138 // the id set retains removed records so merges can carry removals 139 if (existing_cursor_maybe == null) { 140 const issue_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 141 const issue_id_set = try DB.SortedSet(.read_write).init(issue_id_set_cursor); 142 try issue_id_set.put(&order_key); 143 } 144 145 // the search index is keyed by the same order key as the sets above 146 try srch_thrd.update(DB, hash_kind, .issue, haxy_moment, arena.child_allocator, &order_key, existing_record_maybe, record_to_write); 147 148 if (!record_to_write.removed) { 149 const status_set = try statusSet(DB, status_to_issues, record_to_write.event.status); 150 try status_set.put(&order_key); 151 152 var label_iter = labelIterator(record_to_write.event.labels); 153 while (label_iter.next()) |label| { 154 // an over-long label goes unindexed rather than failing the consume, 155 // which would wedge the branch it arrived on 156 if (label.len == 0 or label.len > label_max_len) continue; 157 var key_buffer: LabelStatusKey = undefined; 158 const label_set_cursor = try label_to_issues.putCursor(try labelStatusKey(&key_buffer, label, record_to_write.event.status)); 159 const label_set = try DB.SortedSet(.read_write).init(label_set_cursor); 160 try label_set.put(&order_key); 161 } 162 } 163 } 164 165 // `status`'s sorted set within `statuses`, keyed by status name 166 fn statusSet( 167 comptime DB: type, 168 statuses: DB.SortedMap(.read_write), 169 status: Status, 170 ) !DB.SortedSet(.read_write) { 171 const cursor = try statuses.putCursor(@tagName(status)); 172 return DB.SortedSet(.read_write).init(cursor); 173 } 174 175 // the resolve form's inputs: null fields keep the live values, `hunks` holds 176 // the description conflict hunks' resolutions in chunk order (absent 177 // trailing ones keep our side) 178 pub const Resolve = struct { 179 title: ?[]const u8 = null, 180 labels: ?[]const u8 = null, 181 hunks: []const []const u8 = &.{}, 182 }; 183 184 // what an update replaces on an issue: its status, its content, or — for a 185 // conflicted issue — the resolution of its conflict 186 pub const Update = union(enum) { 187 status: Status, 188 fields: struct { title: []const u8, labels: []const u8, description: []const u8 }, 189 resolve: Resolve, 190 }; 191 192 // re-emit the issue `id_bytes` names with `change` applied, preserving the 193 // rest of its current state. error.NotFound when the id names no issue, 194 // error.InvalidFields when a resolve composes invalid fields. `repo` must be 195 // writable. 196 pub fn update( 197 host: evt.Host, 198 comptime repo_kind: rp.RepoKind, 199 comptime repo_opts: rp.RepoOpts(repo_kind), 200 io: std.Io, 201 allocator: std.mem.Allocator, 202 repo: *rp.Repo(repo_kind, repo_opts), 203 id_bytes: *const [evt.event_id_size]u8, 204 change: Update, 205 author: evt.CommitAuthor, 206 ) !void { 207 var arena = std.heap.ArenaAllocator.init(allocator); 208 defer arena.deinit(); 209 210 const issue = (try readById(repo_kind, repo_opts, io, allocator, &arena, repo, id_bytes)) orelse return error.NotFound; 211 if (issue.removed) return error.NotFound; 212 213 var updated = issue.event; 214 switch (change) { 215 .status => |status| { 216 if (updated.status == status) return; 217 updated.status = status; 218 }, 219 .fields => |fields| { 220 updated.title = fields.title; 221 updated.labels = fields.labels; 222 updated.description = fields.description; 223 }, 224 .resolve => |resolve| { 225 updated = try resolveFields(repo_kind, repo_opts, io, allocator, &arena, repo, id_bytes, issue, resolve); 226 if (!fieldsValid(updated.title, updated.labels)) return error.InvalidFields; 227 }, 228 } 229 230 try evt.consume(host, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 231 .id = std.fmt.bytesToHex(id_bytes.*, .lower), 232 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 233 .author = author, 234 .event = .{ .issue = updated }, 235 }}); 236 } 237 238 // the live fields with `resolve` applied: submitted title/labels stand in for the 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