worktree-compose 0.2.0 → 1.0.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 +458 -140
- 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 +15 -0
- package/dist/compose/isolation.js +62 -0
- package/dist/compose/parse.d.ts +5 -0
- package/dist/compose/parse.js +89 -11
- package/dist/compose/status.d.ts +84 -0
- package/dist/compose/status.js +227 -0
- package/dist/compose/types.d.ts +20 -0
- package/dist/config.d.ts +31 -0
- package/dist/config.js +23 -6
- package/dist/context.d.ts +89 -3
- package/dist/context.js +278 -45
- package/dist/core/clean.d.ts +91 -0
- package/dist/core/clean.js +343 -0
- package/dist/core/compose.d.ts +30 -0
- package/dist/core/compose.js +45 -0
- package/dist/core/doctor.d.ts +28 -0
- package/dist/core/doctor.js +277 -0
- package/dist/core/list.d.ts +42 -0
- package/dist/core/list.js +94 -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 +75 -0
- package/dist/core/restart.d.ts +14 -0
- package/dist/core/restart.js +18 -0
- package/dist/core/start.d.ts +31 -0
- package/dist/core/start.js +99 -0
- package/dist/core/stop.d.ts +32 -0
- package/dist/core/stop.js +57 -0
- package/dist/core/teardown.d.ts +121 -0
- package/dist/core/teardown.js +298 -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 +14 -3
- package/dist/ports/allocate.js +20 -4
- 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 +77 -2
- package/dist/sync/env.js +151 -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 +18 -14
- package/wtc.schema.json +62 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { buildContext, getEnvTargets } from "../context.js";
|
|
4
|
+
import { runSafe, run } from "../utils/exec.js";
|
|
5
|
+
import { isWithin } from "../utils/paths.js";
|
|
6
|
+
import { getDockerfiles, resolveSyncEntries } from "../sync/files.js";
|
|
7
|
+
import { getStatusPaths } from "../git/status.js";
|
|
8
|
+
import { samePath } from "../git/worktree.js";
|
|
9
|
+
import { forgetRecords } from "../state/registry.js";
|
|
10
|
+
import { reportContextWarnings, throwIfAborted } from "./compose.js";
|
|
11
|
+
import { orphanSkipReason, sweepStaleResources, teardownStack } from "./teardown.js";
|
|
12
|
+
/** Count a clean run's failures (see CleanResult.failures). */
|
|
13
|
+
function countFailures(result) {
|
|
14
|
+
if (result.dryRun)
|
|
15
|
+
return 0;
|
|
16
|
+
const sweep = result.sweep;
|
|
17
|
+
return (result.stacks.filter((s) => !s.ok).length +
|
|
18
|
+
result.removeFailed.length +
|
|
19
|
+
(sweep
|
|
20
|
+
? sweep.containers.failed.length +
|
|
21
|
+
sweep.networks.failed.length +
|
|
22
|
+
sweep.volumes.failed.length +
|
|
23
|
+
(sweep.error ? 1 : 0)
|
|
24
|
+
: 0));
|
|
25
|
+
}
|
|
26
|
+
/** True when `rel` is byte-identical (or the same symlink) in the worktree and in main. */
|
|
27
|
+
function mirrorsMain(repoRoot, wtPath, rel) {
|
|
28
|
+
const ours = path.join(wtPath, rel);
|
|
29
|
+
const theirs = path.join(repoRoot, rel);
|
|
30
|
+
try {
|
|
31
|
+
const a = fs.lstatSync(ours);
|
|
32
|
+
const b = fs.lstatSync(theirs);
|
|
33
|
+
if (a.isSymbolicLink() && b.isSymbolicLink()) {
|
|
34
|
+
return fs.readlinkSync(ours) === fs.readlinkSync(theirs);
|
|
35
|
+
}
|
|
36
|
+
if (!a.isFile() || !b.isFile() || a.size !== b.size)
|
|
37
|
+
return false;
|
|
38
|
+
return fs.readFileSync(ours).equals(fs.readFileSync(theirs));
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Which uncommitted paths in a worktree are wtc's own artifacts rather than
|
|
46
|
+
* the user's work:
|
|
47
|
+
* - the env files wtc writes: `<dir>/<envFile>` for the root and each
|
|
48
|
+
* configured workspace, plus whatever the worktree's last `start` wrote
|
|
49
|
+
* (`--env`/`--workspace` may name others) — not every `.env.*` file, so an
|
|
50
|
+
* edited `.env.test` or `apps/x/.env.production` is the user's work;
|
|
51
|
+
* - compose sources (incl. includes), synced Dockerfiles and files under
|
|
52
|
+
* `sync` entries, but only while they are byte-identical to the main
|
|
53
|
+
* checkout's copy, i.e. exactly what start's sync wrote. An edit made in
|
|
54
|
+
* the worktree (or a file only the worktree has) is the user's work. If
|
|
55
|
+
* main changed since the last start this errs towards "dirty", which
|
|
56
|
+
* --force overrides.
|
|
57
|
+
*/
|
|
58
|
+
export function managedPathMatcher(ctx, wtPath, envFiles = []) {
|
|
59
|
+
const toPosix = (p) => p.split(path.sep).join("/");
|
|
60
|
+
const mirrored = new Set();
|
|
61
|
+
for (const src of ctx.composeFile.sourceFiles) {
|
|
62
|
+
mirrored.add(toPosix(path.relative(ctx.repoRoot, src)));
|
|
63
|
+
}
|
|
64
|
+
for (const df of getDockerfiles(ctx.composeFile, ctx.repoRoot)) {
|
|
65
|
+
mirrored.add(toPosix(df));
|
|
66
|
+
}
|
|
67
|
+
const prefixes = resolveSyncEntries(ctx.repoRoot, ctx.config.sync).entries.map(toPosix);
|
|
68
|
+
const envPaths = new Set(envFiles.map(toPosix));
|
|
69
|
+
try {
|
|
70
|
+
for (const t of getEnvTargets(ctx))
|
|
71
|
+
envPaths.add(path.posix.join(toPosix(t.dir), t.envFileName));
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
envPaths.add(ctx.config.envFile ?? ".env");
|
|
75
|
+
}
|
|
76
|
+
return (rel) => {
|
|
77
|
+
const p = rel.replace(/\/+$/, "");
|
|
78
|
+
if (envPaths.has(p))
|
|
79
|
+
return true;
|
|
80
|
+
const synced = mirrored.has(p) || prefixes.some((pre) => p === pre || p.startsWith(`${pre}/`));
|
|
81
|
+
return synced && mirrorsMain(ctx.repoRoot, wtPath, p);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** Uncommitted paths in a worktree that wtc did not put there. */
|
|
85
|
+
export function unmanagedChanges(statusPaths, isManaged) {
|
|
86
|
+
return statusPaths.filter((p) => !isManaged(p));
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Commits that removing the worktree would make unreachable, beyond what
|
|
90
|
+
* `git status` shows:
|
|
91
|
+
* - a detached HEAD with commits no branch, tag or remote reaches (the
|
|
92
|
+
* worktree's HEAD reflog goes with it);
|
|
93
|
+
* - submodule commits not on any remote: the submodules' repositories live
|
|
94
|
+
* in the worktree's admin dir, which `git worktree remove --force`
|
|
95
|
+
* deletes (plain `remove` refuses worktrees with submodules for this
|
|
96
|
+
* reason; wtc needs --force for its own artifacts).
|
|
97
|
+
* Returns a reason, or `null` when nothing would be lost.
|
|
98
|
+
*/
|
|
99
|
+
export function unreachableWork(wt) {
|
|
100
|
+
if (wt.detached) {
|
|
101
|
+
try {
|
|
102
|
+
const sha = run("git", [
|
|
103
|
+
"-C",
|
|
104
|
+
wt.path,
|
|
105
|
+
"rev-list",
|
|
106
|
+
"--max-count=1",
|
|
107
|
+
"HEAD",
|
|
108
|
+
"--not",
|
|
109
|
+
"--branches",
|
|
110
|
+
"--tags",
|
|
111
|
+
"--remotes",
|
|
112
|
+
]);
|
|
113
|
+
if (sha) {
|
|
114
|
+
return `detached HEAD has commits no branch or tag reaches (HEAD ${sha.slice(0, 12)}); save them with 'git -C ${wt.path} branch <name>'`;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
return `could not check its detached HEAD (${err instanceof Error ? err.message.split("\n")[0] : String(err)})`;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const hasSubmodules = (wt.adminDir !== undefined && fs.existsSync(path.join(wt.adminDir, "modules"))) ||
|
|
122
|
+
fs.existsSync(path.join(wt.path, ".gitmodules"));
|
|
123
|
+
if (hasSubmodules) {
|
|
124
|
+
try {
|
|
125
|
+
const out = run("git", [
|
|
126
|
+
"-C",
|
|
127
|
+
wt.path,
|
|
128
|
+
"submodule",
|
|
129
|
+
"foreach",
|
|
130
|
+
"--quiet",
|
|
131
|
+
"--recursive",
|
|
132
|
+
"git rev-list -n1 --all --not --remotes",
|
|
133
|
+
]);
|
|
134
|
+
if (out.trim())
|
|
135
|
+
return "a submodule has commits not on any remote; push them first";
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
return `could not check its submodules (${err instanceof Error ? err.message.split("\n")[0] : String(err)})`;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Tear down every worktree stack (live and orphaned), remove worktrees that
|
|
145
|
+
* hold no work of their own, sweep leftovers this checkout owns, and forget
|
|
146
|
+
* what was removed. Never removes the main checkout or the worktree it runs
|
|
147
|
+
* from.
|
|
148
|
+
*/
|
|
149
|
+
export async function clean(options, reporter, io) {
|
|
150
|
+
const dryRun = options.dryRun === true;
|
|
151
|
+
// probeStacks: with no registry yet (an upgrade from ≤0.2, or a lost
|
|
152
|
+
// state file) identities are adopted from the running stacks, so teardown
|
|
153
|
+
// targets their real project names instead of positional guesses.
|
|
154
|
+
const ctx = buildContext({}, {
|
|
155
|
+
cwd: io.cwd,
|
|
156
|
+
allowMissingCompose: true,
|
|
157
|
+
probeStacks: true,
|
|
158
|
+
readOnly: dryRun,
|
|
159
|
+
requireMainFor: "clean up worktrees",
|
|
160
|
+
});
|
|
161
|
+
reportContextWarnings(ctx, reporter, { isolation: false });
|
|
162
|
+
const result = {
|
|
163
|
+
dryRun,
|
|
164
|
+
stacks: [],
|
|
165
|
+
removed: [],
|
|
166
|
+
skipped: [],
|
|
167
|
+
removeFailed: [],
|
|
168
|
+
sweep: null,
|
|
169
|
+
forgotten: [],
|
|
170
|
+
failures: 0,
|
|
171
|
+
};
|
|
172
|
+
if (dryRun)
|
|
173
|
+
reporter.info("Dry run: nothing will be changed.");
|
|
174
|
+
let engine = null;
|
|
175
|
+
try {
|
|
176
|
+
engine = ctx.getEngine();
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
reporter.warn(`${err instanceof Error ? err.message : String(err)} Skipping container teardown; registry records are kept so 'wtc prune' can finish later.`);
|
|
180
|
+
}
|
|
181
|
+
// Worktrees git still lists but wtc cannot manage (locked and missing on
|
|
182
|
+
// disk, or now the main checkout): their stacks and records stay.
|
|
183
|
+
for (const u of ctx.unavailable) {
|
|
184
|
+
const why = u.reason === "missing"
|
|
185
|
+
? "its worktree is locked and missing on disk (unmounted drive?)"
|
|
186
|
+
: "its worktree is now the main checkout";
|
|
187
|
+
result.skipped.push({ path: u.path, reason: why });
|
|
188
|
+
reporter.warn(`Skipping ${u.project}: ${why}.`);
|
|
189
|
+
}
|
|
190
|
+
// 1. Tear down every stack wtc knows about.
|
|
191
|
+
const targets = [
|
|
192
|
+
...ctx.worktrees.map((w) => ({ project: w.project, index: w.index, path: w.path, orphan: null })),
|
|
193
|
+
...ctx.orphans.map((o) => ({ project: o.project, index: o.index, path: o.path, orphan: o })),
|
|
194
|
+
];
|
|
195
|
+
const tornDown = new Set();
|
|
196
|
+
// Orphans whose worktree came back while cleaning (or could not be
|
|
197
|
+
// re-checked): never touched.
|
|
198
|
+
const leftAlone = new Set();
|
|
199
|
+
for (const t of targets) {
|
|
200
|
+
throwIfAborted(io);
|
|
201
|
+
if (!engine) {
|
|
202
|
+
result.stacks.push({ project: t.project, orphan: t.orphan !== null, ok: false, error: "no compose engine" });
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (t.orphan) {
|
|
206
|
+
const why = orphanSkipReason(ctx, t.orphan);
|
|
207
|
+
if (why) {
|
|
208
|
+
leftAlone.add(t.project);
|
|
209
|
+
reporter.warn(`Skipping ${t.project}: ${why}.`);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (dryRun) {
|
|
214
|
+
reporter.info(`Would stop ${t.project} (down -v --remove-orphans)${t.orphan ? " [removed worktree]" : ""}`);
|
|
215
|
+
result.stacks.push({ project: t.project, orphan: t.orphan !== null, ok: true });
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
reporter.info(`Stopping ${t.project}...`);
|
|
219
|
+
const outcome = await teardownStack(ctx, engine, t, io);
|
|
220
|
+
result.stacks.push({ project: t.project, orphan: t.orphan !== null, ...outcome });
|
|
221
|
+
if (outcome.ok) {
|
|
222
|
+
tornDown.add(t.project);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
reporter.warn(`Could not stop ${t.project}: ${outcome.error}`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
// 2. Remove worktrees that hold no work of their own.
|
|
229
|
+
if (!options.keepWorktrees) {
|
|
230
|
+
for (const wt of ctx.worktrees) {
|
|
231
|
+
throwIfAborted(io);
|
|
232
|
+
// In a bare-repo layout the main checkout is a linked worktree: it is
|
|
233
|
+
// the env/config source and the user's primary checkout.
|
|
234
|
+
if (samePath(wt.path, ctx.repo.mainRoot)) {
|
|
235
|
+
result.skipped.push({ path: wt.path, reason: "main checkout" });
|
|
236
|
+
reporter.warn(`Skipping removal of the main checkout: ${wt.path}`);
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (isWithin(wt.path, ctx.cwd) || samePath(wt.path, ctx.repo.currentRoot)) {
|
|
240
|
+
result.skipped.push({ path: wt.path, reason: "current worktree" });
|
|
241
|
+
reporter.warn(`Skipping removal of current worktree: ${wt.path}`);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (wt.locked && !options.force) {
|
|
245
|
+
result.skipped.push({ path: wt.path, reason: "locked" });
|
|
246
|
+
reporter.warn(`Skipping locked worktree ${wt.path} (unlock it, or use --force).`);
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
let dirty;
|
|
250
|
+
try {
|
|
251
|
+
dirty = unmanagedChanges(getStatusPaths(wt.path), managedPathMatcher(ctx, wt.path, wt.envFiles));
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
dirty = [`(git status failed: ${err instanceof Error ? err.message.split("\n")[0] : String(err)})`];
|
|
255
|
+
}
|
|
256
|
+
if (dirty.length > 0 && !options.force) {
|
|
257
|
+
const shown = dirty.slice(0, 5).join(", ") + (dirty.length > 5 ? `, … (${dirty.length} total)` : "");
|
|
258
|
+
result.skipped.push({ path: wt.path, reason: `uncommitted changes: ${shown}` });
|
|
259
|
+
reporter.warn(`Skipping ${wt.path}: uncommitted changes (${shown}). Commit them, or use --force to discard.`);
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
const unreachable = unreachableWork(wt);
|
|
263
|
+
if (unreachable && !options.force) {
|
|
264
|
+
result.skipped.push({ path: wt.path, reason: unreachable });
|
|
265
|
+
reporter.warn(`Skipping ${wt.path}: ${unreachable} (or use --force to discard).`);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (dryRun) {
|
|
269
|
+
reporter.info(`Would remove worktree ${wt.path}`);
|
|
270
|
+
result.removed.push(wt.path);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
// --force is needed only because of wtc's own artifacts; a second
|
|
274
|
+
// --force is git's way of removing a locked worktree.
|
|
275
|
+
const force = wt.locked ? ["--force", "--force"] : ["--force"];
|
|
276
|
+
try {
|
|
277
|
+
run("git", ["-C", ctx.repoRoot, "worktree", "remove", ...force, "--", wt.path]);
|
|
278
|
+
result.removed.push(wt.path);
|
|
279
|
+
reporter.success(`Removed worktree ${wt.path}`);
|
|
280
|
+
if (unreachable)
|
|
281
|
+
reporter.warn(`Discarded with --force: ${unreachable}.`);
|
|
282
|
+
}
|
|
283
|
+
catch (err) {
|
|
284
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
285
|
+
result.removeFailed.push({ path: wt.path, reason });
|
|
286
|
+
reporter.warn(`Could not remove ${wt.path}: ${reason}`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
// 3. Sweep leftovers this checkout owns (and, with --sweep, stale ones
|
|
291
|
+
// whose worktree directory is gone). A request cancelled during the
|
|
292
|
+
// last teardown stops here.
|
|
293
|
+
throwIfAborted(io);
|
|
294
|
+
if (engine) {
|
|
295
|
+
result.sweep = sweepStaleResources(engine.cliArgv, ctx.repoName, {
|
|
296
|
+
dryRun,
|
|
297
|
+
reporter,
|
|
298
|
+
runner: options.sweepRunner,
|
|
299
|
+
registryProjects: targets.map((t) => t.project),
|
|
300
|
+
// A dry run's `down -v` above would remove these first.
|
|
301
|
+
skipProjects: [
|
|
302
|
+
...leftAlone,
|
|
303
|
+
...ctx.unavailable.map((u) => u.project),
|
|
304
|
+
...(dryRun ? targets.map((t) => t.project) : []),
|
|
305
|
+
],
|
|
306
|
+
mainRoot: ctx.repo.mainRoot,
|
|
307
|
+
checkouts: targets.map((t) => t.path),
|
|
308
|
+
sweep: options.sweep === true,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
// 4. Prune git metadata and forget what is gone.
|
|
312
|
+
if (!dryRun) {
|
|
313
|
+
runSafe("git", ["-C", ctx.repoRoot, "worktree", "prune"]);
|
|
314
|
+
const removed = new Set(result.removed);
|
|
315
|
+
const forget = targets.filter((t) => tornDown.has(t.project) && (t.orphan !== null || removed.has(t.path)));
|
|
316
|
+
forgetRecords(ctx.repo.commonDir, forget.map((t) => t.project));
|
|
317
|
+
result.forgotten = forget.map((t) => t.path);
|
|
318
|
+
}
|
|
319
|
+
result.failures = countFailures(result);
|
|
320
|
+
const sweep = result.sweep;
|
|
321
|
+
const swept = sweep
|
|
322
|
+
? `${sweep.containers.removed.length} container(s), ${sweep.networks.removed.length} network(s), ${sweep.volumes.removed.length} volume(s)`
|
|
323
|
+
: "nothing (no compose engine)";
|
|
324
|
+
const sweepFailed = sweep
|
|
325
|
+
? sweep.containers.failed.length + sweep.networks.failed.length + sweep.volumes.failed.length
|
|
326
|
+
: 0;
|
|
327
|
+
const kept = sweep ? (options.sweep ? 0 : sweep.stale.length) + sweep.notOwned.length : 0;
|
|
328
|
+
const stopped = result.stacks.filter((s) => s.ok).length;
|
|
329
|
+
const summary = `${dryRun ? "Dry run complete" : result.failures > 0 ? "Cleanup finished with failures" : "Cleanup complete"}: ` +
|
|
330
|
+
`${stopped}/${result.stacks.length} stack(s) ${dryRun ? "to stop" : "stopped"}, ` +
|
|
331
|
+
`${result.removed.length} worktree(s) ${dryRun ? "to remove" : "removed"}` +
|
|
332
|
+
(result.skipped.length > 0 ? ` (${result.skipped.length} skipped)` : "") +
|
|
333
|
+
(result.removeFailed.length > 0 ? ` (${result.removeFailed.length} failed)` : "") +
|
|
334
|
+
`, ${dryRun ? "would sweep" : "swept"} ${swept}` +
|
|
335
|
+
(sweepFailed > 0 ? ` (${sweepFailed} failed)` : "") +
|
|
336
|
+
(kept > 0 ? `, ${kept} leftover(s) kept` : "") +
|
|
337
|
+
".";
|
|
338
|
+
if (result.failures > 0)
|
|
339
|
+
reporter.warn(summary);
|
|
340
|
+
else
|
|
341
|
+
reporter.success(summary);
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { RunResult } from "../utils/exec.js";
|
|
2
|
+
import type { ComposeEngine } from "../compose/engine.js";
|
|
3
|
+
import type { PortAllocation } from "../ports/types.js";
|
|
4
|
+
import type { Reporter } from "../utils/log.js";
|
|
5
|
+
import type { WtcContext } from "../context.js";
|
|
6
|
+
import type { CoreIo } from "./types.js";
|
|
7
|
+
/**
|
|
8
|
+
* Process environment for a compose call on a worktree stack: the project
|
|
9
|
+
* name plus every allocated port, so `${VAR}` interpolation resolves to the
|
|
10
|
+
* worktree's ports no matter which env filename is configured.
|
|
11
|
+
*/
|
|
12
|
+
export declare function composeEnv(project: string, allocations: PortAllocation[]): Record<string, string>;
|
|
13
|
+
export interface ComposeCallOptions {
|
|
14
|
+
cwd: string;
|
|
15
|
+
env?: Record<string, string>;
|
|
16
|
+
live: boolean;
|
|
17
|
+
signal?: AbortSignal;
|
|
18
|
+
onLine?: (line: string) => void;
|
|
19
|
+
}
|
|
20
|
+
/** Run `<compose> -p <project> <args…>` without blocking the event loop. */
|
|
21
|
+
export declare function composeRun(engine: ComposeEngine, project: string, args: string[], opts: ComposeCallOptions): Promise<RunResult>;
|
|
22
|
+
/** Prefix an error with what was being done, keeping CommandError details. */
|
|
23
|
+
export declare function withContext(prefix: string, err: unknown): Error;
|
|
24
|
+
/** Report a context's general warnings (and isolation warnings when asked). */
|
|
25
|
+
export declare function reportContextWarnings(ctx: WtcContext, reporter: Reporter, opts: {
|
|
26
|
+
isolation: boolean;
|
|
27
|
+
}): void;
|
|
28
|
+
/** Throw if the operation was cancelled (checked between worktrees). */
|
|
29
|
+
export declare function throwIfAborted(io: CoreIo): void;
|
|
30
|
+
export declare const NO_WORKTREES_HINT = "No extra worktrees found. Create one with:\n git worktree add ../my-branch my-branch";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { CommandError, runAsync } from "../utils/exec.js";
|
|
2
|
+
import { composeArgs, composeBin } from "../compose/engine.js";
|
|
3
|
+
import { allocationEnv } from "../ports/allocate.js";
|
|
4
|
+
import { PROJECT_NAME_VAR } from "../sync/env.js";
|
|
5
|
+
/**
|
|
6
|
+
* Process environment for a compose call on a worktree stack: the project
|
|
7
|
+
* name plus every allocated port, so `${VAR}` interpolation resolves to the
|
|
8
|
+
* worktree's ports no matter which env filename is configured.
|
|
9
|
+
*/
|
|
10
|
+
export function composeEnv(project, allocations) {
|
|
11
|
+
return { [PROJECT_NAME_VAR]: project, ...allocationEnv(allocations) };
|
|
12
|
+
}
|
|
13
|
+
/** Run `<compose> -p <project> <args…>` without blocking the event loop. */
|
|
14
|
+
export function composeRun(engine, project, args, opts) {
|
|
15
|
+
return runAsync(composeBin(engine), composeArgs(engine, project, ...args), {
|
|
16
|
+
cwd: opts.cwd,
|
|
17
|
+
env: { ...process.env, ...opts.env },
|
|
18
|
+
live: opts.live,
|
|
19
|
+
signal: opts.signal,
|
|
20
|
+
onLine: opts.onLine,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/** Prefix an error with what was being done, keeping CommandError details. */
|
|
24
|
+
export function withContext(prefix, err) {
|
|
25
|
+
if (err instanceof CommandError) {
|
|
26
|
+
err.message = `${prefix}: ${err.message}`;
|
|
27
|
+
return err;
|
|
28
|
+
}
|
|
29
|
+
return new Error(`${prefix}: ${err instanceof Error ? err.message : String(err)}`);
|
|
30
|
+
}
|
|
31
|
+
/** Report a context's general warnings (and isolation warnings when asked). */
|
|
32
|
+
export function reportContextWarnings(ctx, reporter, opts) {
|
|
33
|
+
for (const w of ctx.warnings)
|
|
34
|
+
reporter.warn(w);
|
|
35
|
+
if (opts.isolation) {
|
|
36
|
+
for (const w of ctx.isolationWarnings)
|
|
37
|
+
reporter.warn(w);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Throw if the operation was cancelled (checked between worktrees). */
|
|
41
|
+
export function throwIfAborted(io) {
|
|
42
|
+
if (io.signal?.aborted)
|
|
43
|
+
throw new Error("Cancelled.");
|
|
44
|
+
}
|
|
45
|
+
export const NO_WORKTREES_HINT = "No extra worktrees found. Create one with:\n git worktree add ../my-branch my-branch";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { CoreIo } from "./types.js";
|
|
2
|
+
export type CheckStatus = "ok" | "warn" | "fail";
|
|
3
|
+
export interface DoctorCheck {
|
|
4
|
+
status: CheckStatus;
|
|
5
|
+
title: string;
|
|
6
|
+
detail?: string;
|
|
7
|
+
hint?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface DoctorResult {
|
|
10
|
+
/** False when any check failed. */
|
|
11
|
+
ok: boolean;
|
|
12
|
+
checks: DoctorCheck[];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* True when something already listens on `port`. Docker publishes on all
|
|
16
|
+
* interfaces, but local dev servers and Homebrew databases usually bind
|
|
17
|
+
* 127.0.0.1 or ::1 only — and on macOS (BSD sockets, libuv's SO_REUSEADDR)
|
|
18
|
+
* a wildcard bind succeeds next to them, so each address is probed. Only
|
|
19
|
+
* EADDRINUSE counts as busy (no IPv6 → not busy).
|
|
20
|
+
*/
|
|
21
|
+
export declare function isPortBusy(port: number): Promise<boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Diagnose the setup: git, repo layout, compose file, config, compose
|
|
24
|
+
* engine and daemon, port isolation, isolation-breaking compose settings,
|
|
25
|
+
* worktree/registry health, and host ports already taken for stopped stacks.
|
|
26
|
+
* Each check is independent so one failure never hides the others.
|
|
27
|
+
*/
|
|
28
|
+
export declare function doctor(io: CoreIo): Promise<DoctorResult>;
|