╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
693 failure: ?[]const u8, 694 ) !Header { 695 if (failure != null) return Header.empty(aa, failure); 696 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 697 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 698 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 699 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 700 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 701 } 702 703 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 704 return .{ .sync_status = if (failure) |name| try aa.print("error: {s}", .{name}) else nothing }; 705 } 706 707 pub fn remote() Header { 708 return .{ .sync_status = null }; 709 } 710 711 pub const View = struct { 712 box: wgt.Box(ui.Widget), 713 session: *ui.Session, 714 tab_ids: [2]usize, 715 // the tab stays selected while the sync button has focus 716 selected_tab: usize, 717 button_id: ?usize, 718 719 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 720 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 721 errdefer box.deinit(allocator); 722 var tab_ids: [2]usize = undefined; 723 const view_labels = [_][]const u8{ active_tab_label, removed_tab_label }; 724 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, view_labels, 0..) |view, view_label, i| { 725 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "", data.moment) orelse return error.RouteTooLong; 726 const count_label = try session.page_arena.allocator().print("({d})", .{data.window(view).count}); 727 var tab = try wgt.TextBox.init(allocator, view_label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = count_label } }); 728 errdefer tab.deinit(allocator); 729 tab.getFocus().mode = .all; 730 const selected = std.meta.activeTag(session.data.current_page) == .repo_events and data.view == view; 731 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 732 tab_ids[i] = tab.getFocus().id; 733 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = @max(view_label.len, count_label.len) + 2, .height = null } }); 734 } 735 736 // pushes the sync button and status to the right 737 { 738 var spacer = try ui.widget.Spacer.init(allocator); 739 errdefer spacer.deinit(allocator); 740 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null, .flex = .grow }); 741 } 742 743 var button_id: ?usize = null; 744 if (data.header.sync_status != null) { 745 box.getFocus().kind = .{ .custom = "form:/sync" }; 746 var button = try wgt.TextBox.init(allocator, "sync", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 747 errdefer button.deinit(allocator); 748 button.getFocus().mode = .all; 749 button.getFocus().kind = .{ .custom = "submit" }; 750 button_id = button.getFocus().id; 751 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 752 } 753 754 if (data.header.sync_status) |status_text| { 755 var status = try wgt.TextBox.init(allocator, status_text, .{ .border = .hidden, .wrap_kind = .none }); 756 errdefer status.deinit(allocator); 757 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 758 } 759 box.getFocus().child_id = tab_ids[@backingInt(data.view)]; 760 return .{ .box = box, .session = session, .tab_ids = tab_ids, .selected_tab = @backingInt(data.view), .button_id = button_id }; 761 } 762 763 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 764 self.box.deinit(allocator); 765 } 766 767 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 768 self.clearGrid(); 769 if (self.getSelectedIndex()) |index| self.selected_tab = index; 770 for (self.tab_ids, 0..) |id, index| { 771 const child = self.box.children.getPtr(id) orelse continue; 772 const tab = switch (child.widget) { 773 .text_box => |*text_box| text_box, 774 else => continue, 775 }; 776 ui.widget.markSelected(tab, index == self.selected_tab); 777 } 778 try self.box.build(allocator, constraint, root_focus); 779 } 780 781 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 782 const selected = self.box.getFocus().child_id orelse return; 783 const stops = [_]?usize{ self.tab_ids[0], self.tab_ids[1], self.button_id }; 784 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 785 const current = std.mem.indexOfScalar(?usize, stops[0..count], selected) orelse return; 786 if (inp.moveTab(key, current, count)) |next| { 787 root_focus.setFocus(stops[next] orelse unreachable); 788 return; 789 } 790 switch (key) { 791 .enter => if (self.syncFocused()) try self.requestSync(allocator), 792 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 793 else => {}, 794 } 795 } 796 797 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 798 if (!self.session.is_terminal) return; 799 if (self.session.host_request != null) return; 800 const button_id = self.button_id orelse return; 801 const child = self.box.children.getPtr(button_id) orelse return; 802 const button = switch (child.widget) { 803 .text_box => |*button| button, 804 else => return, 805 }; 806 try button.setContent(allocator, "syncing..."); 807 self.session.host_request = .sync_events; 808 } 809 810 fn syncFocused(self: *Header.View) bool { 811 const button_id = self.button_id orelse return false; 812 return self.box.getFocus().child_id == button_id; 813 } 814 815 pub fn getSelectedIndex(self: *Header.View) ?usize { 816 const id = self.box.getFocus().child_id orelse return null; 817 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 818 } 819 820 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 821 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 822 } 823 824 pub fn clearGrid(self: *Header.View) void { 825 self.box.clearGrid(); 826 } 827 828 pub fn getGrid(self: Header.View) ?Grid { 829 return self.box.getGrid(); 830 } 831 832 pub fn getFocus(self: *Header.View) *Focus { 833 return self.box.getFocus(); 834 } 835 }; 836 }; 837 838 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 839 if (comptime builtin.target.cpu.arch == .wasm32) return; 840 const io = session.io orelse return; 841 const source = session.local orelse return; 842 defer session.refresh_requested = true; 843 const failure = sync(io, allocator, source) catch |err| { 844 session.data.sync_failure = @errorName(err); 845 return; 846 }; 847 if (failure) |message| { 848 defer allocator.free(message); 849 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 850 return; 851 } 852 session.data.sync_failure = null; 853 switch (session.data.current_page) { 854 .repo_events => |route| { 855 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 856 session.data.current_page = first; 857 }, 858 else => {}, 859 } 860 } 861 862 fn remoteName( 863 comptime repo_kind: rp.RepoKind, 864 comptime repo_opts: rp.RepoOpts(repo_kind), 865 repo: *rp.Repo(repo_kind, repo_opts), 866 io: std.Io, 867 allocator: std.mem.Allocator, 868 ) !?[]u8 { 869 var remotes = try repo.listRemotes(io, allocator); 870 defer remotes.deinit(); 871 const names = remotes.sections.keys(); 872 if (names.len == 0) return null; 873 for (names) |name| { 874 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 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