╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
633 try self.box.build(allocator, constraint, root_focus); 634 } 635 636 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 637 const selected = self.box.getFocus().child_id orelse return; 638 const current = self.box.children.getIndex(selected) orelse return; 639 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 640 if (inp.moveTab(key, current, count)) |next| { 641 root_focus.setFocus(self.box.children.keys()[next]); 642 return; 643 } 644 switch (key) { 645 .enter => if (self.syncFocused()) try self.requestSync(allocator), 646 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 647 else => {}, 648 } 649 } 650 651 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 652 if (!self.session.is_terminal) return; 653 if (self.session.host_request != null) return; 654 const button_id = self.button_id orelse return; 655 const child = self.box.children.getPtr(button_id) orelse return; 656 const button = switch (child.widget) { 657 .text_box => |*button| button, 658 else => return, 659 }; 660 try button.setContent(allocator, "syncing..."); 661 self.session.host_request = .sync_events; 662 } 663 664 fn syncFocused(self: *Header.View) bool { 665 const button_id = self.button_id orelse return false; 666 return self.box.getFocus().child_id == button_id; 667 } 668 669 pub fn getSelectedIndex(self: *Header.View) ?usize { 670 const id = self.box.getFocus().child_id orelse return null; 671 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 672 } 673 674 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 675 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 676 } 677 678 pub fn clearGrid(self: *Header.View) void { 679 self.box.clearGrid(); 680 } 681 682 pub fn getGrid(self: Header.View) ?Grid { 683 return self.box.getGrid(); 684 } 685 686 pub fn getFocus(self: *Header.View) *Focus { 687 return self.box.getFocus(); 688 } 689 }; 690 }; 691 692 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 693 if (comptime builtin.target.cpu.arch == .wasm32) return; 694 const io = session.io orelse return; 695 const source = session.local orelse return; 696 defer session.refresh_requested = true; 697 const failure = sync(io, allocator, source) catch |err| { 698 session.data.sync_failure = @errorName(err); 699 return; 700 }; 701 if (failure) |message| { 702 defer allocator.free(message); 703 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 704 return; 705 } 706 session.data.sync_failure = null; 707 switch (session.data.current_page) { 708 .repo_events => |route| { 709 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "")) |first| 710 session.data.current_page = first; 711 }, 712 else => {}, 713 } 714 } 715 716 fn remoteName( 717 comptime repo_kind: rp.RepoKind, 718 comptime repo_opts: rp.RepoOpts(repo_kind), 719 repo: *rp.Repo(repo_kind, repo_opts), 720 io: std.Io, 721 allocator: std.mem.Allocator, 722 ) !?[]u8 { 723 var remotes = try repo.listRemotes(io, allocator); 724 defer remotes.deinit(); 725 const names = remotes.sections.keys(); 726 if (names.len == 0) return null; 727 for (names) |name| { 728 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 729 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 730 } 731 return try allocator.dupe(u8, names[0]); 732 } 733 734 fn isLoopbackSshUrl(url: []const u8) bool { 735 const uri = std.Uri.parse(url) catch return false; 736 const host = switch (uri.host orelse return false) { 737 .raw => |value| value, 738 .percent_encoded => |value| value, 739 }; 740 return std.mem.eql(u8, host, "localhost") or 741 std.mem.eql(u8, host, "127.0.0.1") or 742 std.mem.eql(u8, host, "::1") or 743 std.mem.eql(u8, host, "[::1]"); 744 } 745 746 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 747 var message: std.Io.Writer.Allocating = .init(allocator); 748 defer message.deinit(); 749 750 switch (source.repo_kind) { 751 inline else => |repo_kind| { 752 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 753 defer any_repo.deinit(io, allocator); 754 switch (any_repo) { 755 inline else => |*repo| { 756 const repo_opts = repo.self_repo_opts; 757 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 758 defer allocator.free(remote_name); 759 760 var config = try repo.listConfig(io, allocator); 761 defer config.deinit(); 762 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 763 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 764 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 765 defer allocator.free(remote_section); 766 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 767 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 768 const push_url = remote_config.get("pushurl") orelse fetch_url; 769 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 770 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 "" }); 771 defer allocator.free(fetch_ssh_command); 772 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 "" }); 773 defer allocator.free(push_ssh_command); 774 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 775 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 776 777 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 }); 778 defer allocator.free(fetch_refspec); 779 var fetch_opts = fetch_transport_opts; 780 fetch_opts.refspecs = &.{fetch_refspec}; 781 message.clearRetainingCapacity(); 782 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 783 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 784 return err; 785 }; 786 787 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 788 const local_oid = try repo.readRef(io, evt.events_ref); 789 const remote_oid = try repo.readRef(io, remote_ref); 790 if (local_oid == null and remote_oid == null) { 791 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 792 return null; 793 } 794 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 795 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 796 797 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 798 defer allocator.free(push_refspec); 799 message.clearRetainingCapacity(); 800 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_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 message.clearRetainingCapacity(); 805 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 806 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 807 return err; 808 }; 809 }, 810 } 811 }, 812 } 813 return null; 814 } 815