╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
396 defer user_repo.deinit(io, allocator); 397 try pch.publish(.{}, io, allocator, admin_repo, &user_repo, &target_repo, fork_path, .{ 398 .id = id_hex, 399 .user_id = user_id, 400 .repo_id = repo_id, 401 .author = author, 402 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 403 }); 404 } 405 406 pub fn mergePatch(data: *const Self, session: *ui.Session, allocator: std.mem.Allocator, author: evt.CommitAuthor, id: []const u8, revision: evt.Patch.MergeRevision) !void { 407 const io = session.io orelse return error.NotFound; 408 const repos_dir = session.repos_dir orelse return error.NotFound; 409 const repo_source = data.repo_source orelse return error.NotFound; 410 if (repo_source.repo_kind != .xit) return error.NotFound; 411 const patch_id = evt.parseEventId(id) catch return error.NotFound; 412 413 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 414 var target_repo = try rp.Repo(.xit, .{}).open(io, allocator, repo_source.localInitOpts()); 415 defer target_repo.deinit(io, allocator); 416 try pch.mergeAndRemoveFork(.{}, io, allocator, repos_dir, &target_repo, .{ 417 .id = id_hex, 418 .revision = revision, 419 .author = author, 420 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 421 }); 422 } 423 424 pub fn editDraft( 425 data: *const Self, 426 session: *ui.Session, 427 allocator: std.mem.Allocator, 428 author: evt.CommitAuthor, 429 id: []const u8, 430 title: []const u8, 431 labels: []const u8, 432 description: []const u8, 433 target_branch: []const u8, 434 ) !void { 435 const io = session.io orelse return error.NotFound; 436 const repos_dir = session.repos_dir orelse return error.NotFound; 437 const admin_repo = session.admin_repo orelse return error.NotFound; 438 const repo_id = data.repo_id orelse return error.NotFound; 439 const repo_source = data.repo_source orelse return error.NotFound; 440 const user_id = session.userId() orelse return error.NotFound; 441 const patch_id = evt.parseEventId(id) catch return error.NotFound; 442 if (!evt.Patch.branchValid(target_branch) or !try repo_source.hasBranch(io, allocator, target_branch)) return error.InvalidTargetBranch; 443 444 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 445 const fork_path = try fork.forkPath(allocator, repos_dir, &id_hex); 446 defer allocator.free(fork_path); 447 var user_repo = (try evt.openUserRepo(io, allocator, repos_dir, &user_id)) orelse return error.NotFound; 448 defer user_repo.deinit(io, allocator); 449 if (!try pch.editDraft(.{}, io, allocator, admin_repo, &user_repo, fork_path, .{ 450 .id = id_hex, 451 .user_id = user_id, 452 .repo_id = repo_id, 453 .title = title, 454 .labels = labels, 455 .description = description, 456 .target_branch = target_branch, 457 .author = author, 458 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 459 })) return error.NotFound; 460 } 461 462 pub fn removeDraft(session: *ui.Session, allocator: std.mem.Allocator, author: evt.CommitAuthor, id: *const [evt.event_id_size]u8) !void { 463 const io = session.io orelse return error.NotFound; 464 const repos_dir = session.repos_dir orelse return error.NotFound; 465 const user_id = session.userId() orelse return error.NotFound; 466 467 const id_hex = std.fmt.bytesToHex(id.*, .lower); 468 var user_repo = (try evt.openUserRepo(io, allocator, repos_dir, &user_id)) orelse return error.NotFound; 469 defer user_repo.deinit(io, allocator); 470 try fork.remove(io, allocator, repos_dir, &user_repo, &id_hex, author); 471 } 472 473 // `status`'s windowed listing. 474 pub fn window(self: *const Self, status: Status) *const Window { 475 return switch (status) { 476 .open => &self.open, 477 .closed => &self.closed, 478 .merged => &self.merged, 479 }; 480 } 481 482 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 483 // theirs: picks. 484 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 485 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 486 while (it.next()) |pick| { 487 if (std.mem.eql(u8, pick, field)) return true; 488 } 489 return false; 490 } 491 492 // the patch `selected_id` names, in whichever window holds it. 493 pub fn selectedThread(self: *const Self) ?*const PatchWithId { 494 if (self.selected_id.len == 0) return null; 495 for ([_]*const Window{ &self.open, &self.closed, &self.merged, &self.drafts }) |win| { 496 for (win.items) |*entry| { 497 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 498 } 499 } 500 return null; 501 } 502 503 // an empty listing, for the wasm / no-repo paths. 504 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, label: []const u8, search: []const u8, selected_id: []const u8, comment_id: []const u8, comments_start: usize, theirs_picks: []const u8, view: ui.RoutablePage.PatchesView) !Self { 505 return .{ 506 .identity = try aa.dupe(u8, identity), 507 .default_target_branch = "", 508 .label = try aa.dupe(u8, label), 509 .search = if (search.len == 0) null else std.Uri.percentDecodeInPlace(try aa.dupe(u8, search)), 510 .selected_id = try aa.dupe(u8, selected_id), 511 .comment_id = try aa.dupe(u8, comment_id), 512 .comments_start = comments_start, 513 .open = .empty, 514 .closed = .empty, 515 .merged = .empty, 516 .drafts = .empty, 517 .conflicts = .empty, 518 .theirs_picks = try aa.dupe(u8, theirs_picks), 519 // a description url shows a status list, the patch's own status 520 // picking it once the patch is read; conflicts and resolve urls also 521 // start there, upgraded by init when the conflict data exists. 522 .view = switch (view) { 523 .description, .conflicts, .resolve => .open, 524 else => view, 525 }, 526 .description_page = view == .description, 527 .labels = &.{}, 528 }; 529 } 530 531 pub fn detailResult(aa: std.mem.Allocator, identity: []const u8, entry: PatchWithId) !Self { 532 var result = try emptyResult(aa, identity, "", "", entry.id, "", 0, "", .open); 533 const items = try aa.dupe(PatchWithId, &.{entry}); 534 const detail_window = Window{ .items = items, .prev_id = null, .next_id = null, .count = 1 }; 535 if (entry.draft) { 536 result.drafts = detail_window; 537 result.view = .drafts; 538 } else switch (entry.record.event.status) { 539 .open => result.open = detail_window, 540 .closed => { 541 result.closed = detail_window; 542 result.view = .closed; 543 }, 544 .merged => { 545 result.merged = detail_window; 546 result.view = .merged; 547 }, 548 } 549 return result; 550 } 551 552 // read one window per status of an opened repo's patches (filtered to `label` 553 // when set), ordered by creation (newest first). the window of the patch 554 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 555 // event db next to it (synced from the events branch on each page build); a 556 // xit repo reads its own db. 557 pub fn init( 558 comptime repo_kind: rp.RepoKind, 559 comptime repo_opts: rp.RepoOpts(repo_kind), 560 arena: *std.heap.ArenaAllocator, 561 repo: *rp.Repo(repo_kind, repo_opts), 562 io: std.Io, 563 // the admin db's moment, for resolving author emails to user names (null 564 // in local mode, which has no users) 565 admin_moment: ?evt.AdminDB.HashMap(.read_only), 566 session: *ui.Session, 567 repo_id_maybe: ?[evt.event_id_size]u8, 568 identity: []const u8, 569 target_branch: []const u8, 570 label: []const u8, 571 search: []const u8, 572 selected_id: []const u8, 573 comment_id: []const u8, 574 comments_start: usize, 575 theirs_picks: []const u8, 576 view: ui.RoutablePage.PatchesView, 577 ) !Self { 578 const allowed_view: ui.RoutablePage.PatchesView = if (session.local != null and view == .drafts) .open else view; 579 var empty = try emptyResult(arena.allocator(), identity, label, search, selected_id, comment_id, comments_start, theirs_picks, allowed_view); 580 empty.default_target_branch = try arena.allocator().dupe(u8, target_branch); 581 582 const aa = arena.allocator(); 583 const DB = evt.EventDB(repo_opts.hash); 584 const rooted = empty.selected_id.len != 0; 585 const labeled = empty.label.len != 0; 586 const drafts_window = if (admin_moment) |admin| 587 if (repo_id_maybe) |repo_id| 588 if (session.data.user_id != null and session.repos_dir != null) 589 try loadDraftWindow(repo_kind, repo_opts, arena, session, admin, &repo_id, empty.selected_id, repo) 590 else 591 Window.empty 592 else 593 Window.empty 594 else 595 Window.empty; 596 var draft_selected = false; 597 if (rooted) for (drafts_window.items) |item| { 598 if (std.mem.eql(u8, item.id, empty.selected_id)) { 599 draft_selected = true; 600 break; 601 } 602 }; 603 empty.drafts = drafts_window; 604 if (draft_selected and view != .edit and view != .publish and view != .remove) empty.view = .drafts; 605 606 // an explicitly named published patch that doesn't exist is a bad url 607 // (NotFound -> 404); drafts, labels, and bare routes can use the empty fallback. 608 const strict = rooted and !draft_selected; 609 610 // a repo with no consumed events has no moment yet. 611 const gpa = arena.child_allocator; 612 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; 613 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 614 const haxy_moment = (if (event_db_maybe) |*event_db| 615 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 616 else if (repo_kind == .git) 617 return empty 618 else 619 evt.currentMoment(repo_opts, repo)) catch { 620 if (strict) return error.NotFound; 621 return empty; 622 }; 623 624 // the sorted sets to window, per status: the label's sets when filtered, 625 // else the top-level per-status sets. a missing set is an empty window. 626 var open_set: ?DB.SortedSet(.read_only) = null; 627 var closed_set: ?DB.SortedSet(.read_only) = null; 628 var merged_set: ?DB.SortedSet(.read_only) = null; 629 if (labeled) { 630 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.label_status_to_id_set_key))) |label_to_patches_cursor| { 631 const label_to_patches = try DB.SortedMap(.read_only).init(label_to_patches_cursor); 632 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.label)); 633 open_set = try thread.labelStatusSet(Self, DB, label_to_patches, decoded, .open); 634 closed_set = try thread.labelStatusSet(Self, DB, label_to_patches, decoded, .closed); 635 merged_set = try thread.labelStatusSet(Self, DB, label_to_patches, decoded, .merged); 636 } 637 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key))) |status_to_patches_cursor| { 638 const status_to_patches = try DB.SortedMap(.read_only).init(status_to_patches_cursor); 639 open_set = try thread.statusSet(Self, DB, status_to_patches, .open); 640 closed_set = try thread.statusSet(Self, DB, status_to_patches, .closed); 641 merged_set = try thread.statusSet(Self, DB, status_to_patches, .merged); 642 } else if (strict) return error.NotFound; 643 644 const event_id_to_patch_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse { 645 if (strict) return error.NotFound; 646 return empty; 647 }; 648 const event_id_to_patch = try DB.HashMap(.read_only).init(event_id_to_patch_cursor); 649 650 // the conflicted patches' container: a set view for membership and 651 // windowing, a map view for the resolve entry, over the same cursor. 652 var conflict_set: ?DB.SortedSet(.read_only) = null; 653 var conflicts_map: ?DB.SortedMap(.read_only) = null; 654 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 655 conflict_set = try DB.SortedSet(.read_only).init(cursor); 656 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 657 } 658 659 // a named patch roots its own status's window at itself (the conflicts 660 // view's window instead); the other windows start at the beginning. 661 var resolved_view = empty.view; 662 var open_root: ?[]const u8 = null; 663 var closed_root: ?[]const u8 = null; 664 var merged_root: ?[]const u8 = null; 665 var conflicts_root: ?[]const u8 = null; 666 var conflict_data: ?Conflict = null; 667 if (rooted and !draft_selected) { 668 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 669 const patch_cursor = try event_id_to_patch.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 670 const patch_map = try DB.HashMap(.read_only).init(patch_cursor); 671 const patch_event = try evt.read(evt.Patch.Record, DB, repo_opts.hash, arena, patch_map); 672 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(patch_event.created_order, &id_bytes)); 673 674 if (view == .conflicts) { 675 // the id roots the conflicts window, so it must be conflicted 676 const set = conflict_set orelse return error.NotFound; 677 if (!try set.contains(order_key)) return error.NotFound; 678 conflicts_root = order_key; 679 } else { 680 // the named patch must be in its windowed set (a label url can name 681 // an patch that doesn't carry the label). 682 const set = (switch (patch_event.event.status) { 683 .open => open_set, 684 .closed => closed_set, 685 .merged => merged_set, 686 }) orelse return error.NotFound; 687 if (!try set.contains(order_key)) return error.NotFound; 688 689 switch (patch_event.event.status) { 690 .open => open_root = order_key, 691 .closed => closed_root = order_key, 692 .merged => merged_root = order_key, 693 } 694 // form urls keep their view; otherwise the patch's status picks it. 695 if (view != .edit and view != .merge and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (patch_event.event.status) { 696 .open => .open, 697 .closed => .closed, 698 .merged => .merged, 699 }; 700 701 // a resolve url shows the resolve view only while the patch still 702 // has a conflict entry; otherwise the status list stands. the 703 // wasm client renders from the snapshot, so the diff machinery 704 // this pulls in is gated out of its build. 705 if (comptime !wasm) { 706 if (view == .resolve) { 707 if (conflicts_map) |map| { 708 if (try map.getCursor(order_key)) |conflict_cursor| { 709 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 710 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, patch_event, conflict_entry); 711 resolved_view = .resolve; 712 } 713 } 714 } 715 } 716 } 717 } 718 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 719 var open_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, open_set, open_root, conflict_set, empty.selected_id, thread_comments_start, empty.search); 720 var closed_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, closed_set, closed_root, conflict_set, empty.selected_id, thread_comments_start, empty.search); 721 var merged_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, merged_set, merged_root, conflict_set, empty.selected_id, thread_comments_start, empty.search); 722 var conflicts_window = try thread.loadWindow(Self, .patch, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, conflict_set, conflicts_root, conflict_set, empty.selected_id, thread_comments_start, null); 723 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.repos_dir, haxy_moment, repo, &open_window); 724 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.repos_dir, haxy_moment, repo, &closed_window); 725 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.repos_dir, haxy_moment, repo, &merged_window); 726 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, session.repos_dir, haxy_moment, repo, &conflicts_window); 727 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 728 729 const comment_page = if (empty.comment_id.len == 0) 730 null 731 else 732 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start); 733 734 const labels = try thread.loadLabels(Self, repo_opts.hash, arena, haxy_moment); 735 736 return .{ 737 .identity = empty.identity, 738 .default_target_branch = empty.default_target_branch, 739 .label = empty.label, 740 .search = empty.search, 741 .selected_id = empty.selected_id, 742 .comment_id = empty.comment_id, 743 .comments_start = comments_start, 744 .comment_page = comment_page, 745 .open = open_window, 746 .closed = closed_window, 747 .merged = merged_window, 748 .drafts = drafts_window, 749 .conflicts = conflicts_window, 750 .conflict = conflict_data, 751 .theirs_picks = empty.theirs_picks, 752 .view = resolved_view, 753 .description_page = empty.description_page, 754 .labels = labels, 755 }; 756 } 757 758 fn setPatchDetails( 759 comptime repo_kind: rp.RepoKind, 760 comptime repo_opts: rp.RepoOpts(repo_kind), 761 io: std.Io, 762 arena: *std.heap.ArenaAllocator, 763 admin_moment: ?evt.AdminDB.HashMap(.read_only), 764 repos_dir: ?[]const u8, 765 haxy_moment: evt.EventDB(repo_opts.hash).HashMap(.read_only), 766 repo: *rp.Repo(repo_kind, repo_opts), 767 target: *Window, 768 ) !void { 769 if (target.items.len == 0) return; 770 const items = try arena.allocator().dupe(PatchWithId, target.items); 771 for (items) |*item| { 772 const id = try evt.parseEventId(item.id); 773 const status = item.record.event.status.kind(); 774 // a fork-backed patch's fork lives in its forker's user repo 775 if (admin_moment) |moment| if (repos_dir) |dir| if (item.record.event.source_branch == null) { 776 if (try evt.readForker(arena, moment, item.record.author_email)) |forker| { 777 item.forker = forker.name; 778 const record = try evt.readForkById(io, arena.child_allocator, arena, dir, &forker.id, &id); 779 item.fork_exists = if (record) |value| !value.removed else false; 780 } 781 }; 782 const target_branch = item.record.event.target_branch; 783 const revision = item.record.event.revision orelse continue; 784 item.commit_count = revision.commit_count; 785 const merge_revision: evt.Patch.MergeRevision = switch (item.record.event.status) { 786 .merged => |merged| merged.revision, 787 else => .source, 788 }; 789 item.revision_oid = switch (merge_revision) { 790 .source => revision.source_oid, 791 .squash => revision.squash_oid, 792 }; 793 if (!item.fork_exists or status == .merged) { 794 const revision_id = try evt.parseEventId(&revision.id); 795 if (try evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, haxy_moment, arena, &revision_id)) |record| { 796 item.base_oid = record.event.base_oid; 797 } 798 } 799 if (status == .merged) { 800 if (merge_revision == .squash) item.commit_count = 1; 801 continue; 802 } 803 if (comptime repo_kind == .xit) { 804 var moment = try repo.core.latestMoment(); 805 const state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 806 item.mergeability = pch.readMergeability(repo_opts, state, io, arena, &id, item.record.event) catch .{}; 807 } 808 const target_oid = try repo.readRef(io, .{ .kind = .head, .name = target_branch }); 809 item.no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, revision.source_oid) else false; 810 } 811 target.items = items; 812 } 813 814 fn loadDraftWindow( 815 comptime repo_kind: rp.RepoKind, 816 comptime repo_opts: rp.RepoOpts(repo_kind), 817 arena: *std.heap.ArenaAllocator, 818 session: *ui.Session, 819 admin_moment: evt.AdminDB.HashMap(.read_only), 820 repo_id: *const [evt.event_id_size]u8, 821 selected_id: []const u8, 822 target_repo: *rp.Repo(repo_kind, repo_opts), 823 ) !Window { 824 const user_id = session.userId() orelse return .empty; 825 const user_name = session.data.user_name orelse return .empty; 826 const repos_dir = session.repos_dir orelse return .empty; 827 const io = session.io orelse return .empty; 828 829 // the viewer's drafts live in their user repo 830 var user_repo = (try evt.openUserRepo(io, arena.child_allocator, repos_dir, &user_id)) orelse return .empty; 831 defer user_repo.deinit(io, arena.child_allocator); 832 const user_moment = (try evt.userMoment(&user_repo)) orelse return .empty; 833 const drafts_cursor = try user_moment.getCursor(hash.hashInt(evt.user_repo_opts.hash, evt.Fork.repo_to_draft_id_set_key)) orelse return .empty; 834 const drafts = try evt.UserDB.HashMap(.read_only).init(drafts_cursor); 835 const set_cursor = try drafts.getCursor(hash.hashInt(evt.user_repo_opts.hash, repo_id)) orelse return .empty; 836 const set = try evt.UserDB.SortedSet(.read_only).init(set_cursor); 837 const aa = arena.allocator(); 838 839 var root_key: ?[]const u8 = null; 840 if (selected_id.len != 0) { 841 const id = evt.parseEventId(selected_id) catch return error.NotFound; 842 if (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, user_moment, arena, &id)) |record| { 843 if (!record.removed and record.event.stage == .draft) { 844 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(record.created_order, &id)); 845 if (!try set.contains(order_key)) return error.NotFound; 846 root_key = order_key; 847 } 848 } 849 } 850 851 var prev_id: ?[]const u8 = null; 852 var iter = if (root_key) |root| blk: { 853 const rank = try set.rank(root); 854 if (rank > 0 and rank <= page_size) { 855 prev_id = ""; 856 } else if (rank > page_size) { 857 const pair = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 858 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 859 _ = try pair.key_cursor.readBytes(&previous); 860 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 861 prev_id = try aa.dupe(u8, &hex); 862 } 863 break :blk try set.iteratorFrom(root); 864 } else try set.iteratorFromIndex(0); 865 866 var items: std.ArrayList(PatchWithId) = .empty; 867 var next_id: ?[]const u8 = null; 868 while (try iter.next()) |cursor| { 869 const id = try evt.readOrderKeyId(evt.UserDB, cursor); 870 const id_hex = std.fmt.bytesToHex(id, .lower); 871 if (items.items.len == page_size) { 872 next_id = try aa.dupe(u8, &id_hex); 873 break; 874 } 875 const path = try fork.forkPath(aa, repos_dir, &id_hex); 876 var fork_repo = rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 877 defer fork_repo.deinit(io, arena.child_allocator); 878 const entry = (try loadDraftEntry(repo_kind, repo_opts, arena, io, admin_moment, &fork_repo, target_repo, id, user_name)) orelse continue; 879 try items.append(aa, entry); 880 } 881 return .{ 882 .items = items.items, 883 .prev_id = prev_id, 884 .next_id = next_id, 885 .count = @intCast(try set.count()), 886 }; 887 } 888 889 pub fn loadDraftEntry( 890 comptime repo_kind: rp.RepoKind, 891 comptime repo_opts: rp.RepoOpts(repo_kind), 892 arena: *std.heap.ArenaAllocator, 893 io: std.Io, 894 admin_moment: ?evt.AdminDB.HashMap(.read_only), 895 fork_repo: *rp.Repo(.xit, .{}), 896 target_repo: *rp.Repo(repo_kind, repo_opts), 897 id: [evt.event_id_size]u8, 898 forker: []const u8, 899 ) !?PatchWithId { 900 const aa = arena.allocator(); 901 const id_hex = std.fmt.bytesToHex(id, .lower); 902 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 903 const moment = evt.currentMoment(.{}, fork_repo) catch return null; 904 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, &id)) orelse return null; 905 const target_branch = patch.event.target_branch; 906 const target_oid = try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }); 907 const commit_count: ?u64 = blk: { 908 const newest = evt.PatchRev.readNewest(evt.EventDB(.sha1), .sha1, moment, arena) catch break :blk null; 909 break :blk if (newest) |revision| revision.record.commit_count else 0; 910 }; 911 return .{ 912 .id = try aa.dupe(u8, &id_hex), 913 .record = patch, 914 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 915 .draft = true, 916 .revision_oid = try aa.dupe(u8, &fork_oid), 917 .commit_count = commit_count, 918 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 919 .fork_exists = true, 920 .forker = try aa.dupe(u8, forker), 921 }; 922 } 923 924 pub const View = thread.View(.patch, Self); 925 pub const Detail = thread.Detail(.patch, Self); 926 pub const detail_widget_name = "repo_patch_detail"; 927 928 pub const Header = thread.Header; 929 930 const open_tab_label = "open"; 931 const closed_tab_label = "closed"; 932 const merged_tab_label = "merged"; 933 const labels_tab_label = "labels"; 934 const edit_tab_label = "edit"; 935 const publish_tab_label = "publish"; 936 const merge_tab_label = "merge"; 937 const reply_tab_label = "reply"; 938 const remove_tab_label = "remove"; 939 const resolve_tab_label = "resolve"; 940 const new_tab_label = "new"; 941 const drafts_tab_label = "drafts"; 942 const conflicts_tab_label = "conflicts"; 943 944 pub fn appendDetails(self: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 945 const aa = session.page_arena.allocator(); 946 const status_kind = entry.record.event.status.kind(); 947 const target_branch = entry.record.event.target_branch; 948 949 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_port) |port| { 950 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, self.identity, entry.id }); 951 const push_command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 952 const clone_name = try cloneDirectoryName(aa, entry.record.event.title); 953 const clone_command = if (clone_name.len == 0) 954 try std.fmt.allocPrint(aa, "git clone {s}", .{url}) 955 else 956 try std.fmt.allocPrint(aa, "git clone {s} {s}", .{ url, clone_name }); 957 const merge_command = try std.fmt.allocPrint(aa, "git fetch '{s}' refs/heads/patch && git merge FETCH_HEAD", .{url}); 958 const choices: [3]ui.widget.CopyableText.Choice = .{ 959 .{ 960 .selector = "push", 961 .text = push_command, 962 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 963 .label = " push to this patch from existing repo ", 964 }, 965 .{ 966 .selector = "clone", 967 .text = clone_command, 968 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 969 .label = " clone this patch ", 970 }, 971 .{ 972 .selector = "merge", 973 .text = merge_command, 974 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, merge_command }), 975 .label = " merge this patch locally ", 976 }, 977 }; 978 var copyable_text = try ui.widget.CopyableText.init(allocator, session, &choices); 979 errdefer copyable_text.deinit(allocator); 980 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 981 }; 982 if (entry.record.event.source_branch) |branch| { 983 var source = try wgt.TextBox.init(allocator, branch, .{ .label = " source branch ", .border_style = .single, .round_corners = true, .wrap_kind = .none }); 984 errdefer source.deinit(allocator); 985 source.getFocus().mode = .all; 986 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .text_box = source }, .rect = null, .min_size = null }); 987 } 988 const fields = [_]ui.widget.CopyableText.Choice{ 989 .{ 990 .selector = "from", 991 .text = entry.revision_oid, 992 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 993 }, 994 .{ 995 .selector = "to", 996 .text = target_branch, 997 .label = if (status_kind == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 998 }, 999 }; 1000 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[@intFromBool(entry.revision_oid.len == 0)..]); 1001 errdefer copyable_text.deinit(allocator); 1002 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 1003 } 1004 1005 fn cloneDirectoryName(allocator: std.mem.Allocator, title: []const u8) ![]const u8 { 1006 var name: [64]u8 = undefined; 1007 var len: usize = 0; 1008 var pending: ?u8 = null; 1009 1010 for (title) |char| { 1011 if (std.ascii.isAlphanumeric(char)) { 1012 const needed: usize = if (pending == null) 1 else 2; 1013 if (len + needed > name.len) break; 1014 if (pending) |value| { 1015 name[len] = value; 1016 len += 1; 1017 } 1018 name[len] = std.ascii.toLower(char); 1019 len += 1; 1020 pending = null; 1021 } else if (char == ' ') { 1022 if (len != 0) pending = '-'; 1023 } else if (char == '.') { 1024 if (len != 0 and pending != '-') pending = '.'; 1025 } 1026 } 1027 if (len == 0) return ""; 1028 return allocator.dupe(u8, name[0..len]); 1029 } 1030 1031 // tabs switching between the patches page's views 1032 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1033 var header = try Header.init(allocator, session, data.search); 1034 errdefer header.deinit(allocator); 1035 const aa = session.page_arena.allocator(); 1036 const selected_index = View.viewIndex(data.view); 1037 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 1038 1039 // a list tab per status, labeled with its listing's patch count 1040 const status_labels = [_][]const u8{ open_tab_label, closed_tab_label, merged_tab_label }; 1041 for ([_]Status{ .open, .closed, .merged }, status_labels, 0..) |status, status_label, index| { 1042 const route = try thread.searchRoute(ui.RoutablePage.repoPatchesRoute(data.identity, status, data.label, ""), data.search); 1043 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 1044 var label_buf: [64]u8 = undefined; 1045 const label = try thread.countLabel(&label_buf, status_label, data.window(status).*); 1046 try header.addTab(allocator, label, link, index); 1047 } 1048 1049 // labels tab, showing the active label filter 1050 { 1051 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.patch, data.identity, data.label), data.search); 1052 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 1053 const label = if (data.label.len == 0) labels_tab_label else blk: { 1054 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.label)); 1055 break :blk try std.fmt.allocPrint(aa, labels_tab_label ++ " ({s})", .{decoded}); 1056 }; 1057 try header.addTab(allocator, label, labels_link, View.viewIndex(.labels)); 1058 } 1059 1060 // new-patch tab; an edit or resolve url shows its tab in this place 1061 { 1062 const route = switch (data.view) { 1063 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 1064 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1065 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 1066 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1067 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1068 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 1069 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 1070 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 1071 }; 1072 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 1073 const label: []const u8 = switch (data.view) { 1074 .edit => edit_tab_label, 1075 .publish => publish_tab_label, 1076 .merge => merge_tab_label, 1077 .new_comment => reply_tab_label, 1078 .edit_comment => edit_tab_label, 1079 .remove => remove_tab_label, 1080 .resolve => resolve_tab_label, 1081 else => new_tab_label, 1082 }; 1083 try header.addTab(allocator, label, link, View.viewIndex(.new)); 1084 } 1085 1086 // drafts belong to the logged-in user and are not available locally 1087 if (session.data.host_kind != .local) { 1088 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 1089 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 1090 var label_buf: [64]u8 = undefined; 1091 const label = try std.fmt.bufPrint(&label_buf, drafts_tab_label ++ " ({d})", .{data.drafts.count}); 1092 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 1093 } 1094 1095 // conflicts tab, labeled with the conflict count 1096 if (data.conflicts.count > 0) { 1097 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 1098 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 1099 var label_buf: [64]u8 = undefined; 1100 const label = try std.fmt.bufPrint(&label_buf, conflicts_tab_label ++ " ({d})", .{data.conflicts.count}); 1101 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 1102 } 1103 1104 header.select(View.viewIndex(data.view)); 1105 return header; 1106 } 1107