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