worktree-compose 1.1.2 → 2.0.1

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.
Files changed (77) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +31 -16
  3. package/SKILL.md +73 -76
  4. package/dist/cli.js +85 -10
  5. package/dist/commands/down.d.ts +2 -0
  6. package/dist/commands/down.js +7 -0
  7. package/dist/commands/list.d.ts +2 -0
  8. package/dist/commands/list.js +43 -6
  9. package/dist/commands/passthrough.d.ts +13 -0
  10. package/dist/commands/passthrough.js +27 -0
  11. package/dist/commands/stop.d.ts +3 -1
  12. package/dist/commands/stop.js +2 -2
  13. package/dist/compose/detect.d.ts +19 -0
  14. package/dist/compose/detect.js +56 -0
  15. package/dist/compose/isolation.d.ts +20 -7
  16. package/dist/compose/isolation.js +88 -36
  17. package/dist/compose/override.d.ts +54 -4
  18. package/dist/compose/override.js +179 -52
  19. package/dist/compose/parse.d.ts +11 -0
  20. package/dist/compose/parse.js +260 -13
  21. package/dist/compose/status.d.ts +14 -0
  22. package/dist/compose/status.js +58 -1
  23. package/dist/compose/types.d.ts +56 -0
  24. package/dist/config.d.ts +33 -0
  25. package/dist/context.d.ts +26 -0
  26. package/dist/context.js +140 -29
  27. package/dist/core/clean.d.ts +3 -1
  28. package/dist/core/clean.js +8 -3
  29. package/dist/core/compose.d.ts +87 -13
  30. package/dist/core/compose.js +282 -57
  31. package/dist/core/doctor.d.ts +2 -8
  32. package/dist/core/doctor.js +116 -79
  33. package/dist/core/down.d.ts +37 -0
  34. package/dist/core/down.js +54 -0
  35. package/dist/core/health.d.ts +90 -0
  36. package/dist/core/health.js +198 -0
  37. package/dist/core/list.d.ts +53 -6
  38. package/dist/core/list.js +144 -58
  39. package/dist/core/passthrough.d.ts +69 -0
  40. package/dist/core/passthrough.js +216 -0
  41. package/dist/core/promote.d.ts +4 -2
  42. package/dist/core/promote.js +5 -3
  43. package/dist/core/restart.d.ts +3 -3
  44. package/dist/core/restart.js +6 -5
  45. package/dist/core/seed.d.ts +50 -0
  46. package/dist/core/seed.js +245 -0
  47. package/dist/core/shared.d.ts +63 -0
  48. package/dist/core/shared.js +194 -0
  49. package/dist/core/start.d.ts +32 -5
  50. package/dist/core/start.js +297 -49
  51. package/dist/core/stop.d.ts +3 -1
  52. package/dist/core/stop.js +6 -3
  53. package/dist/core/teardown.d.ts +5 -2
  54. package/dist/core/teardown.js +33 -8
  55. package/dist/core/types.d.ts +2 -0
  56. package/dist/git/promote.d.ts +11 -3
  57. package/dist/git/promote.js +19 -5
  58. package/dist/mcp/server.js +154 -28
  59. package/dist/ports/allocate.d.ts +35 -23
  60. package/dist/ports/allocate.js +108 -58
  61. package/dist/ports/probe.d.ts +11 -0
  62. package/dist/ports/probe.js +40 -0
  63. package/dist/ports/types.d.ts +5 -0
  64. package/dist/selectors.d.ts +17 -0
  65. package/dist/selectors.js +19 -0
  66. package/dist/state/registry.d.ts +23 -3
  67. package/dist/state/registry.js +57 -17
  68. package/dist/sync/env.d.ts +42 -2
  69. package/dist/sync/env.js +79 -8
  70. package/dist/sync/envsync.d.ts +91 -0
  71. package/dist/sync/envsync.js +338 -0
  72. package/dist/sync/files.d.ts +87 -5
  73. package/dist/sync/files.js +343 -18
  74. package/dist/utils/paths.d.ts +2 -0
  75. package/dist/utils/paths.js +9 -0
  76. package/package.json +1 -1
  77. package/wtc.schema.json +155 -13
