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