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