╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
451 const stack = self.contentStack(); 452 const id = stack.getFocus().child_id orelse return .active; 453 return @fromBackingInt(@intCast(stack.children.getIndex(id) orelse 0)); 454 } 455 456 fn eventWindow(self: *View) *const Window { 457 return self.data.window(self.selectedView()); 458 } 459 460 fn content(self: *View) *wgt.Box(ui.Widget) { 461 const stack = self.contentStack(); 462 return &stack.children.values()[@backingInt(self.selectedView())].box; 463 } 464 465 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 466 return &self.content().children.values()[list_index].widget.scroll; 467 } 468 469 fn listBox(self: *View) *wgt.Box(ui.Widget) { 470 return &self.listScroll().child.box; 471 } 472 473 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 474 return &self.content().children.values()[detail_index].widget.scroll; 475 } 476 477 fn detailBox(self: *View) *wgt.Box(ui.Widget) { 478 return &self.detailScroll().child.box; 479 } 480 481 fn detailActive(self: *View) bool { 482 return self.content().getFocus().child_id == self.detailScroll().getFocus().id; 483 } 484 485 fn headerActive(self: *View) bool { 486 return self.box.getFocus().child_id == self.header().getFocus().id; 487 } 488 489 fn selectedEventIndex(self: *View) ?usize { 490 const id = self.listBox().getFocus().child_id orelse return null; 491 const index = self.listBox().children.getIndex(id) orelse return null; 492 return if (index < self.eventWindow().events.len) index else null; 493 } 494 495 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 496 self.clearGrid(); 497 if (self.header().getSelectedIndex()) |index| 498 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[index]; 499 try self.refreshDetail(allocator); 500 501 const list = self.listBox(); 502 for (list.children.keys(), list.children.values()) |id, *child| switch (child.widget) { 503 .text_box => |*text_box| ui.widget.markSelected(text_box, list.getFocus().child_id == id), 504 else => {}, 505 }; 506 507 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 508 self.content().children.values()[list_index].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 509 const detail_width = if (constraint.max_size.width) |width| if (both_fit) width - list_max_width else width else detail_min_width; 510 self.content().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 511 for (self.detailBox().children.values()) |*child| child.max_size = .{ .width = detail_width -| 2, .height = null }; 512 513 try self.box.build(allocator, constraint, root_focus); 514 } 515 516 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 517 const index = self.selectedEventIndex() orelse return; 518 const detailed_index = &self.detailed_index[@backingInt(self.selectedView())]; 519 if (detailed_index.*) |old| if (old == index) return; 520 const box = self.detailBox(); 521 for (box.children.values()) |*child| child.widget.deinit(allocator); 522 box.children.clearAndFree(allocator); 523 box.getFocus().child_id = null; 524 525 const item = self.eventWindow().events[index]; 526 if (item.view_url) |url| { 527 var link = try wgt.TextBox.init(allocator, "view event", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 528 errdefer link.deinit(allocator); 529 link.getFocus().mode = .all; 530 link.getFocus().kind = .{ .custom = try self.session.page_arena.allocator().print("a:{s}", .{url}) }; 531 try box.children.put(allocator, link.getFocus().id, .{ .widget = .{ .text_box = link }, .rect = null, .min_size = null }); 532 } 533 { 534 var kind = try wgt.TextBox.init(allocator, @tagName(item.kind), .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = " kind " } }); 535 errdefer kind.deinit(allocator); 536 kind.getFocus().mode = .all; 537 try box.children.put(allocator, kind.getFocus().id, .{ .widget = .{ .text_box = kind }, .rect = null, .min_size = null }); 538 } 539 { 540 var author = try ui.authorBox(allocator, self.session.page_arena, item.author); 541 errdefer author.deinit(allocator); 542 try box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 543 } 544 545 if (box.children.count() > 0) box.getFocus().child_id = box.children.keys()[0]; 546 self.detailScroll().x = 0; 547 self.detailScroll().y = 0; 548 self.detailScroll().getFocus().version +%= 1; 549 detailed_index.* = index; 550 } 551 552 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 553 // scrolling crosses the sub header boundary the same way arrows do 554 const direction = inp.vertDirection(key); 555 if (self.headerActive()) { 556 if (direction == .down) root_focus.setFocus(self.content().getFocus().id) else try self.header().input(allocator, key, root_focus); 557 return; 558 } 559 if (direction == .up and self.contentAtTop()) { 560 self.header().focusCurrent(root_focus); 561 return; 562 } 563 if (self.detailActive()) { 564 if (key == .arrow_left) { 565 root_focus.setFocus(self.listScroll().getFocus().id); 566 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 567 ui.widget.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 568 } 569 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 570 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 571 } else switch (key) { 572 .arrow_right => if (self.detailBox().children.count() > 0) root_focus.setFocus(self.detailScroll().getFocus().id), 573 .enter => if (self.selectedEventIndex() != null) root_focus.setFocus(self.detailScroll().getFocus().id), 574 else => {}, 575 } 576 } 577 578 fn contentAtTop(self: *View) bool { 579 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 580 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 581 const id = box.getFocus().child_id orelse return true; 582 return box.children.getIndex(id) == 0 and scroll.y == 0; 583 } 584 585 pub fn atTop(self: *View) bool { 586 return self.headerActive(); 587 } 588 589 // arriving from the page's tabs lands on the sub header, not the content 590 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 591 self.header().focusCurrent(root_focus); 592 return true; 593 } 594 595 pub fn clearGrid(self: *View) void { 596 self.box.clearGrid(); 597 } 598 599 pub fn getGrid(self: View) ?Grid { 600 return self.box.getGrid(); 601 } 602 603 pub fn getFocus(self: *View) *Focus { 604 return self.box.getFocus(); 605 } 606 }; 607 608 pub const Header = struct { 609 sync_status: ?[]const u8, 610 611 const nothing = "nothing to sync"; 612 const unsynced = "sync needed!"; 613 614 const Progress = struct { 615 message: *std.Io.Writer.Allocating, 616 617 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 618 switch (event) { 619 .text => |value| { 620 self.message.clearRetainingCapacity(); 621 try self.message.writer.writeAll(value); 622 }, 623 else => {}, 624 } 625 } 626 }; 627 628 pub fn local( 629 comptime repo_kind: rp.RepoKind, 630 comptime repo_opts: rp.RepoOpts(repo_kind), 631 aa: std.mem.Allocator, 632 repo: *rp.Repo(repo_kind, repo_opts), 633 io: std.Io, 634 failure: ?[]const u8, 635 ) !Header { 636 if (failure != null) return Header.empty(aa, failure); 637 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 638 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 639 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 640 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 641 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 642 } 643 644 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 645 return .{ .sync_status = if (failure) |name| try aa.print("error: {s}", .{name}) else nothing }; 646 } 647 648 pub fn remote() Header { 649 return .{ .sync_status = null }; 650 } 651 652 pub const View = struct { 653 box: wgt.Box(ui.Widget), 654 session: *ui.Session, 655 tab_ids: [2]usize, 656 // the tab stays selected while the sync button has focus 657 selected_tab: usize, 658 button_id: ?usize, 659 660 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 661 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 662 errdefer box.deinit(allocator); 663 var tab_ids: [2]usize = undefined; 664 const view_labels = [_][]const u8{ active_tab_label, removed_tab_label }; 665 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, view_labels, 0..) |view, view_label, i| { 666 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "", data.moment) orelse return error.RouteTooLong; 667 const count = data.window(view).count; 668 var label_buf: [64]u8 = undefined; 669 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ view_label, count }); 670 var tab = try wgt.TextBox.init(allocator, label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 671 errdefer tab.deinit(allocator); 672 tab.getFocus().mode = .all; 673 const selected = std.meta.activeTag(session.data.current_page) == .repo_events and data.view == view; 674 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 675 tab_ids[i] = tab.getFocus().id; 676 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 677 } 678 679 // pushes the sync button and status to the right 680 { 681 var spacer = try ui.widget.Spacer.init(allocator); 682 errdefer spacer.deinit(allocator); 683 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null, .flex = .grow }); 684 } 685 686 var button_id: ?usize = null; 687 if (data.header.sync_status != null) { 688 box.getFocus().kind = .{ .custom = "form:/sync" }; 689 var button = try wgt.TextBox.init(allocator, "sync", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 690 errdefer button.deinit(allocator); 691 button.getFocus().mode = .all; 692 button.getFocus().kind = .{ .custom = "submit" }; 693 button_id = button.getFocus().id; 694 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 695 } 696 697 if (data.header.sync_status) |status_text| { 698 var status = try wgt.TextBox.init(allocator, status_text, .{ .border = .hidden, .wrap_kind = .none }); 699 errdefer status.deinit(allocator); 700 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 701 } 702 box.getFocus().child_id = tab_ids[@backingInt(data.view)]; 703 return .{ .box = box, .session = session, .tab_ids = tab_ids, .selected_tab = @backingInt(data.view), .button_id = button_id }; 704 } 705 706 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 707 self.box.deinit(allocator); 708 } 709 710 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 711 self.clearGrid(); 712 if (self.getSelectedIndex()) |index| self.selected_tab = index; 713 for (self.tab_ids, 0..) |id, index| { 714 const child = self.box.children.getPtr(id) orelse continue; 715 const tab = switch (child.widget) { 716 .text_box => |*text_box| text_box, 717 else => continue, 718 }; 719 ui.widget.markSelected(tab, index == self.selected_tab); 720 } 721 try self.box.build(allocator, constraint, root_focus); 722 } 723 724 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 725 const selected = self.box.getFocus().child_id orelse return; 726 const stops = [_]?usize{ self.tab_ids[0], self.tab_ids[1], self.button_id }; 727 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 728 const current = std.mem.indexOfScalar(?usize, stops[0..count], selected) orelse return; 729 if (inp.moveTab(key, current, count)) |next| { 730 root_focus.setFocus(stops[next] orelse unreachable); 731 return; 732 } 733 switch (key) { 734 .enter => if (self.syncFocused()) try self.requestSync(allocator), 735 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 736 else => {}, 737 } 738 } 739 740 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 741 if (!self.session.is_terminal) return; 742 if (self.session.host_request != null) return; 743 const button_id = self.button_id orelse return; 744 const child = self.box.children.getPtr(button_id) orelse return; 745 const button = switch (child.widget) { 746 .text_box => |*button| button, 747 else => return, 748 }; 749 try button.setContent(allocator, "syncing..."); 750 self.session.host_request = .sync_events; 751 } 752 753 fn syncFocused(self: *Header.View) bool { 754 const button_id = self.button_id orelse return false; 755 return self.box.getFocus().child_id == button_id; 756 } 757 758 pub fn getSelectedIndex(self: *Header.View) ?usize { 759 const id = self.box.getFocus().child_id orelse return null; 760 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 761 } 762 763 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 764 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 765 } 766 767 pub fn clearGrid(self: *Header.View) void { 768 self.box.clearGrid(); 769 } 770 771 pub fn getGrid(self: Header.View) ?Grid { 772 return self.box.getGrid(); 773 } 774 775 pub fn getFocus(self: *Header.View) *Focus { 776 return self.box.getFocus(); 777 } 778 }; 779 }; 780 781 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 782 if (comptime builtin.target.cpu.arch == .wasm32) return; 783 const io = session.io orelse return; 784 const source = session.local orelse return; 785 defer session.refresh_requested = true; 786 const failure = sync(io, allocator, source) catch |err| { 787 session.data.sync_failure = @errorName(err); 788 return; 789 }; 790 if (failure) |message| { 791 defer allocator.free(message); 792 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 793 return; 794 } 795 session.data.sync_failure = null; 796 switch (session.data.current_page) { 797 .repo_events => |route| { 798 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 799 session.data.current_page = first; 800 }, 801 else => {}, 802 } 803 } 804 805 fn remoteName( 806 comptime repo_kind: rp.RepoKind, 807 comptime repo_opts: rp.RepoOpts(repo_kind), 808 repo: *rp.Repo(repo_kind, repo_opts), 809 io: std.Io, 810 allocator: std.mem.Allocator, 811 ) !?[]u8 { 812 var remotes = try repo.listRemotes(io, allocator); 813 defer remotes.deinit(); 814 const names = remotes.sections.keys(); 815 if (names.len == 0) return null; 816 for (names) |name| { 817 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 818 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 819 } 820 return try allocator.dupe(u8, names[0]); 821 } 822 823 fn isLoopbackSshUrl(url: []const u8) bool { 824 const uri = std.Uri.parse(url) catch return false; 825 const host = switch (uri.host orelse return false) { 826 .raw => |value| value, 827 .percent_encoded => |value| value, 828 }; 829 return std.mem.eql(u8, host, "localhost") or 830 std.mem.eql(u8, host, "127.0.0.1") or 831 std.mem.eql(u8, host, "::1") or 832 std.mem.eql(u8, host, "[::1]"); 833 } 834 835 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 836 var message: std.Io.Writer.Allocating = .init(allocator); 837 defer message.deinit(); 838 839 switch (source.repo_kind) { 840 inline else => |repo_kind| { 841 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 842 defer any_repo.deinit(io, allocator); 843 switch (any_repo) { 844 inline else => |*repo| { 845 const repo_opts = repo.self_repo_opts; 846 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 847 defer allocator.free(remote_name); 848 849 var config = try repo.listConfig(io, allocator); 850 defer config.deinit(); 851 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 852 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 853 const remote_section = try allocator.print("remote.{s}", .{remote_name}); 854 defer allocator.free(remote_section); 855 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 856 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 857 const push_url = remote_config.get("pushurl") orelse fetch_url; 858 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 859 const fetch_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 860 defer allocator.free(fetch_ssh_command); 861 const push_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 862 defer allocator.free(push_ssh_command); 863 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 864 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 865 866 const fetch_refspec = try allocator.print("+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 867 defer allocator.free(fetch_refspec); 868 var fetch_opts = fetch_transport_opts; 869 fetch_opts.refspecs = &.{fetch_refspec}; 870 message.clearRetainingCapacity(); 871 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 872 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 873 return err; 874 }; 875 876 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 877 const local_oid = try repo.readRef(io, evt.events_ref); 878 const remote_oid = try repo.readRef(io, remote_ref); 879 if (local_oid == null and remote_oid == null) { 880 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 881 return null; 882 } 883 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 884 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 885 886 const push_refspec = try allocator.print("refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 887 defer allocator.free(push_refspec); 888 message.clearRetainingCapacity(); 889 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 890 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 891 return err; 892 }; 893 message.clearRetainingCapacity(); 894 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 895 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 896 return err; 897 }; 898 }, 899 } 900 }, 901 } 902 return null; 903 } 904