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