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