╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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