╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
802 const child = self.scroll.row().children.getPtr(button_id) orelse return; 803 const button = switch (child.widget) { 804 .text_box => |*button| button, 805 else => return, 806 }; 807 try button.setContent(allocator, "syncing..."); 808 self.session.host_request = .sync_events; 809 } 810 811 fn syncFocused(self: *Header.View) bool { 812 const button_id = self.button_id orelse return false; 813 return self.scroll.row().getFocus().child_id == button_id; 814 } 815 816 pub fn getSelectedIndex(self: *Header.View) ?usize { 817 const id = self.scroll.row().getFocus().child_id orelse return null; 818 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 819 } 820 821 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 822 root_focus.setFocus(self.scroll.row().getFocus().child_id orelse self.tab_ids[0]); 823 } 824 825 pub fn clearGrid(self: *Header.View) void { 826 self.scroll.clearGrid(); 827 } 828 829 pub fn getGrid(self: Header.View) ?Grid { 830 return self.scroll.getGrid(); 831 } 832 833 pub fn getFocus(self: *Header.View) *Focus { 834 return self.scroll.getFocus(); 835 } 836 }; 837 }; 838 839 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 840 if (comptime builtin.target.cpu.arch == .wasm32) return; 841 const io = session.io orelse return; 842 const source = session.local orelse return; 843 defer session.refresh_requested = true; 844 const failure = sync(io, allocator, source) catch |err| { 845 session.data.sync_failure = @errorName(err); 846 return; 847 }; 848 if (failure) |message| { 849 defer allocator.free(message); 850 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 851 return; 852 } 853 session.data.sync_failure = null; 854 switch (session.data.current_page) { 855 .repo_events => |route| { 856 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 857 session.data.current_page = first; 858 }, 859 else => {}, 860 } 861 } 862 863 fn remoteName( 864 comptime repo_kind: rp.RepoKind, 865 comptime repo_opts: rp.RepoOpts(repo_kind), 866 repo: *rp.Repo(repo_kind, repo_opts), 867 io: std.Io, 868 allocator: std.mem.Allocator, 869 ) !?[]u8 { 870 var remotes = try repo.listRemotes(io, allocator); 871 defer remotes.deinit(); 872 const names = remotes.sections.keys(); 873 if (names.len == 0) return null; 874 for (names) |name| { 875 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 876 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 877 } 878 return try allocator.dupe(u8, names[0]); 879 } 880 881 fn isLoopbackSshUrl(url: []const u8) bool { 882 const uri = std.Uri.parse(url) catch return false; 883 const host = switch (uri.host orelse return false) { 884 .raw => |value| value, 885 .percent_encoded => |value| value, 886 }; 887 return std.mem.eql(u8, host, "localhost") or 888 std.mem.eql(u8, host, "127.0.0.1") or 889 std.mem.eql(u8, host, "::1") or 890 std.mem.eql(u8, host, "[::1]"); 891 } 892 893 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 894 var message: std.Io.Writer.Allocating = .init(allocator); 895 defer message.deinit(); 896 897 switch (source.repo_kind) { 898 inline else => |repo_kind| { 899 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 900 defer any_repo.deinit(io, allocator); 901 switch (any_repo) { 902 inline else => |*repo| { 903 const repo_opts = repo.self_repo_opts; 904 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 905 defer allocator.free(remote_name); 906 907 var config = try repo.listConfig(io, allocator); 908 defer config.deinit(); 909 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 910 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 911 const remote_section = try allocator.print("remote.{s}", .{remote_name}); 912 defer allocator.free(remote_section); 913 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 914 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 915 const push_url = remote_config.get("pushurl") orelse fetch_url; 916 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 917 const fetch_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 918 defer allocator.free(fetch_ssh_command); 919 const push_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 920 defer allocator.free(push_ssh_command); 921 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 922 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 923 924 const fetch_refspec = try allocator.print("+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 925 defer allocator.free(fetch_refspec); 926 var fetch_opts = fetch_transport_opts; 927 fetch_opts.refspecs = &.{fetch_refspec}; 928 message.clearRetainingCapacity(); 929 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 930 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 931 return err; 932 }; 933 934 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 935 const local_oid = try repo.readRef(io, evt.events_ref); 936 const remote_oid = try repo.readRef(io, remote_ref); 937 if (local_oid == null and remote_oid == null) { 938 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 939 return null; 940 } 941 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 942 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 943 944 const push_refspec = try allocator.print("refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 945 defer allocator.free(push_refspec); 946 message.clearRetainingCapacity(); 947 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 948 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 949 return err; 950 }; 951 message.clearRetainingCapacity(); 952 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 953 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 954 return err; 955 }; 956 }, 957 } 958 }, 959 } 960 return null; 961 } 962