╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
168 !session.data.is_local and 169 session.data.user_id != null; 170 } 171 172 pub fn createDraft( 173 data: *const Self, 174 session: *ui.Session, 175 allocator: std.mem.Allocator, 176 title: []const u8, 177 tags: []const u8, 178 description: []const u8, 179 target_branch: []const u8, 180 ) ![evt.event_id_size * 2]u8 { 181 if (!evt.Patch.fieldsValid(title, tags)) return error.InvalidFields; 182 const io = session.io orelse return error.NotFound; 183 const repos_dir = session.repos_dir orelse return error.NotFound; 184 const admin_repo = session.admin_repo orelse return error.NotFound; 185 const user_id = session.userId() orelse return error.NotFound; 186 const repo_id = data.repo_id orelse return error.NotFound; 187 const repo_source = data.repo_source orelse return error.NotFound; 188 const author = (try session.eventAuthor()) orelse return error.NotFound; 189 if (!try repo_source.hasBranch(io, allocator, target_branch)) return error.InvalidFields; 190 var id_bytes: [evt.event_id_size]u8 = undefined; 191 io.random(&id_bytes); 192 const id = std.fmt.bytesToHex(id_bytes, .lower); 193 const fork_path = try fork.create(.{}, io, allocator, repos_dir, admin_repo, .{ 194 .id = id, 195 .user_id = user_id, 196 .repo_id = repo_id, 197 .title = title, 198 .description = description, 199 .tags = tags, 200 .target_branch = target_branch, 201 .author = author, 202 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 203 }); 204 allocator.free(fork_path); 205 return id; 206 } 207 208 pub fn publishDraft(data: *const Self, session: *ui.Session, allocator: std.mem.Allocator, id: []const u8) !void { 209 const io = session.io orelse return error.NotFound; 210 const repos_dir = session.repos_dir orelse return error.NotFound; 211 const admin_repo = session.admin_repo orelse return error.NotFound; 212 const repo_source = data.repo_source orelse return error.NotFound; 213 const repo_id = data.repo_id orelse return error.NotFound; 214 const user_id = session.userId() orelse return error.NotFound; 215 const patch_id = evt.parseEventId(id) catch return error.NotFound; 216 const author = (try session.eventAuthor()) orelse return error.NotFound; 217 218 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 219 const fork_path = try fork.forkPath(allocator, repos_dir, &id_hex); 220 defer allocator.free(fork_path); 221 222 var target_repo = try rp.Repo(.xit, .{}).open(io, allocator, repo_source.localInitOpts()); 223 defer target_repo.deinit(io, allocator); 224 try pch.publish(.{}, io, allocator, admin_repo, &target_repo, fork_path, .{ 225 .id = id_hex, 226 .user_id = user_id, 227 .repo_id = repo_id, 228 .author = author, 229 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 230 }); 231 } 232 233 pub fn mergePatch(data: *const Self, session: *ui.Session, allocator: std.mem.Allocator, id: []const u8, squash: bool) !void { 234 const io = session.io orelse return error.NotFound; 235 const repos_dir = session.repos_dir orelse return error.NotFound; 236 const admin_repo = session.admin_repo orelse return error.NotFound; 237 const repo_source = data.repo_source orelse return error.NotFound; 238 if (repo_source.repo_kind != .xit) return error.NotFound; 239 const patch_id = evt.parseEventId(id) catch return error.NotFound; 240 const author = (try session.eventAuthor()) orelse return error.NotFound; 241 242 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 243 var target_repo = try rp.Repo(.xit, .{}).open(io, allocator, repo_source.localInitOpts()); 244 defer target_repo.deinit(io, allocator); 245 try pch.mergeAndRemoveFork(.{}, io, allocator, repos_dir, admin_repo, &target_repo, .{ 246 .id = id_hex, 247 .revision = if (squash) .squash else .source, 248 .author = author, 249 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 250 }); 251 } 252 253 pub fn editDraft( 254 data: *const Self, 255 session: *ui.Session, 256 allocator: std.mem.Allocator, 257 id: []const u8, 258 title: []const u8, 259 tags: []const u8, 260 description: []const u8, 261 target_branch: []const u8, 262 ) !void { 263 const io = session.io orelse return error.NotFound; 264 const repos_dir = session.repos_dir orelse return error.NotFound; 265 const admin_repo = session.admin_repo orelse return error.NotFound; 266 const repo_id = data.repo_id orelse return error.NotFound; 267 const repo_source = data.repo_source orelse return error.NotFound; 268 const user_id = session.userId() orelse return error.NotFound; 269 const patch_id = evt.parseEventId(id) catch return error.NotFound; 270 const author = (try session.eventAuthor()) orelse return error.NotFound; 271 if (!try repo_source.hasBranch(io, allocator, target_branch)) return error.InvalidFields; 272 273 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 274 const fork_path = try fork.forkPath(allocator, repos_dir, &id_hex); 275 defer allocator.free(fork_path); 276 if (!try pch.editDraft(.{}, io, allocator, admin_repo, fork_path, .{ 277 .id = id_hex, 278 .user_id = user_id, 279 .repo_id = repo_id, 280 .title = title, 281 .tags = tags, 282 .description = description, 283 .target_branch = target_branch, 284 .author = author, 285 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 286 })) return error.NotFound; 287 } 288 289 pub fn removeDraft(session: *ui.Session, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8) !void { 290 const io = session.io orelse return error.NotFound; 291 const repos_dir = session.repos_dir orelse return error.NotFound; 292 const admin_repo = session.admin_repo orelse return error.NotFound; 293 const user_id = session.userId() orelse return error.NotFound; 294 const author = (try session.eventAuthor()) orelse return error.NotFound; 295 296 const id_hex = std.fmt.bytesToHex(id.*, .lower); 297 try fork.remove(io, allocator, repos_dir, admin_repo, &id_hex, &user_id, author); 298 } 299 300 // `status`'s windowed listing. 301 pub fn window(self: *const Self, status: Status) *const Window { 302 return switch (status) { 303 .open => &self.open, 304 .closed => &self.closed, 305 .merged => &self.merged, 306 }; 307 } 308 309 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 310 // theirs: picks. 311 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 312 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 313 while (it.next()) |pick| { 314 if (std.mem.eql(u8, pick, field)) return true; 315 } 316 return false; 317 } 318 319 // the patch `selected_id` names, in whichever window holds it. 320 pub fn selectedThread(self: *const Self) ?*const PatchWithId { 321 if (self.selected_id.len == 0) return null; 322 for ([_]*const Window{ &self.open, &self.closed, &self.merged, &self.drafts }) |win| { 323 for (win.items) |*entry| { 324 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 325 } 326 } 327 return null; 328 } 329 330 // an empty listing, for the wasm / no-repo paths. 331 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, tag: []const u8, selected_id: []const u8, comment_id: []const u8, comments_start: usize, theirs_picks: []const u8, view: ui.RoutablePage.PatchesView) !Self { 332 return .{ 333 .identity = try aa.dupe(u8, identity), 334 .default_target_branch = "", 335 .tag = try aa.dupe(u8, tag), 336 .selected_id = try aa.dupe(u8, selected_id), 337 .comment_id = try aa.dupe(u8, comment_id), 338 .comments_start = comments_start, 339 .open = .empty, 340 .closed = .empty, 341 .merged = .empty, 342 .drafts = .empty, 343 .conflicts = .empty, 344 .theirs_picks = try aa.dupe(u8, theirs_picks), 345 // a description url shows a status list, the patch's own status 346 // picking it once the patch is read; conflicts and resolve urls also 347 // start there, upgraded by init when the conflict data exists. 348 .view = switch (view) { 349 .description, .conflicts, .resolve => .open, 350 else => view, 351 }, 352 .description_page = view == .description, 353 .tags = &.{}, 354 }; 355 } 356 357 pub fn detailResult(aa: std.mem.Allocator, identity: []const u8, entry: PatchWithId) !Self { 358 var result = try emptyResult(aa, identity, "", entry.id, "", 0, "", .open); 359 const items = try aa.dupe(PatchWithId, &.{entry}); 360 const detail_window = Window{ .items = items, .prev_id = null, .next_id = null, .count = 1 }; 361 if (entry.draft) { 362 result.drafts = detail_window; 363 result.view = .drafts; 364 } else switch (entry.record.event.status) { 365 .open => result.open = detail_window, 366 .closed => { 367 result.closed = detail_window; 368 result.view = .closed; 369 }, 370 .merged => { 371 result.merged = detail_window; 372 result.view = .merged; 373 }, 374 } 375 return result; 376 } 377 378 // read one window per status of an opened repo's patches (filtered to `tag` 379 // when set), ordered by creation (newest first). the window of the patch 380 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 381 // event db next to it (synced from the events branch on each page build); a 382 // xit repo reads its own db. 383 pub fn init( 384 comptime repo_kind: rp.RepoKind, 385 comptime repo_opts: rp.RepoOpts(repo_kind), 386 arena: *std.heap.ArenaAllocator, 387 repo: *rp.Repo(repo_kind, repo_opts), 388 io: std.Io, 389 // the admin db's moment, for resolving author emails to user names (null 390 // in local mode, which has no users) 391 admin_moment: ?evt.AdminDB.HashMap(.read_only), 392 session: *ui.Session, 393 repo_id_maybe: ?[evt.event_id_size]u8, 394 identity: []const u8, 395 target_branch: []const u8, 396 tag: []const u8, 397 selected_id: []const u8, 398 comment_id: []const u8, 399 comments_start: usize, 400 theirs_picks: []const u8, 401 view: ui.RoutablePage.PatchesView, 402 ) !Self { 403 const allowed_view: ui.RoutablePage.PatchesView = if (session.local != null and (view == .new or view == .drafts)) .open else view; 404 var empty = try emptyResult(arena.allocator(), identity, tag, selected_id, comment_id, comments_start, theirs_picks, allowed_view); 405 empty.default_target_branch = try arena.allocator().dupe(u8, target_branch); 406 407 const aa = arena.allocator(); 408 const DB = evt.EventDB(repo_opts.hash); 409 const rooted = empty.selected_id.len != 0; 410 const tagged = empty.tag.len != 0; 411 const drafts_window = if (admin_moment) |admin| 412 if (repo_id_maybe) |repo_id| 413 if (session.data.user_id != null and session.repos_dir != null) 414 try loadDraftWindow(repo_kind, repo_opts, arena, session, admin, &repo_id, empty.selected_id, repo) 415 else 416 Window.empty 417 else 418 Window.empty 419 else 420 Window.empty; 421 var draft_selected = false; 422 if (rooted) for (drafts_window.items) |item| { 423 if (std.mem.eql(u8, item.id, empty.selected_id)) { 424 draft_selected = true; 425 break; 426 } 427 }; 428 empty.drafts = drafts_window; 429 if (draft_selected and view != .edit and view != .publish and view != .remove) empty.view = .drafts; 430 431 // an explicitly named published patch that doesn't exist is a bad url 432 // (NotFound -> 404); drafts, tags, and bare routes can use the empty fallback. 433 const strict = rooted and !draft_selected; 434 435 // a repo with no consumed events has no moment yet. 436 const gpa = arena.child_allocator; 437 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, gpa, repo.core.repo_dir) else null; 438 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 439 const haxy_moment = (if (event_db_maybe) |*event_db| 440 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 441 else if (repo_kind == .git) 442 return empty 443 else 444 evt.currentMoment(repo_opts, repo)) catch { 445 if (strict) return error.NotFound; 446 return empty; 447 }; 448 449 // the sorted sets to window, per status: the tag's sets when filtered, 450 // else the top-level per-status sets. a missing set is an empty window. 451 var open_set: ?DB.SortedSet(.read_only) = null; 452 var closed_set: ?DB.SortedSet(.read_only) = null; 453 var merged_set: ?DB.SortedSet(.read_only) = null; 454 if (tagged) { 455 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key))) |tag_to_patches_cursor| { 456 const tag_to_patches = try DB.SortedMap(.read_only).init(tag_to_patches_cursor); 457 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 458 open_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .open); 459 closed_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .closed); 460 merged_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .merged); 461 } 462 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key))) |status_to_patches_cursor| { 463 const status_to_patches = try DB.SortedMap(.read_only).init(status_to_patches_cursor); 464 open_set = try thread.statusSet(Self, DB, status_to_patches, .open); 465 closed_set = try thread.statusSet(Self, DB, status_to_patches, .closed); 466 merged_set = try thread.statusSet(Self, DB, status_to_patches, .merged); 467 } else if (strict) return error.NotFound; 468 469 const event_id_to_patch_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse { 470 if (strict) return error.NotFound; 471 return empty; 472 }; 473 const event_id_to_patch = try DB.HashMap(.read_only).init(event_id_to_patch_cursor); 474 475 // the conflicted patches' container: a set view for membership and 476 // windowing, a map view for the resolve entry, over the same cursor. 477 var conflict_set: ?DB.SortedSet(.read_only) = null; 478 var conflicts_map: ?DB.SortedMap(.read_only) = null; 479 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 480 conflict_set = try DB.SortedSet(.read_only).init(cursor); 481 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 482 } 483 484 // a named patch roots its own status's window at itself (the conflicts 485 // view's window instead); the other windows start at the beginning. 486 var resolved_view = empty.view; 487 var open_root: ?[]const u8 = null; 488 var closed_root: ?[]const u8 = null; 489 var merged_root: ?[]const u8 = null; 490 var conflicts_root: ?[]const u8 = null; 491 var conflict_data: ?Conflict = null; 492 if (rooted and !draft_selected) { 493 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 494 const patch_cursor = try event_id_to_patch.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 495 const patch_map = try DB.HashMap(.read_only).init(patch_cursor); 496 const patch_event = try evt.read(evt.Patch.Record, DB, repo_opts.hash, arena, patch_map); 497 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(patch_event.created_order, &id_bytes)); 498 499 if (view == .conflicts) { 500 // the id roots the conflicts window, so it must be conflicted 501 const set = conflict_set orelse return error.NotFound; 502 if (!try set.contains(order_key)) return error.NotFound; 503 conflicts_root = order_key; 504 } else { 505 // the named patch must be in its windowed set (a tag url can name 506 // an patch that doesn't carry the tag). 507 const set = (switch (patch_event.event.status) { 508 .open => open_set, 509 .closed => closed_set, 510 .merged => merged_set, 511 }) orelse return error.NotFound; 512 if (!try set.contains(order_key)) return error.NotFound; 513 514 switch (patch_event.event.status) { 515 .open => open_root = order_key, 516 .closed => closed_root = order_key, 517 .merged => merged_root = order_key, 518 } 519 // form urls keep their view; otherwise the patch's status picks it. 520 if (view != .edit and view != .merge and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (patch_event.event.status) { 521 .open => .open, 522 .closed => .closed, 523 .merged => .merged, 524 }; 525 526 // a resolve url shows the resolve view only while the patch still 527 // has a conflict entry; otherwise the status list stands. the 528 // wasm client renders from the snapshot, so the diff machinery 529 // this pulls in is gated out of its build. 530 if (comptime !wasm) { 531 if (view == .resolve) { 532 if (conflicts_map) |map| { 533 if (try map.getCursor(order_key)) |conflict_cursor| { 534 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 535 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, patch_event, conflict_entry); 536 resolved_view = .resolve; 537 } 538 } 539 } 540 } 541 } 542 } 543 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 544 var open_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, open_set, open_root, conflict_set, empty.selected_id, thread_comments_start); 545 var closed_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, closed_set, closed_root, conflict_set, empty.selected_id, thread_comments_start); 546 var merged_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, merged_set, merged_root, conflict_set, empty.selected_id, thread_comments_start); 547 var conflicts_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, conflict_set, conflicts_root, conflict_set, empty.selected_id, thread_comments_start); 548 try setForkDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &open_window); 549 try setForkDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &closed_window); 550 try setForkDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &merged_window); 551 try setForkDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &conflicts_window); 552 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 553 554 const comment_page = if (empty.comment_id.len == 0) 555 null 556 else 557 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start); 558 559 const tags = try thread.loadTags(Self, repo_opts.hash, arena, haxy_moment); 560 561 return .{ 562 .identity = empty.identity, 563 .default_target_branch = empty.default_target_branch, 564 .tag = empty.tag, 565 .selected_id = empty.selected_id, 566 .comment_id = empty.comment_id, 567 .comments_start = comments_start, 568 .comment_page = comment_page, 569 .open = open_window, 570 .closed = closed_window, 571 .merged = merged_window, 572 .drafts = drafts_window, 573 .conflicts = conflicts_window, 574 .conflict = conflict_data, 575 .theirs_picks = empty.theirs_picks, 576 .view = resolved_view, 577 .description_page = empty.description_page, 578 .tags = tags, 579 }; 580 } 581 582 fn setForkDetails( 583 comptime repo_kind: rp.RepoKind, 584 comptime repo_opts: rp.RepoOpts(repo_kind), 585 io: std.Io, 586 arena: *std.heap.ArenaAllocator, 587 admin_moment: ?evt.AdminDB.HashMap(.read_only), 588 repo: *rp.Repo(repo_kind, repo_opts), 589 target: *Window, 590 ) !void { 591 if (target.items.len == 0) return; 592 const items = try arena.allocator().dupe(PatchWithId, target.items); 593 for (items) |*item| { 594 if (admin_moment) |moment| { 595 const id = try evt.parseEventId(item.id); 596 const record = try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &id); 597 item.fork_exists = if (record) |value| !value.removed else false; 598 } 599 const target_branch = item.record.event.target_branch; 600 const revision = item.record.event.revision orelse continue; 601 item.fork_oid = switch (item.record.event.status) { 602 .merged => |oid| oid, 603 else => revision.source_oid, 604 }; 605 if (item.record.event.status.kind() == .merged) continue; 606 const target_oid = try repo.readRef(io, .{ .kind = .head, .name = target_branch }); 607 item.no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, revision.source_oid) else false; 608 } 609 target.items = items; 610 } 611 612 fn loadDraftWindow( 613 comptime repo_kind: rp.RepoKind, 614 comptime repo_opts: rp.RepoOpts(repo_kind), 615 arena: *std.heap.ArenaAllocator, 616 session: *ui.Session, 617 admin_moment: evt.AdminDB.HashMap(.read_only), 618 repo_id: *const [evt.event_id_size]u8, 619 selected_id: []const u8, 620 target_repo: *rp.Repo(repo_kind, repo_opts), 621 ) !Window { 622 const user_id = session.data.user_id orelse return .empty; 623 if (user_id.len != evt.event_id_size) return .empty; 624 const repos_dir = session.repos_dir orelse return .empty; 625 const io = session.io orelse return .empty; 626 const key = evt.Fork.draftKey(repo_id, user_id); 627 const drafts_cursor = try admin_moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, evt.Fork.repo_user_to_draft_id_set_key)) orelse return .empty; 628 const drafts = try evt.AdminDB.HashMap(.read_only).init(drafts_cursor); 629 const set_cursor = try drafts.getCursor(hash.hashInt(evt.admin_repo_opts.hash, &key)) orelse return .empty; 630 const set = try evt.AdminDB.SortedSet(.read_only).init(set_cursor); 631 const aa = arena.allocator(); 632 633 var root_key: ?[]const u8 = null; 634 if (selected_id.len != 0) { 635 const id = evt.parseEventId(selected_id) catch return error.NotFound; 636 if (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, arena, &id)) |record| { 637 if (!record.removed and record.event.stage == .draft) { 638 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(record.created_order, &id)); 639 if (!try set.contains(order_key)) return error.NotFound; 640 root_key = order_key; 641 } 642 } 643 } 644 645 var prev_id: ?[]const u8 = null; 646 var iter = if (root_key) |root| blk: { 647 const rank = try set.rank(root); 648 if (rank > 0 and rank <= page_size) { 649 prev_id = ""; 650 } else if (rank > page_size) { 651 const pair = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 652 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 653 _ = try pair.key_cursor.readBytes(&previous); 654 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 655 prev_id = try aa.dupe(u8, &hex); 656 } 657 break :blk try set.iteratorFrom(root); 658 } else try set.iteratorFromIndex(0); 659 660 var items: std.ArrayList(PatchWithId) = .empty; 661 var next_id: ?[]const u8 = null; 662 while (try iter.next()) |cursor| { 663 const id = try evt.readOrderKeyId(evt.AdminDB, cursor); 664 const id_hex = std.fmt.bytesToHex(id, .lower); 665 if (items.items.len == page_size) { 666 next_id = try aa.dupe(u8, &id_hex); 667 break; 668 } 669 const path = try fork.forkPath(aa, repos_dir, &id_hex); 670 var fork_repo = rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 671 defer fork_repo.deinit(io, arena.child_allocator); 672 const entry = (try loadDraftEntry(repo_kind, repo_opts, arena, io, admin_moment, &fork_repo, target_repo, id)) orelse continue; 673 try items.append(aa, entry); 674 } 675 return .{ 676 .items = items.items, 677 .prev_id = prev_id, 678 .next_id = next_id, 679 .count = @intCast(try set.count()), 680 }; 681 } 682 683 pub fn loadDraftEntry( 684 comptime repo_kind: rp.RepoKind, 685 comptime repo_opts: rp.RepoOpts(repo_kind), 686 arena: *std.heap.ArenaAllocator, 687 io: std.Io, 688 admin_moment: ?evt.AdminDB.HashMap(.read_only), 689 fork_repo: *rp.Repo(.xit, .{}), 690 target_repo: *rp.Repo(repo_kind, repo_opts), 691 id: [evt.event_id_size]u8, 692 ) !?PatchWithId { 693 const aa = arena.allocator(); 694 const id_hex = std.fmt.bytesToHex(id, .lower); 695 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 696 const moment = evt.currentMoment(.{}, fork_repo) catch return null; 697 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, &id)) orelse return null; 698 const target_branch = patch.event.target_branch; 699 const target_oid = try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }); 700 return .{ 701 .id = try aa.dupe(u8, &id_hex), 702 .record = patch, 703 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 704 .draft = true, 705 .fork_oid = try aa.dupe(u8, &fork_oid), 706 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 707 .fork_exists = true, 708 }; 709 } 710 711 pub const View = thread.View(.patch, Self); 712 pub const Detail = thread.Detail(.patch, Self); 713 pub const detail_widget_name = "repo_patch_detail"; 714 715 pub const Header = thread.Header; 716 717 pub fn appendDetails(self: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 718 const aa = session.page_arena.allocator(); 719 const status_kind = entry.record.event.status.kind(); 720 const target_branch = entry.record.event.target_branch; 721 var fields: [2]ui.widget.CopyableText.Choice = undefined; 722 var field_count: usize = 0; 723 724 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_port) |port| { 725 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, self.identity, entry.id }); 726 const push_command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 727 const clone_name = try cloneDirectoryName(aa, entry.record.event.title); 728 const clone_command = if (clone_name.len == 0) 729 try std.fmt.allocPrint(aa, "git clone {s}", .{url}) 730 else 731 try std.fmt.allocPrint(aa, "git clone {s} {s}", .{ url, clone_name }); 732 const choices: [2]ui.widget.CopyableText.Choice = .{ 733 .{ 734 .selector = "push", 735 .text = push_command, 736 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 737 .label = " push to this patch from existing repo ", 738 }, 739 .{ 740 .selector = "clone", 741 .text = clone_command, 742 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 743 .label = " clone this patch ", 744 }, 745 }; 746 var copyable_text = try ui.widget.CopyableText.init(allocator, session, &choices); 747 errdefer copyable_text.deinit(allocator); 748 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 749 }; 750 if (entry.fork_oid.len != 0) { 751 fields[field_count] = .{ 752 .selector = "from", 753 .text = entry.fork_oid, 754 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 755 }; 756 field_count += 1; 757 } 758 fields[field_count] = .{ 759 .selector = "to", 760 .text = target_branch, 761 .label = if (status_kind == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 762 }; 763 field_count += 1; 764 765 if (field_count > 0) { 766 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[0..field_count]); 767 errdefer copyable_text.deinit(allocator); 768 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 769 } 770 } 771 772 fn cloneDirectoryName(allocator: std.mem.Allocator, title: []const u8) ![]const u8 { 773 var name: [64]u8 = undefined; 774 var len: usize = 0; 775 var pending: ?u8 = null; 776 777 for (title) |char| { 778 if (std.ascii.isAlphanumeric(char)) { 779 const needed: usize = if (pending == null) 1 else 2; 780 if (len + needed > name.len) break; 781 if (pending) |value| { 782 name[len] = value; 783 len += 1; 784 } 785 name[len] = std.ascii.toLower(char); 786 len += 1; 787 pending = null; 788 } else if (char == ' ') { 789 if (len != 0) pending = '-'; 790 } else if (char == '.') { 791 if (len != 0 and pending != '-') pending = '.'; 792 } 793 } 794 if (len == 0) return ""; 795 return allocator.dupe(u8, name[0..len]); 796 } 797 798 // tabs switching between the patches page's views 799 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 800 var header = try Header.init(allocator); 801 errdefer header.deinit(allocator); 802 const aa = session.page_arena.allocator(); 803 const selected_index = View.viewIndex(data.view); 804 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 805 806 // a list tab per status, labeled with its listing's patch count 807 for ([_]Status{ .open, .closed, .merged }, 0..) |status, index| { 808 const route = ui.RoutablePage.repoPatchesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 809 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 810 var label_buf: [64]u8 = undefined; 811 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 812 try header.addTab(allocator, label, link, index); 813 } 814 815 // tags tab, labeled with the active tag filter 816 { 817 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.patch, data.identity, data.tag) orelse return error.RouteTooLong; 818 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 819 const label = if (data.tag.len == 0) "tags" else blk: { 820 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 821 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 822 }; 823 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 824 } 825 826 // new-patch tab; an edit or resolve url shows its tab in this place 827 if (session.local == null) { 828 const route = switch (data.view) { 829 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 830 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 831 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 832 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 833 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 834 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 835 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 836 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 837 }; 838 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 839 const label: []const u8 = switch (data.view) { 840 .edit => "edit", 841 .publish => "publish", 842 .merge => "merge", 843 .new_comment => "reply", 844 .edit_comment => "edit", 845 .remove => "remove", 846 .resolve => "resolve", 847 else => "new", 848 }; 849 try header.addTab(allocator, label, link, View.viewIndex(.new)); 850 } 851 852 // drafts belong to the logged-in user and are not available locally 853 if (session.local == null) { 854 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 855 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 856 var label_buf: [64]u8 = undefined; 857 const label = try std.fmt.bufPrint(&label_buf, "drafts ({d})", .{data.drafts.count}); 858 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 859 } 860 861 // conflicts tab, labeled with the conflict count 862 if (data.conflicts.count > 0) { 863 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 864 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 865 var label_buf: [64]u8 = undefined; 866 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 867 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 868 } 869 870 header.select(View.viewIndex(data.view)); 871 return header; 872 } 873