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