╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
749 const child = self.box.children.getPtr(button_id) orelse return; 750 const button = switch (child.widget) { 751 .text_box => |*button| button, 752 else => return, 753 }; 754 try button.setContent(allocator, "syncing..."); 755 self.session.host_request = .sync_events; 756 } 757 758 fn syncFocused(self: *Header.View) bool { 759 const button_id = self.button_id orelse return false; 760 return self.box.getFocus().child_id == button_id; 761 } 762 763 pub fn getSelectedIndex(self: *Header.View) ?usize { 764 const id = self.box.getFocus().child_id orelse return null; 765 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 766 } 767 768 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 769 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 770 } 771 772 pub fn clearGrid(self: *Header.View) void { 773 self.box.clearGrid(); 774 } 775 776 pub fn getGrid(self: Header.View) ?Grid { 777 return self.box.getGrid(); 778 } 779 780 pub fn getFocus(self: *Header.View) *Focus { 781 return self.box.getFocus(); 782 } 783 }; 784 }; 785 786 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 787 if (comptime builtin.target.cpu.arch == .wasm32) return; 788 const io = session.io orelse return; 789 const source = session.local orelse return; 790 defer session.refresh_requested = true; 791 const failure = sync(io, allocator, source) catch |err| { 792 session.data.sync_failure = @errorName(err); 793 return; 794 }; 795 if (failure) |message| { 796 defer allocator.free(message); 797 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 798 return; 799 } 800 session.data.sync_failure = null; 801 switch (session.data.current_page) { 802 .repo_events => |route| { 803 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 804 session.data.current_page = first; 805 }, 806 else => {}, 807 } 808 } 809 810 fn remoteName( 811 comptime repo_kind: rp.RepoKind, 812 comptime repo_opts: rp.RepoOpts(repo_kind), 813 repo: *rp.Repo(repo_kind, repo_opts), 814 io: std.Io, 815 allocator: std.mem.Allocator, 816 ) !?[]u8 { 817 var remotes = try repo.listRemotes(io, allocator); 818 defer remotes.deinit(); 819 const names = remotes.sections.keys(); 820 if (names.len == 0) return null; 821 for (names) |name| { 822 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 823 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 824 } 825 return try allocator.dupe(u8, names[0]); 826 } 827 828 fn isLoopbackSshUrl(url: []const u8) bool { 829 const uri = std.Uri.parse(url) catch return false; 830 const host = switch (uri.host orelse return false) { 831 .raw => |value| value, 832 .percent_encoded => |value| value, 833 }; 834 return std.mem.eql(u8, host, "localhost") or 835 std.mem.eql(u8, host, "127.0.0.1") or 836 std.mem.eql(u8, host, "::1") or 837 std.mem.eql(u8, host, "[::1]"); 838 } 839 840 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 841 var message: std.Io.Writer.Allocating = .init(allocator); 842 defer message.deinit(); 843 844 switch (source.repo_kind) { 845 inline else => |repo_kind| { 846 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 847 defer any_repo.deinit(io, allocator); 848 switch (any_repo) { 849 inline else => |*repo| { 850 const repo_opts = repo.self_repo_opts; 851 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 852 defer allocator.free(remote_name); 853 854 var config = try repo.listConfig(io, allocator); 855 defer config.deinit(); 856 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 857 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 858 const remote_section = try allocator.print("remote.{s}", .{remote_name}); 859 defer allocator.free(remote_section); 860 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 861 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 862 const push_url = remote_config.get("pushurl") orelse fetch_url; 863 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 864 const fetch_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 865 defer allocator.free(fetch_ssh_command); 866 const push_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 867 defer allocator.free(push_ssh_command); 868 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 869 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 870 871 const fetch_refspec = try allocator.print("+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 872 defer allocator.free(fetch_refspec); 873 var fetch_opts = fetch_transport_opts; 874 fetch_opts.refspecs = &.{fetch_refspec}; 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 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 882 const local_oid = try repo.readRef(io, evt.events_ref); 883 const remote_oid = try repo.readRef(io, remote_ref); 884 if (local_oid == null and remote_oid == null) { 885 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 886 return null; 887 } 888 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 889 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 890 891 const push_refspec = try allocator.print("refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 892 defer allocator.free(push_refspec); 893 message.clearRetainingCapacity(); 894 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 895 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 896 return err; 897 }; 898 message.clearRetainingCapacity(); 899 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 900 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 901 return err; 902 }; 903 }, 904 } 905 }, 906 } 907 return null; 908 } 909