╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
433 } 434 435 pub fn getSelectedIndex(self: *View) ?usize { 436 if (self.headerActive()) return 0; 437 return if (self.header().isFocusable() or !self.contentAtTop()) 1 else 0; 438 } 439 440 pub fn clearGrid(self: *View) void { 441 self.box.clearGrid(); 442 } 443 444 pub fn getGrid(self: View) ?Grid { 445 return self.box.getGrid(); 446 } 447 448 pub fn getFocus(self: *View) *Focus { 449 return self.box.getFocus(); 450 } 451 }; 452 453 pub const Header = struct { 454 status: []const u8, 455 is_local: bool, 456 457 const nothing = "nothing to sync"; 458 const unsynced = "sync needed!"; 459 460 const Progress = struct { 461 message: *std.Io.Writer.Allocating, 462 463 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 464 switch (event) { 465 .text => |value| { 466 self.message.clearRetainingCapacity(); 467 try self.message.writer.writeAll(value); 468 }, 469 else => {}, 470 } 471 } 472 }; 473 474 pub fn local( 475 comptime repo_kind: rp.RepoKind, 476 comptime repo_opts: rp.RepoOpts(repo_kind), 477 aa: std.mem.Allocator, 478 repo: *rp.Repo(repo_kind, repo_opts), 479 io: std.Io, 480 failure: ?[]const u8, 481 ) !Header { 482 if (failure != null) return Header.empty(aa, failure); 483 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .status = nothing, .is_local = true }; 484 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 485 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .status = nothing, .is_local = true }; 486 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .status = nothing, .is_local = true }; 487 return .{ .status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced, .is_local = true }; 488 } 489 490 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 491 return .{ .status = if (failure) |name| try std.fmt.allocPrint(aa, "error: {s}", .{name}) else nothing, .is_local = true }; 492 } 493 494 pub fn remote(aa: std.mem.Allocator, count: usize) !Header { 495 return .{ .status = try std.fmt.allocPrint(aa, "{d} events found", .{count}), .is_local = false }; 496 } 497 498 fn withCount(self: Header, aa: std.mem.Allocator, count: usize) !Header { 499 return .{ .status = try std.fmt.allocPrint(aa, "{d} events found - {s}", .{ count, self.status }), .is_local = self.is_local }; 500 } 501 502 pub const View = struct { 503 box: wgt.Box(ui.Widget), 504 session: *ui.Session, 505 button_id: ?usize, 506 507 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 508 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 509 errdefer box.deinit(allocator); 510 var button_id: ?usize = null; 511 if (data.is_local) { 512 box.getFocus().kind = .{ .custom = "form:/sync" }; 513 var button = try wgt.TextBox(ui.Widget).init(allocator, "sync", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 514 errdefer button.deinit(allocator); 515 button.getFocus().focusable = true; 516 button.getFocus().kind = .{ .custom = "submit" }; 517 button_id = button.getFocus().id; 518 box.getFocus().child_id = button_id; 519 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 520 } 521 522 var status = try wgt.TextBox(ui.Widget).init(allocator, data.status, .{ .border_style = .hidden, .wrap_kind = .none }); 523 errdefer status.deinit(allocator); 524 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .shrink = true }); 525 return .{ .box = box, .session = session, .button_id = button_id }; 526 } 527 528 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 529 self.box.deinit(allocator); 530 } 531 532 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 533 self.clearGrid(); 534 try self.box.build(allocator, constraint, root_focus); 535 } 536 537 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 538 const button_id = self.button_id orelse return; 539 if (!self.session.is_terminal) return; 540 if (self.session.host_request != null) return; 541 switch (key) { 542 .enter => self.requestSync(), 543 .mouse => |mouse| if (inp.leftClickOn(root_focus, button_id, mouse)) self.requestSync(), 544 else => {}, 545 } 546 _ = allocator; 547 } 548 549 fn requestSync(self: *Header.View) void { 550 self.session.host_request = .sync_events; 551 const button_id = self.button_id orelse return; 552 const child = self.box.children.getPtr(button_id) orelse return; 553 const button = switch (child.widget) { 554 .text_box => |*button| button, 555 else => return, 556 }; 557 button.content = "syncing..."; 558 for (button.box.children.values()) |*line| switch (line.widget) { 559 .text => |*text| text.content = "syncing...", 560 else => {}, 561 }; 562 } 563 564 pub fn focusButton(self: *Header.View, root_focus: *Focus) bool { 565 const button_id = self.button_id orelse return false; 566 root_focus.setFocus(button_id); 567 return true; 568 } 569 570 pub fn isFocusable(self: *Header.View) bool { 571 return self.button_id != null; 572 } 573 574 pub fn clearGrid(self: *Header.View) void { 575 self.box.clearGrid(); 576 } 577 578 pub fn getGrid(self: Header.View) ?Grid { 579 return self.box.getGrid(); 580 } 581 582 pub fn getFocus(self: *Header.View) *Focus { 583 return self.box.getFocus(); 584 } 585 }; 586 }; 587 588 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 589 if (comptime builtin.target.cpu.arch == .wasm32) return; 590 const io = session.io orelse return; 591 const source = session.local orelse return; 592 defer session.refresh_requested = true; 593 const failure = sync(io, allocator, source) catch |err| { 594 session.data.sync_failure = @errorName(err); 595 return; 596 }; 597 if (failure) |message| { 598 defer allocator.free(message); 599 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 600 return; 601 } 602 session.data.sync_failure = null; 603 switch (session.data.current_page) { 604 .repo_events => |route| { 605 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), null, "")) |first| 606 session.data.current_page = first; 607 }, 608 else => {}, 609 } 610 } 611 612 fn remoteName( 613 comptime repo_kind: rp.RepoKind, 614 comptime repo_opts: rp.RepoOpts(repo_kind), 615 repo: *rp.Repo(repo_kind, repo_opts), 616 io: std.Io, 617 allocator: std.mem.Allocator, 618 ) !?[]u8 { 619 var remotes = try repo.listRemotes(io, allocator); 620 defer remotes.deinit(); 621 const names = remotes.sections.keys(); 622 if (names.len == 0) return null; 623 for (names) |name| { 624 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 625 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 626 } 627 return try allocator.dupe(u8, names[0]); 628 } 629 630 fn isLoopbackSshUrl(url: []const u8) bool { 631 const uri = std.Uri.parse(url) catch return false; 632 const host = switch (uri.host orelse return false) { 633 .raw => |value| value, 634 .percent_encoded => |value| value, 635 }; 636 return std.mem.eql(u8, host, "localhost") or 637 std.mem.eql(u8, host, "127.0.0.1") or 638 std.mem.eql(u8, host, "::1") or 639 std.mem.eql(u8, host, "[::1]"); 640 } 641 642 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 643 var message: std.Io.Writer.Allocating = .init(allocator); 644 defer message.deinit(); 645 646 switch (source.repo_kind) { 647 inline else => |repo_kind| { 648 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, .{ .path = source.path }); 649 defer any_repo.deinit(io, allocator); 650 switch (any_repo) { 651 inline else => |*repo| { 652 const repo_opts = repo.self_repo_opts; 653 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 654 defer allocator.free(remote_name); 655 656 var config = try repo.listConfig(io, allocator); 657 defer config.deinit(); 658 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 659 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 660 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 661 defer allocator.free(remote_section); 662 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 663 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 664 const push_url = remote_config.get("pushurl") orelse fetch_url; 665 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 666 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 "" }); 667 defer allocator.free(fetch_ssh_command); 668 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 "" }); 669 defer allocator.free(push_ssh_command); 670 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 671 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 672 673 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 }); 674 defer allocator.free(fetch_refspec); 675 var fetch_opts = fetch_transport_opts; 676 fetch_opts.refspecs = &.{fetch_refspec}; 677 message.clearRetainingCapacity(); 678 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 679 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 680 return err; 681 }; 682 683 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 684 const local_oid = try repo.readRef(io, evt.events_ref); 685 const remote_oid = try repo.readRef(io, remote_ref); 686 if (local_oid == null and remote_oid == null) { 687 try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 688 return null; 689 } 690 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 691 try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 692 693 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 694 defer allocator.free(push_refspec); 695 message.clearRetainingCapacity(); 696 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 697 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 698 return err; 699 }; 700 message.clearRetainingCapacity(); 701 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 702 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 703 return err; 704 }; 705 }, 706 } 707 }, 708 } 709 return null; 710 } 711