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