worktree-compose 0.2.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +501 -143
- package/SKILL.md +113 -0
- package/dist/cli.js +94 -33
- package/dist/commands/clean.d.ts +2 -1
- package/dist/commands/clean.js +8 -48
- package/dist/commands/doctor.d.ts +3 -0
- package/dist/commands/doctor.js +30 -0
- package/dist/commands/list.d.ts +4 -1
- package/dist/commands/list.js +62 -44
- package/dist/commands/promote.d.ts +3 -1
- package/dist/commands/promote.js +4 -34
- package/dist/commands/prune.d.ts +2 -0
- package/dist/commands/prune.js +7 -0
- package/dist/commands/restart.d.ts +2 -1
- package/dist/commands/restart.js +11 -5
- package/dist/commands/skill.d.ts +4 -0
- package/dist/commands/skill.js +10 -0
- package/dist/commands/start.d.ts +2 -1
- package/dist/commands/start.js +7 -34
- package/dist/commands/stop.d.ts +1 -1
- package/dist/commands/stop.js +6 -23
- package/dist/compose/detect.d.ts +1 -2
- package/dist/compose/detect.js +1 -10
- package/dist/compose/engine.d.ts +61 -0
- package/dist/compose/engine.js +129 -0
- package/dist/compose/isolation.d.ts +26 -0
- package/dist/compose/isolation.js +91 -0
- package/dist/compose/override.d.ts +152 -0
- package/dist/compose/override.js +542 -0
- package/dist/compose/parse.d.ts +12 -0
- package/dist/compose/parse.js +199 -29
- package/dist/compose/status.d.ts +84 -0
- package/dist/compose/status.js +227 -0
- package/dist/compose/types.d.ts +43 -0
- package/dist/config.d.ts +31 -0
- package/dist/config.js +23 -6
- package/dist/context.d.ts +104 -3
- package/dist/context.js +299 -45
- package/dist/core/clean.d.ts +91 -0
- package/dist/core/clean.js +347 -0
- package/dist/core/compose.d.ts +92 -0
- package/dist/core/compose.js +195 -0
- package/dist/core/doctor.d.ts +29 -0
- package/dist/core/doctor.js +342 -0
- package/dist/core/list.d.ts +46 -0
- package/dist/core/list.js +101 -0
- package/dist/core/promote.d.ts +40 -0
- package/dist/core/promote.js +117 -0
- package/dist/core/prune.d.ts +32 -0
- package/dist/core/prune.js +79 -0
- package/dist/core/restart.d.ts +14 -0
- package/dist/core/restart.js +18 -0
- package/dist/core/start.d.ts +33 -0
- package/dist/core/start.js +113 -0
- package/dist/core/stop.d.ts +32 -0
- package/dist/core/stop.js +58 -0
- package/dist/core/teardown.d.ts +123 -0
- package/dist/core/teardown.js +305 -0
- package/dist/core/types.d.ts +15 -0
- package/dist/core/types.js +1 -0
- package/dist/git/promote.d.ts +49 -2
- package/dist/git/promote.js +160 -38
- package/dist/git/repo.d.ts +72 -0
- package/dist/git/repo.js +167 -0
- package/dist/git/status.d.ts +8 -0
- package/dist/git/status.js +25 -0
- package/dist/git/worktree.d.ts +63 -3
- package/dist/git/worktree.js +173 -29
- package/dist/mcp/server.d.ts +2 -0
- package/dist/mcp/server.js +230 -115
- package/dist/ports/allocate.d.ts +39 -5
- package/dist/ports/allocate.js +96 -16
- package/dist/ports/extract.d.ts +12 -0
- package/dist/ports/extract.js +48 -4
- package/dist/ports/types.d.ts +12 -1
- package/dist/selectors.d.ts +10 -0
- package/dist/selectors.js +106 -0
- package/dist/skill.d.ts +13 -0
- package/dist/skill.js +28 -0
- package/dist/state/registry.d.ts +194 -0
- package/dist/state/registry.js +595 -0
- package/dist/sync/env.d.ts +93 -2
- package/dist/sync/env.js +169 -32
- package/dist/sync/files.d.ts +15 -1
- package/dist/sync/files.js +126 -38
- package/dist/utils/exec.d.ts +69 -4
- package/dist/utils/exec.js +221 -15
- package/dist/utils/log.d.ts +37 -0
- package/dist/utils/log.js +68 -4
- package/dist/utils/paths.d.ts +61 -0
- package/dist/utils/paths.js +163 -0
- package/dist/utils/sanitize.d.ts +7 -0
- package/dist/utils/sanitize.js +12 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/dist/workspaces/resolve.d.ts +21 -0
- package/dist/workspaces/resolve.js +235 -0
- package/package.json +20 -16
- package/wtc.schema.json +62 -0
package/dist/config.d.ts
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
export interface WtcConfig {
|
|
2
|
+
/** JSON Schema reference for editor completion; ignored by wtc. */
|
|
3
|
+
$schema?: string;
|
|
4
|
+
/** Extra repo-relative files/directories copied from the main checkout on start. */
|
|
2
5
|
sync?: string[];
|
|
3
6
|
envOverrides?: Record<string, string>;
|
|
7
|
+
/**
|
|
8
|
+
* Env filename to copy from the main checkout into each worktree and inject
|
|
9
|
+
* port overrides into. Workspace targets use the same name. Defaults to `.env`.
|
|
10
|
+
*/
|
|
11
|
+
envFile?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Monorepo workspaces (by package name, directory name, or path) that also
|
|
14
|
+
* receive env file copies + port override injection, in addition to the repo
|
|
15
|
+
* root. Empty/omitted means root-only (the zero-config behavior).
|
|
16
|
+
*/
|
|
17
|
+
workspaces?: string[];
|
|
18
|
+
/**
|
|
19
|
+
* Per-workspace env overrides, keyed by workspace name (or directory).
|
|
20
|
+
* Values support `${VAR}` interpolation against allocated port values.
|
|
21
|
+
*/
|
|
22
|
+
workspaceEnvOverrides?: Record<string, Record<string, string>>;
|
|
4
23
|
/**
|
|
5
24
|
* Per-worktree-index port stride. Allocated port =
|
|
6
25
|
* 20000 + defaultPort + worktreeIndex * portStride. Defaults to 1.
|
|
@@ -8,5 +27,17 @@ export interface WtcConfig {
|
|
|
8
27
|
* ports so that adjacent worktrees do not collide across services.
|
|
9
28
|
*/
|
|
10
29
|
portStride?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Compose command to run instead of auto-detection, e.g. `"podman compose"`
|
|
32
|
+
* or `["docker-compose"]`. `WTC_COMPOSE_COMMAND` takes precedence.
|
|
33
|
+
*/
|
|
34
|
+
composeCommand?: string | string[];
|
|
35
|
+
}
|
|
36
|
+
export interface LoadedConfig {
|
|
37
|
+
config: WtcConfig;
|
|
38
|
+
/** File the config came from, or `null` for the zero-config default. */
|
|
39
|
+
source: string | null;
|
|
11
40
|
}
|
|
41
|
+
/** Load `.wtcrc.json`, else `package.json#wtc`, recording which file was used. */
|
|
42
|
+
export declare function loadConfigWithSource(repoRoot: string): LoadedConfig;
|
|
12
43
|
export declare function loadConfig(repoRoot: string): WtcConfig;
|
package/dist/config.js
CHANGED
|
@@ -1,16 +1,33 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
|
|
3
|
+
function readJsonFile(file) {
|
|
4
|
+
const raw = fs.readFileSync(file, "utf-8");
|
|
5
|
+
try {
|
|
6
|
+
return JSON.parse(raw);
|
|
7
|
+
}
|
|
8
|
+
catch (err) {
|
|
9
|
+
throw new Error(`Invalid JSON in ${file}: ${err instanceof Error ? err.message : String(err)}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** Load `.wtcrc.json`, else `package.json#wtc`, recording which file was used. */
|
|
13
|
+
export function loadConfigWithSource(repoRoot) {
|
|
4
14
|
const rcPath = path.join(repoRoot, ".wtcrc.json");
|
|
5
15
|
if (fs.existsSync(rcPath)) {
|
|
6
|
-
|
|
16
|
+
const doc = readJsonFile(rcPath);
|
|
17
|
+
if (!doc || typeof doc !== "object" || Array.isArray(doc)) {
|
|
18
|
+
throw new Error(`Invalid config in ${rcPath}: expected a JSON object.`);
|
|
19
|
+
}
|
|
20
|
+
return { config: doc, source: rcPath };
|
|
7
21
|
}
|
|
8
22
|
const pkgPath = path.join(repoRoot, "package.json");
|
|
9
23
|
if (fs.existsSync(pkgPath)) {
|
|
10
|
-
const pkg =
|
|
11
|
-
if (pkg
|
|
12
|
-
return pkg.wtc;
|
|
24
|
+
const pkg = readJsonFile(pkgPath);
|
|
25
|
+
if (pkg && typeof pkg.wtc === "object" && pkg.wtc !== null && !Array.isArray(pkg.wtc)) {
|
|
26
|
+
return { config: pkg.wtc, source: `${pkgPath}#wtc` };
|
|
13
27
|
}
|
|
14
28
|
}
|
|
15
|
-
return {};
|
|
29
|
+
return { config: {}, source: null };
|
|
30
|
+
}
|
|
31
|
+
export function loadConfig(repoRoot) {
|
|
32
|
+
return loadConfigWithSource(repoRoot).config;
|
|
16
33
|
}
|
package/dist/context.d.ts
CHANGED
|
@@ -1,15 +1,116 @@
|
|
|
1
|
+
import type { ComposeEngine } from "./compose/engine.js";
|
|
2
|
+
import type { RemapTargets } from "./compose/override.js";
|
|
3
|
+
import type { PsContainer, StackProbe } from "./compose/status.js";
|
|
1
4
|
import type { ComposeFile } from "./compose/types.js";
|
|
2
5
|
import type { PortMapping } from "./ports/types.js";
|
|
3
6
|
import type { WtcConfig } from "./config.js";
|
|
7
|
+
import type { RepoInfo } from "./git/repo.js";
|
|
4
8
|
import type { WorktreeInfo } from "./git/worktree.js";
|
|
9
|
+
import type { AdoptableStack, ResolvedWorktree, UnavailableRecord, WorktreeRecord } from "./state/registry.js";
|
|
10
|
+
import type { EnvTarget } from "./sync/env.js";
|
|
11
|
+
/** One-run CLI overrides for env handling. Values fall back to `.wtcrc.json`. */
|
|
12
|
+
export interface EnvCliOptions {
|
|
13
|
+
/** Env filename to copy and inject (`--env`). */
|
|
14
|
+
envFile?: string;
|
|
15
|
+
/** Workspaces to also inject env into (`--workspace`, repeatable). */
|
|
16
|
+
workspaces?: string[];
|
|
17
|
+
}
|
|
18
|
+
export interface BuildContextOptions {
|
|
19
|
+
/** Directory wtc runs from (defaults to `process.cwd()`). */
|
|
20
|
+
cwd?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Resolve the compose engine and probe stack states (status display). A
|
|
23
|
+
* worktree seen for the first time may then adopt the identity of a
|
|
24
|
+
* stack already running from it, and an empty registry is persisted —
|
|
25
|
+
* without a probe it is only computed (see ReconcileOptions.adoptable).
|
|
26
|
+
*/
|
|
27
|
+
probeStacks?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Compute identities without side effects (`--dry-run`): never write the
|
|
30
|
+
* registry, create worktree ids, or move a corrupt registry aside.
|
|
31
|
+
*/
|
|
32
|
+
readOnly?: boolean;
|
|
33
|
+
/** Tolerate a main checkout without a compose file (promote, clean). */
|
|
34
|
+
allowMissingCompose?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* The command syncs files into, starts, or removes other checkouts: refuse
|
|
37
|
+
* (naming this action) when the main checkout cannot be located, instead
|
|
38
|
+
* of treating the current checkout as the main one.
|
|
39
|
+
*/
|
|
40
|
+
requireMainFor?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Decide runtime remapping of hardcoded ports and container_name (see
|
|
43
|
+
* compose/override.ts), which may probe the engine. Implied by
|
|
44
|
+
* `probeStacks`; set it for other commands that run compose on stacks.
|
|
45
|
+
*/
|
|
46
|
+
resolveRemap?: boolean;
|
|
47
|
+
}
|
|
5
48
|
export interface WtcContext {
|
|
49
|
+
repo: RepoInfo;
|
|
50
|
+
/** The MAIN worktree — source of compose, config and env files. */
|
|
6
51
|
repoRoot: string;
|
|
7
52
|
repoName: string;
|
|
53
|
+
cwd: string;
|
|
8
54
|
composeFile: ComposeFile;
|
|
55
|
+
/** False only with `allowMissingCompose` when no compose file exists. */
|
|
56
|
+
hasComposeFile: boolean;
|
|
9
57
|
portMappings: PortMapping[];
|
|
10
58
|
config: WtcConfig;
|
|
11
59
|
portStride: number;
|
|
12
|
-
worktrees
|
|
60
|
+
/** Live linked worktrees with their persistent index/project, in git order. */
|
|
61
|
+
worktrees: ResolvedWorktree[];
|
|
62
|
+
/** Registry records whose worktree is gone (stack may still exist). */
|
|
63
|
+
orphans: WorktreeRecord[];
|
|
64
|
+
/**
|
|
65
|
+
* Records of worktrees git still lists but wtc cannot start (locked and
|
|
66
|
+
* missing on disk, or now the main checkout): their stacks are left alone.
|
|
67
|
+
*/
|
|
68
|
+
unavailable: UnavailableRecord[];
|
|
69
|
+
/** Worktrees git still lists but whose directory is missing. */
|
|
70
|
+
prunable: WorktreeInfo[];
|
|
71
|
+
/** General warnings (missing worktrees, registry problems, skipped sync entries). */
|
|
72
|
+
warnings: string[];
|
|
73
|
+
/** Settings that break isolation between worktree stacks. */
|
|
74
|
+
isolationWarnings: string[];
|
|
75
|
+
/**
|
|
76
|
+
* Hardcoded host ports and fixed container names wtc remaps at runtime
|
|
77
|
+
* (only with `probeStacks` / `resolveRemap`); null when there are none or
|
|
78
|
+
* the engine cannot (the reason is among the isolation warnings).
|
|
79
|
+
*/
|
|
80
|
+
remap: RemapTargets | null;
|
|
81
|
+
/** What runtime remapping does, for `wtc start` (informational). */
|
|
82
|
+
remapNotes: string[];
|
|
83
|
+
/** Stack states (only with `probeStacks`). */
|
|
84
|
+
stacks: StackProbe | null;
|
|
85
|
+
/** Resolve the compose engine (lazily; throws when none is available). */
|
|
86
|
+
getEngine(): ComposeEngine;
|
|
13
87
|
}
|
|
14
|
-
|
|
15
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Identities of running stacks that worktrees may adopt, keyed by worktree
|
|
90
|
+
* path: compose projects named like this repository's worktree stacks
|
|
91
|
+
* (`<repo>-wt-<n>-…`) whose `working_dir` label is one of `worktrees`.
|
|
92
|
+
* Compose records the directory it ran in, so a registry that was lost,
|
|
93
|
+
* corrupted, or never written (0.2) is rebuilt from the stacks themselves
|
|
94
|
+
* instead of renumbering them. One-off `compose run` containers are
|
|
95
|
+
* ignored; when several projects ran from one worktree, the one with the
|
|
96
|
+
* most running (then total) containers, then the lowest index, wins.
|
|
97
|
+
*/
|
|
98
|
+
export declare function adoptableStacks(containers: PsContainer[], repoName: string, worktrees: WorktreeInfo[]): Map<string, AdoptableStack>;
|
|
99
|
+
export declare function buildContext(cli?: EnvCliOptions, opts?: BuildContextOptions): WtcContext;
|
|
100
|
+
/** Merge CLI overrides over the loaded config and validate it. */
|
|
101
|
+
export declare function resolveConfig(config: WtcConfig, cli: EnvCliOptions): WtcConfig;
|
|
102
|
+
/**
|
|
103
|
+
* Env targets for a run: the repo root always, plus every selected workspace.
|
|
104
|
+
* Workspace targets are additive — the root env file is what docker compose
|
|
105
|
+
* interpolates port values from, so it must never be skipped.
|
|
106
|
+
*/
|
|
107
|
+
export declare function getEnvTargets(ctx: WtcContext): EnvTarget[];
|
|
108
|
+
/**
|
|
109
|
+
* Validate env-related CLI options up front: build the context and resolve
|
|
110
|
+
* its env targets, which throws for invalid env file names, malformed
|
|
111
|
+
* config, and unknown workspace selectors. Restart paths call this before
|
|
112
|
+
* stopping any worktree, so bad options never leave things stopped.
|
|
113
|
+
*/
|
|
114
|
+
export declare function preflightEnvOptions(cli?: EnvCliOptions, cwd?: string, opts?: {
|
|
115
|
+
requireMainFor?: string;
|
|
116
|
+
}): void;
|
package/dist/context.js
CHANGED
|
@@ -1,69 +1,323 @@
|
|
|
1
|
-
import
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { detectComposeFile } from "./compose/detect.js";
|
|
2
3
|
import { parseComposeFile } from "./compose/parse.js";
|
|
4
|
+
import { resolveEngine } from "./compose/engine.js";
|
|
5
|
+
import { createStackProbe } from "./compose/status.js";
|
|
6
|
+
import { composeIsolationWarnings, portCollisionWarnings, rawPortWarnings, remapNotes, } from "./compose/isolation.js";
|
|
7
|
+
import { decideRemap } from "./compose/override.js";
|
|
3
8
|
import { extractPortMappings } from "./ports/extract.js";
|
|
4
|
-
import { detectCrossWorktreeCollisions, recommendPortStride, } from "./ports/allocate.js";
|
|
5
9
|
import { loadConfig } from "./config.js";
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
import { assertMainCheckout, getRepoInfo, listRepoWorktrees } from "./git/repo.js";
|
|
11
|
+
import { isCandidate, samePath } from "./git/worktree.js";
|
|
12
|
+
import { hasRecord, peekState, reconcileWorktrees } from "./state/registry.js";
|
|
13
|
+
import { escapeRegExp, sanitize } from "./utils/sanitize.js";
|
|
14
|
+
import { resolveWorkspaces } from "./workspaces/resolve.js";
|
|
15
|
+
import { resolveSyncEntries } from "./sync/files.js";
|
|
16
|
+
import { DEFAULT_ENV_FILE, findEnvSource } from "./sync/env.js";
|
|
17
|
+
function emptyComposeFile(composePath) {
|
|
18
|
+
return {
|
|
19
|
+
services: [],
|
|
20
|
+
composePath,
|
|
21
|
+
sourceFiles: [],
|
|
22
|
+
sharedVolumes: [],
|
|
23
|
+
sharedNetworks: [],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** Characters compose allows in a project name. */
|
|
27
|
+
const COMPOSE_PROJECT_NAME = /^[a-z0-9][a-z0-9_-]*$/;
|
|
28
|
+
/**
|
|
29
|
+
* Identities of running stacks that worktrees may adopt, keyed by worktree
|
|
30
|
+
* path: compose projects named like this repository's worktree stacks
|
|
31
|
+
* (`<repo>-wt-<n>-…`) whose `working_dir` label is one of `worktrees`.
|
|
32
|
+
* Compose records the directory it ran in, so a registry that was lost,
|
|
33
|
+
* corrupted, or never written (0.2) is rebuilt from the stacks themselves
|
|
34
|
+
* instead of renumbering them. One-off `compose run` containers are
|
|
35
|
+
* ignored; when several projects ran from one worktree, the one with the
|
|
36
|
+
* most running (then total) containers, then the lowest index, wins.
|
|
37
|
+
*/
|
|
38
|
+
export function adoptableStacks(containers, repoName, worktrees) {
|
|
39
|
+
const prefix = sanitize(repoName);
|
|
40
|
+
const pattern = new RegExp(`^${prefix ? `${escapeRegExp(prefix)}-` : ""}wt-(\\d+)-`);
|
|
41
|
+
const stacks = new Map();
|
|
42
|
+
for (const c of containers) {
|
|
43
|
+
if (!c.project || !c.workingDir || c.oneoff)
|
|
44
|
+
continue;
|
|
45
|
+
// Only a name compose accepts may become a record's project: a label
|
|
46
|
+
// that could not be parsed cleanly must never be persisted.
|
|
47
|
+
if (!COMPOSE_PROJECT_NAME.test(c.project))
|
|
48
|
+
continue;
|
|
49
|
+
const m = pattern.exec(c.project);
|
|
50
|
+
if (!m)
|
|
51
|
+
continue;
|
|
52
|
+
const wt = worktrees.find((w) => samePath(w.path, c.workingDir));
|
|
53
|
+
if (!wt)
|
|
54
|
+
continue;
|
|
55
|
+
const stack = stacks.get(c.project) ?? {
|
|
56
|
+
index: Number(m[1]),
|
|
57
|
+
project: c.project,
|
|
58
|
+
path: path.resolve(wt.path),
|
|
59
|
+
running: 0,
|
|
60
|
+
total: 0,
|
|
61
|
+
};
|
|
62
|
+
stack.total++;
|
|
63
|
+
if (c.running)
|
|
64
|
+
stack.running++;
|
|
65
|
+
stacks.set(c.project, stack);
|
|
66
|
+
}
|
|
67
|
+
const best = new Map();
|
|
68
|
+
const ranked = [...stacks.values()].sort((a, b) => b.running - a.running || b.total - a.total || a.index - b.index);
|
|
69
|
+
for (const s of ranked) {
|
|
70
|
+
if (s.index > 0 && !best.has(s.path))
|
|
71
|
+
best.set(s.path, { index: s.index, project: s.project });
|
|
72
|
+
}
|
|
73
|
+
return best;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* What worktrees seen for the first time may adopt — computed before the
|
|
77
|
+
* registry lock is taken: a slow engine must never hold the lock other wtc
|
|
78
|
+
* processes wait on. `undefined` when the engine is unavailable (no probe).
|
|
79
|
+
*/
|
|
80
|
+
function stacksToAdopt(stacks, repo, listWorktrees) {
|
|
81
|
+
const recorded = peekState(repo.commonDir);
|
|
82
|
+
const unrecorded = listWorktrees().filter((wt) => isCandidate(wt) && !hasRecord(recorded, wt));
|
|
83
|
+
// Every live worktree has a record: nothing to adopt, no `ps` needed.
|
|
84
|
+
if (unrecorded.length === 0)
|
|
85
|
+
return new Map();
|
|
86
|
+
if (stacks.problem !== null)
|
|
87
|
+
return undefined;
|
|
88
|
+
const containers = stacks.containers();
|
|
89
|
+
// Unparseable `ps` (per-project fallback): the engine answers, but no labels to adopt from.
|
|
90
|
+
return containers ? adoptableStacks(containers, repo.repoName, unrecorded) : new Map();
|
|
91
|
+
}
|
|
92
|
+
export function buildContext(cli = {}, opts = {}) {
|
|
93
|
+
const cwd = path.resolve(opts.cwd ?? process.cwd());
|
|
94
|
+
const repo = getRepoInfo(cwd);
|
|
95
|
+
if (opts.requireMainFor)
|
|
96
|
+
assertMainCheckout(repo, opts.requireMainFor);
|
|
97
|
+
const repoRoot = repo.mainRoot;
|
|
98
|
+
const warnings = [...repo.warnings];
|
|
11
99
|
const composePath = detectComposeFile(repoRoot);
|
|
12
|
-
if (!composePath) {
|
|
100
|
+
if (!composePath && !opts.allowMissingCompose) {
|
|
13
101
|
throw new Error(`No compose file found in ${repoRoot}. Expected one of: compose.yaml, compose.yml, docker-compose.yaml, docker-compose.yml`);
|
|
14
102
|
}
|
|
15
|
-
const composeFile =
|
|
103
|
+
const composeFile = composePath
|
|
104
|
+
? parseComposeFile(composePath)
|
|
105
|
+
: emptyComposeFile(path.join(repoRoot, "compose.yaml"));
|
|
16
106
|
const portMappings = extractPortMappings(composeFile.services);
|
|
17
|
-
const config = loadConfig(repoRoot);
|
|
107
|
+
const config = resolveConfig(loadConfig(repoRoot), cli);
|
|
18
108
|
const portStride = config.portStride ?? 1;
|
|
19
109
|
if (!Number.isInteger(portStride) || portStride < 1) {
|
|
20
110
|
throw new Error(`Invalid "portStride": ${String(portStride)}. Expected a positive integer.`);
|
|
21
111
|
}
|
|
22
|
-
|
|
23
|
-
|
|
112
|
+
warnings.push(...resolveSyncEntries(repoRoot, config.sync).warnings);
|
|
113
|
+
let engine = null;
|
|
114
|
+
const getEngine = () => {
|
|
115
|
+
engine ??= resolveEngine(config);
|
|
116
|
+
return engine;
|
|
117
|
+
};
|
|
118
|
+
let stacks = null;
|
|
119
|
+
if (opts.probeStacks) {
|
|
120
|
+
let problem = null;
|
|
121
|
+
try {
|
|
122
|
+
getEngine();
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
problem = err instanceof Error ? err.message : String(err);
|
|
126
|
+
}
|
|
127
|
+
stacks = createStackProbe(engine, problem, repoRoot);
|
|
128
|
+
}
|
|
129
|
+
const listWorktrees = () => listRepoWorktrees(repo);
|
|
130
|
+
const reconciled = reconcileWorktrees({
|
|
131
|
+
commonDir: repo.commonDir,
|
|
132
|
+
repoName: repo.repoName,
|
|
133
|
+
listWorktrees,
|
|
134
|
+
adoptable: stacks ? stacksToAdopt(stacks, repo, listWorktrees) : undefined,
|
|
135
|
+
// Without the main checkout identities are only computed: a stand-in
|
|
136
|
+
// main (and the repo name derived from it) must never be persisted.
|
|
137
|
+
readOnly: opts.readOnly || repo.mainRootIsFallback,
|
|
138
|
+
mainCheckout: repo.recordMain ? repo.mainRoot : undefined,
|
|
139
|
+
});
|
|
140
|
+
warnings.push(...reconciled.warnings);
|
|
141
|
+
const all = reconciled.listed;
|
|
142
|
+
const prunable = all.filter((wt) => !wt.isMain && wt.prunable);
|
|
143
|
+
for (const wt of prunable) {
|
|
144
|
+
warnings.push(`Worktree ${wt.path} is missing on disk — run 'git worktree prune'.`);
|
|
145
|
+
}
|
|
146
|
+
for (const wt of all.filter((w) => !w.isMain && w.missing)) {
|
|
147
|
+
const held = reconciled.unavailable.find((u) => u.reason === "missing" && samePath(u.path, wt.path));
|
|
148
|
+
warnings.push(`Worktree ${wt.path} is locked but missing on disk (unmounted drive?) — skipped` +
|
|
149
|
+
(held ? `; its index ${held.index} and stack ${held.project} are kept until it is back.` : "."));
|
|
150
|
+
}
|
|
151
|
+
for (const u of reconciled.unavailable.filter((r) => r.reason === "main")) {
|
|
152
|
+
warnings.push(`The main checkout ${u.path} was worktree ${u.index} (${u.project}) before it became the main checkout; ` +
|
|
153
|
+
`wtc leaves that stack alone (stop or remove it yourself if you no longer need it).`);
|
|
154
|
+
}
|
|
155
|
+
const decision = opts.probeStacks || opts.resolveRemap
|
|
156
|
+
? decideRemap({
|
|
157
|
+
composeFile,
|
|
158
|
+
mappings: portMappings,
|
|
159
|
+
mainRoot: repoRoot,
|
|
160
|
+
commonDir: repo.commonDir,
|
|
161
|
+
envFiles: [
|
|
162
|
+
findEnvSource(repoRoot, { dir: "", label: "root", envFileName: config.envFile ?? DEFAULT_ENV_FILE }),
|
|
163
|
+
path.join(repoRoot, DEFAULT_ENV_FILE),
|
|
164
|
+
],
|
|
165
|
+
envOverrides: config.envOverrides,
|
|
166
|
+
engine: getEngine,
|
|
167
|
+
})
|
|
168
|
+
: { targets: null, problem: null, warnings: [] };
|
|
169
|
+
const remap = decision.targets;
|
|
170
|
+
const remapped = remap?.ports ?? [];
|
|
171
|
+
const isolationWarnings = [
|
|
172
|
+
...(decision.problem ? [decision.problem] : []),
|
|
173
|
+
...decision.warnings,
|
|
174
|
+
...rawPortWarnings(portMappings, remapped),
|
|
175
|
+
...composeIsolationWarnings(composeFile, new Set(remap?.containerNames.map((c) => c.service))),
|
|
176
|
+
...portCollisionWarnings(portMappings, portStride, [
|
|
177
|
+
...reconciled.worktrees.map((w) => w.index),
|
|
178
|
+
...reconciled.orphans.map((o) => o.index),
|
|
179
|
+
...reconciled.unavailable.map((u) => u.index),
|
|
180
|
+
], remapped),
|
|
181
|
+
];
|
|
24
182
|
return {
|
|
183
|
+
repo,
|
|
25
184
|
repoRoot,
|
|
26
|
-
repoName,
|
|
185
|
+
repoName: repo.repoName,
|
|
186
|
+
cwd,
|
|
27
187
|
composeFile,
|
|
188
|
+
hasComposeFile: composePath !== null,
|
|
28
189
|
portMappings,
|
|
29
190
|
config,
|
|
30
191
|
portStride,
|
|
31
|
-
worktrees,
|
|
192
|
+
worktrees: reconciled.worktrees,
|
|
193
|
+
orphans: reconciled.orphans,
|
|
194
|
+
unavailable: reconciled.unavailable,
|
|
195
|
+
prunable,
|
|
196
|
+
warnings,
|
|
197
|
+
isolationWarnings,
|
|
198
|
+
remap,
|
|
199
|
+
remapNotes: remap ? remapNotes(remap, portMappings) : [],
|
|
200
|
+
stacks,
|
|
201
|
+
getEngine,
|
|
32
202
|
};
|
|
33
203
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
204
|
+
/** Merge CLI overrides over the loaded config and validate it. */
|
|
205
|
+
export function resolveConfig(config, cli) {
|
|
206
|
+
const envFile = cli.envFile ?? config.envFile ?? DEFAULT_ENV_FILE;
|
|
207
|
+
validateEnvFileName(envFile);
|
|
208
|
+
const workspaces = cli.workspaces && cli.workspaces.length > 0
|
|
209
|
+
? cli.workspaces
|
|
210
|
+
: (config.workspaces ?? []);
|
|
211
|
+
if (!Array.isArray(workspaces) ||
|
|
212
|
+
workspaces.some((w) => typeof w !== "string" || w.trim() === "")) {
|
|
213
|
+
throw new Error(`Invalid "workspaces" config: expected an array of workspace names.`);
|
|
42
214
|
}
|
|
43
|
-
|
|
44
|
-
|
|
215
|
+
if (config.sync !== undefined &&
|
|
216
|
+
(!Array.isArray(config.sync) ||
|
|
217
|
+
config.sync.some((s) => typeof s !== "string" || s.trim() === ""))) {
|
|
218
|
+
throw new Error(`Invalid "sync" config: expected an array of repo-relative paths.`);
|
|
219
|
+
}
|
|
220
|
+
const cmd = config.composeCommand;
|
|
221
|
+
if (cmd !== undefined &&
|
|
222
|
+
!(typeof cmd === "string" && cmd.trim() !== "") &&
|
|
223
|
+
!(Array.isArray(cmd) &&
|
|
224
|
+
cmd.length > 0 &&
|
|
225
|
+
cmd.every((c) => typeof c === "string" && c.trim() !== ""))) {
|
|
226
|
+
throw new Error(`Invalid "composeCommand" config: expected a command string such as "podman compose" or an array such as ["docker-compose"].`);
|
|
45
227
|
}
|
|
46
|
-
|
|
228
|
+
validateOverrides("envOverrides", config.envOverrides);
|
|
229
|
+
if (config.workspaceEnvOverrides !== undefined) {
|
|
230
|
+
const map = config.workspaceEnvOverrides;
|
|
231
|
+
if (typeof map !== "object" || map === null || Array.isArray(map)) {
|
|
232
|
+
throw new Error(`Invalid "workspaceEnvOverrides" config: expected an object of workspace -> overrides.`);
|
|
233
|
+
}
|
|
234
|
+
for (const [name, overrides] of Object.entries(map)) {
|
|
235
|
+
validateOverrides(`workspaceEnvOverrides.${name}`, overrides);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return { ...config, envFile, workspaces };
|
|
239
|
+
}
|
|
240
|
+
function validateEnvFileName(name) {
|
|
241
|
+
if (!name ||
|
|
242
|
+
name === "." ||
|
|
243
|
+
name === ".." ||
|
|
244
|
+
name.includes("/") ||
|
|
245
|
+
name.includes("\\") ||
|
|
246
|
+
path.isAbsolute(name)) {
|
|
247
|
+
throw new Error(`Invalid env file name "${name}". Expected a bare filename such as ".env" or ".env.development".`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function validateOverrides(label, overrides) {
|
|
251
|
+
if (overrides === undefined)
|
|
47
252
|
return;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
253
|
+
if (typeof overrides !== "object" ||
|
|
254
|
+
overrides === null ||
|
|
255
|
+
Array.isArray(overrides)) {
|
|
256
|
+
throw new Error(`Invalid "${label}" config: expected an object of VAR -> value.`);
|
|
257
|
+
}
|
|
258
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
259
|
+
if (typeof value !== "string") {
|
|
260
|
+
throw new Error(`Invalid "${label}.${key}": expected a string value (got ${typeof value}).`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
58
263
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
264
|
+
/**
|
|
265
|
+
* Env targets for a run: the repo root always, plus every selected workspace.
|
|
266
|
+
* Workspace targets are additive — the root env file is what docker compose
|
|
267
|
+
* interpolates port values from, so it must never be skipped.
|
|
268
|
+
*/
|
|
269
|
+
export function getEnvTargets(ctx) {
|
|
270
|
+
const envFileName = ctx.config.envFile ?? DEFAULT_ENV_FILE;
|
|
271
|
+
const targets = [
|
|
272
|
+
{
|
|
273
|
+
dir: "",
|
|
274
|
+
label: "root",
|
|
275
|
+
envFileName,
|
|
276
|
+
overrides: ctx.config.envOverrides,
|
|
277
|
+
},
|
|
278
|
+
];
|
|
279
|
+
const workspaces = resolveWorkspaces(ctx.repoRoot, ctx.config.workspaces ?? []);
|
|
280
|
+
for (const ws of workspaces) {
|
|
281
|
+
targets.push({
|
|
282
|
+
dir: ws.dir,
|
|
283
|
+
label: `workspace ${ws.name} (${ws.dir})`,
|
|
284
|
+
envFileName,
|
|
285
|
+
overrides: findWorkspaceOverrides(ctx.config.workspaceEnvOverrides, ws, workspaces),
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
return targets;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Validate env-related CLI options up front: build the context and resolve
|
|
292
|
+
* its env targets, which throws for invalid env file names, malformed
|
|
293
|
+
* config, and unknown workspace selectors. Restart paths call this before
|
|
294
|
+
* stopping any worktree, so bad options never leave things stopped.
|
|
295
|
+
*/
|
|
296
|
+
export function preflightEnvOptions(cli = {}, cwd, opts = {}) {
|
|
297
|
+
getEnvTargets(buildContext(cli, { cwd, readOnly: true, requireMainFor: opts.requireMainFor }));
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Look up a workspace's overrides by package name, then directory basename,
|
|
301
|
+
* then repo-relative path — the key order resolveWorkspaces matches
|
|
302
|
+
* selectors in. A bare basename applies only when it identifies exactly one
|
|
303
|
+
* workspace, mirroring selector resolution's ambiguity rule: workspaces
|
|
304
|
+
* sharing a directory name must be reached by full path.
|
|
305
|
+
*/
|
|
306
|
+
function findWorkspaceOverrides(map, ws, workspaces) {
|
|
307
|
+
if (!map)
|
|
308
|
+
return undefined;
|
|
309
|
+
const basename = path.posix.basename(ws.dir);
|
|
310
|
+
const basenameUnique = workspaces.filter((w) => path.posix.basename(w.dir) === basename)
|
|
311
|
+
.length === 1;
|
|
312
|
+
for (const key of [
|
|
313
|
+
ws.name,
|
|
314
|
+
...(basenameUnique ? [basename] : []),
|
|
315
|
+
ws.dir,
|
|
316
|
+
`./${ws.dir}`,
|
|
317
|
+
]) {
|
|
318
|
+
const overrides = map[key];
|
|
319
|
+
if (overrides && typeof overrides === "object")
|
|
320
|
+
return overrides;
|
|
321
|
+
}
|
|
322
|
+
return undefined;
|
|
69
323
|
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { SweepResult, SweepRunner } from "./teardown.js";
|
|
2
|
+
import type { WtcContext } from "../context.js";
|
|
3
|
+
import type { ResolvedWorktree } from "../state/registry.js";
|
|
4
|
+
import type { Reporter } from "../utils/log.js";
|
|
5
|
+
import type { CoreIo } from "./types.js";
|
|
6
|
+
export interface CleanOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Also remove worktrees with uncommitted changes, commits on no branch,
|
|
9
|
+
* or unpushed submodule commits — and locked ones.
|
|
10
|
+
*/
|
|
11
|
+
force?: boolean;
|
|
12
|
+
/** Tear down stacks (deleting their volumes) but keep every worktree. */
|
|
13
|
+
keepWorktrees?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Report what would happen; change nothing — not even the registry (the
|
|
16
|
+
* context is built read-only).
|
|
17
|
+
*/
|
|
18
|
+
dryRun?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Also remove stacks named after this repo's worktree stacks
|
|
21
|
+
* (`<repo>-wt-<n>-…`) whose working directory no longer exists — stale
|
|
22
|
+
* leftovers, e.g. of wtc ≤0.2. Never a stack whose directory exists.
|
|
23
|
+
*/
|
|
24
|
+
sweep?: boolean;
|
|
25
|
+
/** Container CLI runner for the stale sweep (tests). */
|
|
26
|
+
sweepRunner?: SweepRunner;
|
|
27
|
+
}
|
|
28
|
+
export interface CleanResult {
|
|
29
|
+
dryRun: boolean;
|
|
30
|
+
stacks: {
|
|
31
|
+
project: string;
|
|
32
|
+
orphan: boolean;
|
|
33
|
+
ok: boolean;
|
|
34
|
+
error?: string;
|
|
35
|
+
}[];
|
|
36
|
+
removed: string[];
|
|
37
|
+
/** Worktrees deliberately kept: dirty, locked, current, main checkout. Not failures. */
|
|
38
|
+
skipped: {
|
|
39
|
+
path: string;
|
|
40
|
+
reason: string;
|
|
41
|
+
}[];
|
|
42
|
+
/** Worktrees whose `git worktree remove` failed. */
|
|
43
|
+
removeFailed: {
|
|
44
|
+
path: string;
|
|
45
|
+
reason: string;
|
|
46
|
+
}[];
|
|
47
|
+
sweep: SweepResult | null;
|
|
48
|
+
forgotten: string[];
|
|
49
|
+
/**
|
|
50
|
+
* Real failures (non-zero → exit 1 / MCP isError): stacks that could not
|
|
51
|
+
* be torn down, worktree removals and sweep removals/listings that
|
|
52
|
+
* failed. Always 0 for a dry run.
|
|
53
|
+
*/
|
|
54
|
+
failures: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Which uncommitted paths in a worktree are wtc's own artifacts rather than
|
|
58
|
+
* the user's work:
|
|
59
|
+
* - the env files wtc writes: `<dir>/<envFile>` for the root and each
|
|
60
|
+
* configured workspace, plus whatever the worktree's last `start` wrote
|
|
61
|
+
* (`--env`/`--workspace` may name others) — not every `.env.*` file, so an
|
|
62
|
+
* edited `.env.test` or `apps/x/.env.production` is the user's work;
|
|
63
|
+
* - compose sources (incl. includes), synced Dockerfiles and files under
|
|
64
|
+
* `sync` entries, but only while they are byte-identical to the main
|
|
65
|
+
* checkout's copy, i.e. exactly what start's sync wrote. An edit made in
|
|
66
|
+
* the worktree (or a file only the worktree has) is the user's work. If
|
|
67
|
+
* main changed since the last start this errs towards "dirty", which
|
|
68
|
+
* --force overrides.
|
|
69
|
+
*/
|
|
70
|
+
export declare function managedPathMatcher(ctx: WtcContext, wtPath: string, envFiles?: string[]): (rel: string) => boolean;
|
|
71
|
+
/** Uncommitted paths in a worktree that wtc did not put there. */
|
|
72
|
+
export declare function unmanagedChanges(statusPaths: string[], isManaged: (rel: string) => boolean): string[];
|
|
73
|
+
/**
|
|
74
|
+
* Commits that removing the worktree would make unreachable, beyond what
|
|
75
|
+
* `git status` shows:
|
|
76
|
+
* - a detached HEAD with commits no branch, tag or remote reaches (the
|
|
77
|
+
* worktree's HEAD reflog goes with it);
|
|
78
|
+
* - submodule commits not on any remote: the submodules' repositories live
|
|
79
|
+
* in the worktree's admin dir, which `git worktree remove --force`
|
|
80
|
+
* deletes (plain `remove` refuses worktrees with submodules for this
|
|
81
|
+
* reason; wtc needs --force for its own artifacts).
|
|
82
|
+
* Returns a reason, or `null` when nothing would be lost.
|
|
83
|
+
*/
|
|
84
|
+
export declare function unreachableWork(wt: ResolvedWorktree): string | null;
|
|
85
|
+
/**
|
|
86
|
+
* Tear down every worktree stack (live and orphaned), remove worktrees that
|
|
87
|
+
* hold no work of their own, sweep leftovers this checkout owns, and forget
|
|
88
|
+
* what was removed. Never removes the main checkout or the worktree it runs
|
|
89
|
+
* from.
|
|
90
|
+
*/
|
|
91
|
+
export declare function clean(options: CleanOptions, reporter: Reporter, io: CoreIo): Promise<CleanResult>;
|