╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
644 645 pub const View = struct { 646 box: wgt.Box(ui.Widget), 647 session: *ui.Session, 648 tab_ids: [2]usize, 649 button_id: ?usize, 650 651 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 652 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 653 errdefer box.deinit(allocator); 654 var tab_ids: [2]usize = undefined; 655 const view_labels = [_][]const u8{ active_tab_label, removed_tab_label }; 656 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, view_labels, 0..) |view, view_label, i| { 657 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "", data.moment) orelse return error.RouteTooLong; 658 const count = data.window(view).count; 659 var label_buf: [64]u8 = undefined; 660 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ view_label, count }); 661 var tab = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 662 errdefer tab.deinit(allocator); 663 tab.getFocus().mode = .all; 664 const selected = std.meta.activeTag(session.data.current_page) == .repo_events and data.view == view; 665 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 666 tab_ids[i] = tab.getFocus().id; 667 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 668 } 669 670 var button_id: ?usize = null; 671 if (data.header.sync_status != null) { 672 box.getFocus().kind = .{ .custom = "form:/sync" }; 673 var button = try wgt.TextBox.init(allocator, "sync", .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 674 errdefer button.deinit(allocator); 675 button.getFocus().mode = .all; 676 button.getFocus().kind = .{ .custom = "submit" }; 677 button_id = button.getFocus().id; 678 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 679 } 680 681 if (data.header.sync_status) |status_text| { 682 var status = try wgt.TextBox.init(allocator, status_text, .{ .border_style = .hidden, .wrap_kind = .none }); 683 errdefer status.deinit(allocator); 684 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 685 } 686 box.getFocus().child_id = tab_ids[@intFromEnum(data.view)]; 687 return .{ .box = box, .session = session, .tab_ids = tab_ids, .button_id = button_id }; 688 } 689 690 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 691 self.box.deinit(allocator); 692 } 693 694 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 695 self.clearGrid(); 696 for (self.tab_ids) |id| { 697 const child = self.box.children.getPtr(id) orelse continue; 698 const tab = switch (child.widget) { 699 .text_box => |*text_box| text_box, 700 else => continue, 701 }; 702 ui.widget.markSelected(tab, self.box.getFocus().child_id == id); 703 } 704 try self.box.build(allocator, constraint, root_focus); 705 } 706 707 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 708 const selected = self.box.getFocus().child_id orelse return; 709 const current = self.box.children.getIndex(selected) orelse return; 710 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 711 if (inp.moveTab(key, current, count)) |next| { 712 root_focus.setFocus(self.box.children.keys()[next]); 713 return; 714 } 715 switch (key) { 716 .enter => if (self.syncFocused()) try self.requestSync(allocator), 717 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 718 else => {}, 719 } 720 } 721 722 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 723 if (!self.session.is_terminal) return; 724 if (self.session.host_request != null) return; 725 const button_id = self.button_id orelse return; 726 const child = self.box.children.getPtr(button_id) orelse return; 727 const button = switch (child.widget) { 728 .text_box => |*button| button, 729 else => return, 730 }; 731 try button.setContent(allocator, "syncing..."); 732 self.session.host_request = .sync_events; 733 } 734 735 fn syncFocused(self: *Header.View) bool { 736 const button_id = self.button_id orelse return false; 737 return self.box.getFocus().child_id == button_id; 738 } 739 740 pub fn getSelectedIndex(self: *Header.View) ?usize { 741 const id = self.box.getFocus().child_id orelse return null; 742 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 743 } 744 745 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 746 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 747 } 748 749 pub fn clearGrid(self: *Header.View) void { 750 self.box.clearGrid(); 751 } 752 753 pub fn getGrid(self: Header.View) ?Grid { 754 return self.box.getGrid(); 755 } 756 757 pub fn getFocus(self: *Header.View) *Focus { 758 return self.box.getFocus(); 759 } 760 }; 761 }; 762 763 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 764 if (comptime builtin.target.cpu.arch == .wasm32) return; 765 const io = session.io orelse return; 766 const source = session.local orelse return; 767 defer session.refresh_requested = true; 768 const failure = sync(io, allocator, source) catch |err| { 769 session.data.sync_failure = @errorName(err); 770 return; 771 }; 772 if (failure) |message| { 773 defer allocator.free(message); 774 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 775 return; 776 } 777 session.data.sync_failure = null; 778 switch (session.data.current_page) { 779 .repo_events => |route| { 780 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 781 session.data.current_page = first; 782 }, 783 else => {}, 784 } 785 } 786 787 fn remoteName( 788 comptime repo_kind: rp.RepoKind, 789 comptime repo_opts: rp.RepoOpts(repo_kind), 790 repo: *rp.Repo(repo_kind, repo_opts), 791 io: std.Io, 792 allocator: std.mem.Allocator, 793 ) !?[]u8 { 794 var remotes = try repo.listRemotes(io, allocator); 795 defer remotes.deinit(); 796 const names = remotes.sections.keys(); 797 if (names.len == 0) return null; 798 for (names) |name| { 799 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 800 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 801 } 802 return try allocator.dupe(u8, names[0]); 803 } 804 805 fn isLoopbackSshUrl(url: []const u8) bool { 806 const uri = std.Uri.parse(url) catch return false; 807 const host = switch (uri.host orelse return false) { 808 .raw => |value| value, 809 .percent_encoded => |value| value, 810 }; 811 return std.mem.eql(u8, host, "localhost") or 812 std.mem.eql(u8, host, "127.0.0.1") or 813 std.mem.eql(u8, host, "::1") or 814 std.mem.eql(u8, host, "[::1]"); 815 } 816 817 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 818 var message: std.Io.Writer.Allocating = .init(allocator); 819 defer message.deinit(); 820 821 switch (source.repo_kind) { 822 inline else => |repo_kind| { 823 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 824 defer any_repo.deinit(io, allocator); 825 switch (any_repo) { 826 inline else => |*repo| { 827 const repo_opts = repo.self_repo_opts; 828 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 829 defer allocator.free(remote_name); 830 831 var config = try repo.listConfig(io, allocator); 832 defer config.deinit(); 833 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 834 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 835 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 836 defer allocator.free(remote_section); 837 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 838 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 839 const push_url = remote_config.get("pushurl") orelse fetch_url; 840 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 841 const fetch_ssh_command = try std.fmt.allocPrint(allocator, "{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 842 defer allocator.free(fetch_ssh_command); 843 const push_ssh_command = try std.fmt.allocPrint(allocator, "{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 844 defer allocator.free(push_ssh_command); 845 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 846 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 847 848 const fetch_refspec = try std.fmt.allocPrint(allocator, "+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 849 defer allocator.free(fetch_refspec); 850 var fetch_opts = fetch_transport_opts; 851 fetch_opts.refspecs = &.{fetch_refspec}; 852 message.clearRetainingCapacity(); 853 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 854 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 855 return err; 856 }; 857 858 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 859 const local_oid = try repo.readRef(io, evt.events_ref); 860 const remote_oid = try repo.readRef(io, remote_ref); 861 if (local_oid == null and remote_oid == null) { 862 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 863 return null; 864 } 865 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 866 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 867 868 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 869 defer allocator.free(push_refspec); 870 message.clearRetainingCapacity(); 871 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 872 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 873 return err; 874 }; 875 message.clearRetainingCapacity(); 876 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 877 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 878 return err; 879 }; 880 }, 881 } 882 }, 883 } 884 return null; 885 } 886