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