╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
721 errdefer spacer.deinit(allocator); 722 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null, .flex = .grow }); 723 } 724 725 var button_id: ?usize = null; 726 if (data.header.sync_status != null) { 727 box.getFocus().kind = .{ .custom = "form:/sync" }; 728 var button = try wgt.TextBox.init(allocator, "sync", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 729 errdefer button.deinit(allocator); 730 button.getFocus().mode = .all; 731 button.getFocus().kind = .{ .custom = "submit" }; 732 button_id = button.getFocus().id; 733 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 734 } 735 736 if (data.header.sync_status) |status_text| { 737 var status = try wgt.TextBox.init(allocator, status_text, .{ .border = .hidden, .wrap_kind = .none }); 738 errdefer status.deinit(allocator); 739 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 740 } 741 box.getFocus().child_id = tab_ids[@backingInt(data.view)]; 742 return .{ .box = box, .session = session, .tab_ids = tab_ids, .selected_tab = @backingInt(data.view), .button_id = button_id }; 743 } 744 745 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 746 self.box.deinit(allocator); 747 } 748 749 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 750 self.clearGrid(); 751 if (self.getSelectedIndex()) |index| self.selected_tab = index; 752 for (self.tab_ids, 0..) |id, index| { 753 const child = self.box.children.getPtr(id) orelse continue; 754 const tab = switch (child.widget) { 755 .text_box => |*text_box| text_box, 756 else => continue, 757 }; 758 ui.widget.markSelected(tab, index == self.selected_tab); 759 } 760 try self.box.build(allocator, constraint, root_focus); 761 } 762 763 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 764 const selected = self.box.getFocus().child_id orelse return; 765 const stops = [_]?usize{ self.tab_ids[0], self.tab_ids[1], self.button_id }; 766 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 767 const current = std.mem.indexOfScalar(?usize, stops[0..count], selected) orelse return; 768 if (inp.moveTab(key, current, count)) |next| { 769 root_focus.setFocus(stops[next] orelse unreachable); 770 return; 771 } 772 switch (key) { 773 .enter => if (self.syncFocused()) try self.requestSync(allocator), 774 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 775 else => {}, 776 } 777 } 778 779 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 780 if (!self.session.is_terminal) return; 781 if (self.session.host_request != null) return; 782 const button_id = self.button_id orelse return; 783 const child = self.box.children.getPtr(button_id) orelse return; 784 const button = switch (child.widget) { 785 .text_box => |*button| button, 786 else => return, 787 }; 788 try button.setContent(allocator, "syncing..."); 789 self.session.host_request = .sync_events; 790 } 791 792 fn syncFocused(self: *Header.View) bool { 793 const button_id = self.button_id orelse return false; 794 return self.box.getFocus().child_id == button_id; 795 } 796 797 pub fn getSelectedIndex(self: *Header.View) ?usize { 798 const id = self.box.getFocus().child_id orelse return null; 799 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 800 } 801 802 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 803 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 804 } 805 806 pub fn clearGrid(self: *Header.View) void { 807 self.box.clearGrid(); 808 } 809 810 pub fn getGrid(self: Header.View) ?Grid { 811 return self.box.getGrid(); 812 } 813 814 pub fn getFocus(self: *Header.View) *Focus { 815 return self.box.getFocus(); 816 } 817 }; 818 }; 819 820 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 821 if (comptime builtin.target.cpu.arch == .wasm32) return; 822 const io = session.io orelse return; 823 const source = session.local orelse return; 824 defer session.refresh_requested = true; 825 const failure = sync(io, allocator, source) catch |err| { 826 session.data.sync_failure = @errorName(err); 827 return; 828 }; 829 if (failure) |message| { 830 defer allocator.free(message); 831 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 832 return; 833 } 834 session.data.sync_failure = null; 835 switch (session.data.current_page) { 836 .repo_events => |route| { 837 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 838 session.data.current_page = first; 839 }, 840 else => {}, 841 } 842 } 843 844 fn remoteName( 845 comptime repo_kind: rp.RepoKind, 846 comptime repo_opts: rp.RepoOpts(repo_kind), 847 repo: *rp.Repo(repo_kind, repo_opts), 848 io: std.Io, 849 allocator: std.mem.Allocator, 850 ) !?[]u8 { 851 var remotes = try repo.listRemotes(io, allocator); 852 defer remotes.deinit(); 853 const names = remotes.sections.keys(); 854 if (names.len == 0) return null; 855 for (names) |name| { 856 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 857 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 858 } 859 return try allocator.dupe(u8, names[0]); 860 } 861 862 fn isLoopbackSshUrl(url: []const u8) bool { 863 const uri = std.Uri.parse(url) catch return false; 864 const host = switch (uri.host orelse return false) { 865 .raw => |value| value, 866 .percent_encoded => |value| value, 867 }; 868 return std.mem.eql(u8, host, "localhost") or 869 std.mem.eql(u8, host, "127.0.0.1") or 870 std.mem.eql(u8, host, "::1") or 871 std.mem.eql(u8, host, "[::1]"); 872 } 873 874 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 875 var message: std.Io.Writer.Allocating = .init(allocator); 876 defer message.deinit(); 877 878 switch (source.repo_kind) { 879 inline else => |repo_kind| { 880 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 881 defer any_repo.deinit(io, allocator); 882 switch (any_repo) { 883 inline else => |*repo| { 884 const repo_opts = repo.self_repo_opts; 885 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 886 defer allocator.free(remote_name); 887 888 var config = try repo.listConfig(io, allocator); 889 defer config.deinit(); 890 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 891 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 892 const remote_section = try allocator.print("remote.{s}", .{remote_name}); 893 defer allocator.free(remote_section); 894 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 895 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 896 const push_url = remote_config.get("pushurl") orelse fetch_url; 897 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 898 const fetch_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 899 defer allocator.free(fetch_ssh_command); 900 const push_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 901 defer allocator.free(push_ssh_command); 902 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 903 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 904 905 const fetch_refspec = try allocator.print("+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 906 defer allocator.free(fetch_refspec); 907 var fetch_opts = fetch_transport_opts; 908 fetch_opts.refspecs = &.{fetch_refspec}; 909 message.clearRetainingCapacity(); 910 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 911 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 912 return err; 913 }; 914 915 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 916 const local_oid = try repo.readRef(io, evt.events_ref); 917 const remote_oid = try repo.readRef(io, remote_ref); 918 if (local_oid == null and remote_oid == null) { 919 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 920 return null; 921 } 922 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 923 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 924 925 const push_refspec = try allocator.print("refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 926 defer allocator.free(push_refspec); 927 message.clearRetainingCapacity(); 928 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 929 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 930 return err; 931 }; 932 message.clearRetainingCapacity(); 933 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 934 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 935 return err; 936 }; 937 }, 938 } 939 }, 940 } 941 return null; 942 } 943