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