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