╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
712 fn remoteName( 713 comptime repo_kind: rp.RepoKind, 714 comptime repo_opts: rp.RepoOpts(repo_kind), 715 repo: *rp.Repo(repo_kind, repo_opts), 716 io: std.Io, 717 allocator: std.mem.Allocator, 718 ) !?[]u8 { 719 var remotes = try repo.listRemotes(io, allocator); 720 defer remotes.deinit(); 721 const names = remotes.sections.keys(); 722 if (names.len == 0) return null; 723 for (names) |name| { 724 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 725 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 726 } 727 return try allocator.dupe(u8, names[0]); 728 } 729 730 fn isLoopbackSshUrl(url: []const u8) bool { 731 const uri = std.Uri.parse(url) catch return false; 732 const host = switch (uri.host orelse return false) { 733 .raw => |value| value, 734 .percent_encoded => |value| value, 735 }; 736 return std.mem.eql(u8, host, "localhost") or 737 std.mem.eql(u8, host, "127.0.0.1") or 738 std.mem.eql(u8, host, "::1") or 739 std.mem.eql(u8, host, "[::1]"); 740 } 741 742 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 743 var message: std.Io.Writer.Allocating = .init(allocator); 744 defer message.deinit(); 745 746 switch (source.repo_kind) { 747 inline else => |repo_kind| { 748 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 749 defer any_repo.deinit(io, allocator); 750 switch (any_repo) { 751 inline else => |*repo| { 752 const repo_opts = repo.self_repo_opts; 753 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 754 defer allocator.free(remote_name); 755 756 var config = try repo.listConfig(io, allocator); 757 defer config.deinit(); 758 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 759 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 760 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 761 defer allocator.free(remote_section); 762 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 763 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 764 const push_url = remote_config.get("pushurl") orelse fetch_url; 765 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 766 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 "" }); 767 defer allocator.free(fetch_ssh_command); 768 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 "" }); 769 defer allocator.free(push_ssh_command); 770 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 771 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 772 773 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 }); 774 defer allocator.free(fetch_refspec); 775 var fetch_opts = fetch_transport_opts; 776 fetch_opts.refspecs = &.{fetch_refspec}; 777 message.clearRetainingCapacity(); 778 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 779 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 780 return err; 781 }; 782 783 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 784 const local_oid = try repo.readRef(io, evt.events_ref); 785 const remote_oid = try repo.readRef(io, remote_ref); 786 if (local_oid == null and remote_oid == null) { 787 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 788 return null; 789 } 790 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 791 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 792 793 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 794 defer allocator.free(push_refspec); 795 message.clearRetainingCapacity(); 796 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 797 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 798 return err; 799 }; 800 message.clearRetainingCapacity(); 801 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 802 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 803 return err; 804 }; 805 }, 806 } 807 }, 808 } 809 return null; 810 } 811