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