╭ Patches.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 xit = @import("xit"); 6 const rp = xit.repo; 7 const hash = xit.hash; 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const diff3 = @import("../../diff3.zig"); 11 const Comment = @import("Comment.zig"); 12 const Attachment = @import("Attachment.zig"); 13 const thread = ui.widget.thread; 14 const fork = @import("../../fork.zig"); 15 const pch = @import("../../patch.zig"); 16 17 const wasm = builtin.target.cpu.arch == .wasm32; 18 19 // how much of a description the detail pane shows before linking to the 20 // /description page, the same limit commit messages use. 21 pub const max_description_size = 2 * 1024; 22 23 // how many patches one window shows before a "next" link appears. 24 pub const page_size = 20; 25 26 // how many tags the tags view shows at most. 27 pub const max_tags = 1000; 28 29 // one patch from the repo's consumed event database, with its hex event id 30 // (the id lives in the event envelope, not the payload). 31 pub const PatchWithId = struct { 32 id: []const u8, 33 record: evt.Patch.Record, 34 // the creation commit's author 35 author: ui.Author = .unknown, 36 // whether the patch has an unresolved merge conflict 37 conflicted: bool = false, 38 comments: Comment.Window = .empty, 39 attachments: []const Attachment.WithId = &.{}, 40 draft: bool = false, 41 revision_ready: bool = false, 42 fork_oid: []const u8 = "", 43 target_branch: []const u8 = "", 44 no_changes: bool = false, 45 fork_exists: bool = false, 46 }; 47 48 pub const Entry = PatchWithId; 49 50 // one side of a conflicted field: its value and who set it 51 pub const Side = struct { 52 text: []const u8, 53 author: ui.Author = .unknown, 54 }; 55 56 pub const FieldConflict = struct { 57 ours: Side, 58 theirs: Side, 59 }; 60 61 // the selected patch's conflict, read for the resolve view. ours is the live 62 // record; the chunks split the description against the merge base. 63 pub const Conflict = struct { 64 title: ?FieldConflict = null, 65 tags: ?FieldConflict = null, 66 status: ?FieldConflict = null, 67 revision: ?FieldConflict = null, 68 description: ?struct { 69 chunks: []const diff3.Chunk, 70 ours_author: ui.Author = .unknown, 71 theirs_author: ui.Author = .unknown, 72 } = null, 73 }; 74 75 // one status's windowed listing. 76 pub const Window = struct { 77 items: []const PatchWithId, 78 // the id of the previous window's first patch, or null when this window 79 // is already the first. 80 prev_id: ?[]const u8, 81 // the id of the next window's first patch, or null when this is the last 82 // window. 83 next_id: ?[]const u8, 84 // how many patches the listing holds across all windows. 85 count: usize, 86 87 pub const empty: Window = .{ .items = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 88 }; 89 90 // "owner/name", so the view can build /repo/owner/name/patches/... links. 91 identity: []const u8, 92 // the url-encoded tag the lists are filtered to ("" = unfiltered). 93 tag: []const u8, 94 // the hex event id of the patch its status's window is rooted at ("" = the 95 // first window), mirrored into the url. 96 selected_id: []const u8, 97 // the comment shown in the selected patch's detail pane. 98 comment_id: []const u8, 99 // the selected patch's comment window (0 = the first window). 100 comments_start: usize, 101 // the selected comment and its immediate replies. 102 comment_page: ?Comment.Permalink = null, 103 open: Window, 104 closed: Window, 105 merged: Window, 106 drafts: Window, 107 // the conflicted patches' listing; its count also gates the conflicts tab. 108 conflicts: Window, 109 // the selected patch's conflict, set when the page shows the resolve view. 110 conflict: ?Conflict = null, 111 // the resolve view's comma-separated fields prefilled from their side, 112 // mirrored from the url. 113 theirs_picks: []const u8 = "", 114 // the view the page shows initially (a selected patch's status overrides the route's) 115 view: ui.RoutablePage.PatchesView, 116 // the /description page: the detail pane shows the selected patch's whole 117 // description behind a back link. 118 description_page: bool = false, 119 // every tag in the repo, in sorted order, for the tags view. 120 tags: []const []const u8, 121 // the on-disk repo this page was read from, for the terminal submit path 122 // (the web posts the new-patch form to the patch route instead). 123 repo_source: ?ui.RepoSource = null, 124 repo_id: ?[evt.event_id_size]u8 = null, 125 126 const Self = @This(); 127 128 pub const Event = evt.Patch; 129 pub const Status = evt.Patch.Status; 130 pub const ViewKind = ui.RoutablePage.PatchesView; 131 pub const thread_name = "patch"; 132 pub const header_widget_name = "repo_patches_header"; 133 134 pub fn listRoute(identity: []const u8, status: Status, tag: []const u8, selected: []const u8) ?ui.RoutablePage { 135 return ui.RoutablePage.repoPatchesRoute(identity, status, tag, selected); 136 } 137 138 pub fn forkRoute(identity: []const u8, id: []const u8) ?ui.RoutablePage { 139 return ui.RoutablePage.forkPatchRoute(identity, id); 140 } 141 142 pub fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 143 return ui.RoutablePage.repoPatchesDraftsRoute(identity); 144 } 145 146 pub fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 147 return ui.RoutablePage.repoPatchesConflictsRoute(identity, selected); 148 } 149 150 pub fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 151 return ui.RoutablePage.repoPatchesResolveRoute(identity, selected, picks); 152 } 153 154 pub fn createDraft( 155 data: *const Self, 156 session: *ui.Session, 157 allocator: std.mem.Allocator, 158 title: []const u8, 159 tags: []const u8, 160 description: []const u8, 161 ) ![evt.event_id_size * 2]u8 { 162 if (!evt.Patch.fieldsValid(title, tags)) return error.InvalidFields; 163 const io = session.io orelse return error.NotFound; 164 const repos_dir = session.repos_dir orelse return error.NotFound; 165 const admin_repo = session.admin_repo orelse return error.NotFound; 166 const user_id = session.userId() orelse return error.NotFound; 167 const repo_id = data.repo_id orelse return error.NotFound; 168 const author = (try session.eventAuthor()) orelse return error.NotFound; 169 var id_bytes: [evt.event_id_size]u8 = undefined; 170 io.random(&id_bytes); 171 const id = std.fmt.bytesToHex(id_bytes, .lower); 172 const fork_path = try fork.create(.{}, io, allocator, repos_dir, admin_repo, .{ 173 .id = id, 174 .user_id = user_id, 175 .repo_id = repo_id, 176 .title = title, 177 .description = description, 178 .tags = tags, 179 .author = author, 180 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 181 }); 182 allocator.free(fork_path); 183 return id; 184 } 185 186 pub fn publishDraft(data: *const Self, session: *ui.Session, allocator: std.mem.Allocator, id: []const u8) !void { 187 const io = session.io orelse return error.NotFound; 188 const repos_dir = session.repos_dir orelse return error.NotFound; 189 const admin_repo = session.admin_repo orelse return error.NotFound; 190 const repo_source = data.repo_source orelse return error.NotFound; 191 const repo_id = data.repo_id orelse return error.NotFound; 192 const user_id = session.userId() orelse return error.NotFound; 193 const patch_id = evt.parseEventId(id) catch return error.NotFound; 194 const author = (try session.eventAuthor()) orelse return error.NotFound; 195 196 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 197 const fork_path = try fork.forkPath(allocator, repos_dir, &id_hex); 198 defer allocator.free(fork_path); 199 200 var target_repo = try rp.Repo(.xit, .{}).open(io, allocator, repo_source.localInitOpts()); 201 defer target_repo.deinit(io, allocator); 202 try pch.publish(.{}, io, allocator, admin_repo, &target_repo, fork_path, .{ 203 .id = id_hex, 204 .user_id = user_id, 205 .repo_id = repo_id, 206 .author = author, 207 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 208 }); 209 } 210 211 pub fn editDraft( 212 data: *const Self, 213 session: *ui.Session, 214 allocator: std.mem.Allocator, 215 id: []const u8, 216 title: []const u8, 217 tags: []const u8, 218 description: []const u8, 219 ) !void { 220 const io = session.io orelse return error.NotFound; 221 const repos_dir = session.repos_dir orelse return error.NotFound; 222 const admin_repo = session.admin_repo orelse return error.NotFound; 223 const repo_id = data.repo_id orelse return error.NotFound; 224 const user_id = session.userId() orelse return error.NotFound; 225 const patch_id = evt.parseEventId(id) catch return error.NotFound; 226 const author = (try session.eventAuthor()) orelse return error.NotFound; 227 228 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 229 const fork_path = try fork.forkPath(allocator, repos_dir, &id_hex); 230 defer allocator.free(fork_path); 231 if (!try pch.editDraft(.{}, io, allocator, admin_repo, fork_path, .{ 232 .id = id_hex, 233 .user_id = user_id, 234 .repo_id = repo_id, 235 .title = title, 236 .tags = tags, 237 .description = description, 238 .author = author, 239 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 240 })) return error.NotFound; 241 } 242 243 pub fn removeDraft(session: *ui.Session, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8) !void { 244 const io = session.io orelse return error.NotFound; 245 const repos_dir = session.repos_dir orelse return error.NotFound; 246 const admin_repo = session.admin_repo orelse return error.NotFound; 247 const user_id = session.userId() orelse return error.NotFound; 248 const author = (try session.eventAuthor()) orelse return error.NotFound; 249 250 const id_hex = std.fmt.bytesToHex(id.*, .lower); 251 try fork.remove(io, allocator, repos_dir, admin_repo, &id_hex, &user_id, author); 252 } 253 254 // `status`'s windowed listing. 255 pub fn window(self: *const Self, status: evt.Patch.Status) *const Window { 256 return switch (status) { 257 .open => &self.open, 258 .closed => &self.closed, 259 .merged => &self.merged, 260 }; 261 } 262 263 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 264 // theirs: picks. 265 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 266 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 267 while (it.next()) |pick| { 268 if (std.mem.eql(u8, pick, field)) return true; 269 } 270 return false; 271 } 272 273 // the patch `selected_id` names, in whichever window holds it. 274 pub fn selectedThread(self: *const Self) ?*const PatchWithId { 275 if (self.selected_id.len == 0) return null; 276 for ([_]*const Window{ &self.open, &self.closed, &self.merged, &self.drafts }) |win| { 277 for (win.items) |*entry| { 278 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 279 } 280 } 281 return null; 282 } 283 284 // an empty listing, for the wasm / no-repo paths. 285 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, tag: []const u8, selected_id: []const u8, comment_id: []const u8, comments_start: usize, theirs_picks: []const u8, view: ui.RoutablePage.PatchesView) !Self { 286 return .{ 287 .identity = try aa.dupe(u8, identity), 288 .tag = try aa.dupe(u8, tag), 289 .selected_id = try aa.dupe(u8, selected_id), 290 .comment_id = try aa.dupe(u8, comment_id), 291 .comments_start = comments_start, 292 .open = .empty, 293 .closed = .empty, 294 .merged = .empty, 295 .drafts = .empty, 296 .conflicts = .empty, 297 .theirs_picks = try aa.dupe(u8, theirs_picks), 298 // a description url shows a status list, the patch's own status 299 // picking it once the patch is read; conflicts and resolve urls also 300 // start there, upgraded by init when the conflict data exists. 301 .view = switch (view) { 302 .description, .conflicts, .resolve => .open, 303 else => view, 304 }, 305 .description_page = view == .description, 306 .tags = &.{}, 307 }; 308 } 309 310 pub fn detailResult(aa: std.mem.Allocator, identity: []const u8, entry: PatchWithId) !Self { 311 var result = try emptyResult(aa, identity, "", entry.id, "", 0, "", .open); 312 const items = try aa.dupe(PatchWithId, &.{entry}); 313 const detail_window = Window{ .items = items, .prev_id = null, .next_id = null, .count = 1 }; 314 if (entry.draft) { 315 result.drafts = detail_window; 316 result.view = .drafts; 317 } else switch (entry.record.event.status) { 318 .open => result.open = detail_window, 319 .closed => { 320 result.closed = detail_window; 321 result.view = .closed; 322 }, 323 .merged => { 324 result.merged = detail_window; 325 result.view = .merged; 326 }, 327 } 328 return result; 329 } 330 331 // read one window per status of an opened repo's patches (filtered to `tag` 332 // when set), ordered by creation (newest first). the window of the patch 333 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 334 // event db next to it (synced from the events branch on each page build); a 335 // xit repo reads its own db. 336 pub fn init( 337 comptime repo_kind: rp.RepoKind, 338 comptime repo_opts: rp.RepoOpts(repo_kind), 339 arena: *std.heap.ArenaAllocator, 340 repo: *rp.Repo(repo_kind, repo_opts), 341 io: std.Io, 342 // the admin db's moment, for resolving author emails to user names (null 343 // in local mode, which has no users) 344 admin_moment: ?evt.AdminDB.HashMap(.read_only), 345 session: *ui.Session, 346 repo_id_maybe: ?[evt.event_id_size]u8, 347 identity: []const u8, 348 target_branch: []const u8, 349 tag: []const u8, 350 selected_id: []const u8, 351 comment_id: []const u8, 352 comments_start: usize, 353 theirs_picks: []const u8, 354 view: ui.RoutablePage.PatchesView, 355 ) !Self { 356 const allowed_view: ui.RoutablePage.PatchesView = if (session.local != null and (view == .new or view == .drafts)) .open else view; 357 var empty = try emptyResult(arena.allocator(), identity, tag, selected_id, comment_id, comments_start, theirs_picks, allowed_view); 358 359 const aa = arena.allocator(); 360 const DB = evt.EventDB(repo_opts.hash); 361 const rooted = empty.selected_id.len != 0; 362 const tagged = empty.tag.len != 0; 363 const drafts_window = if (admin_moment) |admin| 364 if (repo_id_maybe) |repo_id| 365 if (session.data.user_id != null and session.repos_dir != null) 366 try loadDraftWindow(repo_kind, repo_opts, arena, session, admin, &repo_id, empty.selected_id, repo, target_branch) 367 else 368 Window.empty 369 else 370 Window.empty 371 else 372 Window.empty; 373 var draft_selected = false; 374 if (rooted) for (drafts_window.items) |item| { 375 if (std.mem.eql(u8, item.id, empty.selected_id)) { 376 draft_selected = true; 377 break; 378 } 379 }; 380 empty.drafts = drafts_window; 381 if (draft_selected and view != .edit and view != .remove) empty.view = .drafts; 382 383 // an explicitly named published patch that doesn't exist is a bad url 384 // (NotFound -> 404); drafts, tags, and bare routes can use the empty fallback. 385 const strict = rooted and !draft_selected; 386 387 // a repo with no consumed events has no moment yet. 388 const gpa = arena.child_allocator; 389 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; 390 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 391 const haxy_moment = (if (event_db_maybe) |*event_db| 392 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 393 else if (repo_kind == .git) 394 return empty 395 else 396 evt.currentMoment(repo_opts, repo)) catch { 397 if (strict) return error.NotFound; 398 return empty; 399 }; 400 401 // the sorted sets to window, per status: the tag's sets when filtered, 402 // else the top-level per-status sets. a missing set is an empty window. 403 var open_set: ?DB.SortedSet(.read_only) = null; 404 var closed_set: ?DB.SortedSet(.read_only) = null; 405 var merged_set: ?DB.SortedSet(.read_only) = null; 406 if (tagged) { 407 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key))) |tag_to_patches_cursor| { 408 const tag_to_patches = try DB.SortedMap(.read_only).init(tag_to_patches_cursor); 409 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 410 open_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .open); 411 closed_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .closed); 412 merged_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .merged); 413 } 414 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key))) |status_to_patches_cursor| { 415 const status_to_patches = try DB.SortedMap(.read_only).init(status_to_patches_cursor); 416 open_set = try thread.statusSet(Self, DB, status_to_patches, .open); 417 closed_set = try thread.statusSet(Self, DB, status_to_patches, .closed); 418 merged_set = try thread.statusSet(Self, DB, status_to_patches, .merged); 419 } else if (strict) return error.NotFound; 420 421 const event_id_to_patch_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse { 422 if (strict) return error.NotFound; 423 return empty; 424 }; 425 const event_id_to_patch = try DB.HashMap(.read_only).init(event_id_to_patch_cursor); 426 427 // the conflicted patches' container: a set view for membership and 428 // windowing, a map view for the resolve entry, over the same cursor. 429 var conflict_set: ?DB.SortedSet(.read_only) = null; 430 var conflicts_map: ?DB.SortedMap(.read_only) = null; 431 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 432 conflict_set = try DB.SortedSet(.read_only).init(cursor); 433 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 434 } 435 436 // a named patch roots its own status's window at itself (the conflicts 437 // view's window instead); the other windows start at the beginning. 438 var resolved_view = empty.view; 439 var open_root: ?[]const u8 = null; 440 var closed_root: ?[]const u8 = null; 441 var merged_root: ?[]const u8 = null; 442 var conflicts_root: ?[]const u8 = null; 443 var conflict_data: ?Conflict = null; 444 if (rooted and !draft_selected) { 445 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 446 const patch_cursor = try event_id_to_patch.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 447 const patch_map = try DB.HashMap(.read_only).init(patch_cursor); 448 const patch_event = try evt.read(evt.Patch.Record, DB, repo_opts.hash, arena, patch_map); 449 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(patch_event.created_order, &id_bytes)); 450 451 if (view == .conflicts) { 452 // the id roots the conflicts window, so it must be conflicted 453 const set = conflict_set orelse return error.NotFound; 454 if (!try set.contains(order_key)) return error.NotFound; 455 conflicts_root = order_key; 456 } else { 457 // the named patch must be in its windowed set (a tag url can name 458 // an patch that doesn't carry the tag). 459 const set = (switch (patch_event.event.status) { 460 .open => open_set, 461 .closed => closed_set, 462 .merged => merged_set, 463 }) orelse return error.NotFound; 464 if (!try set.contains(order_key)) return error.NotFound; 465 466 switch (patch_event.event.status) { 467 .open => open_root = order_key, 468 .closed => closed_root = order_key, 469 .merged => merged_root = order_key, 470 } 471 // form urls keep their view; otherwise the patch's status picks it. 472 if (view != .edit and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (patch_event.event.status) { 473 .open => .open, 474 .closed => .closed, 475 .merged => .merged, 476 }; 477 478 // a resolve url shows the resolve view only while the patch still 479 // has a conflict entry; otherwise the status list stands. the 480 // wasm client renders from the snapshot, so the diff machinery 481 // this pulls in is gated out of its build. 482 if (comptime !wasm) { 483 if (view == .resolve) { 484 if (conflicts_map) |map| { 485 if (try map.getCursor(order_key)) |conflict_cursor| { 486 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 487 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, patch_event, conflict_entry); 488 resolved_view = .resolve; 489 } 490 } 491 } 492 } 493 } 494 } 495 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 496 var open_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, open_set, open_root, conflict_set, empty.selected_id, thread_comments_start); 497 var closed_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, closed_set, closed_root, conflict_set, empty.selected_id, thread_comments_start); 498 var merged_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, merged_set, merged_root, conflict_set, empty.selected_id, thread_comments_start); 499 var conflicts_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, conflict_set, conflicts_root, conflict_set, empty.selected_id, thread_comments_start); 500 try setForkDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &open_window); 501 try setForkDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &closed_window); 502 try setForkDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &merged_window); 503 try setForkDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &conflicts_window); 504 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 505 506 const comment_page = if (empty.comment_id.len == 0) 507 null 508 else 509 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start); 510 511 const tags = try thread.loadTags(Self, repo_opts.hash, arena, haxy_moment); 512 513 return .{ 514 .identity = empty.identity, 515 .tag = empty.tag, 516 .selected_id = empty.selected_id, 517 .comment_id = empty.comment_id, 518 .comments_start = comments_start, 519 .comment_page = comment_page, 520 .open = open_window, 521 .closed = closed_window, 522 .merged = merged_window, 523 .drafts = drafts_window, 524 .conflicts = conflicts_window, 525 .conflict = conflict_data, 526 .theirs_picks = empty.theirs_picks, 527 .view = resolved_view, 528 .description_page = empty.description_page, 529 .tags = tags, 530 }; 531 } 532 533 fn setForkDetails( 534 comptime repo_kind: rp.RepoKind, 535 comptime repo_opts: rp.RepoOpts(repo_kind), 536 io: std.Io, 537 arena: *std.heap.ArenaAllocator, 538 admin_moment: ?evt.AdminDB.HashMap(.read_only), 539 repo: *rp.Repo(repo_kind, repo_opts), 540 target: *Window, 541 ) !void { 542 if (target.items.len == 0) return; 543 const items = try arena.allocator().dupe(PatchWithId, target.items); 544 for (items) |*item| { 545 if (admin_moment) |moment| { 546 const id = try evt.parseEventId(item.id); 547 const record = try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &id); 548 item.fork_exists = if (record) |value| !value.removed else false; 549 } 550 const revision = item.record.event.revision orelse continue; 551 const target_branch = revision.targetBranch() orelse continue; 552 item.fork_oid = revision.source_oid; 553 item.target_branch = target_branch; 554 if (item.record.event.status == .merged) continue; 555 const target_oid = try repo.readRef(io, .{ .kind = .head, .name = target_branch }); 556 item.no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, revision.source_oid) else false; 557 } 558 target.items = items; 559 } 560 561 fn loadDraftWindow( 562 comptime repo_kind: rp.RepoKind, 563 comptime repo_opts: rp.RepoOpts(repo_kind), 564 arena: *std.heap.ArenaAllocator, 565 session: *ui.Session, 566 admin_moment: evt.AdminDB.HashMap(.read_only), 567 repo_id: *const [evt.event_id_size]u8, 568 selected_id: []const u8, 569 target_repo: *rp.Repo(repo_kind, repo_opts), 570 default_target_branch: []const u8, 571 ) !Window { 572 const user_id = session.data.user_id orelse return .empty; 573 if (user_id.len != evt.event_id_size) return .empty; 574 const repos_dir = session.repos_dir orelse return .empty; 575 const io = session.io orelse return .empty; 576 const key = evt.Fork.draftKey(repo_id, user_id); 577 const drafts_cursor = try admin_moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, evt.Fork.repo_user_to_draft_id_set_key)) orelse return .empty; 578 const drafts = try evt.AdminDB.HashMap(.read_only).init(drafts_cursor); 579 const set_cursor = try drafts.getCursor(hash.hashInt(evt.admin_repo_opts.hash, &key)) orelse return .empty; 580 const set = try evt.AdminDB.SortedSet(.read_only).init(set_cursor); 581 const aa = arena.allocator(); 582 583 var root_key: ?[]const u8 = null; 584 if (selected_id.len != 0) { 585 const id = evt.parseEventId(selected_id) catch return error.NotFound; 586 if (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, arena, &id)) |record| { 587 if (!record.removed and record.event.stage == .draft) { 588 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(record.created_order, &id)); 589 if (!try set.contains(order_key)) return error.NotFound; 590 root_key = order_key; 591 } 592 } 593 } 594 595 var prev_id: ?[]const u8 = null; 596 var iter = if (root_key) |root| blk: { 597 const rank = try set.rank(root); 598 if (rank > 0 and rank <= page_size) { 599 prev_id = ""; 600 } else if (rank > page_size) { 601 const pair = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 602 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 603 _ = try pair.key_cursor.readBytes(&previous); 604 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 605 prev_id = try aa.dupe(u8, &hex); 606 } 607 break :blk try set.iteratorFrom(root); 608 } else try set.iteratorFromIndex(0); 609 610 var items: std.ArrayList(PatchWithId) = .empty; 611 var next_id: ?[]const u8 = null; 612 while (try iter.next()) |cursor| { 613 const id = try evt.readOrderKeyId(evt.AdminDB, cursor); 614 const id_hex = std.fmt.bytesToHex(id, .lower); 615 if (items.items.len == page_size) { 616 next_id = try aa.dupe(u8, &id_hex); 617 break; 618 } 619 const path = try fork.forkPath(aa, repos_dir, &id_hex); 620 var fork_repo = rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 621 defer fork_repo.deinit(io, arena.child_allocator); 622 const entry = (try loadDraftEntry(repo_kind, repo_opts, arena, io, admin_moment, &fork_repo, target_repo, id, default_target_branch)) orelse continue; 623 try items.append(aa, entry); 624 } 625 return .{ 626 .items = items.items, 627 .prev_id = prev_id, 628 .next_id = next_id, 629 .count = @intCast(try set.count()), 630 }; 631 } 632 633 pub fn loadDraftEntry( 634 comptime repo_kind: rp.RepoKind, 635 comptime repo_opts: rp.RepoOpts(repo_kind), 636 arena: *std.heap.ArenaAllocator, 637 io: std.Io, 638 admin_moment: ?evt.AdminDB.HashMap(.read_only), 639 fork_repo: *rp.Repo(.xit, .{}), 640 target_repo: *rp.Repo(repo_kind, repo_opts), 641 id: [evt.event_id_size]u8, 642 default_target_branch: []const u8, 643 ) !?PatchWithId { 644 const aa = arena.allocator(); 645 const id_hex = std.fmt.bytesToHex(id, .lower); 646 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 647 const moment = evt.currentMoment(.{}, fork_repo) catch return null; 648 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, &id)) orelse return null; 649 var revision_ready = false; 650 var target_branch = default_target_branch; 651 if (patch.event.revision) |revision| { 652 target_branch = revision.targetBranch() orelse return null; 653 const revision_id = evt.parseEventId(&revision.id) catch return null; 654 if (try evt.PatchRev.readById(evt.EventDB(.sha1), .sha1, moment, arena, &revision_id)) |record| 655 revision_ready = revision.matches(record); 656 } 657 const target_oid = if (target_branch.len != 0) 658 try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }) 659 else 660 null; 661 return .{ 662 .id = try aa.dupe(u8, &id_hex), 663 .record = patch, 664 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 665 .draft = true, 666 .revision_ready = revision_ready, 667 .fork_oid = try aa.dupe(u8, &fork_oid), 668 .target_branch = try aa.dupe(u8, target_branch), 669 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 670 .fork_exists = true, 671 }; 672 } 673 674 pub const View = thread.View(.patch, Self); 675 pub const Detail = thread.Detail(.patch, Self); 676 pub const detail_widget_name = "repo_patch_detail"; 677 678 pub const Header = thread.Header; 679 680 pub fn appendDetails(self: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 681 const aa = session.page_arena.allocator(); 682 var fields: [2]ui.widget.CopyableText.Choice = undefined; 683 var field_count: usize = 0; 684 685 if (entry.record.event.status != .merged and entry.fork_exists and entry.target_branch.len != 0) if (session.data.git_ssh_port) |port| { 686 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}/patch:{s}/branch:{s}", .{ port, self.identity, entry.id, entry.target_branch }); 687 const push_command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 688 const clone_name = try cloneDirectoryName(aa, entry.record.event.title); 689 const clone_command = if (clone_name.len == 0) 690 try std.fmt.allocPrint(aa, "git clone {s}", .{url}) 691 else 692 try std.fmt.allocPrint(aa, "git clone {s} {s}", .{ url, clone_name }); 693 const choices: [2]ui.widget.CopyableText.Choice = .{ 694 .{ 695 .selector = "push", 696 .text = push_command, 697 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 698 .label = " push to this patch from existing repo ", 699 }, 700 .{ 701 .selector = "clone", 702 .text = clone_command, 703 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 704 .label = " clone this patch ", 705 }, 706 }; 707 var copyable_text = try ui.widget.CopyableText.init(allocator, session, &choices); 708 errdefer copyable_text.deinit(allocator); 709 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 710 }; 711 if (entry.fork_oid.len != 0) { 712 fields[field_count] = .{ 713 .selector = "from", 714 .text = entry.fork_oid, 715 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 716 }; 717 field_count += 1; 718 } 719 if (entry.target_branch.len != 0) { 720 fields[field_count] = .{ 721 .selector = "to", 722 .text = entry.target_branch, 723 .label = if (entry.record.event.status == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 724 .bottom_label = if (entry.record.event.status == .merged) "" else " (set by the url you push to above) ", 725 }; 726 field_count += 1; 727 } 728 729 if (field_count > 0) { 730 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[0..field_count]); 731 errdefer copyable_text.deinit(allocator); 732 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 733 } 734 } 735 736 fn cloneDirectoryName(allocator: std.mem.Allocator, title: []const u8) ![]const u8 { 737 var name: [64]u8 = undefined; 738 var len: usize = 0; 739 var pending: ?u8 = null; 740 741 for (title) |char| { 742 if (std.ascii.isAlphanumeric(char)) { 743 const needed: usize = if (pending == null) 1 else 2; 744 if (len + needed > name.len) break; 745 if (pending) |value| { 746 name[len] = value; 747 len += 1; 748 } 749 name[len] = std.ascii.toLower(char); 750 len += 1; 751 pending = null; 752 } else if (char == ' ') { 753 if (len != 0) pending = '-'; 754 } else if (char == '.') { 755 if (len != 0 and pending != '-') pending = '.'; 756 } 757 } 758 if (len == 0) return ""; 759 return allocator.dupe(u8, name[0..len]); 760 } 761 762 // tabs switching between the patches page's views 763 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 764 var header = try Header.init(allocator); 765 errdefer header.deinit(allocator); 766 const aa = session.page_arena.allocator(); 767 const selected_index = View.viewIndex(data.view); 768 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 769 770 // a list tab per status, labeled with its listing's patch count 771 for ([_]evt.Patch.Status{ .open, .closed, .merged }, 0..) |status, index| { 772 const route = ui.RoutablePage.repoPatchesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 773 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 774 var label_buf: [64]u8 = undefined; 775 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 776 try header.addTab(allocator, label, link, index); 777 } 778 779 // tags tab, labeled with the active tag filter 780 { 781 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.patch, data.identity, data.tag) orelse return error.RouteTooLong; 782 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 783 const label = if (data.tag.len == 0) "tags" else blk: { 784 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 785 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 786 }; 787 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 788 } 789 790 // new-patch tab; an edit or resolve url shows its tab in this place 791 if (session.local == null) { 792 const route = switch (data.view) { 793 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 794 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 795 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 796 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 797 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 798 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 799 }; 800 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 801 const label: []const u8 = switch (data.view) { 802 .edit => "edit", 803 .new_comment => "reply", 804 .edit_comment => "edit", 805 .remove => "remove", 806 .resolve => "resolve", 807 else => "new", 808 }; 809 try header.addTab(allocator, label, link, View.viewIndex(.new)); 810 } 811 812 // drafts belong to the logged-in user and are not available locally 813 if (session.local == null) { 814 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 815 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 816 var label_buf: [64]u8 = undefined; 817 const label = try std.fmt.bufPrint(&label_buf, "drafts ({d})", .{data.drafts.count}); 818 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 819 } 820 821 // conflicts tab, labeled with the conflict count 822 if (data.conflicts.count > 0) { 823 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 824 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 825 var label_buf: [64]u8 = undefined; 826 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 827 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 828 } 829 830 header.select(View.viewIndex(data.view)); 831 return header; 832 } 833