╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
521 pub fn getFocus(self: *View) *Focus { 522 return self.box.getFocus(); 523 } 524 }; 525 526 pub const Header = struct { 527 sync_status: ?[]const u8, 528 529 const nothing = "nothing to sync"; 530 const unsynced = "sync needed!"; 531 532 const Progress = struct { 533 message: *std.Io.Writer.Allocating, 534 535 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 536 switch (event) { 537 .text => |value| { 538 self.message.clearRetainingCapacity(); 539 try self.message.writer.writeAll(value); 540 }, 541 else => {}, 542 } 543 } 544 }; 545 546 pub fn local( 547 comptime repo_kind: rp.RepoKind, 548 comptime repo_opts: rp.RepoOpts(repo_kind), 549 aa: std.mem.Allocator, 550 repo: *rp.Repo(repo_kind, repo_opts), 551 io: std.Io, 552 failure: ?[]const u8, 553 ) !Header { 554 if (failure != null) return Header.empty(aa, failure); 555 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 556 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 557 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 558 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 559 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 560 } 561 562 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 563 return .{ .sync_status = if (failure) |name| try std.fmt.allocPrint(aa, "error: {s}", .{name}) else nothing }; 564 } 565 566 pub fn remote() Header { 567 return .{ .sync_status = null }; 568 } 569 570 pub const View = struct { 571 box: wgt.Box(ui.Widget), 572 session: *ui.Session, 573 tab_ids: [2]usize, 574 button_id: ?usize, 575 576 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 577 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 578 errdefer box.deinit(allocator); 579 var tab_ids: [2]usize = undefined; 580 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, 0..) |view, i| { 581 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "") orelse return error.RouteTooLong; 582 const count = data.window(view).count; 583 var label_buf: [64]u8 = undefined; 584 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(view), count }); 585 var tab = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 586 errdefer tab.deinit(allocator); 587 tab.getFocus().focusable = true; 588 const selected = std.meta.activeTag(session.data.current_page) == .repo_events and data.view == view; 589 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 590 tab_ids[i] = tab.getFocus().id; 591 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 592 } 593 594 var button_id: ?usize = null; 595 if (data.header.sync_status != null) { 596 box.getFocus().kind = .{ .custom = "form:/sync" }; 597 var button = try wgt.TextBox.init(allocator, "sync", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 598 errdefer button.deinit(allocator); 599 button.getFocus().focusable = true; 600 button.getFocus().kind = .{ .custom = "submit" }; 601 button_id = button.getFocus().id; 602 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 603 } 604 605 if (data.header.sync_status) |status_text| { 606 var status = try wgt.TextBox.init(allocator, status_text, .{ .border_style = .hidden, .wrap_kind = .none }); 607 errdefer status.deinit(allocator); 608 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 609 } 610 box.getFocus().child_id = tab_ids[@intFromEnum(data.view)]; 611 return .{ .box = box, .session = session, .tab_ids = tab_ids, .button_id = button_id }; 612 } 613 614 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 615 self.box.deinit(allocator); 616 } 617 618 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 619 self.clearGrid(); 620 for (self.tab_ids) |id| { 621 const child = self.box.children.getPtr(id) orelse continue; 622 const tab = switch (child.widget) { 623 .text_box => |*text_box| text_box, 624 else => continue, 625 }; 626 tab.options.border_style = if (self.box.getFocus().child_id == id) .single else .hidden; 627 } 628 try self.box.build(allocator, constraint, root_focus); 629 } 630 631 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 632 const selected = self.box.getFocus().child_id orelse return; 633 const current = self.box.children.getIndex(selected) orelse return; 634 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 635 if (inp.moveTab(key, current, count)) |next| { 636 root_focus.setFocus(self.box.children.keys()[next]); 637 return; 638 } 639 switch (key) { 640 .enter => if (self.syncFocused()) try self.requestSync(allocator), 641 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 642 else => {}, 643 } 644 } 645 646 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 647 if (!self.session.is_terminal) return; 648 if (self.session.host_request != null) return; 649 const button_id = self.button_id orelse return; 650 const child = self.box.children.getPtr(button_id) orelse return; 651 const button = switch (child.widget) { 652 .text_box => |*button| button, 653 else => return, 654 }; 655 try button.setContent(allocator, "syncing..."); 656 self.session.host_request = .sync_events; 657 } 658 659 fn syncFocused(self: *Header.View) bool { 660 const button_id = self.button_id orelse return false; 661 return self.box.getFocus().child_id == button_id; 662 } 663 664 pub fn getSelectedIndex(self: *Header.View) ?usize { 665 const id = self.box.getFocus().child_id orelse return null; 666 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 667 } 668 669 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 670 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 671 } 672 673 pub fn clearGrid(self: *Header.View) void { 674 self.box.clearGrid(); 675 } 676 677 pub fn getGrid(self: Header.View) ?Grid { 678 return self.box.getGrid(); 679 } 680 681 pub fn getFocus(self: *Header.View) *Focus { 682 return self.box.getFocus(); 683 } 684 }; 685 }; 686 687 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 688 if (comptime builtin.target.cpu.arch == .wasm32) return; 689 const io = session.io orelse return; 690 const source = session.local orelse return; 691 defer session.refresh_requested = true; 692 const failure = sync(io, allocator, source) catch |err| { 693 session.data.sync_failure = @errorName(err); 694 return; 695 }; 696 if (failure) |message| { 697 defer allocator.free(message); 698 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 699 return; 700 } 701 session.data.sync_failure = null; 702 switch (session.data.current_page) { 703 .repo_events => |route| { 704 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "")) |first| 705 session.data.current_page = first; 706 }, 707 else => {}, 708 } 709 } 710 711 fn remoteName( 712 comptime repo_kind: rp.RepoKind, 713 comptime repo_opts: rp.RepoOpts(repo_kind), 714 repo: *rp.Repo(repo_kind, repo_opts), 715 io: std.Io, 716 allocator: std.mem.Allocator, 717 ) !?[]u8 { 718 var remotes = try repo.listRemotes(io, allocator); 719 defer remotes.deinit(); 720 const names = remotes.sections.keys(); 721 if (names.len == 0) return null; 722 for (names) |name| { 723 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 724 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 725 } 726 return try allocator.dupe(u8, names[0]); 727 } 728 729 fn isLoopbackSshUrl(url: []const u8) bool { 730 const uri = std.Uri.parse(url) catch return false; 731 const host = switch (uri.host orelse return false) { 732 .raw => |value| value, 733 .percent_encoded => |value| value, 734 }; 735 return std.mem.eql(u8, host, "localhost") or 736 std.mem.eql(u8, host, "127.0.0.1") or 737 std.mem.eql(u8, host, "::1") or 738 std.mem.eql(u8, host, "[::1]"); 739 } 740 741 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 742 var message: std.Io.Writer.Allocating = .init(allocator); 743 defer message.deinit(); 744 745 switch (source.repo_kind) { 746 inline else => |repo_kind| { 747 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 748 defer any_repo.deinit(io, allocator); 749 switch (any_repo) { 750 inline else => |*repo| { 751 const repo_opts = repo.self_repo_opts; 752 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 753 defer allocator.free(remote_name); 754 755 var config = try repo.listConfig(io, allocator); 756 defer config.deinit(); 757 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 758 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 759 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 760 defer allocator.free(remote_section); 761 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 762 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 763 const push_url = remote_config.get("pushurl") orelse fetch_url; 764 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 765 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 "" }); 766 defer allocator.free(fetch_ssh_command); 767 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 "" }); 768 defer allocator.free(push_ssh_command); 769 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 770 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 771 772 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 }); 773 defer allocator.free(fetch_refspec); 774 var fetch_opts = fetch_transport_opts; 775 fetch_opts.refspecs = &.{fetch_refspec}; 776 message.clearRetainingCapacity(); 777 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 778 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 779 return err; 780 }; 781 782 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 783 const local_oid = try repo.readRef(io, evt.events_ref); 784 const remote_oid = try repo.readRef(io, remote_ref); 785 if (local_oid == null and remote_oid == null) { 786 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 787 return null; 788 } 789 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 790 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 791 792 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 793 defer allocator.free(push_refspec); 794 message.clearRetainingCapacity(); 795 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 796 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 797 return err; 798 }; 799 message.clearRetainingCapacity(); 800 repo.fetch(io, allocator, remote_name, fetch_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 }, 805 } 806 }, 807 } 808 return null; 809 } 810