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