╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patches.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
264 } 265 266 pub fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 267 return ui.RoutablePage.repoPatchesConflictsRoute(identity, selected); 268 } 269 270 pub fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 271 return ui.RoutablePage.repoPatchesResolveRoute(identity, selected, picks); 272 } 273 274 pub fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 275 return ui.RoutablePage.repoPatchPublishRoute(identity, selected); 276 } 277 278 pub fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 279 return ui.RoutablePage.repoPatchMergeRoute(identity, selected); 280 } 281 282 pub fn canMerge(entry: Entry, session: *const ui.Session) bool { 283 return !entry.draft and 284 (entry.fork_exists or entry.record.event.source_branch != null) and 285 !entry.conflicted and 286 entry.record.event.revision != null and 287 entry.record.event.status.kind() == .open and 288 session.data.host_kind == .server and 289 session.data.user_id != null; 290 } 291 292 pub fn create( 293 data: *const Self, 294 session: *ui.Session, 295 allocator: std.mem.Allocator, 296 title: []const u8, 297 tags: []const u8, 298 description: []const u8, 299 target_branch: []const u8, 300 source_branch: ?[]const u8, 301 ) ![evt.event_id_size * 2]u8 { 302 if (!evt.Patch.fieldsValid(title, tags)) return error.InvalidFields; 303 const io = session.io orelse return error.NotFound; 304 const repo_source = data.repo_source orelse return error.NotFound; 305 const author = (try session.eventAuthor()) orelse return error.NotFound; 306 var id_bytes: [evt.event_id_size]u8 = undefined; 307 io.random(&id_bytes); 308 const id = std.fmt.bytesToHex(id_bytes, .lower); 309 if (source_branch) |branch| { 310 switch (repo_source.repo_kind) { 311 inline else => |repo_kind| { 312 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, repo_source.localInitOpts()); 313 defer any_repo.deinit(io, allocator); 314 switch (any_repo) { 315 inline else => |*repo| try pch.writeBranchPatch(session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, id, .{ 316 .title = title, 317 .tags = tags, 318 .description = description, 319 .target_branch = target_branch, 320 .source_branch = branch, 321 }, null, author), 322 } 323 }, 324 } 325 return id; 326 } 327 if (session.data.host_kind == .local) return error.InvalidSourceBranch; 328 const repos_dir = session.repos_dir orelse return error.NotFound; 329 const admin_repo = session.admin_repo orelse return error.NotFound; 330 const user_id = session.userId() orelse return error.NotFound; 331 const repo_id = data.repo_id orelse return error.NotFound; 332 if (!evt.Patch.branchValid(target_branch) or !try repo_source.hasBranch(io, allocator, target_branch)) return error.InvalidTargetBranch; 333 const fork_path = try fork.create(.{}, io, allocator, repos_dir, admin_repo, .{ 334 .id = id, 335 .user_id = user_id, 336 .repo_id = repo_id, 337 .title = title, 338 .description = description, 339 .tags = tags, 340 .target_branch = target_branch, 341 .author = author, 342 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 343 }); 344 allocator.free(fork_path); 345 return id; 346 } 347 348 pub fn publishDraft(data: *const Self, session: *ui.Session, allocator: std.mem.Allocator, id: []const u8) !void { 349 const io = session.io orelse return error.NotFound; 350 const repos_dir = session.repos_dir orelse return error.NotFound; 351 const admin_repo = session.admin_repo orelse return error.NotFound; 352 const repo_source = data.repo_source orelse return error.NotFound; 353 const repo_id = data.repo_id orelse return error.NotFound; 354 const user_id = session.userId() orelse return error.NotFound; 355 const patch_id = evt.parseEventId(id) catch return error.NotFound; 356 const author = (try session.eventAuthor()) orelse return error.NotFound; 357 358 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 359 const fork_path = try fork.forkPath(allocator, repos_dir, &id_hex); 360 defer allocator.free(fork_path); 361 362 var target_repo = try rp.Repo(.xit, .{}).open(io, allocator, repo_source.localInitOpts()); 363 defer target_repo.deinit(io, allocator); 364 try pch.publish(.{}, io, allocator, admin_repo, &target_repo, fork_path, .{ 365 .id = id_hex, 366 .user_id = user_id, 367 .repo_id = repo_id, 368 .author = author, 369 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 370 }); 371 } 372 373 pub fn mergePatch(data: *const Self, session: *ui.Session, allocator: std.mem.Allocator, id: []const u8, revision: pch.MergeRevision) !void { 374 const io = session.io orelse return error.NotFound; 375 const repos_dir = session.repos_dir orelse return error.NotFound; 376 const admin_repo = session.admin_repo orelse return error.NotFound; 377 const repo_source = data.repo_source orelse return error.NotFound; 378 if (repo_source.repo_kind != .xit) return error.NotFound; 379 const patch_id = evt.parseEventId(id) catch return error.NotFound; 380 const author = (try session.eventAuthor()) orelse return error.NotFound; 381 382 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 383 var target_repo = try rp.Repo(.xit, .{}).open(io, allocator, repo_source.localInitOpts()); 384 defer target_repo.deinit(io, allocator); 385 try pch.mergeAndRemoveFork(.{}, io, allocator, repos_dir, admin_repo, &target_repo, .{ 386 .id = id_hex, 387 .revision = revision, 388 .author = author, 389 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 390 }); 391 } 392 393 pub fn editDraft( 394 data: *const Self, 395 session: *ui.Session, 396 allocator: std.mem.Allocator, 397 id: []const u8, 398 title: []const u8, 399 tags: []const u8, 400 description: []const u8, 401 target_branch: []const u8, 402 ) !void { 403 const io = session.io orelse return error.NotFound; 404 const repos_dir = session.repos_dir orelse return error.NotFound; 405 const admin_repo = session.admin_repo orelse return error.NotFound; 406 const repo_id = data.repo_id orelse return error.NotFound; 407 const repo_source = data.repo_source orelse return error.NotFound; 408 const user_id = session.userId() orelse return error.NotFound; 409 const patch_id = evt.parseEventId(id) catch return error.NotFound; 410 const author = (try session.eventAuthor()) orelse return error.NotFound; 411 if (!evt.Patch.branchValid(target_branch) or !try repo_source.hasBranch(io, allocator, target_branch)) return error.InvalidTargetBranch; 412 413 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 414 const fork_path = try fork.forkPath(allocator, repos_dir, &id_hex); 415 defer allocator.free(fork_path); 416 if (!try pch.editDraft(.{}, io, allocator, admin_repo, fork_path, .{ 417 .id = id_hex, 418 .user_id = user_id, 419 .repo_id = repo_id, 420 .title = title, 421 .tags = tags, 422 .description = description, 423 .target_branch = target_branch, 424 .author = author, 425 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 426 })) return error.NotFound; 427 } 428 429 pub fn removeDraft(session: *ui.Session, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8) !void { 430 const io = session.io orelse return error.NotFound; 431 const repos_dir = session.repos_dir orelse return error.NotFound; 432 const admin_repo = session.admin_repo orelse return error.NotFound; 433 const user_id = session.userId() orelse return error.NotFound; 434 const author = (try session.eventAuthor()) orelse return error.NotFound; 435 436 const id_hex = std.fmt.bytesToHex(id.*, .lower); 437 try fork.remove(io, allocator, repos_dir, admin_repo, &id_hex, &user_id, author); 438 } 439 440 // `status`'s windowed listing. 441 pub fn window(self: *const Self, status: Status) *const Window { 442 return switch (status) { 443 .open => &self.open, 444 .closed => &self.closed, 445 .merged => &self.merged, 446 }; 447 } 448 449 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 450 // theirs: picks. 451 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 452 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 453 while (it.next()) |pick| { 454 if (std.mem.eql(u8, pick, field)) return true; 455 } 456 return false; 457 } 458 459 // the patch `selected_id` names, in whichever window holds it. 460 pub fn selectedThread(self: *const Self) ?*const PatchWithId { 461 if (self.selected_id.len == 0) return null; 462 for ([_]*const Window{ &self.open, &self.closed, &self.merged, &self.drafts }) |win| { 463 for (win.items) |*entry| { 464 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 465 } 466 } 467 return null; 468 } 469 470 // an empty listing, for the wasm / no-repo paths. 471 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 { 472 return .{ 473 .identity = try aa.dupe(u8, identity), 474 .default_target_branch = "", 475 .tag = try aa.dupe(u8, tag), 476 .selected_id = try aa.dupe(u8, selected_id), 477 .comment_id = try aa.dupe(u8, comment_id), 478 .comments_start = comments_start, 479 .open = .empty, 480 .closed = .empty, 481 .merged = .empty, 482 .drafts = .empty, 483 .conflicts = .empty, 484 .theirs_picks = try aa.dupe(u8, theirs_picks), 485 // a description url shows a status list, the patch's own status 486 // picking it once the patch is read; conflicts and resolve urls also 487 // start there, upgraded by init when the conflict data exists. 488 .view = switch (view) { 489 .description, .conflicts, .resolve => .open, 490 else => view, 491 }, 492 .description_page = view == .description, 493 .tags = &.{}, 494 }; 495 } 496 497 pub fn detailResult(aa: std.mem.Allocator, identity: []const u8, entry: PatchWithId) !Self { 498 var result = try emptyResult(aa, identity, "", entry.id, "", 0, "", .open); 499 const items = try aa.dupe(PatchWithId, &.{entry}); 500 const detail_window = Window{ .items = items, .prev_id = null, .next_id = null, .count = 1 }; 501 if (entry.draft) { 502 result.drafts = detail_window; 503 result.view = .drafts; 504 } else switch (entry.record.event.status) { 505 .open => result.open = detail_window, 506 .closed => { 507 result.closed = detail_window; 508 result.view = .closed; 509 }, 510 .merged => { 511 result.merged = detail_window; 512 result.view = .merged; 513 }, 514 } 515 return result; 516 } 517 518 // read one window per status of an opened repo's patches (filtered to `tag` 519 // when set), ordered by creation (newest first). the window of the patch 520 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 521 // event db next to it (synced from the events branch on each page build); a 522 // xit repo reads its own db. 523 pub fn init( 524 comptime repo_kind: rp.RepoKind, 525 comptime repo_opts: rp.RepoOpts(repo_kind), 526 arena: *std.heap.ArenaAllocator, 527 repo: *rp.Repo(repo_kind, repo_opts), 528 io: std.Io, 529 // the admin db's moment, for resolving author emails to user names (null 530 // in local mode, which has no users) 531 admin_moment: ?evt.AdminDB.HashMap(.read_only), 532 session: *ui.Session, 533 repo_id_maybe: ?[evt.event_id_size]u8, 534 identity: []const u8, 535 target_branch: []const u8, 536 tag: []const u8, 537 selected_id: []const u8, 538 comment_id: []const u8, 539 comments_start: usize, 540 theirs_picks: []const u8, 541 view: ui.RoutablePage.PatchesView, 542 ) !Self { 543 const allowed_view: ui.RoutablePage.PatchesView = if (session.local != null and view == .drafts) .open else view; 544 var empty = try emptyResult(arena.allocator(), identity, tag, selected_id, comment_id, comments_start, theirs_picks, allowed_view); 545 empty.default_target_branch = try arena.allocator().dupe(u8, target_branch); 546 547 const aa = arena.allocator(); 548 const DB = evt.EventDB(repo_opts.hash); 549 const rooted = empty.selected_id.len != 0; 550 const tagged = empty.tag.len != 0; 551 const drafts_window = if (admin_moment) |admin| 552 if (repo_id_maybe) |repo_id| 553 if (session.data.user_id != null and session.repos_dir != null) 554 try loadDraftWindow(repo_kind, repo_opts, arena, session, admin, &repo_id, empty.selected_id, repo) 555 else 556 Window.empty 557 else 558 Window.empty 559 else 560 Window.empty; 561 var draft_selected = false; 562 if (rooted) for (drafts_window.items) |item| { 563 if (std.mem.eql(u8, item.id, empty.selected_id)) { 564 draft_selected = true; 565 break; 566 } 567 }; 568 empty.drafts = drafts_window; 569 if (draft_selected and view != .edit and view != .publish and view != .remove) empty.view = .drafts; 570 571 // an explicitly named published patch that doesn't exist is a bad url 572 // (NotFound -> 404); drafts, tags, and bare routes can use the empty fallback. 573 const strict = rooted and !draft_selected; 574 575 // a repo with no consumed events has no moment yet. 576 const gpa = arena.child_allocator; 577 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; 578 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 579 const haxy_moment = (if (event_db_maybe) |*event_db| 580 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 581 else if (repo_kind == .git) 582 return empty 583 else 584 evt.currentMoment(repo_opts, repo)) catch { 585 if (strict) return error.NotFound; 586 return empty; 587 }; 588 589 // the sorted sets to window, per status: the tag's sets when filtered, 590 // else the top-level per-status sets. a missing set is an empty window. 591 var open_set: ?DB.SortedSet(.read_only) = null; 592 var closed_set: ?DB.SortedSet(.read_only) = null; 593 var merged_set: ?DB.SortedSet(.read_only) = null; 594 if (tagged) { 595 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key))) |tag_to_patches_cursor| { 596 const tag_to_patches = try DB.SortedMap(.read_only).init(tag_to_patches_cursor); 597 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 598 open_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .open); 599 closed_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .closed); 600 merged_set = try thread.tagStatusSet(Self, DB, tag_to_patches, decoded, .merged); 601 } 602 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key))) |status_to_patches_cursor| { 603 const status_to_patches = try DB.SortedMap(.read_only).init(status_to_patches_cursor); 604 open_set = try thread.statusSet(Self, DB, status_to_patches, .open); 605 closed_set = try thread.statusSet(Self, DB, status_to_patches, .closed); 606 merged_set = try thread.statusSet(Self, DB, status_to_patches, .merged); 607 } else if (strict) return error.NotFound; 608 609 const event_id_to_patch_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse { 610 if (strict) return error.NotFound; 611 return empty; 612 }; 613 const event_id_to_patch = try DB.HashMap(.read_only).init(event_id_to_patch_cursor); 614 615 // the conflicted patches' container: a set view for membership and 616 // windowing, a map view for the resolve entry, over the same cursor. 617 var conflict_set: ?DB.SortedSet(.read_only) = null; 618 var conflicts_map: ?DB.SortedMap(.read_only) = null; 619 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 620 conflict_set = try DB.SortedSet(.read_only).init(cursor); 621 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 622 } 623 624 // a named patch roots its own status's window at itself (the conflicts 625 // view's window instead); the other windows start at the beginning. 626 var resolved_view = empty.view; 627 var open_root: ?[]const u8 = null; 628 var closed_root: ?[]const u8 = null; 629 var merged_root: ?[]const u8 = null; 630 var conflicts_root: ?[]const u8 = null; 631 var conflict_data: ?Conflict = null; 632 if (rooted and !draft_selected) { 633 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 634 const patch_cursor = try event_id_to_patch.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 635 const patch_map = try DB.HashMap(.read_only).init(patch_cursor); 636 const patch_event = try evt.read(evt.Patch.Record, DB, repo_opts.hash, arena, patch_map); 637 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(patch_event.created_order, &id_bytes)); 638 639 if (view == .conflicts) { 640 // the id roots the conflicts window, so it must be conflicted 641 const set = conflict_set orelse return error.NotFound; 642 if (!try set.contains(order_key)) return error.NotFound; 643 conflicts_root = order_key; 644 } else { 645 // the named patch must be in its windowed set (a tag url can name 646 // an patch that doesn't carry the tag). 647 const set = (switch (patch_event.event.status) { 648 .open => open_set, 649 .closed => closed_set, 650 .merged => merged_set, 651 }) orelse return error.NotFound; 652 if (!try set.contains(order_key)) return error.NotFound; 653 654 switch (patch_event.event.status) { 655 .open => open_root = order_key, 656 .closed => closed_root = order_key, 657 .merged => merged_root = order_key, 658 } 659 // form urls keep their view; otherwise the patch's status picks it. 660 if (view != .edit and view != .merge and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (patch_event.event.status) { 661 .open => .open, 662 .closed => .closed, 663 .merged => .merged, 664 }; 665 666 // a resolve url shows the resolve view only while the patch still 667 // has a conflict entry; otherwise the status list stands. the 668 // wasm client renders from the snapshot, so the diff machinery 669 // this pulls in is gated out of its build. 670 if (comptime !wasm) { 671 if (view == .resolve) { 672 if (conflicts_map) |map| { 673 if (try map.getCursor(order_key)) |conflict_cursor| { 674 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 675 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, patch_event, conflict_entry); 676 resolved_view = .resolve; 677 } 678 } 679 } 680 } 681 } 682 } 683 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 684 var open_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, open_set, open_root, conflict_set, empty.selected_id, thread_comments_start); 685 var closed_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, closed_set, closed_root, conflict_set, empty.selected_id, thread_comments_start); 686 var merged_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, merged_set, merged_root, conflict_set, empty.selected_id, thread_comments_start); 687 var conflicts_window = try thread.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, conflict_set, conflicts_root, conflict_set, empty.selected_id, thread_comments_start); 688 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &open_window); 689 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &closed_window); 690 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &merged_window); 691 try setPatchDetails(repo_kind, repo_opts, io, arena, admin_moment, repo, &conflicts_window); 692 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 693 694 const comment_page = if (empty.comment_id.len == 0) 695 null 696 else 697 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start); 698 699 const tags = try thread.loadTags(Self, repo_opts.hash, arena, haxy_moment); 700 701 return .{ 702 .identity = empty.identity, 703 .default_target_branch = empty.default_target_branch, 704 .tag = empty.tag, 705 .selected_id = empty.selected_id, 706 .comment_id = empty.comment_id, 707 .comments_start = comments_start, 708 .comment_page = comment_page, 709 .open = open_window, 710 .closed = closed_window, 711 .merged = merged_window, 712 .drafts = drafts_window, 713 .conflicts = conflicts_window, 714 .conflict = conflict_data, 715 .theirs_picks = empty.theirs_picks, 716 .view = resolved_view, 717 .description_page = empty.description_page, 718 .tags = tags, 719 }; 720 } 721 722 fn setPatchDetails( 723 comptime repo_kind: rp.RepoKind, 724 comptime repo_opts: rp.RepoOpts(repo_kind), 725 io: std.Io, 726 arena: *std.heap.ArenaAllocator, 727 admin_moment: ?evt.AdminDB.HashMap(.read_only), 728 repo: *rp.Repo(repo_kind, repo_opts), 729 target: *Window, 730 ) !void { 731 if (target.items.len == 0) return; 732 const items = try arena.allocator().dupe(PatchWithId, target.items); 733 for (items) |*item| { 734 if (admin_moment) |moment| { 735 const id = try evt.parseEventId(item.id); 736 const record = try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &id); 737 item.fork_exists = if (record) |value| !value.removed else false; 738 } 739 const target_branch = item.record.event.target_branch; 740 const revision = item.record.event.revision orelse continue; 741 item.commit_count = revision.commit_count; 742 item.revision_oid = switch (item.record.event.status) { 743 .merged => |oid| oid, 744 else => revision.source_oid, 745 }; 746 if (item.record.event.status.kind() == .merged) continue; 747 if (comptime repo_kind == .xit) { 748 var moment = try repo.core.latestMoment(); 749 const state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 750 const id = try evt.parseEventId(item.id); 751 item.mergeability = pch.readMergeability(repo_opts, state, io, arena, &id, item.record.event) catch .{}; 752 } 753 const target_oid = try repo.readRef(io, .{ .kind = .head, .name = target_branch }); 754 item.no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, revision.source_oid) else false; 755 } 756 target.items = items; 757 } 758 759 fn loadDraftWindow( 760 comptime repo_kind: rp.RepoKind, 761 comptime repo_opts: rp.RepoOpts(repo_kind), 762 arena: *std.heap.ArenaAllocator, 763 session: *ui.Session, 764 admin_moment: evt.AdminDB.HashMap(.read_only), 765 repo_id: *const [evt.event_id_size]u8, 766 selected_id: []const u8, 767 target_repo: *rp.Repo(repo_kind, repo_opts), 768 ) !Window { 769 const user_id = session.data.user_id orelse return .empty; 770 if (user_id.len != evt.event_id_size) return .empty; 771 const repos_dir = session.repos_dir orelse return .empty; 772 const io = session.io orelse return .empty; 773 const key = evt.Fork.draftKey(repo_id, user_id); 774 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; 775 const drafts = try evt.AdminDB.HashMap(.read_only).init(drafts_cursor); 776 const set_cursor = try drafts.getCursor(hash.hashInt(evt.admin_repo_opts.hash, &key)) orelse return .empty; 777 const set = try evt.AdminDB.SortedSet(.read_only).init(set_cursor); 778 const aa = arena.allocator(); 779 780 var root_key: ?[]const u8 = null; 781 if (selected_id.len != 0) { 782 const id = evt.parseEventId(selected_id) catch return error.NotFound; 783 if (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, arena, &id)) |record| { 784 if (!record.removed and record.event.stage == .draft) { 785 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(record.created_order, &id)); 786 if (!try set.contains(order_key)) return error.NotFound; 787 root_key = order_key; 788 } 789 } 790 } 791 792 var prev_id: ?[]const u8 = null; 793 var iter = if (root_key) |root| blk: { 794 const rank = try set.rank(root); 795 if (rank > 0 and rank <= page_size) { 796 prev_id = ""; 797 } else if (rank > page_size) { 798 const pair = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 799 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 800 _ = try pair.key_cursor.readBytes(&previous); 801 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 802 prev_id = try aa.dupe(u8, &hex); 803 } 804 break :blk try set.iteratorFrom(root); 805 } else try set.iteratorFromIndex(0); 806 807 var items: std.ArrayList(PatchWithId) = .empty; 808 var next_id: ?[]const u8 = null; 809 while (try iter.next()) |cursor| { 810 const id = try evt.readOrderKeyId(evt.AdminDB, cursor); 811 const id_hex = std.fmt.bytesToHex(id, .lower); 812 if (items.items.len == page_size) { 813 next_id = try aa.dupe(u8, &id_hex); 814 break; 815 } 816 const path = try fork.forkPath(aa, repos_dir, &id_hex); 817 var fork_repo = rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 818 defer fork_repo.deinit(io, arena.child_allocator); 819 const entry = (try loadDraftEntry(repo_kind, repo_opts, arena, io, admin_moment, &fork_repo, target_repo, id)) orelse continue; 820 try items.append(aa, entry); 821 } 822 return .{ 823 .items = items.items, 824 .prev_id = prev_id, 825 .next_id = next_id, 826 .count = @intCast(try set.count()), 827 }; 828 } 829 830 pub fn loadDraftEntry( 831 comptime repo_kind: rp.RepoKind, 832 comptime repo_opts: rp.RepoOpts(repo_kind), 833 arena: *std.heap.ArenaAllocator, 834 io: std.Io, 835 admin_moment: ?evt.AdminDB.HashMap(.read_only), 836 fork_repo: *rp.Repo(.xit, .{}), 837 target_repo: *rp.Repo(repo_kind, repo_opts), 838 id: [evt.event_id_size]u8, 839 ) !?PatchWithId { 840 const aa = arena.allocator(); 841 const id_hex = std.fmt.bytesToHex(id, .lower); 842 const fork_oid = (try fork_repo.readRef(io, fork.ref)) orelse return null; 843 const moment = evt.currentMoment(.{}, fork_repo) catch return null; 844 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, &id)) orelse return null; 845 const target_branch = patch.event.target_branch; 846 const target_oid = try target_repo.readRef(io, .{ .kind = .head, .name = target_branch }); 847 const commit_count: ?u64 = blk: { 848 const newest = evt.PatchRev.readNewest(evt.EventDB(.sha1), .sha1, moment, arena) catch break :blk null; 849 break :blk if (newest) |revision| revision.record.commit_count else 0; 850 }; 851 return .{ 852 .id = try aa.dupe(u8, &id_hex), 853 .record = patch, 854 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 855 .draft = true, 856 .revision_oid = try aa.dupe(u8, &fork_oid), 857 .commit_count = commit_count, 858 .no_changes = if (target_oid) |oid| std.mem.eql(u8, &oid, &fork_oid) else false, 859 .fork_exists = true, 860 }; 861 } 862 863 pub const View = thread.View(.patch, Self); 864 pub const Detail = thread.Detail(.patch, Self); 865 pub const detail_widget_name = "repo_patch_detail"; 866 867 pub const Header = thread.Header; 868 869 pub fn appendDetails(self: *const Self, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, entry: Entry) !void { 870 const aa = session.page_arena.allocator(); 871 const status_kind = entry.record.event.status.kind(); 872 const target_branch = entry.record.event.target_branch; 873 874 if (status_kind != .merged and entry.fork_exists) if (session.data.git_ssh_port) |port| { 875 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, self.identity, entry.id }); 876 const push_command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 877 const clone_name = try cloneDirectoryName(aa, entry.record.event.title); 878 const clone_command = if (clone_name.len == 0) 879 try std.fmt.allocPrint(aa, "git clone {s}", .{url}) 880 else 881 try std.fmt.allocPrint(aa, "git clone {s} {s}", .{ url, clone_name }); 882 const merge_command = try std.fmt.allocPrint(aa, "git fetch '{s}' refs/heads/patch && git merge FETCH_HEAD", .{url}); 883 const choices: [3]ui.widget.CopyableText.Choice = .{ 884 .{ 885 .selector = "push", 886 .text = push_command, 887 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, push_command }), 888 .label = " push to this patch from existing repo ", 889 }, 890 .{ 891 .selector = "clone", 892 .text = clone_command, 893 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, clone_command }), 894 .label = " clone this patch ", 895 }, 896 .{ 897 .selector = "merge", 898 .text = merge_command, 899 .copyable_text = try std.fmt.allocPrint(aa, "{s}{s}", .{ session.data.git_ssh_prefix, merge_command }), 900 .label = " merge this patch locally ", 901 }, 902 }; 903 var copyable_text = try ui.widget.CopyableText.init(allocator, session, &choices); 904 errdefer copyable_text.deinit(allocator); 905 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 906 }; 907 if (entry.record.event.source_branch) |branch| { 908 var source = try wgt.TextBox.init(allocator, branch, .{ .label = " source branch ", .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 909 errdefer source.deinit(allocator); 910 source.getFocus().mode = .all; 911 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .text_box = source }, .rect = null, .min_size = null }); 912 } 913 const fields = [_]ui.widget.CopyableText.Choice{ 914 .{ 915 .selector = "from", 916 .text = entry.revision_oid, 917 .label = if (entry.no_changes) " object id of this patch (no changes) " else " object id of this patch ", 918 }, 919 .{ 920 .selector = "to", 921 .text = target_branch, 922 .label = if (status_kind == .merged) " target branch this patch was merged into " else " target branch this patch will go to ", 923 }, 924 }; 925 var copyable_text = try ui.widget.CopyableText.init(allocator, session, fields[@intFromBool(entry.revision_oid.len == 0)..]); 926 errdefer copyable_text.deinit(allocator); 927 try box.children.put(allocator, copyable_text.getFocus().id, .{ .widget = .{ .copyable_text = copyable_text }, .rect = null, .min_size = null }); 928 } 929 930 fn cloneDirectoryName(allocator: std.mem.Allocator, title: []const u8) ![]const u8 { 931 var name: [64]u8 = undefined; 932 var len: usize = 0; 933 var pending: ?u8 = null; 934 935 for (title) |char| { 936 if (std.ascii.isAlphanumeric(char)) { 937 const needed: usize = if (pending == null) 1 else 2; 938 if (len + needed > name.len) break; 939 if (pending) |value| { 940 name[len] = value; 941 len += 1; 942 } 943 name[len] = std.ascii.toLower(char); 944 len += 1; 945 pending = null; 946 } else if (char == ' ') { 947 if (len != 0) pending = '-'; 948 } else if (char == '.') { 949 if (len != 0 and pending != '-') pending = '.'; 950 } 951 } 952 if (len == 0) return ""; 953 return allocator.dupe(u8, name[0..len]); 954 } 955 956 // tabs switching between the patches page's views 957 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 958 var header = try Header.init(allocator); 959 errdefer header.deinit(allocator); 960 const aa = session.page_arena.allocator(); 961 const selected_index = View.viewIndex(data.view); 962 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 963 964 // a list tab per status, labeled with its listing's patch count 965 for ([_]Status{ .open, .closed, .merged }, 0..) |status, index| { 966 const route = ui.RoutablePage.repoPatchesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 967 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 968 var label_buf: [64]u8 = undefined; 969 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 970 try header.addTab(allocator, label, link, index); 971 } 972 973 // tags tab, labeled with the active tag filter 974 { 975 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.patch, data.identity, data.tag) orelse return error.RouteTooLong; 976 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 977 const label = if (data.tag.len == 0) "tags" else blk: { 978 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 979 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 980 }; 981 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 982 } 983 984 // new-patch tab; an edit or resolve url shows its tab in this place 985 { 986 const route = switch (data.view) { 987 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 988 .publish => ui.RoutablePage.repoPatchPublishRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 989 .merge => ui.RoutablePage.repoPatchMergeRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 990 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 991 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 992 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 993 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 994 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 995 }; 996 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 997 const label: []const u8 = switch (data.view) { 998 .edit => "edit", 999 .publish => "publish", 1000 .merge => "merge", 1001 .new_comment => "reply", 1002 .edit_comment => "edit", 1003 .remove => "remove", 1004 .resolve => "resolve", 1005 else => "new", 1006 }; 1007 try header.addTab(allocator, label, link, View.viewIndex(.new)); 1008 } 1009 1010 // drafts belong to the logged-in user and are not available locally 1011 if (session.data.host_kind != .local) { 1012 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 1013 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 1014 var label_buf: [64]u8 = undefined; 1015 const label = try std.fmt.bufPrint(&label_buf, "drafts ({d})", .{data.drafts.count}); 1016 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 1017 } 1018 1019 // conflicts tab, labeled with the conflict count 1020 if (data.conflicts.count > 0) { 1021 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 1022 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 1023 var label_buf: [64]u8 = undefined; 1024 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 1025 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 1026 } 1027 1028 header.select(View.viewIndex(data.view)); 1029 return header; 1030 } 1031