@@ -1,6 +1,81 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
- import { escapesRoot, isSameFile, lstatQuiet, parentPlacement, prepareWritablePath, resolveInside, } from "../utils/paths.js";
3
+ import { escapesRoot, isSameFile, lstatQuiet, parentPlacement, prepareWritablePath, realPath, resolveInside, } from "../utils/paths.js";
4
+ import { runSafe } from "../utils/exec.js";
5
+ /** Name of the file listing untracked files every new worktree should get (gitignore syntax). */
6
+ export const WORKTREE_INCLUDE_FILE = ".worktreeinclude";
7
+ /** A directory wtc copies on its own account only when it is this small: larger ones are data, not config. */
8
+ const AUTO_DIR_MAX_FILES = 2000;
9
+ const AUTO_DIR_MAX_BYTES = 200 * 1024 * 1024;
10
+ /**
11
+ * Copy a file's content. Asks for a copy-on-write clone (APFS, btrfs, XFS),
12
+ * which costs no time or space however large the file; other filesystems
13
+ * fall back to a plain copy.
14
+ */
15
+ function cloneFile(src, dst) {
16
+ fs.copyFileSync(src, dst, fs.constants.COPYFILE_FICLONE);
17
+ }
18
+ /**
19
+ * The paths among `rels` that git tracks in `dir` (for a directory: the
20
+ * tracked files under it). Null when git cannot be asked.
21
+ *
22
+ * On a case-insensitive filesystem a compose file may spell a path in
23
+ * another case than git tracks it (`dockerfile.dev` for `Dockerfile.dev`):
24
+ * it is still the tracked file, so git is asked case-insensitively and a
25
+ * match that is the same file on disk counts, under the spelling asked for.
26
+ */
27
+ export function trackedPaths(dir, rels) {
28
+ if (rels.length === 0)
29
+ return new Set();
30
+ const out = runSafe("git", ["-C", dir, "ls-files", "-z", "--", ...rels.map((rel) => `:(icase,literal)${rel.split(path.sep).join("/")}`)], { trim: false });
31
+ if (out === null)
32
+ return null;
33
+ const tracked = out.split("\0").filter(Boolean).map((p) => path.normalize(p));
34
+ const result = new Set(tracked);
35
+ const asked = rels.map((rel) => path.normalize(rel).replace(new RegExp(`\\${path.sep}+$`), ""));
36
+ for (const t of tracked) {
37
+ const lower = t.toLowerCase();
38
+ for (const rel of asked) {
39
+ if (rel === t)
40
+ continue;
41
+ const relLower = rel.toLowerCase();
42
+ if (lower === relLower) {
43
+ if (isSameFile(path.join(dir, rel), path.join(dir, t)))
44
+ result.add(rel);
45
+ }
46
+ else if (lower.startsWith(relLower + path.sep)) {
47
+ const prefix = t.slice(0, rel.length);
48
+ if (prefix !== rel && isSameFile(path.join(dir, rel), path.join(dir, prefix))) {
49
+ result.add(rel + t.slice(rel.length));
50
+ }
51
+ }
52
+ }
53
+ }
54
+ return result;
55
+ }
56
+ /**
57
+ * The untracked files at or under `rel` in `dir` (ignored ones included),
58
+ * relative to `dir`. Null when git cannot be asked.
59
+ */
60
+ export function untrackedUnder(dir, rel) {
61
+ const out = runSafe("git", ["-C", dir, "ls-files", "--others", "-z", "--", `:(literal)${rel.split(path.sep).join("/")}`], { trim: false });
62
+ if (out === null)
63
+ return null;
64
+ return out.split("\0").filter(Boolean).map((p) => path.normalize(p));
65
+ }
66
+ /** True when git tracks `rel` in `dir`, or (for a directory) anything under it. */
67
+ function tracksAny(tracked, rel) {
68
+ if (!tracked)
69
+ return false;
70
+ const norm = path.normalize(rel);
71
+ if (tracked.has(norm))
72
+ return true;
73
+ const prefix = norm.endsWith(path.sep) ? norm : norm + path.sep;
74
+ for (const t of tracked)
75
+ if (t.startsWith(prefix))
76
+ return true;
77
+ return false;
78
+ }
4
79
  /**
5
80
  * Make `rel` in the worktree a symlink carrying the same target as in main.
6
81
  * Symlinks are recreated, never followed: following one could copy files
@@ -17,6 +92,8 @@ function copySymlink(run, rel) {
17
92
  const existing = lstatQuiet(path.join(run.wtPath, rel));
18
93
  if (existing?.isSymbolicLink() && fs.readlinkSync(path.join(run.wtPath, rel)) === target)
19
94
  return;
95
+ if (existing && run.missingOnly)
96
+ return;
20
97
  if (existing?.isDirectory()) {
21
98
  run.warnings.push(`Skipping sync of ${rel}: it is a symlink in the main checkout but a directory in the worktree. Remove the worktree's directory yourself to resync.`);
22
99
  return;
@@ -37,6 +114,19 @@ function copyFile(run, rel) {
37
114
  const src = path.join(run.repoRoot, rel);
38
115
  if (!fs.existsSync(src))
39
116
  return;
117
+ if (run.missingOnly) {
118
+ const existing = lstatQuiet(path.join(run.wtPath, rel));
119
+ // An empty directory where a file belongs is what the engine leaves when it
120
+ // starts a container whose bind-mounted file is missing: make room for the file.
121
+ if (existing?.isDirectory() && fs.readdirSync(path.join(run.wtPath, rel)).length === 0) {
122
+ if (parentPlacement(run.wtPath, rel) !== "inside")
123
+ return;
124
+ fs.rmdirSync(path.join(run.wtPath, rel));
125
+ }
126
+ else if (existing) {
127
+ return;
128
+ }
129
+ }
40
130
  const dst = prepareWritablePath(run.wtPath, rel, run.warnings, { label: `sync of ${rel}` });
41
131
  if (!dst)
42
132
  return;
@@ -44,8 +134,18 @@ function copyFile(run, rel) {
44
134
  run.warnings.push(`Skipping sync of ${rel}: it is the main checkout's own file.`);
45
135
  return;
46
136
  }
47
- fs.mkdirSync(path.dirname(dst), { recursive: true });
48
- fs.copyFileSync(src, dst);
137
+ try {
138
+ fs.mkdirSync(path.dirname(dst), { recursive: true });
139
+ cloneFile(src, dst);
140
+ }
141
+ catch (err) {
142
+ // What wtc copies unasked must never stop a start: say so and go on.
143
+ if (!run.missingOnly)
144
+ throw err;
145
+ run.warnings.push(`Could not copy ${rel} from the main checkout: ${err.message}`);
146
+ return;
147
+ }
148
+ run.copied?.push(rel);
49
149
  }
50
150
  /** Copy one entry: directories recurse, symlinks are recreated, special files skipped. */
