worktree-compose 1.1.1 → 2.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/LICENSE +2 -2
- package/README.md +66 -720
- package/SKILL.md +73 -76
- package/dist/cli.js +85 -10
- package/dist/commands/down.d.ts +2 -0
- package/dist/commands/down.js +7 -0
- package/dist/commands/list.d.ts +2 -0
- package/dist/commands/list.js +43 -6
- package/dist/commands/passthrough.d.ts +13 -0
- package/dist/commands/passthrough.js +27 -0
- package/dist/commands/stop.d.ts +3 -1
- package/dist/commands/stop.js +2 -2
- package/dist/compose/detect.d.ts +19 -0
- package/dist/compose/detect.js +56 -0
- package/dist/compose/isolation.d.ts +20 -7
- package/dist/compose/isolation.js +88 -36
- package/dist/compose/override.d.ts +54 -4
- package/dist/compose/override.js +179 -52
- package/dist/compose/parse.d.ts +11 -0
- package/dist/compose/parse.js +260 -13
- package/dist/compose/status.d.ts +14 -0
- package/dist/compose/status.js +58 -1
- package/dist/compose/types.d.ts +56 -0
- package/dist/config.d.ts +33 -0
- package/dist/context.d.ts +26 -0
- package/dist/context.js +140 -29
- package/dist/core/clean.d.ts +3 -1
- package/dist/core/clean.js +8 -3
- package/dist/core/compose.d.ts +87 -13
- package/dist/core/compose.js +282 -57
- package/dist/core/doctor.d.ts +2 -8
- package/dist/core/doctor.js +116 -79
- package/dist/core/down.d.ts +37 -0
- package/dist/core/down.js +54 -0
- package/dist/core/health.d.ts +90 -0
- package/dist/core/health.js +198 -0
- package/dist/core/list.d.ts +53 -6
- package/dist/core/list.js +144 -58
- package/dist/core/passthrough.d.ts +69 -0
- package/dist/core/passthrough.js +216 -0
- package/dist/core/promote.d.ts +4 -2
- package/dist/core/promote.js +5 -3
- package/dist/core/restart.d.ts +3 -3
- package/dist/core/restart.js +6 -5
- package/dist/core/seed.d.ts +50 -0
- package/dist/core/seed.js +245 -0
- package/dist/core/shared.d.ts +63 -0
- package/dist/core/shared.js +194 -0
- package/dist/core/start.d.ts +32 -5
- package/dist/core/start.js +297 -49
- package/dist/core/stop.d.ts +3 -1
- package/dist/core/stop.js +6 -3
- package/dist/core/teardown.d.ts +5 -2
- package/dist/core/teardown.js +33 -8
- package/dist/core/types.d.ts +2 -0
- package/dist/git/promote.d.ts +11 -3
- package/dist/git/promote.js +19 -5
- package/dist/mcp/server.js +154 -28
- package/dist/ports/allocate.d.ts +35 -23
- package/dist/ports/allocate.js +108 -58
- package/dist/ports/probe.d.ts +11 -0
- package/dist/ports/probe.js +40 -0
- package/dist/ports/types.d.ts +5 -0
- package/dist/selectors.d.ts +17 -0
- package/dist/selectors.js +19 -0
- package/dist/state/registry.d.ts +23 -3
- package/dist/state/registry.js +57 -17
- package/dist/sync/env.d.ts +42 -2
- package/dist/sync/env.js +79 -8
- package/dist/sync/envsync.d.ts +91 -0
- package/dist/sync/envsync.js +338 -0
- package/dist/sync/files.d.ts +87 -5
- package/dist/sync/files.js +343 -18
- package/dist/utils/paths.d.ts +2 -0
- package/dist/utils/paths.js +9 -0
- package/package.json +9 -2
- package/wtc.schema.json +155 -13
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { isSameFile, lstatQuiet, parentPlacement, prepareWritablePath } from "../utils/paths.js";
|
|
5
|
+
import { withStateLock } from "../state/registry.js";
|
|
6
|
+
import { trackedPaths } from "./files.js";
|
|
7
|
+
import { findEnvSource, hasOverrideBlock, injectPortOverrides, isLocalEnvFileName, prepareEnvFile, rewriteHostPorts, } from "./env.js";
|
|
8
|
+
/** The tracked env files a development stack reads: their worktree values also belong in `.env.local`. */
|
|
9
|
+
const DEVELOPMENT_ENV_FILES = new Set([".env", ".env.development", ".env.dev"]);
|
|
10
|
+
function rel(dir, name) {
|
|
11
|
+
return dir ? path.join(dir, name) : name;
|
|
12
|
+
}
|
|
13
|
+
/** Local env files (`.env`, `.env.*`, no templates) in a directory of the main checkout. */
|
|
14
|
+
function envFamily(repoRoot, dir) {
|
|
15
|
+
let names;
|
|
16
|
+
try {
|
|
17
|
+
names = fs.readdirSync(dir ? path.join(repoRoot, dir) : repoRoot);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
return names
|
|
23
|
+
.filter(isLocalEnvFileName)
|
|
24
|
+
.filter((name) => lstatQuiet(path.join(repoRoot, rel(dir, name)))?.isFile())
|
|
25
|
+
.sort();
|
|
26
|
+
}
|
|
27
|
+
function noteRewrites(result, file, keys) {
|
|
28
|
+
if (keys.length === 0)
|
|
29
|
+
return;
|
|
30
|
+
result.notes.push(`Pointed ${keys.join(", ")} in ${file} at this worktree's ports.`);
|
|
31
|
+
}
|
|
32
|
+
/** Rewrite the `localhost:<port>` values of an env file wtc put into the worktree, in place. */
|
|
33
|
+
function rewriteInPlace(input, file, result) {
|
|
34
|
+
if (!input.rewritePorts)
|
|
35
|
+
return;
|
|
36
|
+
// The branch may have turned a directory on the way into a symlink since wtc copied the file.
|
|
37
|
+
if (parentPlacement(input.wtPath, path.relative(input.wtPath, file)) !== "inside")
|
|
38
|
+
return;
|
|
39
|
+
const content = fs.readFileSync(file, "utf-8");
|
|
40
|
+
const rewritten = rewriteHostPorts(content, input.allocations, input.moved);
|
|
41
|
+
const keys = Object.keys(rewritten.changed);
|
|
42
|
+
if (keys.length === 0)
|
|
43
|
+
return;
|
|
44
|
+
fs.writeFileSync(file, rewritten.content, "utf-8");
|
|
45
|
+
noteRewrites(result, path.relative(input.wtPath, file), keys);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* An untracked env file next to a target (`.env.local`…): copied from the
|
|
49
|
+
* main checkout when the worktree has none. One wtc copied earlier is kept
|
|
50
|
+
* and only has stale ports corrected; one the user put there is not touched.
|
|
51
|
+
*/
|
|
52
|
+
function bringLocalEnvFile(input, relPath, result) {
|
|
53
|
+
const src = path.join(input.repoRoot, relPath);
|
|
54
|
+
const existing = lstatQuiet(path.join(input.wtPath, relPath));
|
|
55
|
+
if (existing) {
|
|
56
|
+
const managed = (input.managed ?? []).includes(relPath.split(path.sep).join("/"));
|
|
57
|
+
if (managed && existing.isFile() && !isSameFile(src, path.join(input.wtPath, relPath))) {
|
|
58
|
+
rewriteInPlace(input, path.join(input.wtPath, relPath), result);
|
|
59
|
+
result.written.push(relPath);
|
|
60
|
+
}
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const dst = prepareWritablePath(input.wtPath, relPath, result.warnings, { label: `env file ${relPath}` });
|
|
64
|
+
if (!dst || isSameFile(src, dst))
|
|
65
|
+
return;
|
|
66
|
+
fs.mkdirSync(path.dirname(dst), { recursive: true });
|
|
67
|
+
fs.copyFileSync(src, dst);
|
|
68
|
+
result.written.push(relPath);
|
|
69
|
+
result.notes.push(`Copied ${relPath} from the main checkout.`);
|
|
70
|
+
rewriteInPlace(input, dst, result);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Copy the worktree's local env files and work out where each target's
|
|
74
|
+
* keys go. Nothing wtc-specific (project name, ports, overrides) is
|
|
75
|
+
* written yet: `injectEnvTargets` does that once the stack's compose files
|
|
76
|
+
* are known.
|
|
77
|
+
*/
|
|
78
|
+
export function syncEnvFiles(input) {
|
|
79
|
+
const result = { destinations: [], written: [], blockOnly: [], sidecars: new Map(), notes: [], warnings: [] };
|
|
80
|
+
// Every env file of every target directory, asked of git in one call.
|
|
81
|
+
const dirs = new Map();
|
|
82
|
+
for (const target of input.targets)
|
|
83
|
+
dirs.set(target.dir, [...(dirs.get(target.dir) ?? []), target]);
|
|
84
|
+
const candidates = new Set();
|
|
85
|
+
for (const [dir, targets] of dirs) {
|
|
86
|
+
for (const name of envFamily(input.repoRoot, dir))
|
|
87
|
+
candidates.add(rel(dir, name));
|
|
88
|
+
for (const name of envFamily(input.repoRoot, dir))
|
|
89
|
+
candidates.add(rel(dir, `${name}.local`));
|
|
90
|
+
candidates.add(rel(dir, ".env.local"));
|
|
91
|
+
for (const t of targets) {
|
|
92
|
+
candidates.add(rel(dir, t.envFileName));
|
|
93
|
+
candidates.add(rel(dir, `${t.envFileName}.local`));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const tracked = trackedPaths(input.wtPath, [...candidates]);
|
|
97
|
+
if (!tracked) {
|
|
98
|
+
// Guessing "untracked" would overwrite the branch's own env files.
|
|
99
|
+
throw new Error(`Could not ask git which env files ${input.wtPath} tracks ('git ls-files' failed there), so wtc wrote none. Check 'git -C ${input.wtPath} status'.`);
|
|
100
|
+
}
|
|
101
|
+
const isTracked = (relPath) => tracked.has(path.normalize(relPath));
|
|
102
|
+
for (const [dir, targets] of dirs) {
|
|
103
|
+
const targetNames = new Set(targets.map((t) => t.envFileName));
|
|
104
|
+
// 1. Untracked env files next to the target, where the worktree has none (the target itself comes in step 2).
|
|
105
|
+
for (const name of envFamily(input.repoRoot, dir)) {
|
|
106
|
+
const relPath = rel(dir, name);
|
|
107
|
+
if (targetNames.has(name) || isTracked(relPath))
|
|
108
|
+
continue;
|
|
109
|
+
bringLocalEnvFile(input, relPath, result);
|
|
110
|
+
}
|
|
111
|
+
// 2. The targets: copied fresh, unless the branch tracks them.
|
|
112
|
+
for (const target of targets) {
|
|
113
|
+
const relPath = rel(dir, target.envFileName);
|
|
114
|
+
if (!isTracked(relPath)) {
|
|
115
|
+
const envWarnings = [];
|
|
116
|
+
const file = prepareEnvFile(input.repoRoot, input.wtPath, target, envWarnings);
|
|
117
|
+
result.warnings.push(...envWarnings);
|
|
118
|
+
if (!file && envWarnings.length === 0) {
|
|
119
|
+
result.warnings.push(`No ${target.envFileName} (or .env.example) found for ${target.label}; skipped`);
|
|
120
|
+
}
|
|
121
|
+
if (file)
|
|
122
|
+
rewriteInPlace(input, file, result);
|
|
123
|
+
result.destinations.push({ target, file, sidecar: false });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// 3. Tracked env files stay as the branch has them: what would change goes to
|
|
127
|
+
// `<file>.local`, and — where every tracked file agrees on a value — to
|
|
128
|
+
// `.env.local` as well, the file loaders read on top of every mode's.
|
|
129
|
+
const trackedFiles = new Set([
|
|
130
|
+
...envFamily(input.repoRoot, dir).map((name) => rel(dir, name)).filter(isTracked),
|
|
131
|
+
...targets.map((t) => rel(dir, t.envFileName)).filter(isTracked),
|
|
132
|
+
]);
|
|
133
|
+
/** What wtc writes into each file of this step, by worktree-relative path. */
|
|
134
|
+
const pending = new Map();
|
|
135
|
+
const want = (relPath) => {
|
|
136
|
+
let entry = pending.get(relPath);
|
|
137
|
+
if (!entry)
|
|
138
|
+
pending.set(relPath, (entry = { entries: {}, siblingOf: [] }));
|
|
139
|
+
return entry;
|
|
140
|
+
};
|
|
141
|
+
const votes = new Map();
|
|
142
|
+
for (const relPath of trackedFiles) {
|
|
143
|
+
const own = path.join(input.wtPath, relPath);
|
|
144
|
+
const target = targets.find((t) => rel(dir, t.envFileName) === relPath);
|
|
145
|
+
let changed = {};
|
|
146
|
+
if (input.rewritePorts && lstatQuiet(own)?.isFile()) {
|
|
147
|
+
changed = rewriteHostPorts(fs.readFileSync(own, "utf-8"), input.allocations, input.moved).changed;
|
|
148
|
+
}
|
|
149
|
+
if (!target && Object.keys(changed).length === 0)
|
|
150
|
+
continue;
|
|
151
|
+
const sibling = want(`${relPath}.local`);
|
|
152
|
+
Object.assign(sibling.entries, changed);
|
|
153
|
+
if (target)
|
|
154
|
+
sibling.target = target;
|
|
155
|
+
sibling.siblingOf.push(relPath);
|
|
156
|
+
// Only what development reads counts for `.env.local`: a test or production
|
|
157
|
+
// file's value (a test database's URL) must not become everyone's.
|
|
158
|
+
if (DEVELOPMENT_ENV_FILES.has(path.basename(relPath))) {
|
|
159
|
+
for (const [key, value] of Object.entries(changed)) {
|
|
160
|
+
votes.set(key, (votes.get(key) ?? new Set()).add(value));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// A key those files give different worktree values has no single value for
|
|
165
|
+
// `.env.local`: it stays in the per-file siblings only.
|
|
166
|
+
const agreed = Object.fromEntries([...votes].filter(([, values]) => values.size === 1).map(([key, values]) => [key, [...values][0]]));
|
|
167
|
+
const localRel = rel(dir, ".env.local");
|
|
168
|
+
let alsoLocal = false;
|
|
169
|
+
if (Object.keys(agreed).length > 0 && !isTracked(localRel)) {
|
|
170
|
+
const asTarget = result.destinations.find((d) => d.file !== null && !d.sidecar && path.relative(input.wtPath, d.file) === localRel);
|
|
171
|
+
if (asTarget) {
|
|
172
|
+
asTarget.extraEntries = { ...agreed, ...asTarget.extraEntries };
|
|
173
|
+
alsoLocal = true;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
const local = want(localRel);
|
|
177
|
+
alsoLocal = local.siblingOf.length === 0;
|
|
178
|
+
// As the sibling of a tracked `.env`, that file's own values come first.
|
|
179
|
+
local.entries = { ...agreed, ...local.entries };
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
for (const [fileRel, write] of pending) {
|
|
183
|
+
const what = write.siblingOf.length > 0 ? write.siblingOf.join(" and ") : "the tracked env files";
|
|
184
|
+
if (isTracked(fileRel)) {
|
|
185
|
+
const keys = Object.keys(write.entries);
|
|
186
|
+
result.warnings.push(`${what} and ${fileRel} are both tracked by git, so wtc writes nothing there: ` +
|
|
187
|
+
`${write.target ? "this worktree's ports and project name are" : `${keys.join(", ")} still point${keys.length === 1 ? "s" : ""} at the main checkout's ports and`} not in an env file here.`);
|
|
188
|
+
if (write.target)
|
|
189
|
+
result.destinations.push({ target: write.target, file: null, sidecar: true });
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
// Whose file is it? One wtc copied or created is wtc's; one that was there
|
|
193
|
+
// before is the user's, and wtc only keeps its block in it.
|
|
194
|
+
const abs = path.join(input.wtPath, fileRel);
|
|
195
|
+
const before = lstatQuiet(abs);
|
|
196
|
+
const owned = !before || result.written.includes(fileRel) || (input.managed ?? []).includes(fileRel.split(path.sep).join("/"));
|
|
197
|
+
// A symlink there (to the main checkout's file, say) is replaced by a regular
|
|
198
|
+
// file, never written through: keep what it showed.
|
|
199
|
+
let linked = null;
|
|
200
|
+
if (before?.isSymbolicLink()) {
|
|
201
|
+
try {
|
|
202
|
+
linked = fs.readFileSync(abs, "utf-8");
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
linked = null;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const file = prepareWritablePath(input.wtPath, fileRel, result.warnings, { label: `env file ${fileRel}` });
|
|
209
|
+
if (!file) {
|
|
210
|
+
if (write.target)
|
|
211
|
+
result.destinations.push({ target: write.target, file: null, sidecar: true });
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (!fs.existsSync(file)) {
|
|
215
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
216
|
+
fs.writeFileSync(file, linked ?? "", "utf-8");
|
|
217
|
+
}
|
|
218
|
+
for (const tracked of write.siblingOf)
|
|
219
|
+
result.sidecars.set(path.join(input.wtPath, tracked), file);
|
|
220
|
+
if (write.target) {
|
|
221
|
+
result.destinations.push({ target: write.target, file, sidecar: true, extraEntries: write.entries, owned });
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
injectPortOverrides(file, [], undefined, {
|
|
225
|
+
root: input.wtPath,
|
|
226
|
+
extraEntries: write.entries,
|
|
227
|
+
extraOnlyIfAbsent: true,
|
|
228
|
+
});
|
|
229
|
+
(owned ? result.written : result.blockOnly).push(fileRel);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
for (const relPath of trackedFiles) {
|
|
233
|
+
const write = pending.get(`${relPath}.local`);
|
|
234
|
+
if (!write)
|
|
235
|
+
continue;
|
|
236
|
+
const keys = Object.keys(rewriteHostPorts(lstatQuiet(path.join(input.wtPath, relPath))?.isFile() && input.rewritePorts
|
|
237
|
+
? fs.readFileSync(path.join(input.wtPath, relPath), "utf-8")
|
|
238
|
+
: "", input.allocations, input.moved).changed);
|
|
239
|
+
result.notes.push(`${relPath} is tracked by git, so wtc leaves it as it is and writes this worktree's values to ${relPath}.local` +
|
|
240
|
+
(keys.length > 0 ? ` (${keys.join(", ")})` : "") +
|
|
241
|
+
`.`);
|
|
242
|
+
}
|
|
243
|
+
if (alsoLocal) {
|
|
244
|
+
result.notes.push(`Also wrote ${Object.keys(agreed).join(", ")} to ${localRel}, which env loaders read on top of every mode's file.`);
|
|
245
|
+
}
|
|
246
|
+
// A block wtc wrote on an earlier start, in a file it has nothing to say to any
|
|
247
|
+
// more, must not keep overriding: clear it.
|
|
248
|
+
const stale = [...[...trackedFiles].map((f) => `${f}.local`), localRel].filter((f) => !pending.has(f) && !isTracked(f) && !result.destinations.some((d) => d.file === path.join(input.wtPath, f)));
|
|
249
|
+
for (const fileRel of new Set(stale)) {
|
|
250
|
+
const abs = path.join(input.wtPath, fileRel);
|
|
251
|
+
if (!lstatQuiet(abs)?.isFile() || parentPlacement(input.wtPath, fileRel) !== "inside")
|
|
252
|
+
continue;
|
|
253
|
+
if (!hasOverrideBlock(fs.readFileSync(abs, "utf-8")))
|
|
254
|
+
continue;
|
|
255
|
+
injectPortOverrides(abs, [], undefined, { root: input.wtPath });
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Write wtc's keys — project name and compose file list (root target),
|
|
262
|
+
* allocated ports, overrides — to each destination `syncEnvFiles` chose.
|
|
263
|
+
* Returns the worktree-relative files written.
|
|
264
|
+
*/
|
|
265
|
+
export function injectEnvTargets(input, synced, injection) {
|
|
266
|
+
const written = [];
|
|
267
|
+
const blockOnly = [];
|
|
268
|
+
const warnings = [];
|
|
269
|
+
for (const dest of synced.destinations) {
|
|
270
|
+
if (!dest.file)
|
|
271
|
+
continue;
|
|
272
|
+
const forCompose = dest.target.compose === true;
|
|
273
|
+
const opts = {
|
|
274
|
+
projectName: injection.projectName,
|
|
275
|
+
writeProjectName: forCompose,
|
|
276
|
+
...(forCompose && injection.composeFile ? { composeFile: injection.composeFile } : {}),
|
|
277
|
+
root: input.wtPath,
|
|
278
|
+
source: dest.sidecar ? undefined : (findEnvSource(input.repoRoot, dest.target) ?? undefined),
|
|
279
|
+
...(dest.extraEntries ? { extraEntries: dest.extraEntries, extraOnlyIfAbsent: true } : {}),
|
|
280
|
+
};
|
|
281
|
+
const outcome = injectPortOverrides(dest.file, input.allocations, dest.target.overrides, opts);
|
|
282
|
+
warnings.push(...outcome.warnings);
|
|
283
|
+
(dest.owned === false ? blockOnly : written).push(path.relative(input.wtPath, dest.file));
|
|
284
|
+
}
|
|
285
|
+
return { written, blockOnly, warnings };
|
|
286
|
+
}
|
|
287
|
+
const EXCLUDE_START = "# --- wtc: env files it writes in worktrees ---";
|
|
288
|
+
const EXCLUDE_END = "# --- end wtc ---";
|
|
289
|
+
/**
|
|
290
|
+
* Keep files wtc created in a worktree out of `git status`: those git
|
|
291
|
+
* neither tracks nor ignores there are listed in `.git/info/exclude` (the
|
|
292
|
+
* repository's local ignore file, never committed; every worktree shares
|
|
293
|
+
* it). Returns the paths added.
|
|
294
|
+
*/
|
|
295
|
+
export function excludeFromGit(commonDir, wtPath, rels) {
|
|
296
|
+
const candidates = [...new Set(rels.map((r) => r.split(path.sep).join("/")))];
|
|
297
|
+
if (candidates.length === 0)
|
|
298
|
+
return [];
|
|
299
|
+
const tracked = trackedPaths(wtPath, candidates);
|
|
300
|
+
if (!tracked)
|
|
301
|
+
return [];
|
|
302
|
+
const loose = candidates.filter((r) => !tracked.has(path.normalize(r)) && notIgnored(wtPath, r));
|
|
303
|
+
if (loose.length === 0)
|
|
304
|
+
return [];
|
|
305
|
+
// Two starts may add their files at the same moment: one at a time.
|
|
306
|
+
return withStateLock(commonDir, () => writeExcludes(commonDir, loose));
|
|
307
|
+
}
|
|
308
|
+
function writeExcludes(commonDir, loose) {
|
|
309
|
+
const file = path.join(commonDir, "info", "exclude");
|
|
310
|
+
let content = "";
|
|
311
|
+
try {
|
|
312
|
+
content = fs.readFileSync(file, "utf-8");
|
|
313
|
+
}
|
|
314
|
+
catch {
|
|
315
|
+
// No exclude file yet.
|
|
316
|
+
}
|
|
317
|
+
const start = content.indexOf(EXCLUDE_START);
|
|
318
|
+
const end = start === -1 ? -1 : content.indexOf(EXCLUDE_END, start);
|
|
319
|
+
const existing = start !== -1 && end !== -1
|
|
320
|
+
? content.slice(start + EXCLUDE_START.length, end).split("\n").map((l) => l.trim()).filter(Boolean)
|
|
321
|
+
: [];
|
|
322
|
+
const lines = [...new Set([...existing, ...loose.map((r) => `/${r}`)])];
|
|
323
|
+
const block = [EXCLUDE_START, ...lines, EXCLUDE_END].join("\n");
|
|
324
|
+
const next = start !== -1 && end !== -1
|
|
325
|
+
? content.slice(0, start) + block + content.slice(end + EXCLUDE_END.length)
|
|
326
|
+
: (content && !content.endsWith("\n") ? content + "\n" : content) + block + "\n";
|
|
327
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
328
|
+
fs.writeFileSync(file, next, "utf-8");
|
|
329
|
+
return loose.filter((r) => !existing.includes(`/${r}`));
|
|
330
|
+
}
|
|
331
|
+
/** True only when git says the path is not ignored: `check-ignore` exits 1 (0: ignored; 128: an error). */
|
|
332
|
+
function notIgnored(wtPath, relPath) {
|
|
333
|
+
const result = spawnSync("git", ["-C", wtPath, "check-ignore", "-q", "--", relPath], {
|
|
334
|
+
stdio: "ignore",
|
|
335
|
+
windowsHide: true,
|
|
336
|
+
});
|
|
337
|
+
return result.status === 1;
|
|
338
|
+
}
|
package/dist/sync/files.d.ts
CHANGED
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
import type { ComposeFile } from "../compose/types.js";
|
|
2
|
+
/** Name of the file listing untracked files every new worktree should get (gitignore syntax). */
|
|
3
|
+
export declare const WORKTREE_INCLUDE_FILE = ".worktreeinclude";
|
|
4
|
+
/**
|
|
5
|
+
* The paths among `rels` that git tracks in `dir` (for a directory: the
|
|
6
|
+
* tracked files under it). Null when git cannot be asked.
|
|
7
|
+
*
|
|
8
|
+
* On a case-insensitive filesystem a compose file may spell a path in
|
|
9
|
+
* another case than git tracks it (`dockerfile.dev` for `Dockerfile.dev`):
|
|
10
|
+
* it is still the tracked file, so git is asked case-insensitively and a
|
|
11
|
+
* match that is the same file on disk counts, under the spelling asked for.
|
|
12
|
+
*/
|
|
13
|
+
export declare function trackedPaths(dir: string, rels: string[]): Set<string> | null;
|
|
14
|
+
/**
|
|
15
|
+
* The untracked files at or under `rel` in `dir` (ignored ones included),
|
|
16
|
+
* relative to `dir`. Null when git cannot be asked.
|
|
17
|
+
*/
|
|
18
|
+
export declare function untrackedUnder(dir: string, rel: string): string[] | null;
|
|
2
19
|
export declare function getDockerfiles(composeFile: ComposeFile, repoRoot: string): string[];
|
|
3
20
|
/**
|
|
4
21
|
* Repo-relative paths of the `sync` entries that stay inside the repo, plus a
|
|
@@ -9,9 +26,74 @@ export declare function resolveSyncEntries(repoRoot: string, entries: string[] |
|
|
|
9
26
|
warnings: string[];
|
|
10
27
|
};
|
|
11
28
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
29
|
+
* Whether an untracked bind-mount source is a service's data rather than
|
|
30
|
+
* its configuration: a database file, a directory that holds one, or a
|
|
31
|
+
* directory mounted where containers keep state (`/var/lib/…`, `…/data`).
|
|
32
|
+
* Data is never copied unasked: a copy taken while the main checkout's
|
|
33
|
+
* stack writes to it is not a consistent one, and an isolated stack is
|
|
34
|
+
* meant to start with its own.
|
|
35
|
+
*/
|
|
36
|
+
export declare function looksLikeData(abs: string, targets?: readonly string[]): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Host paths inside the repository that the stack reads and that only git
|
|
39
|
+
* could have put into a worktree: bind-mount sources, `env_file` entries,
|
|
40
|
+
* and the files of top-level `secrets:` / `configs:`. Repo-relative.
|
|
41
|
+
*/
|
|
42
|
+
export declare function stackHostPaths(composeFile: ComposeFile, repoRoot: string): string[];
|
|
43
|
+
/** The same paths, each with the container paths it is bind-mounted at (none for an `env_file` or a secret). */
|
|
44
|
+
export declare function stackHostMounts(composeFile: ComposeFile, repoRoot: string): Map<string, string[]>;
|
|
45
|
+
/**
|
|
46
|
+
* Untracked paths of the main checkout that `.worktreeinclude` lists
|
|
47
|
+
* (gitignore syntax; a wholly matching directory comes back once, with a
|
|
48
|
+
* trailing `/`). Empty without that file.
|
|
49
|
+
*/
|
|
50
|
+
export declare function worktreeIncludePaths(repoRoot: string): string[];
|
|
51
|
+
export interface NeededFile {
|
|
52
|
+
/** Repo-relative path in the main checkout. */
|
|
53
|
+
rel: string;
|
|
54
|
+
/** Why wtc copies it: a service reads it, or `.worktreeinclude` lists it. */
|
|
55
|
+
reason: "stack" | "include";
|
|
56
|
+
/** A directory too large to copy unasked (only for `stack` paths). */
|
|
57
|
+
tooLarge?: boolean;
|
|
58
|
+
/** A service's data, not its configuration (only for `stack` paths): never copied unasked. */
|
|
59
|
+
data?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* For a directory git tracks in part (a `certs/` with a tracked `.gitkeep`
|
|
62
|
+
* and untracked certificates): its untracked files, the only ones a
|
|
63
|
+
* worktree lacks.
|
|
64
|
+
*/
|
|
65
|
+
files?: string[];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Untracked files of the main checkout a worktree needs and git cannot
|
|
69
|
+
* give it: what the stack reads (see `stackHostPaths`) and what
|
|
70
|
+
* `.worktreeinclude` lists. Tracked paths are left out: the worktree's
|
|
71
|
+
* branch has its own copy.
|
|
72
|
+
*/
|
|
73
|
+
export declare function neededUntrackedFiles(repoRoot: string, composeFile: ComposeFile): NeededFile[];
|
|
74
|
+
export interface SyncResult {
|
|
75
|
+
/** Skipped entries and destinations wtc refused to write or replaced, with the reason. */
|
|
76
|
+
warnings: string[];
|
|
77
|
+
/** Untracked files the worktree lacked and wtc copied from the main checkout (repo-relative). */
|
|
78
|
+
copied: string[];
|
|
79
|
+
/** Directories a service bind-mounts that are too large to copy unasked. */
|
|
80
|
+
tooLarge: string[];
|
|
81
|
+
/** Bind-mounted paths that hold a service's data: the worktree starts without them. */
|
|
82
|
+
data: string[];
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Bring a worktree's untracked infrastructure up to date with the main
|
|
86
|
+
* checkout's, never touching what the worktree's branch tracks:
|
|
87
|
+
*
|
|
88
|
+
* - compose files (with includes) and Dockerfiles are copied only where the
|
|
89
|
+
* worktree has none, or its copy is not tracked by git there (a local,
|
|
90
|
+
* ignored file). A tracked one is the branch's own: the stack runs it as
|
|
91
|
+
* the branch has it, so a branch can change its compose file or Dockerfile;
|
|
92
|
+
* - `sync` entries are copied as asked, replacing what is there;
|
|
93
|
+
* - untracked files the stack reads (bind-mount sources, `env_file`s,
|
|
94
|
+
* secret and config files) and the ones `.worktreeinclude` lists are
|
|
95
|
+
* copied where the worktree lacks them — never over an existing file,
|
|
96
|
+
* never into a directory the worktree already has, and never a service's
|
|
97
|
+
* data (see `looksLikeData`).
|
|
16
98
|
*/
|
|
17
|
-
export declare function syncWorktreeFiles(repoRoot: string, wtPath: string, composeFile: ComposeFile, extraSync?: string[]):
|
|
99
|
+
export declare function syncWorktreeFiles(repoRoot: string, wtPath: string, composeFile: ComposeFile, extraSync?: string[]): SyncResult;
|