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