╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
451 } 452 453 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 454 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 455 errdefer row.deinit(allocator); 456 row.getFocus().mode = .all; 457 row.getFocus().kind = .{ .custom = link }; 458 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 459 } 460 461 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 462 self.box.deinit(allocator); 463 } 464 465 fn header(self: *View) *Header.View { 466 return &self.box.children.values()[header_index].widget.repo_events_header; 467 } 468 469 fn contentStack(self: *View) *wgt.Stack(ui.Widget) { 470 return &self.box.children.values()[content_index].widget.stack; 471 } 472 473 fn selectedView(self: *View) ui.RoutablePage.EventsView { 474 const stack = self.contentStack(); 475 const id = stack.getFocus().child_id orelse return .active; 476 return @fromBackingInt(@intCast(stack.children.getIndex(id) orelse 0)); 477 } 478 479 fn eventWindow(self: *View) *const Window { 480 return self.data.window(self.selectedView()); 481 } 482 483 fn content(self: *View) *wgt.Box(ui.Widget) { 484 const stack = self.contentStack(); 485 return &stack.children.values()[@backingInt(self.selectedView())].box; 486 } 487 488 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 489 return &self.content().children.values()[list_index].widget.scroll; 490 } 491 492 fn listBox(self: *View) *wgt.Box(ui.Widget) { 493 return &self.listScroll().child.box; 494 } 495 496 fn detailFrame(self: *View) *wgt.Box(ui.Widget) { 497 return &self.content().children.values()[detail_index].widget.box; 498 } 499 500 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 501 return &self.detailFrame().children.values()[ui.widget.list_pane_index].widget.scroll; 502 } 503 504 fn detailBox(self: *View) *wgt.Box(ui.Widget) { 505 return &self.detailScroll().child.box; 506 } 507 508 fn detailActive(self: *View) bool { 509 return self.content().getFocus().child_id == self.detailFrame().getFocus().id; 510 } 511 512 fn headerActive(self: *View) bool { 513 return self.box.getFocus().child_id == self.header().getFocus().id; 514 } 515 516 fn selectedEventIndex(self: *View) ?usize { 517 const id = self.listBox().getFocus().child_id orelse return null; 518 const index = self.listBox().children.getIndex(id) orelse return null; 519 return if (index < self.eventWindow().events.len) index else null; 520 } 521 522 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 523 self.clearGrid(); 524 if (self.header().getSelectedIndex()) |index| 525 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[index]; 526 if (self.eventWindow().isEmpty()) return self.box.build(allocator, constraint, root_focus); 527 try self.refreshDetail(allocator); 528 529 const list = self.listBox(); 530 for (list.children.keys(), list.children.values()) |id, *child| switch (child.widget) { 531 .text_box => |*text_box| ui.widget.markSelected(text_box, list.getFocus().child_id == id), 532 else => {}, 533 }; 534 535 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 536 ui.widget.showListBack(self.detailFrame(), !both_fit); 537 self.content().children.values()[list_index].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 538 const detail_width = if (constraint.max_size.width) |width| if (both_fit) width - list_max_width else width else detail_min_width; 539 self.content().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 540 for (self.detailBox().children.values()) |*child| child.max_size = .{ .width = detail_width -| 2, .height = null }; 541 542 try self.box.build(allocator, constraint, root_focus); 543 } 544 545 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 546 const index = self.selectedEventIndex() orelse return; 547 const detailed_index = &self.detailed_index[@backingInt(self.selectedView())]; 548 if (detailed_index.*) |old| if (old == index) return; 549 const box = self.detailBox(); 550 for (box.children.values()) |*child| child.widget.deinit(allocator); 551 box.children.clearAndFree(allocator); 552 box.getFocus().child_id = null; 553 554 const item = self.eventWindow().events[index]; 555 if (item.view_url) |url| { 556 var link = try wgt.TextBox.init(allocator, "view event", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 557 errdefer link.deinit(allocator); 558 link.getFocus().mode = .all; 559 link.getFocus().kind = .{ .custom = try self.session.page_arena.allocator().print("a:{s}", .{url}) }; 560 try box.children.put(allocator, link.getFocus().id, .{ .widget = .{ .text_box = link }, .rect = null, .min_size = null }); 561 } 562 { 563 var kind = try wgt.TextBox.init(allocator, @tagName(item.kind), .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = " kind " } }); 564 errdefer kind.deinit(allocator); 565 kind.getFocus().mode = .all; 566 try box.children.put(allocator, kind.getFocus().id, .{ .widget = .{ .text_box = kind }, .rect = null, .min_size = null }); 567 } 568 { 569 var author = try ui.authorBox(allocator, self.session.page_arena, item.author); 570 errdefer author.deinit(allocator); 571 try box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 572 } 573 574 if (box.children.count() > 0) box.getFocus().child_id = box.children.keys()[0]; 575 self.detailScroll().x = 0; 576 self.detailScroll().y = 0; 577 self.detailScroll().getFocus().version +%= 1; 578 ui.widget.setListBackLink(self.detailFrame(), self.listBox().children.values()[index].widget.text_box.getFocus().kind.custom); 579 detailed_index.* = index; 580 } 581 582 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 583 // scrolling crosses the sub header boundary the same way arrows do 584 const direction = inp.vertDirection(key); 585 if (self.headerActive()) { 586 if (direction == .down) { 587 // the empty message has nothing to focus 588 if (!self.eventWindow().isEmpty()) root_focus.setFocus(self.content().getFocus().id); 589 } else try self.header().input(allocator, key, root_focus); 590 return; 591 } 592 // the way back to the list, by enter or a click 593 if (self.detailActive()) if (ui.widget.listBackId(self.detailFrame())) |id| { 594 if (inp.activated(root_focus, id, key)) return self.focusList(root_focus); 595 if (root_focus.grandchild_id == id) return switch (direction) { 596 .up => self.header().focusCurrent(root_focus), 597 .down => root_focus.setFocus(self.detailScroll().getFocus().id), 598 .none => if (key == .arrow_left) self.focusList(root_focus), 599 }; 600 if (direction == .up and self.contentAtTop()) return root_focus.setFocus(id); 601 }; 602 if (direction == .up and self.contentAtTop()) { 603 self.header().focusCurrent(root_focus); 604 return; 605 } 606 if (self.detailActive()) { 607 if (key == .arrow_left) { 608 self.focusList(root_focus); 609 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 610 ui.widget.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 611 } 612 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 613 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 614 } else switch (key) { 615 .arrow_right => if (self.detailBox().children.count() > 0) self.focusDetail(root_focus), 616 .enter => if (self.selectedEventIndex() != null) self.focusDetail(root_focus), 617 // when the window is too narrow to lay out the detail pane beside 618 // the list, a click on a row opens it like enter. the row was just 619 // selected, so the detail is swapped to it ahead of the build. 620 .mouse => |mouse| if (self.selectedEventIndex() != null and self.content().children.values()[detail_index].rect == null and ui.widget.clickOnSelectedRow(self.listBox(), root_focus, mouse)) { 621 try self.refreshDetail(allocator); 622 self.focusDetail(root_focus); 623 }, 624 else => {}, 625 } 626 } 627 628 // enter the detail pane, landing on the way back to the list when it shows 629 fn focusDetail(self: *View, root_focus: *Focus) void { 630 if (ui.widget.listBackId(self.detailFrame()) != null) return ui.widget.focusListBack(self.detailFrame(), root_focus); 631 root_focus.setFocus(self.detailScroll().getFocus().id); 632 } 633 634 fn focusList(self: *View, root_focus: *Focus) void { 635 root_focus.setFocus(self.listScroll().getFocus().id); 636 } 637 638 fn contentAtTop(self: *View) bool { 639 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 640 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 641 const id = box.getFocus().child_id orelse return true; 642 return box.children.getIndex(id) == 0 and scroll.y == 0; 643 } 644 645 pub fn atTop(self: *View) bool { 646 return self.headerActive(); 647 } 648 649 // arriving from the page's tabs lands on the sub header, not the content 650 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 651 self.header().focusCurrent(root_focus); 652 return true; 653 } 654 655 pub fn clearGrid(self: *View) void { 656 self.box.clearGrid(); 657 } 658 659 pub fn getGrid(self: View) ?Grid { 660 return self.box.getGrid(); 661 } 662 663 pub fn getFocus(self: *View) *Focus { 664 return self.box.getFocus(); 665 } 666 }; 667 668 pub const Header = struct { 669 sync_status: ?[]const u8, 670 671 const nothing = "nothing to sync"; 672 const unsynced = "sync needed!"; 673 674 const Progress = struct { 675 message: *std.Io.Writer.Allocating, 676 677 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 678 switch (event) { 679 .text => |value| { 680 self.message.clearRetainingCapacity(); 681 try self.message.writer.writeAll(value); 682 }, 683 else => {}, 684 } 685 } 686 }; 687 688 pub fn local( 689 comptime repo_kind: rp.RepoKind, 690 comptime repo_opts: rp.RepoOpts(repo_kind), 691 aa: std.mem.Allocator, 692 repo: *rp.Repo(repo_kind, repo_opts), 693 io: std.Io, 694 failure: ?[]const u8, 695 ) !Header { 696 if (failure != null) return Header.empty(aa, failure); 697 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 698 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 699 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 700 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 701 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 702 } 703 704 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 705 return .{ .sync_status = if (failure) |name| try aa.print("error: {s}", .{name}) else nothing }; 706 } 707 708 pub fn remote() Header { 709 return .{ .sync_status = null }; 710 } 711 712 pub const View = struct { 713 box: wgt.Box(ui.Widget), 714 session: *ui.Session, 715 tab_ids: [2]usize, 716 // the tab stays selected while the sync button has focus 717 selected_tab: usize, 718 button_id: ?usize, 719 720 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 721 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 722 errdefer box.deinit(allocator); 723 var tab_ids: [2]usize = undefined; 724 const view_labels = [_][]const u8{ active_tab_label, removed_tab_label }; 725 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, view_labels, 0..) |view, view_label, i| { 726 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "", data.moment) orelse return error.RouteTooLong; 727 const count = data.window(view).count; 728 var label_buf: [64]u8 = undefined; 729 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ view_label, count }); 730 var tab = try wgt.TextBox.init(allocator, label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 731 errdefer tab.deinit(allocator); 732 tab.getFocus().mode = .all; 733 const selected = std.meta.activeTag(session.data.current_page) == .repo_events and data.view == view; 734 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 735 tab_ids[i] = tab.getFocus().id; 736 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 737 } 738 739 // pushes the sync button and status to the right 740 { 741 var spacer = try ui.widget.Spacer.init(allocator); 742 errdefer spacer.deinit(allocator); 743 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null, .flex = .grow }); 744 } 745 746 var button_id: ?usize = null; 747 if (data.header.sync_status != null) { 748 box.getFocus().kind = .{ .custom = "form:/sync" }; 749 var button = try wgt.TextBox.init(allocator, "sync", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 750 errdefer button.deinit(allocator); 751 button.getFocus().mode = .all; 752 button.getFocus().kind = .{ .custom = "submit" }; 753 button_id = button.getFocus().id; 754 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 755 } 756 757 if (data.header.sync_status) |status_text| { 758 var status = try wgt.TextBox.init(allocator, status_text, .{ .border = .hidden, .wrap_kind = .none }); 759 errdefer status.deinit(allocator); 760 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 761 } 762 box.getFocus().child_id = tab_ids[@backingInt(data.view)]; 763 return .{ .box = box, .session = session, .tab_ids = tab_ids, .selected_tab = @backingInt(data.view), .button_id = button_id }; 764 } 765 766 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 767 self.box.deinit(allocator); 768 } 769 770 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 771 self.clearGrid(); 772 if (self.getSelectedIndex()) |index| self.selected_tab = index; 773 for (self.tab_ids, 0..) |id, index| { 774 const child = self.box.children.getPtr(id) orelse continue; 775 const tab = switch (child.widget) { 776 .text_box => |*text_box| text_box, 777 else => continue, 778 }; 779 ui.widget.markSelected(tab, index == self.selected_tab); 780 } 781 try self.box.build(allocator, constraint, root_focus); 782 } 783 784 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 785 const selected = self.box.getFocus().child_id orelse return; 786 const stops = [_]?usize{ self.tab_ids[0], self.tab_ids[1], self.button_id }; 787 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 788 const current = std.mem.indexOfScalar(?usize, stops[0..count], selected) orelse return; 789 if (inp.moveTab(key, current, count)) |next| { 790 root_focus.setFocus(stops[next] orelse unreachable); 791 return; 792 } 793 switch (key) { 794 .enter => if (self.syncFocused()) try self.requestSync(allocator), 795 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 796 else => {}, 797 } 798 } 799 800 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 801 if (!self.session.is_terminal) return; 802 if (self.session.host_request != null) return; 803 const button_id = self.button_id orelse return; 804 const child = self.box.children.getPtr(button_id) orelse return; 805 const button = switch (child.widget) { 806 .text_box => |*button| button, 807 else => return, 808 }; 809 try button.setContent(allocator, "syncing..."); 810 self.session.host_request = .sync_events; 811 } 812 813 fn syncFocused(self: *Header.View) bool { 814 const button_id = self.button_id orelse return false; 815 return self.box.getFocus().child_id == button_id; 816 } 817 818 pub fn getSelectedIndex(self: *Header.View) ?usize { 819 const id = self.box.getFocus().child_id orelse return null; 820 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 821 } 822 823 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 824 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 825 } 826 827 pub fn clearGrid(self: *Header.View) void { 828 self.box.clearGrid(); 829 } 830 831 pub fn getGrid(self: Header.View) ?Grid { 832 return self.box.getGrid(); 833 } 834 835 pub fn getFocus(self: *Header.View) *Focus { 836 return self.box.getFocus(); 837 } 838 }; 839 }; 840 841 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 842 if (comptime builtin.target.cpu.arch == .wasm32) return; 843 const io = session.io orelse return; 844 const source = session.local orelse return; 845 defer session.refresh_requested = true; 846 const failure = sync(io, allocator, source) catch |err| { 847 session.data.sync_failure = @errorName(err); 848 return; 849 }; 850 if (failure) |message| { 851 defer allocator.free(message); 852 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 853 return; 854 } 855 session.data.sync_failure = null; 856 switch (session.data.current_page) { 857 .repo_events => |route| { 858 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 859 session.data.current_page = first; 860 }, 861 else => {}, 862 } 863 } 864 865 fn remoteName( 866 comptime repo_kind: rp.RepoKind, 867 comptime repo_opts: rp.RepoOpts(repo_kind), 868 repo: *rp.Repo(repo_kind, repo_opts), 869 io: std.Io, 870 allocator: std.mem.Allocator, 871 ) !?[]u8 { 872 var remotes = try repo.listRemotes(io, allocator); 873 defer remotes.deinit(); 874 const names = remotes.sections.keys(); 875 if (names.len == 0) return null; 876 for (names) |name| { 877 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 878 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 879 } 880 return try allocator.dupe(u8, names[0]); 881 } 882 883 fn isLoopbackSshUrl(url: []const u8) bool { 884 const uri = std.Uri.parse(url) catch return false; 885 const host = switch (uri.host orelse return false) { 886 .raw => |value| value, 887 .percent_encoded => |value| value, 888 }; 889 return std.mem.eql(u8, host, "localhost") or 890 std.mem.eql(u8, host, "127.0.0.1") or 891 std.mem.eql(u8, host, "::1") or 892 std.mem.eql(u8, host, "[::1]"); 893 } 894 895 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 896 var message: std.Io.Writer.Allocating = .init(allocator); 897 defer message.deinit(); 898 899 switch (source.repo_kind) { 900 inline else => |repo_kind| { 901 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 902 defer any_repo.deinit(io, allocator); 903 switch (any_repo) { 904 inline else => |*repo| { 905 const repo_opts = repo.self_repo_opts; 906 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 907 defer allocator.free(remote_name); 908 909 var config = try repo.listConfig(io, allocator); 910 defer config.deinit(); 911 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 912 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 913 const remote_section = try allocator.print("remote.{s}", .{remote_name}); 914 defer allocator.free(remote_section); 915 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 916 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 917 const push_url = remote_config.get("pushurl") orelse fetch_url; 918 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 919 const fetch_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 920 defer allocator.free(fetch_ssh_command); 921 const push_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 922 defer allocator.free(push_ssh_command); 923 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 924 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 925 926 const fetch_refspec = try allocator.print("+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 927 defer allocator.free(fetch_refspec); 928 var fetch_opts = fetch_transport_opts; 929 fetch_opts.refspecs = &.{fetch_refspec}; 930 message.clearRetainingCapacity(); 931 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 932 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 933 return err; 934 }; 935 936 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 937 const local_oid = try repo.readRef(io, evt.events_ref); 938 const remote_oid = try repo.readRef(io, remote_ref); 939 if (local_oid == null and remote_oid == null) { 940 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 941 return null; 942 } 943 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 944 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 945 946 const push_refspec = try allocator.print("refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 947 defer allocator.free(push_refspec); 948 message.clearRetainingCapacity(); 949 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 950 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 951 return err; 952 }; 953 message.clearRetainingCapacity(); 954 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 955 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 956 return err; 957 }; 958 }, 959 } 960 }, 961 } 962 return null; 963 } 964