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