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