51
151
  function copyEntry(run, rel) {
@@ -83,7 +183,17 @@ function copyDir(run, rel) {
83
183
  return;
84
184
  fs.mkdirSync(dst, { recursive: true });
85
185
  }
86
- for (const entry of fs.readdirSync(path.join(run.repoRoot, rel), { withFileTypes: true })) {
186
+ let entries;
187
+ try {
188
+ entries = fs.readdirSync(path.join(run.repoRoot, rel), { withFileTypes: true });
189
+ }
190
+ catch (err) {
191
+ if (!run.missingOnly)
192
+ throw err;
193
+ run.warnings.push(`Could not read ${rel} in the main checkout: ${err.message}`);
194
+ return;
195
+ }
196
+ for (const entry of entries) {
87
197
  copyEntry(run, path.join(rel, entry.name));
88
198
  }
89
199
  }
@@ -129,31 +239,246 @@ export function resolveSyncEntries(repoRoot, entries) {
129
239
  }
130
240
  return { entries: ok, warnings };
131
241
  }
242
+ /** Files a database keeps its data in. */
243
+ const DATA_FILE = /\.(sqlite3?|db|mdb)(-wal|-shm|-journal)?$/i;
244
+ /** Entries that mark a directory as a database's data directory. */
245
+ const DATA_MARKERS = new Set([
246
+ "PG_VERSION",
247
+ "postmaster.pid",
248
+ "ibdata1",
249
+ "WiredTiger",
250
+ "appendonly.aof",
251
+ "appendonlydir",
252
+ "dump.rdb",
253
+ "nodes",
254
+ ]);
255
+ /** Mount points under which containers keep state. */
256
+ const DATA_TARGET = /^\/var\/lib\/|(^|\/)(data|db|pgdata)(\/|$)/i;
257
+ /**
258
+ * Whether an untracked bind-mount source is a service's data rather than
259
+ * its configuration: a database file, a directory that holds one, or a
260
+ * directory mounted where containers keep state (`/var/lib/…`, `…/data`).
261
+ * Data is never copied unasked: a copy taken while the main checkout's
262
+ * stack writes to it is not a consistent one, and an isolated stack is
263
+ * meant to start with its own.
264
+ */
265
+ export function looksLikeData(abs, targets = []) {
266
+ const stat = lstatQuiet(abs);
267
+ if (!stat)
268
+ return false;
269
+ if (stat.isFile())
270
+ return DATA_FILE.test(path.basename(abs));
271
+ if (!stat.isDirectory())
272
+ return false;
273
+ if (targets.some((t) => DATA_TARGET.test(t)))
274
+ return true;
275
+ try {
276
+ return fs.readdirSync(abs).some((name) => DATA_MARKERS.has(name) || DATA_FILE.test(name));
277
+ }
278
+ catch {
279
+ return false;
280
+ }
281
+ }
282
+ /** Files and total size under `dir` stay within the limits; false also when it cannot be read. */
283
+ function smallDirectory(dir) {
284
+ let files = 0;
285
+ let bytes = 0;
286
+ const stack = [dir];
287
+ while (stack.length > 0) {
288
+ const current = stack.pop();
289
+ let entries;
290
+ try {
291
+ entries = fs.readdirSync(current, { withFileTypes: true });
292
+ }
293
+ catch {
294
+ return false;
295
+ }
296
+ for (const entry of entries) {
297
+ const full = path.join(current, entry.name);
298
+ if (entry.isDirectory()) {
299
+ stack.push(full);
300
+ }
301
+ else if (entry.isFile()) {
302
+ files++;
303
+ bytes += fs.statSync(full).size;
304
+ if (files > AUTO_DIR_MAX_FILES || bytes > AUTO_DIR_MAX_BYTES)
305
+ return false;
306
+ }
307
+ }
308
+ }
309
+ return true;
310
+ }
311
+ /**
312
+ * Host paths inside the repository that the stack reads and that only git
313
+ * could have put into a worktree: bind-mount sources, `env_file` entries,
314
+ * and the files of top-level `secrets:` / `configs:`. Repo-relative.
315
+ */
316
+ export function stackHostPaths(composeFile, repoRoot) {
317
+ return [...stackHostMounts(composeFile, repoRoot).keys()];
318
+ }
319
+ /** The same paths, each with the container paths it is bind-mounted at (none for an `env_file` or a secret). */
320
+ export function stackHostMounts(composeFile, repoRoot) {
321
+ const out = new Map();
322
+ // The compose file's paths are resolved against its real directory.
323
+ const root = realPath(repoRoot);
324
+ const add = (abs, target) => {
325
+ const rel = path.relative(root, abs);
326
+ if (rel === "" || escapesRoot(rel))
327
+ return;
328
+ out.set(rel, [...(out.get(rel) ?? []), ...(target ? [target] : [])]);
329
+ };
330
+ for (const svc of composeFile.services) {
331
+ for (const bind of svc.binds ?? [])
332
+ add(bind.source, bind.target);
333
+ const baseDir = svc.baseDir ?? path.dirname(svc.sourcePath);
334
+ for (const envFile of svc.envFile ?? []) {
335
+ if (!envFile.includes("$"))
336
+ add(path.resolve(baseDir, envFile));
337
+ }
338
+ }
339
+ for (const file of composeFile.fileSources ?? [])
340
+ add(file);
341
+ return out;
342
+ }
343
+ /**
344
+ * Untracked paths of the main checkout that `.worktreeinclude` lists
345
+ * (gitignore syntax; a wholly matching directory comes back once, with a
346
+ * trailing `/`). Empty without that file.
347
+ */
348
+ export function worktreeIncludePaths(repoRoot) {
349
+ const file = path.join(repoRoot, WORKTREE_INCLUDE_FILE);
350
+ if (!fs.existsSync(file))
351
+ return [];
352
+ const out = runSafe("git", ["-C", repoRoot, "ls-files", "--others", "--ignored", `--exclude-from=${file}`, "--directory", "-z"], { trim: false });
353
+ return (out ?? "").split("\0").filter(Boolean);
354
+ }
355
+ /**
356
+ * Untracked files of the main checkout a worktree needs and git cannot
357
+ * give it: what the stack reads (see `stackHostPaths`) and what
358
+ * `.worktreeinclude` lists. Tracked paths are left out: the worktree's
359
+ * branch has its own copy.
360
+ */
361
+ export function neededUntrackedFiles(repoRoot, composeFile) {
362
+ const out = new Map();
363
+ const mounts = stackHostMounts(composeFile, repoRoot);
364
+ const stackPaths = [...mounts.keys()].filter((rel) => lstatQuiet(path.join(repoRoot, rel)));
365
+ const tracked = trackedPaths(repoRoot, stackPaths);
366
+ for (const rel of stackPaths) {
367
+ // Without git's answer nothing is copied unasked.
368
+ if (!tracked)
369
+ continue;
370
+ const abs = path.join(repoRoot, rel);
371
+ const isDir = lstatQuiet(abs)?.isDirectory() === true;
372
+ let files;
373
+ if (tracksAny(tracked, rel)) {
374
+ // A tracked file is the branch's own. A directory git tracks in part
375
+ // still has files only the main checkout has.
376
+ if (!isDir)
377
+ continue;
378
+ const untracked = untrackedUnder(repoRoot, rel);
379
+ if (!untracked || untracked.length === 0)
380
+ continue;
381
+ files = untracked;
382
+ }
383
+ if (looksLikeData(abs, mounts.get(rel))) {
384
+ out.set(rel, { rel, reason: "stack", data: true });
385
+ continue;
386
+ }
387
+ const tooLarge = isDir && !smallDirectory(abs);
388
+ out.set(rel, { rel, reason: "stack", ...(tooLarge ? { tooLarge } : {}), ...(files ? { files } : {}) });
389
+ }
390
+ for (const entry of worktreeIncludePaths(repoRoot)) {
391
+ const rel = path.normalize(entry.replace(/\/$/, ""));
392
+ if (rel && !escapesRoot(rel))
393
+ out.set(rel, { rel, reason: "include" });
394
+ }
395
+ return [...out.values()];
396
+ }
132
397
  /**
133
- * Copy compose files (with includes), Dockerfiles and `sync` entries from the
134
- * main checkout into a worktree. Returns warnings for skipped entries (sync
135
- * entries outside the repo, and destinations wtc refuses to write or
136
- * replaced instead of writing through) so `start` can report them.
398
+ * Bring a worktree's untracked infrastructure up to date with the main
399
+ * checkout's, never touching what the worktree's branch tracks:
400
+ *
401
+ * - compose files (with includes) and Dockerfiles are copied only where the
402
+ * worktree has none, or its copy is not tracked by git there (a local,
403
+ * ignored file). A tracked one is the branch's own: the stack runs it as
404
+ * the branch has it, so a branch can change its compose file or Dockerfile;
405
+ * - `sync` entries are copied as asked, replacing what is there;
406
+ * - untracked files the stack reads (bind-mount sources, `env_file`s,
407
+ * secret and config files) and the ones `.worktreeinclude` lists are
408
+ * copied where the worktree lacks them — never over an existing file,
409
+ * never into a directory the worktree already has, and never a service's
410
+ * data (see `looksLikeData`).
137
411
  */
