╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
344 errdefer detail_box.deinit(allocator); 345 var detail_scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 346 errdefer detail_scroll.deinit(allocator); 347 try content_box.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 348 349 content_box.getFocus().child_id = content_box.children.keys()[list_index]; 350 return content_box; 351 } 352 353 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 354 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 355 errdefer row.deinit(allocator); 356 row.getFocus().focusable = true; 357 row.getFocus().kind = .{ .custom = link }; 358 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 359 } 360 361 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 362 self.box.deinit(allocator); 363 } 364 365 fn header(self: *View) *Header.View { 366 return &self.box.children.values()[header_index].widget.repo_events_header; 367 } 368 369 fn contentStack(self: *View) *wgt.Stack(ui.Widget) { 370 return &self.box.children.values()[content_index].widget.stack; 371 } 372 373 fn selectedView(self: *View) ui.RoutablePage.EventsView { 374 const stack = self.contentStack(); 375 const id = stack.getFocus().child_id orelse return .active; 376 return @enumFromInt(stack.children.getIndex(id) orelse 0); 377 } 378 379 fn eventWindow(self: *View) *const Window { 380 return self.data.window(self.selectedView()); 381 } 382 383 fn content(self: *View) *wgt.Box(ui.Widget) { 384 const stack = self.contentStack(); 385 return &stack.children.values()[@intFromEnum(self.selectedView())].box; 386 } 387 388 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 389 return &self.content().children.values()[list_index].widget.scroll; 390 } 391 392 fn listBox(self: *View) *wgt.Box(ui.Widget) { 393 return &self.listScroll().child.box; 394 } 395 396 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 397 return &self.content().children.values()[detail_index].widget.scroll; 398 } 399 400 fn detailBox(self: *View) *wgt.Box(ui.Widget) { 401 return &self.detailScroll().child.box; 402 } 403 404 fn detailActive(self: *View) bool { 405 return self.content().getFocus().child_id == self.detailScroll().getFocus().id; 406 } 407 408 fn headerActive(self: *View) bool { 409 return self.box.getFocus().child_id == self.header().getFocus().id; 410 } 411 412 fn selectedEventIndex(self: *View) ?usize { 413 const id = self.listBox().getFocus().child_id orelse return null; 414 const index = self.listBox().children.getIndex(id) orelse return null; 415 return if (index < self.eventWindow().events.len) index else null; 416 } 417 418 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 419 self.clearGrid(); 420 if (self.header().getSelectedIndex()) |index| 421 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[index]; 422 const view = self.selectedView(); 423 var selected_kind: ?evt.EventKind = null; 424 var selected_id: []const u8 = ""; 425 if (view == self.data.view) { 426 if (self.data.selected) |selected| { 427 selected_kind = selected.kind; 428 selected_id = selected.id; 429 } 430 } 431 if (ui.RoutablePage.repoEventsRoute(self.data.identity, view, selected_kind, selected_id)) |route| 432 self.session.data.current_page = route; 433 try self.refreshDetail(allocator); 434 435 if (root_focus.grandchild_id) |id| if (self.box.getFocus().children.contains(id)) { 436 if (self.selectedEventIndex()) |index| { 437 const event = self.eventWindow().events[index]; 438 if (ui.RoutablePage.repoEventsRoute(self.data.identity, view, event.kind, event.id)) |route| 439 self.session.data.current_page = route; 440 } 441 }; 442 443 const list = self.listBox(); 444 for (list.children.keys(), list.children.values()) |id, *child| switch (child.widget) { 445 .text_box => |*text_box| text_box.options.border_style = if (list.getFocus().child_id == id) .single else .hidden, 446 else => {}, 447 }; 448 449 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 450 self.content().children.values()[list_index].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 451 const detail_width = if (constraint.max_size.width) |width| if (both_fit) width - list_max_width else width else detail_min_width; 452 self.content().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 453 for (self.detailBox().children.values()) |*child| child.max_size = .{ .width = detail_width -| 2, .height = null }; 454 455 try self.box.build(allocator, constraint, root_focus); 456 } 457 458 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 459 const index = self.selectedEventIndex() orelse return; 460 const detailed_index = &self.detailed_index[@intFromEnum(self.selectedView())]; 461 if (detailed_index.*) |old| if (old == index) return; 462 const box = self.detailBox(); 463 for (box.children.values()) |*child| child.widget.deinit(allocator); 464 box.children.clearAndFree(allocator); 465 box.getFocus().child_id = null; 466 467 const item = self.eventWindow().events[index]; 468 if (item.view_url) |url| { 469 var link = try wgt.TextBox(ui.Widget).init(allocator, "view event", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 470 errdefer link.deinit(allocator); 471 link.getFocus().focusable = true; 472 link.getFocus().kind = .{ .custom = try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{url}) }; 473 try box.children.put(allocator, link.getFocus().id, .{ .widget = .{ .text_box = link }, .rect = null, .min_size = null }); 474 } 475 { 476 var kind = try wgt.TextBox(ui.Widget).init(allocator, @tagName(item.kind), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " kind " }); 477 errdefer kind.deinit(allocator); 478 kind.getFocus().focusable = true; 479 try box.children.put(allocator, kind.getFocus().id, .{ .widget = .{ .text_box = kind }, .rect = null, .min_size = null }); 480 } 481 { 482 var author = try ui.authorBox(allocator, self.session.page_arena, item.author); 483 errdefer author.deinit(allocator); 484 try box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 485 } 486 487 if (box.children.count() > 0) box.getFocus().child_id = box.children.keys()[0]; 488 self.detailScroll().x = 0; 489 self.detailScroll().y = 0; 490 self.detailScroll().getFocus().version +%= 1; 491 detailed_index.* = index; 492 } 493 494 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 495 if (self.headerActive()) { 496 if (key == .arrow_down) root_focus.setFocus(self.content().getFocus().id) else try self.header().input(allocator, key, root_focus); 497 return; 498 } 499 if (key == .arrow_up and self.contentAtTop()) { 500 self.header().focusCurrent(root_focus); 501 return; 502 } 503 if (self.detailActive()) { 504 if (key == .arrow_left) { 505 root_focus.setFocus(self.listScroll().getFocus().id); 506 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 507 ui.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 508 } 509 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 510 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 511 } else switch (key) { 512 .arrow_right => if (self.detailBox().children.count() > 0) root_focus.setFocus(self.detailScroll().getFocus().id), 513 .enter => if (self.selectedEventIndex() != null) root_focus.setFocus(self.detailScroll().getFocus().id), 514 else => {}, 515 } 516 } 517 518 fn contentAtTop(self: *View) bool { 519 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 520 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 521 const id = box.getFocus().child_id orelse return true; 522 return box.children.getIndex(id) == 0 and scroll.y == 0; 523 } 524 525 pub fn getSelectedIndex(self: *View) ?usize { 526 return if (self.headerActive()) 0 else 1; 527 } 528 529 pub fn clearGrid(self: *View) void { 530 self.box.clearGrid(); 531 } 532 533 pub fn getGrid(self: View) ?Grid { 534 return self.box.getGrid(); 535 } 536 537 pub fn getFocus(self: *View) *Focus { 538 return self.box.getFocus(); 539 } 540 }; 541 542 pub const Header = struct { 543 sync_status: ?[]const u8, 544 545 const nothing = "nothing to sync"; 546 const unsynced = "sync needed!"; 547 548 const Progress = struct { 549 message: *std.Io.Writer.Allocating, 550 551 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 552 switch (event) { 553 .text => |value| { 554 self.message.clearRetainingCapacity(); 555 try self.message.writer.writeAll(value); 556 }, 557 else => {}, 558 } 559 } 560 }; 561 562 pub fn local( 563 comptime repo_kind: rp.RepoKind, 564 comptime repo_opts: rp.RepoOpts(repo_kind), 565 aa: std.mem.Allocator, 566 repo: *rp.Repo(repo_kind, repo_opts), 567 io: std.Io, 568 failure: ?[]const u8, 569 ) !Header { 570 if (failure != null) return Header.empty(aa, failure); 571 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 572 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 573 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 574 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 575 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 576 } 577 578 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 579 return .{ .sync_status = if (failure) |name| try std.fmt.allocPrint(aa, "error: {s}", .{name}) else nothing }; 580 } 581 582 pub fn remote() Header { 583 return .{ .sync_status = null }; 584 } 585 586 pub const View = struct { 587 box: wgt.Box(ui.Widget), 588 session: *ui.Session, 589 tab_ids: [2]usize, 590 button_id: ?usize, 591 592 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 593 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 594 errdefer box.deinit(allocator); 595 var tab_ids: [2]usize = undefined; 596 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, 0..) |view, i| { 597 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "") orelse return error.RouteTooLong; 598 const count = data.window(view).count; 599 var label_buf: [64]u8 = undefined; 600 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(view), count }); 601 var tab = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 602 errdefer tab.deinit(allocator); 603 tab.getFocus().focusable = true; 604 tab.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "ai:{s}", .{try route.toUrl(session.page_arena)}) }; 605 tab_ids[i] = tab.getFocus().id; 606 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 607 } 608 609 var button_id: ?usize = null; 610 if (data.header.sync_status != null) { 611 box.getFocus().kind = .{ .custom = "form:/sync" }; 612 var button = try wgt.TextBox(ui.Widget).init(allocator, "sync", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 613 errdefer button.deinit(allocator); 614 button.getFocus().focusable = true; 615 button.getFocus().kind = .{ .custom = "submit" }; 616 button_id = button.getFocus().id; 617 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 618 } 619 620 if (data.header.sync_status) |status_text| { 621 var status = try wgt.TextBox(ui.Widget).init(allocator, status_text, .{ .border_style = .hidden, .wrap_kind = .none }); 622 errdefer status.deinit(allocator); 623 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 624 } 625 box.getFocus().child_id = tab_ids[@intFromEnum(data.view)]; 626 return .{ .box = box, .session = session, .tab_ids = tab_ids, .button_id = button_id }; 627 } 628 629 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 630 self.box.deinit(allocator); 631 } 632 633 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 634 self.clearGrid(); 635 for (self.tab_ids) |id| { 636 const child = self.box.children.getPtr(id) orelse continue; 637 const tab = switch (child.widget) { 638 .text_box => |*text_box| text_box, 639 else => continue, 640 }; 641 tab.options.border_style = if (self.box.getFocus().child_id == id) .single else .hidden; 642 } 643 try self.box.build(allocator, constraint, root_focus); 644 } 645 646 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 647 const selected = self.box.getFocus().child_id orelse return; 648 const current = self.box.children.getIndex(selected) orelse return; 649 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 650 if (inp.moveTab(key, current, count)) |next| { 651 root_focus.setFocus(self.box.children.keys()[next]); 652 return; 653 } 654 switch (key) { 655 .enter => if (self.syncFocused()) try self.requestSync(allocator), 656 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 657 else => {}, 658 } 659 } 660 661 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 662 if (!self.session.is_terminal) return; 663 if (self.session.host_request != null) return; 664 const button_id = self.button_id orelse return; 665 const child = self.box.children.getPtr(button_id) orelse return; 666 const button = switch (child.widget) { 667 .text_box => |*button| button, 668 else => return, 669 }; 670 try button.setContent(allocator, "syncing..."); 671 self.session.host_request = .sync_events; 672 } 673 674 fn syncFocused(self: *Header.View) bool { 675 const button_id = self.button_id orelse return false; 676 return self.box.getFocus().child_id == button_id; 677 } 678 679 pub fn getSelectedIndex(self: *Header.View) ?usize { 680 const id = self.box.getFocus().child_id orelse return null; 681 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 682 } 683 684 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 685 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 686 } 687 688 pub fn clearGrid(self: *Header.View) void { 689 self.box.clearGrid(); 690 } 691 692 pub fn getGrid(self: Header.View) ?Grid { 693 return self.box.getGrid(); 694 } 695 696 pub fn getFocus(self: *Header.View) *Focus { 697 return self.box.getFocus(); 698 } 699 }; 700 }; 701 702 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 703 if (comptime builtin.target.cpu.arch == .wasm32) return; 704 const io = session.io orelse return; 705 const source = session.local orelse return; 706 defer session.refresh_requested = true; 707 const failure = sync(io, allocator, source) catch |err| { 708 session.data.sync_failure = @errorName(err); 709 return; 710 }; 711 if (failure) |message| { 712 defer allocator.free(message); 713 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 714 return; 715 } 716 session.data.sync_failure = null; 717 switch (session.data.current_page) { 718 .repo_events => |route| { 719 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "")) |first| 720 session.data.current_page = first; 721 }, 722 else => {}, 723 } 724 } 725 726 fn remoteName( 727 comptime repo_kind: rp.RepoKind, 728 comptime repo_opts: rp.RepoOpts(repo_kind), 729 repo: *rp.Repo(repo_kind, repo_opts), 730 io: std.Io, 731 allocator: std.mem.Allocator, 732 ) !?[]u8 { 733 var remotes = try repo.listRemotes(io, allocator); 734 defer remotes.deinit(); 735 const names = remotes.sections.keys(); 736 if (names.len == 0) return null; 737 for (names) |name| { 738 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 739 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 740 } 741 return try allocator.dupe(u8, names[0]); 742 } 743 744 fn isLoopbackSshUrl(url: []const u8) bool { 745 const uri = std.Uri.parse(url) catch return false; 746 const host = switch (uri.host orelse return false) { 747 .raw => |value| value, 748 .percent_encoded => |value| value, 749 }; 750 return std.mem.eql(u8, host, "localhost") or 751 std.mem.eql(u8, host, "127.0.0.1") or 752 std.mem.eql(u8, host, "::1") or 753 std.mem.eql(u8, host, "[::1]"); 754 } 755 756 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 757 var message: std.Io.Writer.Allocating = .init(allocator); 758 defer message.deinit(); 759 760 switch (source.repo_kind) { 761 inline else => |repo_kind| { 762 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 763 defer any_repo.deinit(io, allocator); 764 switch (any_repo) { 765 inline else => |*repo| { 766 const repo_opts = repo.self_repo_opts; 767 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 768 defer allocator.free(remote_name); 769 770 var config = try repo.listConfig(io, allocator); 771 defer config.deinit(); 772 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 773 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 774 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 775 defer allocator.free(remote_section); 776 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 777 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 778 const push_url = remote_config.get("pushurl") orelse fetch_url; 779 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 780 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 "" }); 781 defer allocator.free(fetch_ssh_command); 782 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 "" }); 783 defer allocator.free(push_ssh_command); 784 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 785 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 786 787 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 }); 788 defer allocator.free(fetch_refspec); 789 var fetch_opts = fetch_transport_opts; 790 fetch_opts.refspecs = &.{fetch_refspec}; 791 message.clearRetainingCapacity(); 792 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 793 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 794 return err; 795 }; 796 797 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 798 const local_oid = try repo.readRef(io, evt.events_ref); 799 const remote_oid = try repo.readRef(io, remote_ref); 800 if (local_oid == null and remote_oid == null) { 801 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 802 return null; 803 } 804 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 805 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 806 807 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 808 defer allocator.free(push_refspec); 809 message.clearRetainingCapacity(); 810 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 811 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 812 return err; 813 }; 814 message.clearRetainingCapacity(); 815 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 816 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 817 return err; 818 }; 819 }, 820 } 821 }, 822 } 823 return null; 824 } 825