| 363 | } |
| 364 | |
| 365 | export async function ensureChildWorktree( |
| 366 | parent: Session.Info & { benchmark: BenchmarkSchema.Parent }, |
| 367 | child: Session.Info & { benchmark: BenchmarkSchema.ChildSession }, |
| 368 | ) { |
| 369 | const worktree = |
| 370 | child.benchmark.worktree ?? path.join(Global.Path.data, "benchmark", parent.id, child.id) |
| 371 | const exists = await fs |
| 372 | .stat(worktree) |
| 373 | .then(() => true) |
| 374 | .catch(() => false) |
| 375 | if (!exists) { |
| 376 | await fs.mkdir(worktree, { recursive: true }) |
| 377 | if (parent.benchmark.baseSnapshot) { |
| 378 | await Snapshot.restoreTo(parent.benchmark.baseSnapshot, worktree, { cleanUntracked: true }) |
| 379 | } |
| 380 | await Session.update(child.id, (draft) => { |
| 381 | if (draft.benchmark?.type === "child") { |
| 382 | draft.benchmark.worktree = worktree |
| 383 | } |
| 384 | }) |
| 385 | await Session.update(parent.id, (draft) => { |
| 386 | if (draft.benchmark?.type === "parent") { |
| 387 | const entry = draft.benchmark.children.find((item) => item.sessionID === child.id) |
| 388 | if (entry) entry.worktree = worktree |
| 389 | } |
| 390 | }) |
| 391 | } |
| 392 | return worktree |
| 393 | } |
| 394 | } |