╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
484 485 const fields_cursor = (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key))) orelse return error.NotFound; 486 const conflicted_fields = try fields_cursor.readBytesAlloc(aa, null); 487 488 const their_cursor = (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key))) orelse return error.NotFound; 489 const theirs = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(their_cursor)); 490 491 // absent when both sides created the issue independently 492 var base: ?evt.Issue.Record = null; 493 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.base_record_key))) |base_cursor| { 494 base = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(base_cursor)); 495 } 496 497 // each side's field->oid map, for attributing the versions; a field a 498 // merge left unattributed reads as an unknown author 499 var their_field_oids: ?DB.SortedMap(.read_only) = null; 500 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_field_to_oid_key))) |cursor| { 501 their_field_oids = try DB.SortedMap(.read_only).init(cursor); 502 } 503 var our_field_oids: ?DB.SortedMap(.read_only) = null; 504 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.id_to_field_to_oid_key))) |map_cursor| { 505 const id_to_field_to_oid = try DB.HashMap(.read_only).init(map_cursor); 506 if (try id_to_field_to_oid.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) |cursor| { 507 our_field_oids = try DB.SortedMap(.read_only).init(cursor); 508 } 509 } 510 511 var conflict = Conflict{}; 512 var field_iter = std.mem.splitScalar(u8, conflicted_fields, ' '); 513 while (field_iter.next()) |field| { 514 // status can't conflict (two changes of a two-value field agree) 515 const known = std.meta.stringToEnum(enum { title, tags, description }, field) orelse continue; 516 const our_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, our_field_oids, field)); 517 const their_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, their_field_oids, field)); 518 switch (known) { 519 .title => conflict.title = .{ 520 .ours = .{ .text = ours.event.title, .author = our_author }, 521 .theirs = .{ .text = theirs.event.title, .author = their_author }, 522 }, 523 .tags => conflict.tags = .{ 524 .ours = .{ .text = ours.event.tags, .author = our_author }, 525 .theirs = .{ .text = theirs.event.tags, .author = their_author }, 526 }, 527 .description => conflict.description = .{ 528 .chunks = try diff3.chunks(io, gpa, arena, if (base) |b| b.event.description else "", ours.event.description, theirs.event.description), 529 .ours_author = our_author, 530 .theirs_author = their_author, 531 }, 532 } 533 } 534 return conflict; 535 } 536 537 // the oid `field` maps to in a field->oid map, or null 538 fn readFieldOid( 539 comptime hash_kind: hash.HashKind, 540 field_oids_maybe: ?evt.EventDB(hash_kind).SortedMap(.read_only), 541 field: []const u8, 542 ) !?[hash.byteLen(hash_kind)]u8 { 543 const field_oids = field_oids_maybe orelse return null; 544 const cursor = (try field_oids.getCursor(field)) orelse return null; 545 var oid: [hash.byteLen(hash_kind)]u8 = undefined; 546 _ = try cursor.readBytes(&oid); 547 return oid; 548 } 549 550 // the author of the event commit `oid_maybe` names 551 fn oidAuthor( 552 comptime repo_kind: rp.RepoKind, 553 comptime repo_opts: rp.RepoOpts(repo_kind), 554 arena: *std.heap.ArenaAllocator, 555 repo: *rp.Repo(repo_kind, repo_opts), 556 io: std.Io, 557 admin_moment: ?evt.AdminDB.HashMap(.read_only), 558 oid_maybe: ?[hash.byteLen(repo_opts.hash)]u8, 559 ) !ui.Author { 560 const oid = oid_maybe orelse return .unknown; 561 const gpa = arena.child_allocator; 562 var start_oids = [_][hash.hexLen(repo_opts.hash)]u8{std.fmt.bytesToHex(oid, .lower)}; 563 var commit_iter = repo.log(io, gpa, start_oids[0..1]) catch return .unknown; 564 defer commit_iter.deinit(); 565 const commit_object = (commit_iter.next(gpa) catch return .unknown) orelse return .unknown; 566 defer commit_object.deinit(); 567 return try ui.Author.init(admin_moment, arena, commit_object.content.commit.metadata.author orelse ""); 568 } 569 570 pub const View = Threads.View(.issue, Self); 571 572 // tabs switching between the issues page's views. 573 pub const Header = struct { 574 box: wgt.Box(ui.Widget), 575 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 576 577 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 578 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 579 errdefer box.deinit(allocator); 580 581 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 582 errdefer tab_ids.deinit(allocator); 583 584 const aa = session.page_arena.allocator(); 585 586 // a list tab per status, labeled with its listing's issue count 587 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 588 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 589 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 590 var label_buf: [64]u8 = undefined; 591 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 592 try addTab(allocator, &box, &tab_ids, label, link); 593 } 594 595 // tags tab, labeled with the active tag filter 596 { 597 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.issue, data.identity, data.tag) orelse return error.RouteTooLong; 598 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 599 const label = if (data.tag.len == 0) "tags" else blk: { 600 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 601 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 602 }; 603 try addTab(allocator, &box, &tab_ids, label, tags_link); 604 } 605 606 // new-issue tab; an edit or resolve url shows its tab in this place 607 { 608 const route = switch (data.view) { 609 .edit => ui.RoutablePage.repoThreadEditRoute(.issue, data.identity, data.selected_id) orelse return error.RouteTooLong, 610 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 611 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 612 .remove => ui.RoutablePage.repoThreadRemoveRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 613 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 614 else => ui.RoutablePage.repoThreadNewRoute(.issue, data.identity) orelse return error.RouteTooLong, 615 }; 616 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 617 const label: []const u8 = switch (data.view) { 618 .edit => "edit", 619 .new_comment => "reply", 620 .edit_comment => "edit", 621 .remove => "remove", 622 .resolve => "resolve", 623 else => "new", 624 }; 625 try addTab(allocator, &box, &tab_ids, label, link); 626 } 627 628 // conflicts tab, labeled with the conflict count 629 if (data.conflicts.count > 0) { 630 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 631 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 632 var label_buf: [64]u8 = undefined; 633 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 634 try addTab(allocator, &box, &tab_ids, label, link); 635 } 636 637 var self = Header{ .box = box, .tab_ids = tab_ids }; 638 // the tab matching the page's view is selected initially. 639 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 640 return self; 641 } 642 643 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 644 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 645 errdefer text_box.deinit(allocator); 646 text_box.getFocus().focusable = true; 647 text_box.getFocus().kind = .{ .custom = link }; 648 try tab_ids.put(allocator, text_box.getFocus().id, {}); 649 try box.children.put(allocator, text_box.getFocus().id, .{ 650 .widget = .{ .text_box = text_box }, 651 .rect = null, 652 .min_size = .{ .width = label.len + 2, .height = null }, 653 }); 654 } 655 656 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 657 self.box.deinit(allocator); 658 self.tab_ids.deinit(allocator); 659 } 660 661 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 662 self.clearGrid(); 663 // only the selected tab shows its border 664 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 665 switch (child.widget) { 666 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 667 else => {}, 668 } 669 } 670 try self.box.build(allocator, constraint, root_focus); 671 } 672 673 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 674 _ = allocator; 675 const current_tab = self.currentTabIndex() orelse return; 676 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 677 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 678 } 679 } 680 681 pub fn clearGrid(self: *Header) void { 682 self.box.clearGrid(); 683 } 684 685 pub fn getGrid(self: Header) ?Grid { 686 return self.box.getGrid(); 687 } 688 689 pub fn getFocus(self: *Header) *Focus { 690 return self.box.getFocus(); 691 } 692 693 pub fn getSelectedIndex(self: Header) ?usize { 694 return self.currentTabIndex(); 695 } 696 697 fn currentTabIndex(self: Header) ?usize { 698 const child_id = self.box.focus.child_id orelse return null; 699 return self.tab_ids.getIndex(child_id); 700 } 701 }; 702