* Check if a container already exists and whether it's valid for reuse. * Returns action to take: skip setup, cleanup invalid container, or create new.
(
containerName: string,
workspacePath: string,
branchName: string
)
| 554 | * Returns action to take: skip setup, cleanup invalid container, or create new. |
| 555 | */ |
| 556 | private async checkExistingContainer( |
| 557 | containerName: string, |
| 558 | workspacePath: string, |
| 559 | branchName: string |
| 560 | ): Promise<ContainerCheckResult> { |
| 561 | const exists = await runDockerCommand(`docker inspect ${containerName}`, 10000); |
| 562 | if (exists.exitCode !== 0) return { action: "create" }; |
| 563 | |
| 564 | const isRunning = await runDockerCommand( |
| 565 | `docker inspect -f '{{.State.Running}}' ${containerName}`, |
| 566 | 10000 |
| 567 | ); |
| 568 | if (isRunning.exitCode !== 0 || isRunning.stdout.trim() !== "true") { |
| 569 | return { action: "cleanup", reason: "Removing stale container from previous attempt..." }; |
| 570 | } |
| 571 | |
| 572 | // Container running - validate it has an initialized git repo |
| 573 | const gitCheck = await runDockerCommand( |
| 574 | `docker exec ${containerName} test -d ${workspacePath}/.git`, |
| 575 | 5000 |
| 576 | ); |
| 577 | if (gitCheck.exitCode !== 0) { |
| 578 | return { |
| 579 | action: "cleanup", |
| 580 | reason: "Container exists but repo not initialized, recreating...", |
| 581 | }; |
| 582 | } |
| 583 | |
| 584 | // Verify correct branch is checked out |
| 585 | // (handles edge case: crash after clone but before checkout left container on wrong branch) |
| 586 | const branchCheck = await runDockerCommand( |
| 587 | `docker exec ${containerName} git -C ${workspacePath} rev-parse --abbrev-ref HEAD`, |
| 588 | 5000 |
| 589 | ); |
| 590 | if (branchCheck.exitCode !== 0 || branchCheck.stdout.trim() !== branchName) { |
| 591 | return { action: "cleanup", reason: "Container exists but wrong branch, recreating..." }; |
| 592 | } |
| 593 | |
| 594 | return { action: "skip" }; |
| 595 | } |
| 596 | |
| 597 | private async detectContainerUser( |
| 598 | containerName: string, |
no test coverage detected