138
412
  export function syncWorktreeFiles(repoRoot, wtPath, composeFile, extraSync) {
139
413
  const { entries, warnings } = resolveSyncEntries(repoRoot, extraSync);
414
+ const result = { warnings, copied: [], tooLarge: [], data: [] };
140
415
  // Copying the main checkout onto itself would at best do nothing.
141
416
  if (isSameFile(repoRoot, wtPath)) {
142
417
  warnings.push(`Skipping file sync: ${wtPath} is the main checkout itself.`);
143
- return warnings;
418
+ return result;
144
419
  }
145
420
  const run = { repoRoot, wtPath, warnings };
146
- for (const src of composeFile.sourceFiles) {
147
- const rel = path.relative(repoRoot, src);
148
- if (rel === "" || escapesRoot(rel))
149
- continue;
150
- copyFile(run, rel);
151
- }
152
- for (const df of getDockerfiles(composeFile, repoRoot)) {
153
- copyFile(run, df);
421
+ const infra = [
422
+ ...composeFile.sourceFiles.map((src) => path.relative(repoRoot, src)).filter((rel) => rel !== "" && !escapesRoot(rel)),
423
+ ...getDockerfiles(composeFile, repoRoot),
424
+ ];
425
+ const ownInfra = trackedPaths(wtPath, infra);
426
+ for (const rel of new Set(infra)) {
427
+ // When git cannot say, an existing file is left alone: never overwrite what may be the branch's.
428
+ const own = ownInfra ? ownInfra.has(path.normalize(rel)) : lstatQuiet(path.join(wtPath, rel)) !== undefined;
429
+ if (!own)
430
+ copyFile(run, rel);
154
431
  }
155
432
  for (const rel of entries) {
156
433
  copyEntry(run, rel);
157
434
  }
158
- return warnings;
435
+ // What wtc copies unasked never stops a start: a path it cannot read or write
436
+ // (a directory the engine created as root) is said, and the rest goes on.
437
+ const reason = (err) => (err instanceof Error ? err.message : String(err));
438
+ const missing = { repoRoot, wtPath, warnings, missingOnly: true, copied: [] };
439
+ let neededFiles = [];
440
+ try {
441
+ neededFiles = neededUntrackedFiles(repoRoot, composeFile);
442
+ }
443
+ catch (err) {
444
+ warnings.push(`Could not work out which untracked files the stack reads: ${reason(err)}`);
445
+ }
446
+ for (const needed of neededFiles) {
447
+ if (entries.includes(needed.rel))
448
+ continue;
449
+ try {
450
+ const present = lstatQuiet(path.join(wtPath, needed.rel));
451
+ if (needed.data || needed.tooLarge) {
452
+ if (!present)
453
+ (needed.data ? result.data : result.tooLarge).push(needed.rel);
454
+ continue;
455
+ }
456
+ // A directory in which the worktree has untracked files of its own is the
457
+ // worktree's: nothing is merged into it. One that holds only what the branch
458
+ // tracks (a `.gitkeep`), or nothing (what the engine leaves for a missing
459
+ // bind mount), still lacks the main checkout's untracked files.
460
+ if (present?.isDirectory() && lstatQuiet(path.join(repoRoot, needed.rel))?.isDirectory()) {
461
+ const own = untrackedUnder(wtPath, needed.rel);
462
+ if (!own || own.length > 0)
463
+ continue;
464
+ }
465
+ const before = missing.copied.length;
466
+ if (needed.files) {
467
+ for (const file of needed.files)
468
+ copyEntry(missing, file);
469
+ }
470
+ else {
471
+ copyEntry(missing, needed.rel);
472
+ }
473
+ // A directory is reported once, however many files went into it.
474
+ if (missing.copied.length > before && lstatQuiet(path.join(repoRoot, needed.rel))?.isDirectory()) {
475
+ missing.copied.splice(before, missing.copied.length - before, `${needed.rel}/`);
476
+ }
477
+ }
478
+ catch (err) {
479
+ warnings.push(`Could not copy ${needed.rel} from the main checkout: ${reason(err)}`);
480
+ }
481
+ }
482
+ result.copied = missing.copied;
483
+ return result;
159
484
  }
@@ -6,6 +6,8 @@ export declare function escapesRoot(rel: string): boolean;
6
6
  * absolute or would land outside `root` (including `root` itself).
7
7
  */
8
8
  export declare function resolveInside(root: string, rel: string): string | null;
9
+ /** `p` with symlinks resolved when it exists (macOS `/tmp`, a symlinked checkout), else as it is. */
10
+ export declare function realPath(p: string): string;
9
11
  /** True if `child` is `parent` or lies inside it. */
10
12
  export declare function isWithin(parent: string, child: string): boolean;
11
13
  /**
@@ -20,6 +20,15 @@ export function resolveInside(root, rel) {
20
20
  return null;
21
21
  return abs;
22
22
  }
23
+ /** `p` with symlinks resolved when it exists (macOS `/tmp`, a symlinked checkout), else as it is. */
24
+ export function realPath(p) {
25
+ try {
26
+ return fs.realpathSync.native(p);
27
+ }
28
+ catch {
29
+ return path.resolve(p);
30
+ }
31
+ }
23
32
  /** True if `child` is `parent` or lies inside it. */
24
33
  export function isWithin(parent, child) {
25
34
  const rel = path.relative(parent, child);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "worktree-compose",
3
- "version": "1.1.2",
3
+ "version": "2.0.1",
4
4
  "description": "Zero-config Docker Compose isolation for git worktrees",
5
5
  "mcpName": "io.github.mostafasudo/worktree-compose",
6
6
  "type": "module",
package/wtc.schema.json CHANGED
@@ -12,51 +12,193 @@
12
12
  },
13
13
  "sync": {
14
14
  "type": "array",
15
- "description": "Extra repo-relative files or directories copied from the main worktree into each worktree on start. Entries that are absolute or escape the repository are skipped.",
16
- "items": { "type": "string", "minLength": 1 },
17
- "examples": [["nginx", "config/local.json"]]
15
+ "description": "Extra repo-relative files or directories copied from the main worktree into each worktree on every start, replacing what is there. Untracked files the stack reads (bind-mount sources, env_file entries, secret and config files) and the ones .worktreeinclude lists are copied by themselves where a worktree lacks them; list a path here to copy it every time, or when it is a large directory. Entries that are absolute or escape the repository are skipped.",
16
+ "items": {
17
+ "type": "string",
18
+ "minLength": 1
19
+ },
20
+ "examples": [
21
+ [
22
+ "nginx",
23
+ "config/local.json"
24
+ ]
25
+ ]
18
26
  },
19
27
  "envFile": {
20
28
  "type": "string",
21
- "description": "Env filename copied from the main worktree and injected with ports (root and selected workspaces). Must be a bare filename.",
29
+ "description": "Env filename copied from the main worktree and injected with ports (root and selected workspaces). Must be a bare filename. A file the worktree's branch tracks is never changed: its values go to <envFile>.local instead.",
22
30
  "default": ".env",
23
31
  "pattern": "^[^/\\\\]+$",
24
- "examples": [".env.development"]
32
+ "examples": [
33
+ ".env.development"
34
+ ]
25
35
  },
26
36
  "workspaces": {
27
37
  "type": "array",
28
38
  "description": "Monorepo workspaces (package name, directory name, or repo-relative path) that also receive env copies and port injection.",
29
- "items": { "type": "string", "minLength": 1 }
39
+ "items": {
40
+ "type": "string",
41
+ "minLength": 1
42
+ }
30
43
  },
31
44
  "envOverrides": {
32
45
  "type": "object",
33
- "description": "Extra variables written to the root env file. Values interpolate ${VAR} with allocated port variables and ${COMPOSE_PROJECT_NAME}.",
34
- "additionalProperties": { "type": "string" },
35
- "examples": [{ "VITE_API_URL": "http://localhost:${BACKEND_PORT}" }]
46
+ "description": "Extra variables written to the root env file. Values interpolate ${VAR} with the port variables (a hardcoded port goes by WTC_<SERVICE>_PORT, e.g. ${WTC_WEB_PORT}), the config's \"ports\", and ${COMPOSE_PROJECT_NAME}.",
47
+ "additionalProperties": {
48
+ "type": "string"
49
+ },
50
+ "examples": [
51
+ {
52
+ "VITE_API_URL": "http://localhost:${BACKEND_PORT}",
53
+ "PUBLIC_URL": "https://localhost:${WTC_WEB_PORT}"
54
+ }
55
+ ]
36
56
  },
37
57
  "workspaceEnvOverrides": {
38
58
  "type": "object",
39
59
  "description": "Per-workspace variables, keyed by workspace package name, directory basename, or path. Same interpolation as envOverrides.",
40
60
  "additionalProperties": {
41
61
  "type": "object",
42
- "additionalProperties": { "type": "string" }
62
+ "additionalProperties": {
63
+ "type": "string"
64
+ }
43
65
  }
44
66
  },
45
67
  "portStride": {
46
68
  "type": "integer",
47
69
  "minimum": 1,
48
70
  "default": 1,
49
- "description": "Allocated port = 20000 + default port + worktree index × portStride. Raise it (e.g. 100) when default host ports are clustered."
71
+ "description": "Where a new port starts: 20000 + default port + worktree index × portStride. A port that is taken moves to the next free one by itself, and a stack keeps the ports it has, so this rarely needs setting."
50
72
  },
51
73
  "composeCommand": {
52
74
  "description": "Which compose tool to use instead of auto-detection (docker compose → podman compose → docker-compose → podman-compose; the first whose daemon answers wins). Repository config may only choose one of these four, so a cloned repository cannot make wtc run an arbitrary program. For a custom command (an absolute path, global flags such as --context, nerdctl, …) set the WTC_COMPOSE_COMMAND environment variable, which takes precedence.",
53
75
  "oneOf": [
54
- { "type": "string", "enum": ["docker compose", "podman compose", "docker-compose", "podman-compose"] },
76
+ {
77
+ "type": "string",
78
+ "enum": [
79
+ "docker compose",
80
+ "podman compose",
81
+ "docker-compose",
82
+ "podman-compose"
83
+ ]
84
+ },
85
+ {
86
+ "type": "array",
87
+ "enum": [
88
+ [
89
+ "docker",
90
+ "compose"
91
+ ],
92
+ [
93
+ "podman",
94
+ "compose"
95
+ ],
96
+ [
97
+ "docker-compose"
98
+ ],
99
+ [
100
+ "podman-compose"
101
+ ]
102
+ ]
103
+ }
104
+ ]
105
+ },
106
+ "composeFile": {
107
+ "description": "The compose file, relative to the repository root, when it is not compose.yaml / compose.yml / docker-compose.yaml / docker-compose.yml at the root: one path, or several that compose merges in order (like -f a -f b). Without it, wtc also finds the single compose file in docker/, .docker/, compose/, deploy/, infra/ or .devcontainer/.",
108
+ "oneOf": [
109
+ {
110
+ "type": "string",
111
+ "minLength": 1
112
+ },
113
+ {
114
+ "type": "array",
115
+ "minItems": 1,
116
+ "items": {
117
+ "type": "string",
118
+ "minLength": 1
119
+ }
120
+ }
121
+ ],
122
+ "examples": [
123
+ "docker/compose.yml",
124
+ [
125
+ "docker-compose.yml",
126
+ "docker-compose.dev.yml"
127
+ ]
128
+ ]
129
+ },
130
+ "services": {
131
+ "type": "array",
132
+ "description": "Services start brings up by default, with whatever they depend on, instead of the whole stack. --service replaces it for a worktree (and is remembered); --all-services brings up everything.",
133
+ "items": {
134
+ "type": "string",
135
+ "minLength": 1
136
+ },
137
+ "examples": [
138
+ [
139
+ "web",
140
+ "worker"
141
+ ]
142
+ ]
143
+ },
144
+ "shared": {
145
+ "type": "array",
146
+ "description": "Services a worktree stack does not run itself: it reaches the main worktree's instance under the same name. For heavy infrastructure worktrees can share (search, queues). Whatever such a service stores is shared too. They must be running in the main worktree. Needs Docker Compose 2.24.4 or newer.",
147
+ "items": {
148
+ "type": "string",
149
+ "minLength": 1
150
+ },
151
+ "examples": [
152
+ [
153
+ "elasticsearch",
154
+ "kafka"
155
+ ]
156
+ ]
157
+ },
158
+ "seedVolumes": {
159
+ "description": "Named volumes copied from the main worktree's stack into a worktree stack the first time it starts, so it begins with the same data and caches: true for all of them, or their keys. Containers writing to a source volume are paused for the copy.",
160
+ "oneOf": [
161
+ {
162
+ "type": "boolean"
163
+ },
55
164
  {
56
165
  "type": "array",
57
- "enum": [["docker", "compose"], ["podman", "compose"], ["docker-compose"], ["podman-compose"]]
166
+ "items": {
167
+ "type": "string",
168
+ "minLength": 1
169
+ }
58
170
  }
171
+ ],
172
+ "default": false,
173
+ "examples": [
174
+ [
175
+ "postgres_data",
176
+ "bundle_cache"
177
+ ]
59
178
  ]
179
+ },
180
+ "ports": {
181
+ "type": "object",
182
+ "description": "Host ports for processes that run outside compose (a dev server started on the host): variable → default port. Each worktree gets its own value, written to its env file and usable in envOverrides.",
183
+ "propertyNames": {
184
+ "pattern": "^[A-Z_][A-Z0-9_]*$"
185
+ },
186
+ "additionalProperties": {
187
+ "type": "integer",
188
+ "minimum": 1,
189
+ "maximum": 65535
190
+ },
191
+ "examples": [
192
+ {
193
+ "PORT": 3000,
194
+ "VITE_PORT": 5173
195
+ }
196
+ ]
197
+ },
198
+ "rewriteEnvPorts": {
199
+ "type": "boolean",
200
+ "default": true,
201
+ "description": "Point localhost:<port> values (also 127.0.0.1, 0.0.0.0, [::1], host.docker.internal, *.localhost) in a worktree's env files at the worktree's own ports. Set false to leave env values as they are."
60
202
  }
61
203
  }
62
204
  }