╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
624 const Progress = struct { 625 message: *std.Io.Writer.Allocating, 626 627 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 628 switch (event) { 629 .text => |value| { 630 self.message.clearRetainingCapacity(); 631 try self.message.writer.writeAll(value); 632 }, 633 else => {}, 634 } 635 } 636 }; 637 638 pub fn local( 639 comptime repo_kind: rp.RepoKind, 640 comptime repo_opts: rp.RepoOpts(repo_kind), 641 aa: std.mem.Allocator, 642 repo: *rp.Repo(repo_kind, repo_opts), 643 io: std.Io, 644 failure: ?[]const u8, 645 ) !Header { 646 if (failure != null) return Header.empty(aa, failure); 647 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 648 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 649 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 650 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 651 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 652 } 653 654 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 655 return .{ .sync_status = if (failure) |name| try std.fmt.allocPrint(aa, "error: {s}", .{name}) else nothing }; 656 } 657 658 pub fn remote() Header { 659 return .{ .sync_status = null }; 660 } 661 662 pub const View = struct { 663 box: wgt.Box(ui.Widget), 664 session: *ui.Session, 665 tab_ids: [2]usize, 666 button_id: ?usize, 667 668 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 669 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 670 errdefer box.deinit(allocator); 671 var tab_ids: [2]usize = undefined; 672 const view_labels = [_][]const u8{ active_tab_label, removed_tab_label }; 673 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, view_labels, 0..) |view, view_label, i| { 674 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "", data.moment) orelse return error.RouteTooLong; 675 const count = data.window(view).count; 676 var label_buf: [64]u8 = undefined; 677 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ view_label, count }); 678 var tab = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 679 errdefer tab.deinit(allocator); 680 tab.getFocus().mode = .all; 681 const selected = std.meta.activeTag(session.data.current_page) == .repo_events and data.view == view; 682 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 683 tab_ids[i] = tab.getFocus().id; 684 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 685 } 686 687 var button_id: ?usize = null; 688 if (data.header.sync_status != null) { 689 box.getFocus().kind = .{ .custom = "form:/sync" }; 690 var button = try wgt.TextBox.init(allocator, "sync", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 691 errdefer button.deinit(allocator); 692 button.getFocus().mode = .all; 693 button.getFocus().kind = .{ .custom = "submit" }; 694 button_id = button.getFocus().id; 695 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 696 } 697 698 if (data.header.sync_status) |status_text| { 699 var status = try wgt.TextBox.init(allocator, status_text, .{ .border_style = .hidden, .wrap_kind = .none }); 700 errdefer status.deinit(allocator); 701 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 702 } 703 box.getFocus().child_id = tab_ids[@intFromEnum(data.view)]; 704 return .{ .box = box, .session = session, .tab_ids = tab_ids, .button_id = button_id }; 705 } 706 707 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 708 self.box.deinit(allocator); 709 } 710 711 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 712 self.clearGrid(); 713 for (self.tab_ids) |id| { 714 const child = self.box.children.getPtr(id) orelse continue; 715 const tab = switch (child.widget) { 716 .text_box => |*text_box| text_box, 717 else => continue, 718 }; 719 tab.options.border_style = if (self.box.getFocus().child_id == id) .single else .hidden; 720 } 721 try self.box.build(allocator, constraint, root_focus); 722 } 723 724 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 725 const selected = self.box.getFocus().child_id orelse return; 726 const current = self.box.children.getIndex(selected) orelse return; 727 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 728 if (inp.moveTab(key, current, count)) |next| { 729 root_focus.setFocus(self.box.children.keys()[next]); 730 return; 731 } 732 switch (key) { 733 .enter => if (self.syncFocused()) try self.requestSync(allocator), 734 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 735 else => {}, 736 } 737 } 738 739 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 740 if (!self.session.is_terminal) return; 741 if (self.session.host_request != null) return; 742 const button_id = self.button_id orelse return; 743 const child = self.box.children.getPtr(button_id) orelse return; 744 const button = switch (child.widget) { 745 .text_box => |*button| button, 746 else => return, 747 }; 748 try button.setContent(allocator, "syncing..."); 749 self.session.host_request = .sync_events; 750 } 751 752 fn syncFocused(self: *Header.View) bool { 753 const button_id = self.button_id orelse return false; 754 return self.box.getFocus().child_id == button_id; 755 } 756 757 pub fn getSelectedIndex(self: *Header.View) ?usize { 758 const id = self.box.getFocus().child_id orelse return null; 759 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 760 } 761 762 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 763 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 764 } 765 766 pub fn clearGrid(self: *Header.View) void { 767 self.box.clearGrid(); 768 } 769 770 pub fn getGrid(self: Header.View) ?Grid { 771 return self.box.getGrid(); 772 } 773 774 pub fn getFocus(self: *Header.View) *Focus { 775 return self.box.getFocus(); 776 } 777 }; 778 }; 779 780 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 781 if (comptime builtin.target.cpu.arch == .wasm32) return; 782 const io = session.io orelse return; 783 const source = session.local orelse return; 784 defer session.refresh_requested = true; 785 const failure = sync(io, allocator, source) catch |err| { 786 session.data.sync_failure = @errorName(err); 787 return; 788 }; 789 if (failure) |message| { 790 defer allocator.free(message); 791 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 792 return; 793 } 794 session.data.sync_failure = null; 795 switch (session.data.current_page) { 796 .repo_events => |route| { 797 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 798 session.data.current_page = first; 799 }, 800 else => {}, 801 } 802 } 803 804 fn remoteName( 805 comptime repo_kind: rp.RepoKind, 806 comptime repo_opts: rp.RepoOpts(repo_kind), 807 repo: *rp.Repo(repo_kind, repo_opts), 808 io: std.Io, 809 allocator: std.mem.Allocator, 810 ) !?[]u8 { 811 var remotes = try repo.listRemotes(io, allocator); 812 defer remotes.deinit(); 813 const names = remotes.sections.keys(); 814 if (names.len == 0) return null; 815 for (names) |name| { 816 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 817 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 818 } 819 return try allocator.dupe(u8, names[0]); 820 } 821 822 fn isLoopbackSshUrl(url: []const u8) bool { 823 const uri = std.Uri.parse(url) catch return false; 824 const host = switch (uri.host orelse return false) { 825 .raw => |value| value, 826 .percent_encoded => |value| value, 827 }; 828 return std.mem.eql(u8, host, "localhost") or 829 std.mem.eql(u8, host, "127.0.0.1") or 830 std.mem.eql(u8, host, "::1") or 831 std.mem.eql(u8, host, "[::1]"); 832 } 833 834 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 835 var message: std.Io.Writer.Allocating = .init(allocator); 836 defer message.deinit(); 837 838 switch (source.repo_kind) { 839 inline else => |repo_kind| { 840 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 841 defer any_repo.deinit(io, allocator); 842 switch (any_repo) { 843 inline else => |*repo| { 844 const repo_opts = repo.self_repo_opts; 845 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 846 defer allocator.free(remote_name); 847 848 var config = try repo.listConfig(io, allocator); 849 defer config.deinit(); 850 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 851 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 852 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 853 defer allocator.free(remote_section); 854 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 855 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 856 const push_url = remote_config.get("pushurl") orelse fetch_url; 857 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 858 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 "" }); 859 defer allocator.free(fetch_ssh_command); 860 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 "" }); 861 defer allocator.free(push_ssh_command); 862 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 863 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 864 865 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 }); 866 defer allocator.free(fetch_refspec); 867 var fetch_opts = fetch_transport_opts; 868 fetch_opts.refspecs = &.{fetch_refspec}; 869 message.clearRetainingCapacity(); 870 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 871 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 872 return err; 873 }; 874 875 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 876 const local_oid = try repo.readRef(io, evt.events_ref); 877 const remote_oid = try repo.readRef(io, remote_ref); 878 if (local_oid == null and remote_oid == null) { 879 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 880 return null; 881 } 882 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 883 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 884 885 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 886 defer allocator.free(push_refspec); 887 message.clearRetainingCapacity(); 888 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 889 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 890 return err; 891 }; 892 message.clearRetainingCapacity(); 893 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 894 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 895 return err; 896 }; 897 }, 898 } 899 }, 900 } 901 return null; 902 } 903