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