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