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