╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
875 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 876 } 877 return try allocator.dupe(u8, names[0]); 878 } 879 880 fn isLoopbackSshUrl(url: []const u8) bool { 881 const uri = std.Uri.parse(url) catch return false; 882 const host = switch (uri.host orelse return false) { 883 .raw => |value| value, 884 .percent_encoded => |value| value, 885 }; 886 return std.mem.eql(u8, host, "localhost") or 887 std.mem.eql(u8, host, "127.0.0.1") or 888 std.mem.eql(u8, host, "::1") or 889 std.mem.eql(u8, host, "[::1]"); 890 } 891 892 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 893 var message: std.Io.Writer.Allocating = .init(allocator); 894 defer message.deinit(); 895 896 switch (source.repo_kind) { 897 inline else => |repo_kind| { 898 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 899 defer any_repo.deinit(io, allocator); 900 switch (any_repo) { 901 inline else => |*repo| { 902 const repo_opts = repo.self_repo_opts; 903 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 904 defer allocator.free(remote_name); 905 906 var config = try repo.listConfig(io, allocator); 907 defer config.deinit(); 908 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 909 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 910 const remote_section = try allocator.print("remote.{s}", .{remote_name}); 911 defer allocator.free(remote_section); 912 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 913 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 914 const push_url = remote_config.get("pushurl") orelse fetch_url; 915 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 916 const fetch_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 917 defer allocator.free(fetch_ssh_command); 918 const push_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 919 defer allocator.free(push_ssh_command); 920 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 921 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 922 923 const fetch_refspec = try allocator.print("+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 924 defer allocator.free(fetch_refspec); 925 var fetch_opts = fetch_transport_opts; 926 fetch_opts.refspecs = &.{fetch_refspec}; 927 message.clearRetainingCapacity(); 928 repo.fetch(io, allocator, remote_name, fetch_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 933 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 934 const local_oid = try repo.readRef(io, evt.events_ref); 935 const remote_oid = try repo.readRef(io, remote_ref); 936 if (local_oid == null and remote_oid == null) { 937 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 938 return null; 939 } 940 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 941 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 942 943 const push_refspec = try allocator.print("refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 944 defer allocator.free(push_refspec); 945 message.clearRetainingCapacity(); 946 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 947 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 948 return err; 949 }; 950 message.clearRetainingCapacity(); 951 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 952 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 953 return err; 954 }; 955 }, 956 } 957 }, 958 } 959 return null; 960 } 961