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