╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const inp = @import("../input.zig"); 6 const xit = @import("xit"); 7 const rp = xit.repo; 8 const rf = xit.ref; 9 const hash = xit.hash; 10 const xitui = xit.xitui; 11 const wgt = xitui.widget; 12 const layout = xitui.layout; 13 const Key = xitui.input.Key; 14 const Grid = xitui.grid.Grid; 15 const Focus = xitui.focus.Focus; 16 17 const active_tab_label = "active"; 18 const removed_tab_label = "removed"; 19 20 const page_size = 20; 21 22 pub const Item = struct { 23 id: []const u8, 24 kind: evt.EventKind, 25 author: ui.Author, 26 view_url: ?[]const u8 = null, 27 }; 28 29 pub const EventRef = struct { 30 id: []const u8, 31 kind: evt.EventKind, 32 }; 33 34 pub const Window = struct { 35 count: usize = 0, 36 events: []const Item = &.{}, 37 next: ?EventRef = null, 38 }; 39 40 identity: []const u8, 41 view: ui.RoutablePage.EventsView, 42 selected: ?EventRef, 43 active: Window, 44 removed: Window, 45 header: Header, 46 // set when the list is limited to the events one transaction wrote 47 moment: ?u64 = null, 48 49 const Self = @This(); 50 51 pub fn init( 52 comptime repo_kind: rp.RepoKind, 53 comptime repo_opts: rp.RepoOpts(repo_kind), 54 arena: *std.heap.ArenaAllocator, 55 repo: *rp.Repo(repo_kind, repo_opts), 56 io: std.Io, 57 admin_moment: ?evt.AdminDB.HashMap(.read_only), 58 identity: []const u8, 59 requested_view: ui.RoutablePage.EventsView, 60 selected_kind: ?evt.EventKind, 61 selected: []const u8, 62 moment_index: ?u64, 63 local: bool, 64 failure: ?[]const u8, 65 ) !Self { 66 const aa = arena.allocator(); 67 var result = try empty(aa, identity, requested_view, local, failure); 68 result.moment = moment_index; 69 if (local) result.header = try Header.local(repo_kind, repo_opts, aa, repo, io, failure); 70 const require_selected_event = selected_kind != null or selected.len != 0; 71 if ((selected_kind == null) != (selected.len == 0)) return error.NotFound; 72 73 const DB = evt.EventDB(repo_opts.hash); 74 const gpa = arena.child_allocator; 75 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, gpa, repo.core.repo_dir) else null; 76 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 77 const haxy_moment = (if (event_db_maybe) |*event_db| 78 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 79 else if (repo_kind == .git) 80 return result 81 else 82 evt.currentMoment(repo_opts, repo)) catch { 83 if (require_selected_event) return error.NotFound; 84 return result; 85 }; 86 87 const kind_map_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.event_index_key)) orelse { 88 if (require_selected_event) return error.NotFound; 89 return result; 90 }; 91 const kind_map = try DB.HashMap(.read_only).init(kind_map_cursor); 92 93 const active_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.active_event_id_set_key)); 94 const removed_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.removed_event_id_set_key)); 95 96 var root_key: ?[@sizeOf(u64) + evt.event_id_size]u8 = null; 97 if (selected.len != 0) { 98 var id: [evt.event_id_size]u8 = undefined; 99 _ = std.fmt.hexToBytes(&id, selected) catch return error.NotFound; 100 if (!std.mem.eql(u8, selected, &std.fmt.bytesToHex(id, .lower))) return error.NotFound; 101 const kind = selected_kind orelse return error.NotFound; 102 const stored_kind = (try evt.readEventKind(repo_opts.hash, kind_map, &id)) orelse return error.NotFound; 103 if (stored_kind != kind) return error.NotFound; 104 result.selected = .{ .id = try aa.dupe(u8, selected), .kind = kind }; 105 const meta = try readMeta(repo_opts.hash, arena, haxy_moment, kind, &id); 106 result.view = if (meta.removed) .removed else .active; 107 root_key = evt.orderKeyDesc(meta.updated_order, &id); 108 } 109 110 if (moment_index) |index| { 111 try readMomentEvents(repo_kind, repo_opts, arena, &result, haxy_moment, kind_map, admin_moment, identity, index, selected); 112 return result; 113 } 114 115 for ([_]ui.RoutablePage.EventsView{ .active, .removed }) |view| { 116 const id_set_cursor = switch (view) { 117 .active => active_cursor, 118 .removed => removed_cursor, 119 }; 120 const event_window = switch (view) { 121 .active => &result.active, 122 .removed => &result.removed, 123 }; 124 const id_set = try DB.SortedSet(.read_only).init(id_set_cursor orelse { 125 if (require_selected_event and result.view == view) return error.NotFound; 126 continue; 127 }); 128 event_window.count = @intCast(try id_set.count()); 129 const selected_key = if (result.view == view) root_key else null; 130 if (selected_key) |*key| if (!try id_set.contains(key)) return error.NotFound; 131 132 var iter = if (selected_key) |*key| try id_set.iteratorFrom(key) else try id_set.iteratorFromIndex(0); 133 var events: std.ArrayList(Item) = .empty; 134 while (try iter.next()) |cursor_value| { 135 const id = try evt.readOrderKeyId(DB, cursor_value); 136 const kind = (try evt.readEventKind(repo_opts.hash, kind_map, &id)) orelse continue; 137 if (events.items.len == page_size) { 138 event_window.next = .{ .id = try formatId(aa, &id), .kind = kind }; 139 break; 140 } 141 const item = (try readItem(repo_opts.hash, arena, haxy_moment, kind_map, admin_moment, identity, view == .active, kind, &id)) orelse continue; 142 try events.append(aa, item); 143 } 144 event_window.events = events.items; 145 } 146 return result; 147 } 148 149 // the moment's set is keyed on the event order, not the update order the 150 // active and removed sets use, so it gets its own walk. one transaction's 151 // events are few, so the whole set is scanned to count both views. 152 fn readMomentEvents( 153 comptime repo_kind: rp.RepoKind, 154 comptime repo_opts: rp.RepoOpts(repo_kind), 155 arena: *std.heap.ArenaAllocator, 156 result: *Self, 157 haxy_moment: evt.EventDB(repo_opts.hash).HashMap(.read_only), 158 kind_map: evt.EventDB(repo_opts.hash).HashMap(.read_only), 159 admin_moment: ?evt.AdminDB.HashMap(.read_only), 160 identity: []const u8, 161 moment_index: u64, 162 selected: []const u8, 163 ) !void { 164 const DB = evt.EventDB(repo_opts.hash); 165 const aa = arena.allocator(); 166 const ids = try evt.momentEventIds(DB, repo_opts.hash, haxy_moment, moment_index) orelse return error.NotFound; 167 168 var events: [2]std.ArrayList(Item) = .{ .empty, .empty }; 169 // the selected event's window starts at it, not at the top 170 var awaiting_selected = selected.len != 0; 171 172 var iter = try ids.iterator(); 173 while (try iter.next()) |cursor_value| { 174 const id = try evt.readOrderKeyId(DB, cursor_value); 175 const kind = (try evt.readEventKind(repo_opts.hash, kind_map, &id)) orelse continue; 176 const meta = try readMeta(repo_opts.hash, arena, haxy_moment, kind, &id); 177 const view: ui.RoutablePage.EventsView = if (meta.removed) .removed else .active; 178 const event_window = switch (view) { 179 .active => &result.active, 180 .removed => &result.removed, 181 }; 182 event_window.count += 1; 183 if (awaiting_selected and view == result.view) { 184 if (!std.mem.eql(u8, selected, &std.fmt.bytesToHex(id, .lower))) continue; 185 awaiting_selected = false; 186 } 187 const window_events = &events[@intFromEnum(view)]; 188 if (window_events.items.len == page_size) { 189 if (event_window.next == null) event_window.next = .{ .id = try formatId(aa, &id), .kind = kind }; 190 continue; 191 } 192 const item = (try readItem(repo_opts.hash, arena, haxy_moment, kind_map, admin_moment, identity, view == .active, kind, &id)) orelse continue; 193 try window_events.append(aa, item); 194 } 195 if (awaiting_selected) return error.NotFound; 196 result.active.events = events[@intFromEnum(ui.RoutablePage.EventsView.active)].items; 197 result.removed.events = events[@intFromEnum(ui.RoutablePage.EventsView.removed)].items; 198 } 199 200 pub fn empty(aa: std.mem.Allocator, identity: []const u8, view: ui.RoutablePage.EventsView, local: bool, failure: ?[]const u8) !Self { 201 return .{ 202 .identity = try aa.dupe(u8, identity), 203 .view = view, 204 .selected = null, 205 .active = .{}, 206 .removed = .{}, 207 .header = if (local) try Header.empty(aa, failure) else Header.remote(), 208 }; 209 } 210 211 fn window(self: *const Self, view: ui.RoutablePage.EventsView) *const Window { 212 return switch (view) { 213 .active => &self.active, 214 .removed => &self.removed, 215 }; 216 } 217 218 fn formatId(aa: std.mem.Allocator, id: *const [evt.event_id_size]u8) ![]const u8 { 219 const hex = std.fmt.bytesToHex(id.*, .lower); 220 return try aa.dupe(u8, &hex); 221 } 222 223 fn readRecord( 224 comptime T: type, 225 comptime hash_kind: hash.HashKind, 226 arena: *std.heap.ArenaAllocator, 227 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 228 id: *const [evt.event_id_size]u8, 229 ) !?T.Record { 230 const DB = evt.EventDB(hash_kind); 231 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, T.record_map_key)) orelse return null; 232 const records = try DB.HashMap(.read_only).init(records_cursor); 233 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 234 const record = try DB.HashMap(.read_only).init(record_cursor); 235 return try evt.read(T.Record, DB, hash_kind, arena, record); 236 } 237 238 const EventMeta = struct { 239 updated_order: u64, 240 removed: bool, 241 }; 242 243 fn readMeta( 244 comptime hash_kind: hash.HashKind, 245 arena: *std.heap.ArenaAllocator, 246 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 247 kind: evt.EventKind, 248 id: *const [evt.event_id_size]u8, 249 ) !EventMeta { 250 return switch (kind) { 251 inline else => |event_kind| blk: { 252 const T = switch (event_kind) { 253 .user => evt.User, 254 .repo => evt.Repo, 255 .fork => evt.Fork, 256 .issue => evt.Issue, 257 .discuss => evt.Discussion, 258 .comment => evt.Comment, 259 .attach => evt.Attachment, 260 .patchrev => evt.PatchRev, 261 .patch => evt.Patch, 262 }; 263 const record = (try readRecord(T, hash_kind, arena, haxy_moment, id)) orelse return error.NotFound; 264 break :blk .{ .updated_order = record.updated_order, .removed = record.removed }; 265 }, 266 }; 267 } 268 269 fn readItem( 270 comptime hash_kind: hash.HashKind, 271 arena: *std.heap.ArenaAllocator, 272 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 273 kind_map: evt.EventDB(hash_kind).HashMap(.read_only), 274 admin_moment: ?evt.AdminDB.HashMap(.read_only), 275 identity: []const u8, 276 include_view_url: bool, 277 kind: evt.EventKind, 278 id: *const [evt.event_id_size]u8, 279 ) !?Item { 280 var item: Item = .{ 281 .id = try formatId(arena.allocator(), id), 282 .kind = kind, 283 .author = .unknown, 284 }; 285 switch (kind) { 286 .user => { 287 const record = (try readRecord(evt.User, hash_kind, arena, haxy_moment, id)) orelse return null; 288 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.event.email); 289 }, 290 .repo => { 291 const record = (try readRecord(evt.Repo, hash_kind, arena, haxy_moment, id)) orelse return null; 292 if (admin_moment) |moment| { 293 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, record.event.user_id)) |user| 294 item.author = .{ .user_name = user.event.name }; 295 } 296 }, 297 .fork => { 298 const record = (try readRecord(evt.Fork, hash_kind, arena, haxy_moment, id)) orelse return null; 299 if (admin_moment) |moment| { 300 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, record.event.user_id)) |user| 301 item.author = .{ .user_name = user.event.name }; 302 } 303 }, 304 .issue => { 305 const record = (try readRecord(evt.Issue, hash_kind, arena, haxy_moment, id)) orelse return null; 306 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 307 if (!include_view_url) return item; 308 const route = ui.RoutablePage.repoThreadCommentsRoute(.issue, identity, item.id, 0) orelse return error.RouteTooLong; 309 item.view_url = try route.toUrl(arena); 310 }, 311 .discuss => { 312 const record = (try readRecord(evt.Discussion, hash_kind, arena, haxy_moment, id)) orelse return null; 313 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 314 if (!include_view_url) return item; 315 const route = ui.RoutablePage.repoThreadCommentsRoute(.discuss, identity, item.id, 0) orelse return error.RouteTooLong; 316 item.view_url = try route.toUrl(arena); 317 }, 318 .comment => { 319 const record = (try readRecord(evt.Comment, hash_kind, arena, haxy_moment, id)) orelse return null; 320 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 321 if (!include_view_url) return item; 322 var thread_id: [evt.event_id_size]u8 = undefined; 323 _ = std.fmt.hexToBytes(&thread_id, &record.event.thread_id) catch return error.InvalidEventId; 324 const thread_kind = (try evt.readEventKind(hash_kind, kind_map, &thread_id)) orelse return item; 325 const route = ui.RoutablePage.repoThreadCommentRoute(thread_kind, identity, &record.event.thread_id, item.id, 0) orelse return item; 326 item.view_url = try route.toUrl(arena); 327 }, 328 .attach => { 329 const record = (try readRecord(evt.Attachment, hash_kind, arena, haxy_moment, id)) orelse return null; 330 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 331 if (!include_view_url) return item; 332 var parent_id: [evt.event_id_size]u8 = undefined; 333 _ = std.fmt.hexToBytes(&parent_id, &record.event.parent_id) catch return error.InvalidEventId; 334 const parent_kind = (try evt.readEventKind(hash_kind, kind_map, &parent_id)) orelse return item; 335 const route = ui.RoutablePage.repoThreadCommentsRoute(parent_kind, identity, &record.event.parent_id, 0) orelse return item; 336 item.view_url = try route.toUrl(arena); 337 }, 338 .patch => { 339 const record = (try readRecord(evt.Patch, hash_kind, arena, haxy_moment, id)) orelse return null; 340 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 341 if (!include_view_url) return item; 342 const route = ui.RoutablePage.repoThreadCommentsRoute(.patch, identity, item.id, 0) orelse return error.RouteTooLong; 343 item.view_url = try route.toUrl(arena); 344 }, 345 .patchrev => { 346 const record = (try readRecord(evt.PatchRev, hash_kind, arena, haxy_moment, id)) orelse return null; 347 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 348 }, 349 } 350 return item; 351 } 352 353 pub const View = struct { 354 box: wgt.Box(ui.Widget), 355 data: *const Self, 356 session: *ui.Session, 357 detailed_index: [2]?usize, 358 359 const header_index = 0; 360 const content_index = 1; 361 const list_index = 0; 362 const detail_index = 1; 363 const list_max_width = 20; 364 const detail_min_width = 40; 365 366 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 367 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 368 errdefer outer.deinit(allocator); 369 370 var header_view = try Header.View.init(allocator, data, session); 371 errdefer header_view.deinit(allocator); 372 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_events_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 373 374 var content_stack = try wgt.Stack(ui.Widget).init(allocator); 375 errdefer content_stack.deinit(allocator); 376 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }) |view| { 377 var content_box = try initContent(allocator, data, session, view); 378 errdefer content_box.deinit(allocator); 379 try content_stack.children.put(allocator, content_box.getFocus().id, .{ .box = content_box }); 380 } 381 content_stack.getFocus().child_id = content_stack.children.keys()[@intFromEnum(data.view)]; 382 try outer.children.put(allocator, content_stack.getFocus().id, .{ .widget = .{ .stack = content_stack }, .rect = null, .min_size = null }); 383 outer.getFocus().child_id = outer.children.keys()[content_index]; 384 return .{ .box = outer, .data = data, .session = session, .detailed_index = .{ null, null } }; 385 } 386 387 fn initContent(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session, view: ui.RoutablePage.EventsView) !wgt.Box(ui.Widget) { 388 var content_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 389 errdefer content_box.deinit(allocator); 390 391 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 392 errdefer list_box.deinit(allocator); 393 const event_window = data.window(view); 394 for (event_window.events) |event| { 395 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, event.kind, event.id, data.moment) orelse return error.RouteTooLong; 396 const link = try std.fmt.allocPrint(session.page_arena.allocator(), "ai:{s}", .{try route.toUrl(session.page_arena)}); 397 try addRow(allocator, &list_box, @tagName(event.kind), link); 398 } 399 if (event_window.next) |next| { 400 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, next.kind, next.id, data.moment) orelse return error.RouteTooLong; 401 const link = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}); 402 try addRow(allocator, &list_box, "next →", link); 403 } 404 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 405 var list_scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 406 errdefer list_scroll.deinit(allocator); 407 try content_box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 408 409 var detail_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 410 errdefer detail_box.deinit(allocator); 411 var detail_scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 412 errdefer detail_scroll.deinit(allocator); 413 detail_scroll.getFocus().mode = .mouse; 414 try content_box.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 415 416 content_box.getFocus().child_id = content_box.children.keys()[list_index]; 417 return content_box; 418 } 419 420 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 421 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 422 errdefer row.deinit(allocator); 423 row.getFocus().mode = .all; 424 row.getFocus().kind = .{ .custom = link }; 425 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 426 } 427 428 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 429 self.box.deinit(allocator); 430 } 431 432 fn header(self: *View) *Header.View { 433 return &self.box.children.values()[header_index].widget.repo_events_header; 434 } 435 436 fn contentStack(self: *View) *wgt.Stack(ui.Widget) { 437 return &self.box.children.values()[content_index].widget.stack; 438 } 439 440 fn selectedView(self: *View) ui.RoutablePage.EventsView { 441 const stack = self.contentStack(); 442 const id = stack.getFocus().child_id orelse return .active; 443 return @enumFromInt(stack.children.getIndex(id) orelse 0); 444 } 445 446 fn eventWindow(self: *View) *const Window { 447 return self.data.window(self.selectedView()); 448 } 449 450 fn content(self: *View) *wgt.Box(ui.Widget) { 451 const stack = self.contentStack(); 452 return &stack.children.values()[@intFromEnum(self.selectedView())].box; 453 } 454 455 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 456 return &self.content().children.values()[list_index].widget.scroll; 457 } 458 459 fn listBox(self: *View) *wgt.Box(ui.Widget) { 460 return &self.listScroll().child.box; 461 } 462 463 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 464 return &self.content().children.values()[detail_index].widget.scroll; 465 } 466 467 fn detailBox(self: *View) *wgt.Box(ui.Widget) { 468 return &self.detailScroll().child.box; 469 } 470 471 fn detailActive(self: *View) bool { 472 return self.content().getFocus().child_id == self.detailScroll().getFocus().id; 473 } 474 475 fn headerActive(self: *View) bool { 476 return self.box.getFocus().child_id == self.header().getFocus().id; 477 } 478 479 fn selectedEventIndex(self: *View) ?usize { 480 const id = self.listBox().getFocus().child_id orelse return null; 481 const index = self.listBox().children.getIndex(id) orelse return null; 482 return if (index < self.eventWindow().events.len) index else null; 483 } 484 485 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 486 self.clearGrid(); 487 if (self.header().getSelectedIndex()) |index| 488 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[index]; 489 try self.refreshDetail(allocator); 490 491 const list = self.listBox(); 492 for (list.children.keys(), list.children.values()) |id, *child| switch (child.widget) { 493 .text_box => |*text_box| text_box.options.border_style = if (list.getFocus().child_id == id) .single else .hidden, 494 else => {}, 495 }; 496 497 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 498 self.content().children.values()[list_index].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 499 const detail_width = if (constraint.max_size.width) |width| if (both_fit) width - list_max_width else width else detail_min_width; 500 self.content().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 501 for (self.detailBox().children.values()) |*child| child.max_size = .{ .width = detail_width -| 2, .height = null }; 502 503 try self.box.build(allocator, constraint, root_focus); 504 } 505 506 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 507 const index = self.selectedEventIndex() orelse return; 508 const detailed_index = &self.detailed_index[@intFromEnum(self.selectedView())]; 509 if (detailed_index.*) |old| if (old == index) return; 510 const box = self.detailBox(); 511 for (box.children.values()) |*child| child.widget.deinit(allocator); 512 box.children.clearAndFree(allocator); 513 box.getFocus().child_id = null; 514 515 const item = self.eventWindow().events[index]; 516 if (item.view_url) |url| { 517 var link = try wgt.TextBox.init(allocator, "view event", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 518 errdefer link.deinit(allocator); 519 link.getFocus().mode = .all; 520 link.getFocus().kind = .{ .custom = try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{url}) }; 521 try box.children.put(allocator, link.getFocus().id, .{ .widget = .{ .text_box = link }, .rect = null, .min_size = null }); 522 } 523 { 524 var kind = try wgt.TextBox.init(allocator, @tagName(item.kind), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " kind " }); 525 errdefer kind.deinit(allocator); 526 kind.getFocus().mode = .all; 527 try box.children.put(allocator, kind.getFocus().id, .{ .widget = .{ .text_box = kind }, .rect = null, .min_size = null }); 528 } 529 { 530 var author = try ui.authorBox(allocator, self.session.page_arena, item.author); 531 errdefer author.deinit(allocator); 532 try box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 533 } 534 535 if (box.children.count() > 0) box.getFocus().child_id = box.children.keys()[0]; 536 self.detailScroll().x = 0; 537 self.detailScroll().y = 0; 538 self.detailScroll().getFocus().version +%= 1; 539 detailed_index.* = index; 540 } 541 542 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 543 // scrolling crosses the sub header boundary the same way arrows do 544 const direction = inp.vertDirection(key); 545 if (self.headerActive()) { 546 if (direction == .down) root_focus.setFocus(self.content().getFocus().id) else try self.header().input(allocator, key, root_focus); 547 return; 548 } 549 if (direction == .up and self.contentAtTop()) { 550 self.header().focusCurrent(root_focus); 551 return; 552 } 553 if (self.detailActive()) { 554 if (key == .arrow_left) { 555 root_focus.setFocus(self.listScroll().getFocus().id); 556 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 557 ui.widget.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 558 } 559 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 560 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 561 } else switch (key) { 562 .arrow_right => if (self.detailBox().children.count() > 0) root_focus.setFocus(self.detailScroll().getFocus().id), 563 .enter => if (self.selectedEventIndex() != null) root_focus.setFocus(self.detailScroll().getFocus().id), 564 else => {}, 565 } 566 } 567 568 fn contentAtTop(self: *View) bool { 569 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 570 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 571 const id = box.getFocus().child_id orelse return true; 572 return box.children.getIndex(id) == 0 and scroll.y == 0; 573 } 574 575 pub fn atTop(self: *View) bool { 576 return self.headerActive(); 577 } 578 579 // arriving from the page's tabs lands on the sub header, not the content 580 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 581 self.header().focusCurrent(root_focus); 582 return true; 583 } 584 585 pub fn clearGrid(self: *View) void { 586 self.box.clearGrid(); 587 } 588 589 pub fn getGrid(self: View) ?Grid { 590 return self.box.getGrid(); 591 } 592 593 pub fn getFocus(self: *View) *Focus { 594 return self.box.getFocus(); 595 } 596 }; 597 598 pub const Header = struct { 599 sync_status: ?[]const u8, 600 601 const nothing = "nothing to sync"; 602 const unsynced = "sync needed!"; 603 604 const Progress = struct { 605 message: *std.Io.Writer.Allocating, 606 607 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 608 switch (event) { 609 .text => |value| { 610 self.message.clearRetainingCapacity(); 611 try self.message.writer.writeAll(value); 612 }, 613 else => {}, 614 } 615 } 616 }; 617 618 pub fn local( 619 comptime repo_kind: rp.RepoKind, 620 comptime repo_opts: rp.RepoOpts(repo_kind), 621 aa: std.mem.Allocator, 622 repo: *rp.Repo(repo_kind, repo_opts), 623 io: std.Io, 624 failure: ?[]const u8, 625 ) !Header { 626 if (failure != null) return Header.empty(aa, failure); 627 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 628 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 629 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 630 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 631 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 632 } 633 634 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 635 return .{ .sync_status = if (failure) |name| try std.fmt.allocPrint(aa, "error: {s}", .{name}) else nothing }; 636 } 637 638 pub fn remote() Header { 639 return .{ .sync_status = null }; 640 } 641 642 pub const View = struct { 643 box: wgt.Box(ui.Widget), 644 session: *ui.Session, 645 tab_ids: [2]usize, 646 button_id: ?usize, 647 648 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 649 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 650 errdefer box.deinit(allocator); 651 var tab_ids: [2]usize = undefined; 652 const view_labels = [_][]const u8{ active_tab_label, removed_tab_label }; 653 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, view_labels, 0..) |view, view_label, i| { 654 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "", data.moment) orelse return error.RouteTooLong; 655 const count = data.window(view).count; 656 var label_buf: [64]u8 = undefined; 657 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ view_label, count }); 658 var tab = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 659 errdefer tab.deinit(allocator); 660 tab.getFocus().mode = .all; 661 const selected = std.meta.activeTag(session.data.current_page) == .repo_events and data.view == view; 662 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 663 tab_ids[i] = tab.getFocus().id; 664 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 665 } 666 667 var button_id: ?usize = null; 668 if (data.header.sync_status != null) { 669 box.getFocus().kind = .{ .custom = "form:/sync" }; 670 var button = try wgt.TextBox.init(allocator, "sync", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 671 errdefer button.deinit(allocator); 672 button.getFocus().mode = .all; 673 button.getFocus().kind = .{ .custom = "submit" }; 674 button_id = button.getFocus().id; 675 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 676 } 677 678 if (data.header.sync_status) |status_text| { 679 var status = try wgt.TextBox.init(allocator, status_text, .{ .border_style = .hidden, .wrap_kind = .none }); 680 errdefer status.deinit(allocator); 681 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 682 } 683 box.getFocus().child_id = tab_ids[@intFromEnum(data.view)]; 684 return .{ .box = box, .session = session, .tab_ids = tab_ids, .button_id = button_id }; 685 } 686 687 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 688 self.box.deinit(allocator); 689 } 690 691 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 692 self.clearGrid(); 693 for (self.tab_ids) |id| { 694 const child = self.box.children.getPtr(id) orelse continue; 695 const tab = switch (child.widget) { 696 .text_box => |*text_box| text_box, 697 else => continue, 698 }; 699 tab.options.border_style = if (self.box.getFocus().child_id == id) .single else .hidden; 700 } 701 try self.box.build(allocator, constraint, root_focus); 702 } 703 704 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 705 const selected = self.box.getFocus().child_id orelse return; 706 const current = self.box.children.getIndex(selected) orelse return; 707 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 708 if (inp.moveTab(key, current, count)) |next| { 709 root_focus.setFocus(self.box.children.keys()[next]); 710 return; 711 } 712 switch (key) { 713 .enter => if (self.syncFocused()) try self.requestSync(allocator), 714 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 715 else => {}, 716 } 717 } 718 719 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 720 if (!self.session.is_terminal) return; 721 if (self.session.host_request != null) return; 722 const button_id = self.button_id orelse return; 723 const child = self.box.children.getPtr(button_id) orelse return; 724 const button = switch (child.widget) { 725 .text_box => |*button| button, 726 else => return, 727 }; 728 try button.setContent(allocator, "syncing..."); 729 self.session.host_request = .sync_events; 730 } 731 732 fn syncFocused(self: *Header.View) bool { 733 const button_id = self.button_id orelse return false; 734 return self.box.getFocus().child_id == button_id; 735 } 736 737 pub fn getSelectedIndex(self: *Header.View) ?usize { 738 const id = self.box.getFocus().child_id orelse return null; 739 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 740 } 741 742 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 743 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 744 } 745 746 pub fn clearGrid(self: *Header.View) void { 747 self.box.clearGrid(); 748 } 749 750 pub fn getGrid(self: Header.View) ?Grid { 751 return self.box.getGrid(); 752 } 753 754 pub fn getFocus(self: *Header.View) *Focus { 755 return self.box.getFocus(); 756 } 757 }; 758 }; 759 760 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 761 if (comptime builtin.target.cpu.arch == .wasm32) return; 762 const io = session.io orelse return; 763 const source = session.local orelse return; 764 defer session.refresh_requested = true; 765 const failure = sync(io, allocator, source) catch |err| { 766 session.data.sync_failure = @errorName(err); 767 return; 768 }; 769 if (failure) |message| { 770 defer allocator.free(message); 771 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 772 return; 773 } 774 session.data.sync_failure = null; 775 switch (session.data.current_page) { 776 .repo_events => |route| { 777 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 778 session.data.current_page = first; 779 }, 780 else => {}, 781 } 782 } 783 784 fn remoteName( 785 comptime repo_kind: rp.RepoKind, 786 comptime repo_opts: rp.RepoOpts(repo_kind), 787 repo: *rp.Repo(repo_kind, repo_opts), 788 io: std.Io, 789 allocator: std.mem.Allocator, 790 ) !?[]u8 { 791 var remotes = try repo.listRemotes(io, allocator); 792 defer remotes.deinit(); 793 const names = remotes.sections.keys(); 794 if (names.len == 0) return null; 795 for (names) |name| { 796 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 797 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 798 } 799 return try allocator.dupe(u8, names[0]); 800 } 801 802 fn isLoopbackSshUrl(url: []const u8) bool { 803 const uri = std.Uri.parse(url) catch return false; 804 const host = switch (uri.host orelse return false) { 805 .raw => |value| value, 806 .percent_encoded => |value| value, 807 }; 808 return std.mem.eql(u8, host, "localhost") or 809 std.mem.eql(u8, host, "127.0.0.1") or 810 std.mem.eql(u8, host, "::1") or 811 std.mem.eql(u8, host, "[::1]"); 812 } 813 814 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 815 var message: std.Io.Writer.Allocating = .init(allocator); 816 defer message.deinit(); 817 818 switch (source.repo_kind) { 819 inline else => |repo_kind| { 820 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 821 defer any_repo.deinit(io, allocator); 822 switch (any_repo) { 823 inline else => |*repo| { 824 const repo_opts = repo.self_repo_opts; 825 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 826 defer allocator.free(remote_name); 827 828 var config = try repo.listConfig(io, allocator); 829 defer config.deinit(); 830 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 831 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 832 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 833 defer allocator.free(remote_section); 834 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 835 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 836 const push_url = remote_config.get("pushurl") orelse fetch_url; 837 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 838 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 "" }); 839 defer allocator.free(fetch_ssh_command); 840 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 "" }); 841 defer allocator.free(push_ssh_command); 842 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 843 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 844 845 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 }); 846 defer allocator.free(fetch_refspec); 847 var fetch_opts = fetch_transport_opts; 848 fetch_opts.refspecs = &.{fetch_refspec}; 849 message.clearRetainingCapacity(); 850 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 851 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 852 return err; 853 }; 854 855 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 856 const local_oid = try repo.readRef(io, evt.events_ref); 857 const remote_oid = try repo.readRef(io, remote_ref); 858 if (local_oid == null and remote_oid == null) { 859 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 860 return null; 861 } 862 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 863 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 864 865 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 866 defer allocator.free(push_refspec); 867 message.clearRetainingCapacity(); 868 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 869 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 870 return err; 871 }; 872 message.clearRetainingCapacity(); 873 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 874 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 875 return err; 876 }; 877 }, 878 } 879 }, 880 } 881 return null; 882 } 883