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