╭ 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 setForkAvailability(arena, admin_moment, &open_window); 501 try setForkAvailability(arena, admin_moment, &closed_window); 502 try setForkAvailability(arena, admin_moment, &merged_window); 503 try setForkAvailability(arena, admin_moment, &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 setForkAvailability(arena: *std.heap.ArenaAllocator, admin_moment: ?evt.AdminDB.HashMap(.read_only), target: *Window) !void { 534 const moment = admin_moment orelse return; 535 if (target.items.len == 0) return; 536 const items = try arena.allocator().dupe(PatchWithId, target.items); 537 for (items) |*item| { 538 const id = try evt.parseEventId(item.id); 539 const record = try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &id); 540 item.fork_exists = if (record) |value| !value.removed else false; 541 } 542 target.items = items; 543 } 544 545 fn loadDraftWindow( 546 comptime repo_kind: rp.RepoKind, 547 comptime repo_opts: rp.RepoOpts(repo_kind), 548 arena: *std.heap.ArenaAllocator, 549 session: *ui.Session, 550 admin_moment: evt.AdminDB.HashMap(.read_only), 551 repo_id: *const [evt.event_id_size]u8, 552 selected_id: []const u8, 553 target_repo: *rp.Repo(repo_kind, repo_opts), 554 default_target_branch: []const u8, 555 ) !Window { 556 const user_id = session.data.user_id orelse return .empty; 557 if (user_id.len != evt.event_id_size) return .empty; 558 const repos_dir = session.repos_dir orelse return .empty; 559 const io = session.io orelse return .empty; 560 const key = evt.Fork.draftKey(repo_id, user_id); 561 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; 562 const drafts = try evt.AdminDB.HashMap(.read_only).init(drafts_cursor); 563 const set_cursor = try drafts.getCursor(hash.hashInt(evt.admin_repo_opts.hash, &key)) orelse return .empty; 564 const set = try evt.AdminDB.SortedSet(.read_only).init(set_cursor); 565 const aa = arena.allocator(); 566 567 var root_key: ?[]const u8 = null; 568 if (selected_id.len != 0) { 569 const id = evt.parseEventId(selected_id) catch return error.NotFound; 570 if (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, arena, &id)) |record| { 571 if (!record.removed and record.event.stage == .draft) { 572 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(record.created_order, &id)); 573 if (!try set.contains(order_key)) return error.NotFound; 574 root_key = order_key; 575 } 576 } 577 } 578 579 var prev_id: ?[]const u8 = null; 580 var iter = if (root_key) |root| blk: { 581 const rank = try set.rank(root); 582 if (rank > 0 and rank <= page_size) { 583 prev_id = ""; 584 } else if (rank > page_size) { 585 const pair = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 586 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 587 _ = try pair.key_cursor.readBytes(&previous); 588 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 589 prev_id = try aa.dupe(u8, &hex); 590 } 591 break :blk try set.iteratorFrom(root); 592 } else try set.iteratorFromIndex(0); 593 594 var items: std.ArrayList(PatchWithId) = .empty; 595 var next_id: ?[]const u8 = null; 596 while (try iter.next()) |cursor| { 597 const id = try evt.readOrderKeyId(evt.AdminDB, cursor); 598 const id_hex = std.fmt.bytesToHex(id, .lower); 599 if (items.items.len == page_size) { 600 next_id = try aa.dupe(u8, &id_hex); 601 break; 602 } 603 const path = try fork.forkPath(aa, repos_dir, &id_hex); 604 var fork_repo = rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 605 defer fork_repo.deinit(io, arena.child_allocator); 606 const entry = (try loadDraftEntry(repo_kind, repo_opts, arena, io, admin_moment, &fork_repo, target_repo, id, default_target_branch)) orelse continue; 607 try items.append(aa, entry); 608 } 609 return .{ 610 .items = items.items, 611 .prev_id = prev_id, 612 .next_id = next_id, 613 .count = @intCast(try set.count()), 614 }; 615 } 616 617 pub fn loadDraftEntry( 618 comptime repo_kind: rp.RepoKind, 619 comptime repo_opts: rp.RepoOpts(repo_kind), 620 arena: *std.heap.ArenaAllocator, 621 io: std.Io, 622 admin_moment: ?evt.AdminDB.HashMap(.read_only), 623 fork_repo: *rp.Repo(.xit, .{}), 624 target_repo: *rp.Repo(repo_kind, repo_opts), 625 id: [evt.event_id_size]u8, 626 default_target_branch: []const u8, 627 ) !?PatchWithId { 628 const aa = arena.allocator(); 629 const id_hex = std.fmt.bytesToHex(id, .lower); 630 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 631 const moment = evt.currentMoment(.{}, fork_repo) catch return null; 632 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, &id)) orelse return null; 633 var revision_ready = false; 634 var target_branch = default_target_branch; 635 if (patch.event.revision) |revision| { 636 target_branch = revision.targetBranch() orelse return null; 637 const revision_id = evt.parseEventId(&revision.id) catch return null; 638 if (try evt.PatchRev.readById(evt.EventDB(.sha1), .sha1, moment, arena, &revision_id)) |record| 639 revision_ready = revision.matches(record); 640 } 641 const target_oid = if (target_branch.len != 0) 642 try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }) 643 else 644 null; 645 return .{ 646 .id = try aa.dupe(u8, &id_hex), 647 .record = patch, 648 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 649 .draft = true, 650 .revision_ready = revision_ready, 651 .fork_oid = try aa.dupe(u8, &fork_oid), 652 .target_branch = try aa.dupe(u8, target_branch), 653 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 654 .fork_exists = true, 655 }; 656 } 657 658 pub const View = thread.View(.patch, Self); 659 660 pub const Header = thread.Header; 661 662 pub fn appendDraftDetails(self: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 663 const aa = session.page_arena.allocator(); 664 var fields: [2]ui.widget.CopyableText.Choice = undefined; 665 var field_count: usize = 0; 666 667 if (entry.target_branch.len != 0) if (session.data.git_ssh_port) |port| { 668 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}/patch:{s}/branch:{s}", .{ port, self.identity, entry.id, entry.target_branch }); 669 const push_command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 670 const clone_name = try cloneDirectoryName(aa, entry.record.event.title); 671 const clone_command = if (clone_name.len == 0) 672 try std.fmt.allocPrint(aa, "git clone {s}", .{url}) 673 else 674 try std.fmt.allocPrint(aa, "git clone {s} {s}", .{ url, clone_name }); 675 const choices: [2]ui.widget.CopyableText.Choice = .{ 676 .{ 677 .selector = "push", 678 .text = push_command, 679 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 680 .label = " push to this patch from existing repo ", 681 }, 682 .{ 683 .selector = "clone", 684 .text = clone_command, 685 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 686 .label = " clone this patch ", 687 }, 688 }; 689 var copyable_text = try ui.widget.CopyableText.init(allocator, session, &choices); 690 errdefer copyable_text.deinit(allocator); 691 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 692 }; 693 if (entry.fork_oid.len != 0) { 694 fields[field_count] = .{ 695 .selector = "from", 696 .text = entry.fork_oid, 697 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 698 }; 699 field_count += 1; 700 } 701 if (entry.target_branch.len != 0) { 702 fields[field_count] = .{ 703 .selector = "to", 704 .text = entry.target_branch, 705 .label = " target branch this patch will go to ", 706 .bottom_label = " (set by the url you push to above) ", 707 }; 708 field_count += 1; 709 } 710 711 if (field_count > 0) { 712 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[0..field_count]); 713 errdefer copyable_text.deinit(allocator); 714 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 715 716 var spacer = try ui.widget.Spacer.init(allocator); 717 errdefer spacer.deinit(allocator); 718 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = .{ .width = null, .height = 1 }, .max_size = .{ .width = null, .height = 1 } }); 719 } 720 } 721 722 fn cloneDirectoryName(allocator: std.mem.Allocator, title: []const u8) ![]const u8 { 723 var name: [64]u8 = undefined; 724 var len: usize = 0; 725 var pending: ?u8 = null; 726 727 for (title) |char| { 728 if (std.ascii.isAlphanumeric(char)) { 729 const needed: usize = if (pending == null) 1 else 2; 730 if (len + needed > name.len) break; 731 if (pending) |value| { 732 name[len] = value; 733 len += 1; 734 } 735 name[len] = std.ascii.toLower(char); 736 len += 1; 737 pending = null; 738 } else if (char == ' ') { 739 if (len != 0) pending = '-'; 740 } else if (char == '.') { 741 if (len != 0 and pending != '-') pending = '.'; 742 } 743 } 744 if (len == 0) return ""; 745 return allocator.dupe(u8, name[0..len]); 746 } 747 748 // tabs switching between the patches page's views 749 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 750 var header = try Header.init(allocator); 751 errdefer header.deinit(allocator); 752 const aa = session.page_arena.allocator(); 753 const selected_index = View.viewIndex(data.view); 754 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 755 756 // a list tab per status, labeled with its listing's patch count 757 for ([_]evt.Patch.Status{ .open, .closed, .merged }, 0..) |status, index| { 758 const route = ui.RoutablePage.repoPatchesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 759 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 760 var label_buf: [64]u8 = undefined; 761 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 762 try header.addTab(allocator, label, link, index); 763 } 764 765 // tags tab, labeled with the active tag filter 766 { 767 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.patch, data.identity, data.tag) orelse return error.RouteTooLong; 768 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 769 const label = if (data.tag.len == 0) "tags" else blk: { 770 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 771 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 772 }; 773 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 774 } 775 776 // new-patch tab; an edit or resolve url shows its tab in this place 777 if (session.local == null) { 778 const route = switch (data.view) { 779 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 780 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 781 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 782 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 783 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 784 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 785 }; 786 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 787 const label: []const u8 = switch (data.view) { 788 .edit => "edit", 789 .new_comment => "reply", 790 .edit_comment => "edit", 791 .remove => "remove", 792 .resolve => "resolve", 793 else => "new", 794 }; 795 try header.addTab(allocator, label, link, View.viewIndex(.new)); 796 } 797 798 // drafts belong to the logged-in user and are not available locally 799 if (session.local == null) { 800 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 801 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 802 var label_buf: [64]u8 = undefined; 803 const label = try std.fmt.bufPrint(&label_buf, "drafts ({d})", .{data.drafts.count}); 804 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 805 } 806 807 // conflicts tab, labeled with the conflict count 808 if (data.conflicts.count > 0) { 809 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 810 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 811 var label_buf: [64]u8 = undefined; 812 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 813 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 814 } 815 816 header.select(View.viewIndex(data.view)); 817 return header; 818 } 819