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