pi-odd-worktree 2.0.0 → 3.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 +54 -311
- package/extensions/odd-worktree-tools.ts +4149 -2447
- package/lib/bash-activity.js +1164 -0
- package/lib/bash-design.js +684 -0
- package/lib/bash-destructive.js +559 -0
- package/lib/bash-frame.js +205 -0
- package/lib/card-content.js +23 -0
- package/lib/card-style.js +175 -0
- package/lib/checkout-facts.js +30 -70
- package/lib/codegraph-card-preview.js +628 -0
- package/lib/codegraph-card.js +515 -0
- package/lib/codegraph-compaction.js +76 -0
- package/lib/codegraph-daemon.js +246 -0
- package/lib/codegraph-edit-sync.js +182 -0
- package/lib/codegraph-guard-target.js +337 -0
- package/lib/codegraph-guard.js +211 -0
- package/lib/codegraph-index.js +1020 -0
- package/lib/codegraph-lock.js +607 -0
- package/lib/codegraph-mcp-config.js +134 -0
- package/lib/codegraph-mcp.js +123 -0
- package/lib/codegraph-paths.js +69 -0
- package/lib/codegraph-prompt/explore.md +11 -0
- package/lib/codegraph-prompt/fix.md +6 -0
- package/lib/codegraph-prompt/generic.md +8 -0
- package/lib/codegraph-prompt/judge.md +9 -0
- package/lib/codegraph-prompt/orchestrator.md +17 -0
- package/lib/codegraph-prompt/verify.md +10 -0
- package/lib/codegraph-prompt/worker.md +7 -0
- package/lib/codegraph-role.js +55 -0
- package/lib/codegraph-rules.js +146 -0
- package/lib/codegraph-session.js +303 -383
- package/lib/codegraph-warmup.js +139 -0
- package/lib/file-card-markdown.js +141 -0
- package/lib/file-card-resolver.js +245 -0
- package/lib/file-card.js +659 -0
- package/lib/files-index.js +387 -0
- package/lib/files-insert.js +81 -0
- package/lib/files-orders.js +326 -0
- package/lib/files-picker-view.js +1416 -0
- package/lib/files-preview.js +338 -0
- package/lib/files-scope-detail.js +171 -0
- package/lib/files-scope-view.js +507 -0
- package/lib/gh-issues.js +308 -14
- package/lib/issue-preview.js +26 -7
- package/lib/issues-bash-mutation.js +206 -0
- package/lib/issues-cache-store.js +261 -0
- package/lib/issues-section.js +710 -0
- package/lib/issues-view.js +649 -52
- package/lib/odd-prompt/odd-worktrees.md +21 -0
- package/lib/odd-worktrees-prompt.js +10 -28
- package/lib/related-issues-cache.js +210 -0
- package/lib/shell-card.js +825 -0
- package/lib/skill-odd-card.js +183 -0
- package/lib/tui-paint.js +4 -5
- package/lib/warp-create-panel.js +538 -250
- package/lib/warp-create-picker.js +56 -9
- package/lib/warp-delete-confirm.js +19 -28
- package/lib/warp-selector-view.js +160 -82
- package/lib/which-command.js +90 -0
- package/lib/worktree-card.js +267 -897
- package/lib/worktree-cards.js +383 -0
- package/lib/worktree-create.js +287 -0
- package/lib/worktree-git.js +3 -3
- package/lib/worktree-guard.js +548 -0
- package/lib/worktree-hooks.js +421 -711
- package/lib/worktree-list.js +120 -0
- package/lib/worktree-registry.js +162 -879
- package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
- package/lib/worktree-setup-runner.js +376 -0
- package/lib/worktree-setup.js +140 -0
- package/lib/worktree-warp-busy.js +462 -85
- package/lib/worktree-warp-sessions.js +1 -2
- package/package.json +2 -2
- package/tools/bash-activity.test.js +1419 -0
- package/tools/bash-design.test.js +1091 -0
- package/tools/bash-destructive.test.js +139 -0
- package/tools/card-content.test.js +44 -0
- package/tools/card-style.test.js +298 -0
- package/tools/codegraph-card-preview.test.js +254 -0
- package/tools/codegraph-card.test.js +1080 -0
- package/tools/codegraph-compaction.test.js +378 -0
- package/tools/codegraph-daemon.test.js +248 -0
- package/tools/codegraph-edit-sync.test.js +295 -0
- package/tools/codegraph-guard-target.test.js +174 -0
- package/tools/codegraph-guard.test.js +238 -0
- package/tools/codegraph-index.test.js +1948 -0
- package/tools/codegraph-lock.test.js +932 -0
- package/tools/codegraph-mcp-config.test.js +181 -0
- package/tools/codegraph-mcp.test.js +194 -0
- package/tools/codegraph-paths.test.js +44 -0
- package/tools/codegraph-role.test.js +30 -0
- package/tools/codegraph-rules.test.js +250 -0
- package/tools/codegraph-session.test.js +660 -391
- package/tools/codegraph-warmup.test.js +316 -0
- package/tools/commands.test.js +510 -31
- package/tools/file-card-markdown.test.js +188 -0
- package/tools/file-card.test.js +1289 -0
- package/tools/files-bridge.test.js +575 -0
- package/tools/files-command.test.js +610 -0
- package/tools/files-index.test.js +473 -0
- package/tools/files-insert.test.js +139 -0
- package/tools/files-orders.test.js +597 -0
- package/tools/files-picker-view.test.js +1871 -0
- package/tools/files-preview.test.js +452 -0
- package/tools/files-scope-detail.test.js +264 -0
- package/tools/files-scope-view.test.js +614 -0
- package/tools/fixtures/card-codegraph-outputs.js +110 -0
- package/tools/fixtures/codegraph-cli-stub.js +16 -0
- package/tools/fixtures/codegraph-init-process.js +28 -0
- package/tools/fixtures/codegraph-lock-worker.js +117 -0
- package/tools/fixtures/daemon-fake.js +98 -0
- package/tools/fixtures/daemon-mcp-server.js +39 -0
- package/tools/fixtures/daemon-warmup-worker.js +12 -0
- package/tools/fixtures/load-extension.js +136 -3
- package/tools/gh-issues-feedback.test.js +31 -7
- package/tools/gh-issues.test.js +391 -1
- package/tools/issue-preview.test.js +44 -7
- package/tools/issues-bash-mutation.test.js +101 -0
- package/tools/issues-cache-store.test.js +222 -0
- package/tools/issues-command.test.js +1512 -66
- package/tools/issues-open-guard.test.js +1096 -0
- package/tools/issues-section.test.js +4195 -0
- package/tools/issues-seed-query-regression.test.js +67 -0
- package/tools/issues-view.test.js +1489 -60
- package/tools/odd-worktrees-prompt.test.js +98 -65
- package/tools/package-surface.test.js +431 -36
- package/tools/related-issues-cache.test.js +324 -0
- package/tools/shell-card.test.js +834 -0
- package/tools/skill-odd-card.test.js +200 -0
- package/tools/t5-session-identity.test.js +44 -22
- package/tools/warp-busy-guard.test.js +2569 -308
- package/tools/warp-create-panel.test.js +499 -74
- package/tools/warp-create-picker.test.js +101 -5
- package/tools/warp-delete-confirm.test.js +49 -15
- package/tools/warp-empty-session.test.js +0 -1
- package/tools/warp-fallback-regression.test.js +533 -0
- package/tools/warp-selector-view.test.js +460 -196
- package/tools/which-command.test.js +126 -0
- package/tools/worktree-card.test.js +551 -0
- package/tools/worktree-cards.test.js +845 -0
- package/tools/worktree-changes.test.js +4 -5
- package/tools/worktree-create.test.js +489 -0
- package/tools/worktree-guard.test.js +823 -0
- package/tools/worktree-hooks.test.js +905 -0
- package/tools/worktree-list.test.js +170 -0
- package/tools/worktree-registry.test.js +89 -230
- package/tools/worktree-remove-async.test.js +318 -0
- package/tools/worktree-setup-runner.test.js +276 -0
- package/tools/worktree-setup.test.js +140 -0
- package/tools/worktree-tools.test.js +477 -93
- package/lib/codegraph-commands.js +0 -165
- package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
- package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
- package/lib/worktree-model-result.js +0 -291
- package/tools/codegraph-commands.test.js +0 -371
- package/tools/herdr-free-create.test.js +0 -161
- package/tools/worktree-close-herdr-async.test.js +0 -204
- package/tools/worktree-odd-migration.test.js +0 -224
- package/tools/worktree-registry-cache.test.js +0 -163
- package/tools/worktree-registry-odd.test.js +0 -231
package/lib/worktree-hooks.js
CHANGED
|
@@ -1,757 +1,467 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
//
|
|
2
|
+
// The preparation hooks of `worktree_create` (S19, S20).
|
|
3
3
|
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
4
|
+
// An ordered chain that runs once Git has created the worktree and before the output
|
|
5
|
+
// is rendered: the submodules, the copy of the untracked inputs, `copy_changes` (S18),
|
|
6
|
+
// the links of build caches and local asset directories, and the slot the setup chain
|
|
7
|
+
// takes. Every step reports `applied`, `skipped: <reason>` or `failed: <reason>`, and a
|
|
8
|
+
// failing step never aborts the chain or the creation: the worktree stays created and
|
|
9
|
+
// usable, and the summary names what failed.
|
|
10
10
|
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
11
|
+
// The chain reaches the world only through the injected seams — filesystem, Git and
|
|
12
|
+
// platform — so a test answers for a machine it invents and no case depends on the
|
|
13
|
+
// host's link privileges. No hook reads a file's contents: an input is copied by the
|
|
14
|
+
// filesystem and never decoded, so nothing secret can reach a log or a summary.
|
|
15
15
|
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// never fail the hook.
|
|
25
|
-
//
|
|
26
|
-
// The candidate list (`unversionedCandidates`) and the command table
|
|
27
|
-
// (`installTable`) are injectable so tests never touch the network and
|
|
28
|
-
// never need real package managers. Defaults match S8 exactly. The `run`
|
|
29
|
-
// and `hasBinary` hooks are injectable for the same reason.
|
|
30
|
-
|
|
31
|
-
const codegraphSession = require('./codegraph-session.js');
|
|
32
|
-
|
|
33
|
-
const { spawnSync } = require('node:child_process');
|
|
34
|
-
const fs = require('node:fs');
|
|
35
|
-
const path = require('node:path');
|
|
36
|
-
const symlinksLib = require('./worktree-symlinks.js');
|
|
37
|
-
|
|
38
|
-
// Shared pure plan helpers (S63): the sync and async hook runners use the
|
|
39
|
-
// SAME detection/format/classification pipeline so the two implementations
|
|
40
|
-
// cannot drift. Detection stays sync lstat (constant-time, millisecond
|
|
41
|
-
// scale, never a 120s subprocess); only subprocesses and file copies go
|
|
42
|
-
// async in the async twin.
|
|
43
|
-
|
|
44
|
-
const DEFAULT_UNVERSIONED_FILES = [
|
|
45
|
-
'.env',
|
|
46
|
-
'.env.local',
|
|
47
|
-
'.env.development',
|
|
48
|
-
'.env.production',
|
|
49
|
-
'.env.test',
|
|
50
|
-
];
|
|
16
|
+
// The lists are fixed here and extend them from `<git-common-dir>/gentle-ai/worktree-hooks.json`,
|
|
17
|
+
// which is local to the clone and never versioned. That file extends the copy and link
|
|
18
|
+
// lists and enables setup for the repository; it carries no command, because a command
|
|
19
|
+
// read from a file must never run.
|
|
20
|
+
|
|
21
|
+
const { dirname, isAbsolute, join } = require('node:path');
|
|
22
|
+
|
|
23
|
+
const WORKTREE_HOOKS_FILENAME = 'worktree-hooks.json';
|
|
51
24
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
25
|
+
/** S20: the clone-local directory, inside the common directory Git never versions. */
|
|
26
|
+
const WORKTREE_LOCAL_DIRECTORY = 'gentle-ai';
|
|
27
|
+
|
|
28
|
+
/** S20: the clone-local file, inside the common directory Git never versions. */
|
|
29
|
+
function worktreeHooksFilePath(commonDir) {
|
|
30
|
+
return join(commonDir, WORKTREE_LOCAL_DIRECTORY, WORKTREE_HOOKS_FILENAME);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** S19: directories a package manager owns. No list can make one of these a link. */
|
|
34
|
+
const WORKTREE_NEVER_LINKED = [
|
|
35
|
+
'node_modules',
|
|
36
|
+
'vendor',
|
|
37
|
+
'.venv',
|
|
38
|
+
'target',
|
|
39
|
+
'deps',
|
|
40
|
+
'.pi/gentle-ai',
|
|
41
|
+
'.gentle-ai',
|
|
63
42
|
];
|
|
64
43
|
|
|
65
|
-
|
|
44
|
+
/** S19: build caches, linked only when main has them untracked. */
|
|
45
|
+
const WORKTREE_BUILD_CACHE_LINKS = ['.turbo', '.next/cache', '.parcel-cache', '.pytest_cache', '.ruff_cache'];
|
|
46
|
+
|
|
47
|
+
/** S19: local asset directories, linked only when main has them untracked. */
|
|
48
|
+
const WORKTREE_ASSET_LINKS = ['.agents', '.atl', '.claude', 'odd'];
|
|
66
49
|
|
|
67
|
-
|
|
68
|
-
|
|
50
|
+
/** S19: the untracked input family copied by default. */
|
|
51
|
+
const WORKTREE_INPUT_PREFIX = '.env';
|
|
52
|
+
|
|
53
|
+
const CONTROL_CHARACTER = /[\u0000-\u001f\u007f]/;
|
|
54
|
+
|
|
55
|
+
// ---- The local configuration (S20) ----
|
|
56
|
+
|
|
57
|
+
function emptyWorktreeHooksConfig() {
|
|
58
|
+
return { copy: [], link: [], setup: false };
|
|
69
59
|
}
|
|
70
60
|
|
|
71
|
-
|
|
72
|
-
|
|
61
|
+
/** One relative path, so no configured entry can reach outside the worktree. */
|
|
62
|
+
function isSafeRelativePath(value) {
|
|
63
|
+
if (typeof value !== 'string' || value.length === 0 || CONTROL_CHARACTER.test(value) || isAbsolute(value)) return false;
|
|
64
|
+
const segments = value.replaceAll('\\', '/').split('/');
|
|
65
|
+
return !segments.some((segment) => segment === '' || segment === '.' || segment === '..');
|
|
73
66
|
}
|
|
74
67
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
try {
|
|
78
|
-
fs.lstatSync(p);
|
|
79
|
-
return true;
|
|
80
|
-
} catch {
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
68
|
+
function relativePath(value) {
|
|
69
|
+
return value.replaceAll('\\', '/').replace(/\/+$/, '');
|
|
83
70
|
}
|
|
84
71
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
function defaultHasBinary(bin, env) {
|
|
88
|
-
if (typeof bin !== 'string' || bin === '') return false;
|
|
89
|
-
if (bin.includes(path.sep) || bin.includes('/')) {
|
|
90
|
-
try {
|
|
91
|
-
fs.accessSync(bin, fs.constants.X_OK);
|
|
92
|
-
return true;
|
|
93
|
-
} catch {
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
const rawPath = (env || {}).PATH || (env || {}).Path || '';
|
|
98
|
-
const dirs = String(rawPath).split(path.delimiter).filter((d) => d.length > 0);
|
|
99
|
-
for (const dir of dirs) {
|
|
100
|
-
try {
|
|
101
|
-
fs.accessSync(path.join(dir, bin), fs.constants.X_OK);
|
|
102
|
-
return true;
|
|
103
|
-
} catch {
|
|
104
|
-
// Not here; keep searching.
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return false;
|
|
72
|
+
function unique(values) {
|
|
73
|
+
return [...new Set(values)];
|
|
108
74
|
}
|
|
109
75
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
env: opts.env,
|
|
115
|
-
timeout: opts.timeoutMs,
|
|
116
|
-
encoding: 'utf8',
|
|
117
|
-
windowsHide: true,
|
|
118
|
-
});
|
|
119
|
-
if (result.error) {
|
|
120
|
-
return {
|
|
121
|
-
status: result.status ?? null,
|
|
122
|
-
stdout: result.stdout || '',
|
|
123
|
-
stderr: result.stderr || '',
|
|
124
|
-
error: String((result.error && result.error.message) || result.error),
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
return {
|
|
128
|
-
status: result.status,
|
|
129
|
-
stdout: result.stdout || '',
|
|
130
|
-
stderr: result.stderr || '',
|
|
131
|
-
error: null,
|
|
132
|
-
};
|
|
76
|
+
function configuredPaths(value) {
|
|
77
|
+
if (!Array.isArray(value)) return [];
|
|
78
|
+
const safe = value.filter((entry) => isSafeRelativePath(entry)).map((entry) => relativePath(entry));
|
|
79
|
+
return unique(safe);
|
|
133
80
|
}
|
|
134
81
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
// the copy rather than risk copying a tracked file.
|
|
155
|
-
if (res.status === null || res.status === undefined || res.error) return true;
|
|
156
|
-
return false;
|
|
82
|
+
/**
|
|
83
|
+
* S20: an absent or unreadable file keeps the defaults, because a bad local file must
|
|
84
|
+
* never break a create. Only the two lists and the boolean flag are read: a key naming
|
|
85
|
+
* a command reaches nothing, and neither does a `version` this code does not know.
|
|
86
|
+
*/
|
|
87
|
+
function parseWorktreeHooksConfig(text) {
|
|
88
|
+
const config = emptyWorktreeHooksConfig();
|
|
89
|
+
if (text === undefined) return config;
|
|
90
|
+
let value;
|
|
91
|
+
try {
|
|
92
|
+
value = JSON.parse(text);
|
|
93
|
+
} catch {
|
|
94
|
+
return config;
|
|
95
|
+
}
|
|
96
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) return config;
|
|
97
|
+
config.copy = configuredPaths(value.copy);
|
|
98
|
+
config.link = configuredPaths(value.link);
|
|
99
|
+
config.setup = value.setup === true;
|
|
100
|
+
return config;
|
|
157
101
|
}
|
|
158
102
|
|
|
159
|
-
// The
|
|
160
|
-
//
|
|
161
|
-
//
|
|
162
|
-
//
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
103
|
+
// ---- The seams ----
|
|
104
|
+
//
|
|
105
|
+
// Everything a hook may touch. Nothing else is importable from here, so a hook cannot
|
|
106
|
+
// open a file, spawn a process or read the environment on its own: `git` runs Git in
|
|
107
|
+
// `cwd` with the same shape the create flow already uses, `kind` answers from `lstat`
|
|
108
|
+
// what stands at a path, `readdir` lists a directory, `readText` reads the local S20
|
|
109
|
+
// file, `copyFile` is a byte copy, `mkdir` creates with parents, `symlink` creates one
|
|
110
|
+
// link of the given type and `platform` names the host.
|
|
111
|
+
|
|
112
|
+
// ---- The chain ----
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Runs the chain in order, one hook at a time, and never lets one stop the next. A hook
|
|
116
|
+
* that throws is recorded as failed with its own name, because a chain that reported
|
|
117
|
+
* nothing would read as a chain that did nothing.
|
|
118
|
+
*
|
|
119
|
+
* `input.mainRoot` is the canonical main worktree, which the main-relative steps (the
|
|
120
|
+
* submodules, the inputs, the links) read. `input.changesRoot` is the checkout whose
|
|
121
|
+
* uncommitted delta `copy_changes` copies — the one the caller stands in, which is not
|
|
122
|
+
* necessarily main — and falls back to `mainRoot` only when a caller names none.
|
|
123
|
+
*/
|
|
124
|
+
async function runWorktreeHooks(input) {
|
|
125
|
+
const config = parseWorktreeHooksConfig(await input.seams.readText(input.configPath));
|
|
126
|
+
const context = {
|
|
127
|
+
mainRoot: input.mainRoot,
|
|
128
|
+
changesRoot: input.changesRoot ?? input.mainRoot,
|
|
129
|
+
path: input.path,
|
|
130
|
+
odd: input.odd,
|
|
131
|
+
copyChanges: input.copyChanges === true,
|
|
132
|
+
config,
|
|
133
|
+
};
|
|
134
|
+
const results = [];
|
|
135
|
+
for (const hook of input.hooks ?? defaultWorktreeHooks()) {
|
|
136
|
+
try {
|
|
137
|
+
results.push(...(await hook.run(context, input.seams)));
|
|
138
|
+
} catch (error) {
|
|
139
|
+
results.push({ name: hook.name, status: 'failed', detail: reasonFor(error) });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const summary = worktreeHooksSummary(results);
|
|
143
|
+
return summary === undefined ? { results, config } : { results, config, summary };
|
|
172
144
|
}
|
|
173
145
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
146
|
+
/**
|
|
147
|
+
* S19 in order, with `copy_changes` (S18) between the inputs and the links so the setup
|
|
148
|
+
* chain installs against the copied manifests. Every caller runs this same list, so no
|
|
149
|
+
* caller reorders it.
|
|
150
|
+
*/
|
|
151
|
+
function defaultWorktreeHooks() {
|
|
152
|
+
return [submodulesHook, inputsHook, copyChangesHook, symlinksHook];
|
|
181
153
|
}
|
|
182
154
|
|
|
183
|
-
//
|
|
184
|
-
|
|
185
|
-
function
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (typeof bin !== 'string' || bin === '') return null;
|
|
189
|
-
return { bin, args: argv.slice(1) };
|
|
155
|
+
// ---- Shared Git questions ----
|
|
156
|
+
|
|
157
|
+
async function isTracked(seams, mainRoot, path) {
|
|
158
|
+
const tracked = await seams.git(mainRoot, ['ls-files', '--error-unmatch', '--', path]);
|
|
159
|
+
return tracked !== undefined && tracked.code === 0;
|
|
190
160
|
}
|
|
191
161
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
if (res && (res.stderr || res.error)) return String(res.stderr || res.error).trim();
|
|
196
|
-
const code = res && res.status !== null && res.status !== undefined ? res.status : 'non-zero';
|
|
197
|
-
return `exit ${code}`;
|
|
162
|
+
async function isIgnored(seams, mainRoot, path) {
|
|
163
|
+
const ignored = await seams.git(mainRoot, ['check-ignore', '--quiet', '--', path]);
|
|
164
|
+
return ignored !== undefined && ignored.code === 0;
|
|
198
165
|
}
|
|
199
166
|
|
|
200
|
-
|
|
201
|
-
function
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
return { status: 'failed', detail: `copied none; ${skipped.join('; ')}`, warning: message };
|
|
205
|
-
}
|
|
206
|
-
const warning = copyError
|
|
207
|
-
? `copy unversioned files partially failed: ${(copyError && copyError.message) || copyError}`
|
|
208
|
-
: null;
|
|
209
|
-
const detail = copied.length > 0
|
|
210
|
-
? `copied ${copied.join(', ')}${skipped.length > 0 ? `; skipped ${skipped.join('; ')}` : ''}`
|
|
211
|
-
: `copied none; ${skipped.join('; ') || 'nothing to copy'}`;
|
|
212
|
-
return { status: copied.length > 0 ? 'done' : 'skipped', detail, warning };
|
|
167
|
+
/** S19: the rule a reader can add to silence this path, only while Git really does not ignore it. */
|
|
168
|
+
async function gitignoreWarning(context, seams, path) {
|
|
169
|
+
if (await isIgnored(seams, context.mainRoot, path)) return undefined;
|
|
170
|
+
return `${path} is not ignored; add ${path} to .gitignore`;
|
|
213
171
|
}
|
|
214
172
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
// that were already correct, and `errors` the ones whose symlink failed — a
|
|
218
|
-
// failed link is reported, never silent, and never fails the creation.
|
|
219
|
-
function formatLinkOutcome(applied, error) {
|
|
220
|
-
if (!applied) {
|
|
221
|
-
const message = `link principal entries failed: ${(error && error.message) || error || 'unknown error'}`;
|
|
222
|
-
return { status: 'failed', detail: message, warning: message };
|
|
223
|
-
}
|
|
224
|
-
const created = applied.created || [];
|
|
225
|
-
const existing = applied.existing || [];
|
|
226
|
-
const skipped = applied.skipped || [];
|
|
227
|
-
const errors = applied.errors || [];
|
|
228
|
-
const parts = [];
|
|
229
|
-
if (created.length > 0) parts.push(`linked ${created.join(', ')}`);
|
|
230
|
-
if (existing.length > 0) parts.push(`already linked ${existing.join(', ')}`);
|
|
231
|
-
if (skipped.length > 0) parts.push(`skipped ${skipped.join('; ')}`);
|
|
232
|
-
const detail = parts.join('; ') || 'nothing to link';
|
|
233
|
-
if (errors.length === 0) {
|
|
234
|
-
return {
|
|
235
|
-
status: created.length > 0 ? 'done' : 'skipped',
|
|
236
|
-
detail,
|
|
237
|
-
warning: null,
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
const message = `link principal entries failed: ${errors.join('; ')}`;
|
|
241
|
-
return { status: 'failed', detail, warning: message };
|
|
173
|
+
function isNeverLinked(path) {
|
|
174
|
+
return WORKTREE_NEVER_LINKED.some((forbidden) => path === forbidden || path.startsWith(`${forbidden}/`));
|
|
242
175
|
}
|
|
243
176
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
// implementation, never two divergent kill paths. Fixed argv, never a
|
|
247
|
-
// shell string, never the main-thread blocker; bounded timeout with
|
|
248
|
-
// SIGTERM-then-SIGKILL escalation and owned-group reaping (POSIX), plus
|
|
249
|
-
// AbortSignal support. Forwards cwd/env/spawnFn/signal/timeoutMs/killGraceMs
|
|
250
|
-
// and resolves the same `{status, stdout, stderr, error}` shape.
|
|
251
|
-
function defaultRunAsync(bin, args, opts = {}) {
|
|
252
|
-
let gitAsync = null;
|
|
253
|
-
try {
|
|
254
|
-
gitAsync = require('./worktree-git.js').runArgvAsync;
|
|
255
|
-
} catch {
|
|
256
|
-
gitAsync = null;
|
|
257
|
-
}
|
|
258
|
-
if (typeof gitAsync === 'function') {
|
|
259
|
-
return gitAsync(bin, args, opts);
|
|
260
|
-
}
|
|
261
|
-
return Promise.resolve({
|
|
262
|
-
status: null,
|
|
263
|
-
stdout: '',
|
|
264
|
-
stderr: '',
|
|
265
|
-
error: 'async runner unavailable: lib/worktree-git.js could not be loaded',
|
|
266
|
-
});
|
|
177
|
+
function reasonFor(error) {
|
|
178
|
+
return error instanceof Error ? error.message : String(error);
|
|
267
179
|
}
|
|
268
180
|
|
|
269
|
-
//
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
181
|
+
// ---- S19 step 1: submodules ----
|
|
182
|
+
|
|
183
|
+
const submodulesHook = {
|
|
184
|
+
name: 'submodules',
|
|
185
|
+
async run(context, seams) {
|
|
186
|
+
if ((await seams.kind(join(context.mainRoot, '.gitmodules'))) === undefined) {
|
|
187
|
+
return [{ name: 'submodules', status: 'skipped', detail: 'no submodules' }];
|
|
188
|
+
}
|
|
189
|
+
const args = ['submodule', 'update', '--init', '--recursive'];
|
|
190
|
+
const updated = await seams.git(context.path, args);
|
|
191
|
+
if (updated === undefined || updated.code !== 0) {
|
|
192
|
+
const outcome = updated === undefined ? 'without a result' : updated.code;
|
|
193
|
+
return [{ name: 'submodules', status: 'failed', detail: `git ${args.join(' ')} exited ${outcome}` }];
|
|
194
|
+
}
|
|
195
|
+
return [{ name: 'submodules', status: 'applied' }];
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// ---- S19 step 2: the untracked inputs ----
|
|
200
|
+
|
|
201
|
+
const inputsHook = {
|
|
202
|
+
name: 'inputs',
|
|
203
|
+
async run(context, seams) {
|
|
204
|
+
const present = (await seams.readdir(context.mainRoot)).filter((name) => name.startsWith(WORKTREE_INPUT_PREFIX));
|
|
205
|
+
const candidates = unique([...present.sort(), ...context.config.copy]);
|
|
206
|
+
if (candidates.length === 0) {
|
|
207
|
+
return [{ name: 'inputs', status: 'skipped', detail: `no ${WORKTREE_INPUT_PREFIX} files in main` }];
|
|
208
|
+
}
|
|
209
|
+
const results = [];
|
|
210
|
+
for (const path of candidates) results.push(await copyInput(context, seams, path));
|
|
211
|
+
return results;
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
/** Copies one input by bytes. A directory, a link and a path already in the worktree are refused. */
|
|
216
|
+
async function copyInput(context, seams, path) {
|
|
217
|
+
const name = `copy ${path}`;
|
|
218
|
+
if (!isSafeRelativePath(path)) return { name, status: 'skipped', detail: `${path} is not a relative path` };
|
|
219
|
+
const source = join(context.mainRoot, path);
|
|
220
|
+
const destination = join(context.path, path);
|
|
221
|
+
const kind = await seams.kind(source);
|
|
222
|
+
if (kind === undefined) return { name, status: 'skipped', detail: `${path} not in main` };
|
|
223
|
+
if (await isTracked(seams, context.mainRoot, path)) return { name, status: 'skipped', detail: 'tracked' };
|
|
224
|
+
if (kind !== 'file') return { name, status: 'skipped', detail: `${path} is not a file` };
|
|
225
|
+
if ((await seams.kind(destination)) !== undefined) return { name, status: 'skipped', detail: `${path} already exists in the worktree` };
|
|
226
|
+
try {
|
|
227
|
+
await seams.mkdir(dirname(destination));
|
|
228
|
+
await seams.copyFile(source, destination);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
return { name, status: 'failed', detail: `could not copy ${path}: ${reasonFor(error)}` };
|
|
231
|
+
}
|
|
232
|
+
const warning = await gitignoreWarning(context, seams, path);
|
|
233
|
+
return warning === undefined ? { name, status: 'applied' } : { name, status: 'applied', warning };
|
|
278
234
|
}
|
|
279
235
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
236
|
+
// ---- S18: the uncommitted changes ----
|
|
237
|
+
//
|
|
238
|
+
// The delta belongs to the checkout the create was asked from (`context.changesRoot`),
|
|
239
|
+
// never to the canonical main worktree: a session standing in another checkout copies
|
|
240
|
+
// its own line of work, exactly as the panel's path-selection transfer does. The patch
|
|
241
|
+
// is still applied in the new worktree, so the source keeps every change it had.
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The patch file Git writes for this step. It lives in the worktree's administrative
|
|
245
|
+
* directory — never in the worktree, so a copied change is the only thing a reader sees
|
|
246
|
+
* — and a fixed name makes it deterministic and overwritten instead of accumulating.
|
|
247
|
+
*/
|
|
248
|
+
const COPY_CHANGES_PATCH_FILENAME = 'gentle-ai-copy-changes.patch';
|
|
249
|
+
|
|
250
|
+
const copyChangesHook = {
|
|
251
|
+
name: 'copy_changes',
|
|
252
|
+
async run(context, seams) {
|
|
253
|
+
// S18: omitted or false copies nothing and prints nothing, so the step neither
|
|
254
|
+
// applies, skips nor fails — it never happened.
|
|
255
|
+
if (!context.copyChanges) return [];
|
|
256
|
+
const changed = await gitPaths(seams, context.changesRoot, ['diff', '--name-only', '--no-renames', '-z', 'HEAD']);
|
|
257
|
+
const untracked = await gitPaths(seams, context.changesRoot, ['ls-files', '--others', '--exclude-standard', '-z']);
|
|
258
|
+
if (changed === undefined || untracked === undefined) {
|
|
259
|
+
return [{ name: 'copy_changes', status: 'failed', detail: 'could not read the uncommitted changes' }];
|
|
260
|
+
}
|
|
261
|
+
if (changed.length === 0 && untracked.length === 0) {
|
|
262
|
+
return [
|
|
263
|
+
{
|
|
264
|
+
name: 'copy_changes',
|
|
265
|
+
status: 'skipped',
|
|
266
|
+
detail: 'no uncommitted changes',
|
|
267
|
+
copyChanges: { copied: 0, failed: [] },
|
|
268
|
+
},
|
|
269
|
+
];
|
|
270
|
+
}
|
|
271
|
+
const patchPath = await copyChangesPatchPath(context, seams);
|
|
272
|
+
if (patchPath === undefined) {
|
|
273
|
+
return [
|
|
274
|
+
{
|
|
275
|
+
name: 'copy_changes',
|
|
276
|
+
status: 'failed',
|
|
277
|
+
detail: "could not read the worktree's administrative directory",
|
|
278
|
+
copyChanges: { copied: 0, failed: [...changed, ...untracked] },
|
|
279
|
+
},
|
|
280
|
+
];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
let copied = 0;
|
|
284
|
+
const failedTracked = [];
|
|
285
|
+
const failedUntracked = [];
|
|
286
|
+
for (const path of changed) {
|
|
287
|
+
if (await applyChangedFile(context, seams, patchPath, path)) copied += 1;
|
|
288
|
+
else failedTracked.push(path);
|
|
289
|
+
}
|
|
290
|
+
for (const path of untracked) {
|
|
291
|
+
const outcome = await copyUntrackedFile(context, seams, path);
|
|
292
|
+
if (outcome === 'copied') copied += 1;
|
|
293
|
+
else if (outcome === 'failed') failedUntracked.push(path);
|
|
294
|
+
}
|
|
295
|
+
// S18: a conflicting patch leaves the worktree exactly as Git created it; the
|
|
296
|
+
// failing paths are reported instead of aborting the creation.
|
|
297
|
+
if (failedTracked.length > 0) await discardFailedChanges(context, seams, failedTracked);
|
|
298
|
+
const failed = [...failedTracked, ...failedUntracked];
|
|
299
|
+
if (failed.length > 0) {
|
|
300
|
+
return [
|
|
301
|
+
{
|
|
302
|
+
name: 'copy_changes',
|
|
303
|
+
status: 'failed',
|
|
304
|
+
detail: `could not copy: ${failed.join(', ')}`,
|
|
305
|
+
copyChanges: { copied, failed },
|
|
306
|
+
},
|
|
307
|
+
];
|
|
308
|
+
}
|
|
309
|
+
return [{ name: 'copy_changes', status: 'applied', copyChanges: { copied, failed: [] } }];
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
/** One Git query whose answer is a NUL-separated path list, or nothing when it failed. */
|
|
314
|
+
async function gitPaths(seams, cwd, args) {
|
|
315
|
+
const result = await seams.git(cwd, args);
|
|
316
|
+
if (result === undefined || result.code !== 0) return undefined;
|
|
317
|
+
return result.stdout.split('\0').filter((path) => path.length > 0);
|
|
283
318
|
}
|
|
284
319
|
|
|
285
|
-
|
|
286
|
-
async function
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
return true;
|
|
292
|
-
}
|
|
293
|
-
if (!res) return true;
|
|
294
|
-
if (res.status === 0) return true;
|
|
295
|
-
if (res.status === null || res.status === undefined || res.error) return true;
|
|
296
|
-
return false;
|
|
320
|
+
/** Git writes the patch, so no hook ever handles its bytes or its encoding. */
|
|
321
|
+
async function copyChangesPatchPath(context, seams) {
|
|
322
|
+
const gitDir = await seams.git(context.path, ['rev-parse', '--path-format=absolute', '--git-dir']);
|
|
323
|
+
if (gitDir === undefined || gitDir.code !== 0) return undefined;
|
|
324
|
+
const path = gitDir.stdout.trim();
|
|
325
|
+
return path.length === 0 ? undefined : join(path, COPY_CHANGES_PATCH_FILENAME);
|
|
297
326
|
}
|
|
298
327
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
principalRoot, worktreePath, env, timeoutMs, runAsync: runWithDefaults,
|
|
411
|
-
});
|
|
412
|
-
} catch (err) {
|
|
413
|
-
linkError = err;
|
|
414
|
-
}
|
|
415
|
-
const outcome = formatLinkOutcome(applied, linkError);
|
|
416
|
-
if (outcome.warning) warnings.push(outcome.warning);
|
|
417
|
-
addStep('link-principal-entries', outcome.status, outcome.detail);
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
// (b) Dependency install by detected stack (shared detection pipeline) and
|
|
421
|
-
// (c) `codegraph init` when the CLI is available. These two are the SLOW
|
|
422
|
-
// half — a package manager can run for minutes — so they are one unit that
|
|
423
|
-
// the interactive create can defer instead of awaiting.
|
|
424
|
-
const runSetupPhases = async () => {
|
|
425
|
-
const setupSteps = [];
|
|
426
|
-
const setupWarnings = [];
|
|
427
|
-
let setupFailed = false;
|
|
428
|
-
const addSetupStep = (name, status, detail) => {
|
|
429
|
-
setupSteps.push({ name, status, detail: String(detail || '') });
|
|
430
|
-
if (status === 'failed') setupFailed = true;
|
|
431
|
-
};
|
|
432
|
-
|
|
433
|
-
{
|
|
434
|
-
const detected = selectInstallEntries(worktreePath, table);
|
|
435
|
-
if (detected.length === 0) {
|
|
436
|
-
addSetupStep('install', 'skipped', 'no lockfile detected');
|
|
437
|
-
}
|
|
438
|
-
for (const entry of detected) {
|
|
439
|
-
const name = `install:${entry.key}`;
|
|
440
|
-
const parsed = installEntryArgv(entry);
|
|
441
|
-
if (!parsed) {
|
|
442
|
-
addSetupStep(name, 'skipped', 'no command configured');
|
|
443
|
-
continue;
|
|
444
|
-
}
|
|
445
|
-
const { bin, args } = parsed;
|
|
446
|
-
if (!hasBinary(bin)) {
|
|
447
|
-
addSetupStep(name, 'skipped', `${bin} not on PATH`);
|
|
448
|
-
continue;
|
|
449
|
-
}
|
|
450
|
-
let res;
|
|
451
|
-
try {
|
|
452
|
-
res = await runWithDefaults(bin, args, { cwd: worktreePath, timeoutMs, env });
|
|
453
|
-
} catch (err) {
|
|
454
|
-
res = { status: null, stdout: '', stderr: '', error: String((err && err.message) || err) };
|
|
455
|
-
}
|
|
456
|
-
if (res && res.status === 0) {
|
|
457
|
-
addSetupStep(name, 'done', `${bin} ${args.join(' ')} exited 0`);
|
|
458
|
-
continue;
|
|
459
|
-
}
|
|
460
|
-
if (Array.isArray(entry.fallbackArgv) && entry.fallbackArgv.length > 0) {
|
|
461
|
-
const fbBin = entry.fallbackArgv[0];
|
|
462
|
-
const fbArgs = entry.fallbackArgv.slice(1);
|
|
463
|
-
if (!hasBinary(fbBin)) {
|
|
464
|
-
const message = `${name}: ${bin} ${args.join(' ')} failed and fallback ${fbBin} not on PATH`;
|
|
465
|
-
setupWarnings.push(message);
|
|
466
|
-
addSetupStep(name, 'failed', message);
|
|
467
|
-
continue;
|
|
468
|
-
}
|
|
469
|
-
let fb;
|
|
470
|
-
try {
|
|
471
|
-
fb = await runWithDefaults(fbBin, fbArgs, { cwd: worktreePath, timeoutMs, env });
|
|
472
|
-
} catch (err) {
|
|
473
|
-
fb = { status: null, stdout: '', stderr: '', error: String((err && err.message) || err) };
|
|
474
|
-
}
|
|
475
|
-
if (fb && fb.status === 0) {
|
|
476
|
-
addSetupStep(name, 'done', `${bin} ${args.join(' ')} failed, ${fbBin} ${fbArgs.join(' ')} exited 0`);
|
|
477
|
-
continue;
|
|
478
|
-
}
|
|
479
|
-
const message = `${name}: ${bin} ${args.join(' ')} failed (${((res && (res.stderr || res.error)) || 'exit non-zero').trim()}), `
|
|
480
|
-
+ `fallback ${fbBin} ${fbArgs.join(' ')} failed (${((fb && (fb.stderr || fb.error)) || 'exit non-zero').trim()})`;
|
|
481
|
-
setupWarnings.push(message);
|
|
482
|
-
addSetupStep(name, 'failed', message);
|
|
483
|
-
continue;
|
|
484
|
-
}
|
|
485
|
-
const cause = runCause(res);
|
|
486
|
-
const message = `${name}: ${bin} ${args.join(' ')} failed (${cause})`;
|
|
487
|
-
setupWarnings.push(message);
|
|
488
|
-
addSetupStep(name, 'failed', message);
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
{
|
|
493
|
-
const codegraph = codegraphDecision(opts, env, hasBinary);
|
|
494
|
-
if (!codegraph.available) {
|
|
495
|
-
addSetupStep('codegraph-init', 'skipped', codegraph.detail);
|
|
496
|
-
} else {
|
|
497
|
-
let res;
|
|
498
|
-
try {
|
|
499
|
-
res = await runWithDefaults('codegraph', ['init'], { cwd: worktreePath, timeoutMs, env });
|
|
500
|
-
} catch (err) {
|
|
501
|
-
res = { status: null, stdout: '', stderr: '', error: String((err && err.message) || err) };
|
|
502
|
-
}
|
|
503
|
-
if (res && res.status === 0) {
|
|
504
|
-
addSetupStep('codegraph-init', 'done', 'codegraph init exited 0');
|
|
505
|
-
} else {
|
|
506
|
-
const cause = runCause(res);
|
|
507
|
-
const message = `codegraph-init: codegraph init failed (${cause})`;
|
|
508
|
-
setupWarnings.push(message);
|
|
509
|
-
addSetupStep('codegraph-init', 'failed', message);
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
return { ok: !setupFailed, steps: setupSteps, warnings: setupWarnings };
|
|
515
|
-
};
|
|
516
|
-
|
|
517
|
-
// Deferred setup: the caller wants the checkout usable NOW and the slow
|
|
518
|
-
// phases later. Nothing is skipped — the phases travel as ONE callable that
|
|
519
|
-
// runs them exactly once (a second call returns the same promise, so a
|
|
520
|
-
// retry can never install twice) and never rejects: a thrown phase becomes a
|
|
521
|
-
// failed step, so a deferred caller can always report an outcome.
|
|
522
|
-
let setupPromise = null;
|
|
523
|
-
const runSetupOnce = () => {
|
|
524
|
-
if (setupPromise === null) {
|
|
525
|
-
setupPromise = (async () => {
|
|
526
|
-
try {
|
|
527
|
-
return await runSetupPhases();
|
|
528
|
-
} catch (err) {
|
|
529
|
-
const message = `creation setup failed: ${(err && err.message) || err}`;
|
|
530
|
-
return { ok: false, steps: [{ name: 'setup', status: 'failed', detail: message }], warnings: [message] };
|
|
531
|
-
}
|
|
532
|
-
})();
|
|
533
|
-
}
|
|
534
|
-
return setupPromise;
|
|
535
|
-
};
|
|
536
|
-
|
|
537
|
-
if (opts.deferSetup === true) {
|
|
538
|
-
// The steps the deferred half WILL report are named as pending, so the
|
|
539
|
-
// caller's own report is honest about what has not happened yet instead of
|
|
540
|
-
// claiming a skipped or a completed setup.
|
|
541
|
-
addStep('install', 'pending', 'setup runs in the background');
|
|
542
|
-
addStep('codegraph-init', 'pending', 'setup runs in the background');
|
|
543
|
-
return { ok: !failed, steps, warnings, deferred: true, setup: runSetupOnce };
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
const setupOutcome = await runSetupOnce();
|
|
547
|
-
for (const step of setupOutcome.steps) steps.push(step);
|
|
548
|
-
for (const warning of setupOutcome.warnings) warnings.push(warning);
|
|
549
|
-
if (!setupOutcome.ok) failed = true;
|
|
550
|
-
|
|
551
|
-
return { ok: !failed, steps, warnings };
|
|
328
|
+
/** S18: one tracked path, patched from the asking checkout and applied with `--3way` in the worktree. */
|
|
329
|
+
async function applyChangedFile(context, seams, patchPath, path) {
|
|
330
|
+
const patch = await seams.git(context.changesRoot, ['diff', '--binary', '--no-renames', `--output=${patchPath}`, 'HEAD', '--', path]);
|
|
331
|
+
if (patch === undefined || patch.code !== 0) return false;
|
|
332
|
+
const applied = await seams.git(context.path, ['apply', '--3way', patchPath]);
|
|
333
|
+
return applied !== undefined && applied.code === 0;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** A byte copy; a path another hook already carried over is left as that hook wrote it. */
|
|
337
|
+
async function copyUntrackedFile(context, seams, path) {
|
|
338
|
+
if (!isSafeRelativePath(path)) return 'failed';
|
|
339
|
+
const destination = join(context.path, path);
|
|
340
|
+
if ((await seams.kind(destination)) !== undefined) return 'present';
|
|
341
|
+
try {
|
|
342
|
+
await seams.mkdir(dirname(destination));
|
|
343
|
+
await seams.copyFile(join(context.changesRoot, path), destination);
|
|
344
|
+
return 'copied';
|
|
345
|
+
} catch {
|
|
346
|
+
return 'failed';
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* S18: `--3way` implies `--index`, so a conflict leaves unmerged entries and conflict
|
|
352
|
+
* markers behind. The index is reset to the worktree's own HEAD and the failing paths
|
|
353
|
+
* are written back from it; both are plumbing over a checkout this hook never owned,
|
|
354
|
+
* so the worktree is usable and clean again.
|
|
355
|
+
*/
|
|
356
|
+
async function discardFailedChanges(context, seams, failed) {
|
|
357
|
+
await seams.git(context.path, ['read-tree', 'HEAD']);
|
|
358
|
+
await seams.git(context.path, ['checkout-index', '-f', '--', ...failed]);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ---- S19 step 4: the build caches and the local asset directories ----
|
|
362
|
+
|
|
363
|
+
const symlinksHook = {
|
|
364
|
+
name: 'symlinks',
|
|
365
|
+
async run(context, seams) {
|
|
366
|
+
const results = [];
|
|
367
|
+
const candidates = unique([...WORKTREE_BUILD_CACHE_LINKS, ...WORKTREE_ASSET_LINKS, ...context.config.link]);
|
|
368
|
+
for (const path of candidates) results.push(await linkPath(context, seams, path));
|
|
369
|
+
return results;
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Links one directory from main. A refused link is retried as a junction, which Windows
|
|
375
|
+
* grants without the developer mode, and a second refusal is reported: the failure is
|
|
376
|
+
* never replaced by a copy that would silently duplicate a cache.
|
|
377
|
+
*/
|
|
378
|
+
async function linkPath(context, seams, path) {
|
|
379
|
+
const name = `symlink ${path}`;
|
|
380
|
+
if (!isSafeRelativePath(path)) return { name, status: 'skipped', detail: `${path} is not a relative path` };
|
|
381
|
+
const relative = relativePath(path);
|
|
382
|
+
if (isNeverLinked(relative)) return { name, status: 'skipped', detail: `${relative} is never linked` };
|
|
383
|
+
const target = join(context.mainRoot, relative);
|
|
384
|
+
if ((await seams.kind(target)) === undefined) return { name, status: 'skipped', detail: `${relative} not in main` };
|
|
385
|
+
if (await isTracked(seams, context.mainRoot, relative)) return { name, status: 'skipped', detail: 'tracked' };
|
|
386
|
+
const link = join(context.path, relative);
|
|
387
|
+
if ((await seams.kind(link)) !== undefined) return { name, status: 'skipped', detail: `${relative} already exists in the worktree` };
|
|
388
|
+
|
|
389
|
+
const attempts = seams.platform === 'win32' ? ['junction', 'dir'] : ['dir', 'junction'];
|
|
390
|
+
let firstFailure;
|
|
391
|
+
for (const type of attempts) {
|
|
392
|
+
try {
|
|
393
|
+
await seams.mkdir(dirname(link));
|
|
394
|
+
await seams.symlink(target, link, type);
|
|
395
|
+
const warning = await gitignoreWarning(context, seams, relative);
|
|
396
|
+
const applied = { name, status: 'applied' };
|
|
397
|
+
if (type === 'junction') applied.detail = 'junction';
|
|
398
|
+
if (warning !== undefined) applied.warning = warning;
|
|
399
|
+
return applied;
|
|
400
|
+
} catch (error) {
|
|
401
|
+
if (firstFailure === undefined) firstFailure = reasonFor(error);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return { name, status: 'failed', detail: `could not link ${relative}: ${firstFailure ?? 'no link type was attempted'}` };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// ---- S16 and S7: what the output prints and the record stores ----
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* S16: the one `hooks:` line. It summarises the counts, names everything that failed and
|
|
411
|
+
* carries the `.gitignore` advice, and it is absent when nothing applied and nothing
|
|
412
|
+
* failed, because an empty line would claim a chain that never ran.
|
|
413
|
+
*/
|
|
414
|
+
function worktreeHooksSummary(results) {
|
|
415
|
+
const applied = results.filter((result) => result.status === 'applied').length;
|
|
416
|
+
const skipped = results.filter((result) => result.status === 'skipped').length;
|
|
417
|
+
const failed = results.filter((result) => result.status === 'failed');
|
|
418
|
+
if (applied === 0 && failed.length === 0) return undefined;
|
|
419
|
+
const parts = [];
|
|
420
|
+
if (applied > 0) parts.push(`${applied} applied`);
|
|
421
|
+
if (skipped > 0) parts.push(`${skipped} skipped`);
|
|
422
|
+
if (failed.length > 0) parts.push(`${failed.length} failed: ${failed.map((result) => `${result.name} (${result.detail ?? 'no reason'})`).join(', ')}`);
|
|
423
|
+
const warnings = results.map((result) => result.warning).filter((warning) => warning !== undefined);
|
|
424
|
+
if (warnings.length > 0) parts.push(`warning: ${warnings.join('; ')}`);
|
|
425
|
+
return `hooks: ${parts.join(', ')}`;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** S18: the one dedicated line, present only when the request passed copy_changes. */
|
|
429
|
+
function worktreeCopyChangesLine(results) {
|
|
430
|
+
const result = results.find((entry) => entry.name === 'copy_changes');
|
|
431
|
+
if (result === undefined) return undefined;
|
|
432
|
+
const outcome = result.copyChanges;
|
|
433
|
+
if (outcome === undefined) return `copy_changes: ${result.detail ?? 'could not copy the uncommitted changes'}`;
|
|
434
|
+
if (outcome.failed.length > 0) {
|
|
435
|
+
return `copy_changes: ${outcome.copied} changes copied; failed: ${outcome.failed.join(', ')}`;
|
|
436
|
+
}
|
|
437
|
+
if (outcome.copied === 0) return 'copy_changes: no uncommitted changes to copy';
|
|
438
|
+
return `copy_changes: ${outcome.copied} changes copied`;
|
|
552
439
|
}
|
|
553
440
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
if (typeof worktreePath !== 'string' || worktreePath === '') {
|
|
561
|
-
throw new TypeError('runCreationHooks requires worktreePath');
|
|
562
|
-
}
|
|
563
|
-
const env = opts.env || process.env;
|
|
564
|
-
const timeoutMs = opts.timeoutMs || DEFAULT_TIMEOUT_MS;
|
|
565
|
-
const candidates = Array.isArray(opts.unversionedCandidates)
|
|
566
|
-
? opts.unversionedCandidates
|
|
567
|
-
: DEFAULT_UNVERSIONED_FILES;
|
|
568
|
-
const table = Array.isArray(opts.installTable) ? opts.installTable : DEFAULT_INSTALL_TABLE;
|
|
569
|
-
const run = typeof opts.run === 'function'
|
|
570
|
-
? (bin, args, o) => opts.run(bin, args, o)
|
|
571
|
-
: (bin, args, o) => defaultRun(bin, args, { ...o, env });
|
|
572
|
-
const hasBinary = typeof opts.hasBinary === 'function'
|
|
573
|
-
? (bin) => opts.hasBinary(bin, env)
|
|
574
|
-
: (bin) => defaultHasBinary(bin, env);
|
|
575
|
-
|
|
576
|
-
const steps = [];
|
|
577
|
-
const warnings = [];
|
|
578
|
-
let failed = false;
|
|
579
|
-
const addStep = (name, status, detail) => {
|
|
580
|
-
steps.push({ name, status, detail: String(detail || '') });
|
|
581
|
-
if (status === 'failed') failed = true;
|
|
582
|
-
};
|
|
583
|
-
|
|
584
|
-
// (a) Copy unversioned files. Each candidate is copied from the
|
|
585
|
-
// principal checkout ONLY when it exists there, is NOT tracked by git,
|
|
586
|
-
// and does NOT already exist in the worktree. Never overwrite, never
|
|
587
|
-
// copy a tracked file, never fail for a missing source.
|
|
588
|
-
{
|
|
589
|
-
const copied = [];
|
|
590
|
-
const skipped = [];
|
|
591
|
-
let copyError = null;
|
|
592
|
-
for (const candidate of candidates) {
|
|
593
|
-
const src = path.join(principalRoot, candidate);
|
|
594
|
-
const dest = path.join(worktreePath, candidate);
|
|
595
|
-
if (!pathExists(src)) {
|
|
596
|
-
skipped.push(`${candidate}: missing source`);
|
|
597
|
-
continue;
|
|
598
|
-
}
|
|
599
|
-
if (pathExists(dest)) {
|
|
600
|
-
skipped.push(`${candidate}: target exists`);
|
|
601
|
-
continue;
|
|
602
|
-
}
|
|
603
|
-
if (isTrackedByGit(run, principalRoot, candidate, timeoutMs, env)) {
|
|
604
|
-
skipped.push(`${candidate}: tracked`);
|
|
605
|
-
continue;
|
|
606
|
-
}
|
|
607
|
-
try {
|
|
608
|
-
fs.copyFileSync(src, dest);
|
|
609
|
-
copied.push(candidate);
|
|
610
|
-
} catch (err) {
|
|
611
|
-
copyError = err;
|
|
612
|
-
skipped.push(`${candidate}: copy failed`);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
if (copyError && copied.length === 0) {
|
|
616
|
-
// A copy error with nothing copied is a failed step plus a warning,
|
|
617
|
-
// but never aborts the remaining hooks.
|
|
618
|
-
const message = `copy unversioned files failed: ${(copyError && copyError.message) || copyError}`;
|
|
619
|
-
warnings.push(message);
|
|
620
|
-
addStep('copy-unversioned', 'failed', `copied none; ${skipped.join('; ')}`);
|
|
621
|
-
} else {
|
|
622
|
-
if (copyError) warnings.push(`copy unversioned files partially failed: ${(copyError && copyError.message) || copyError}`);
|
|
623
|
-
addStep(
|
|
624
|
-
'copy-unversioned',
|
|
625
|
-
copied.length > 0 ? 'done' : 'skipped',
|
|
626
|
-
copied.length > 0
|
|
627
|
-
? `copied ${copied.join(', ')}${skipped.length > 0 ? `; skipped ${skipped.join('; ')}` : ''}`
|
|
628
|
-
: `copied none; ${skipped.join('; ') || 'nothing to copy'}`,
|
|
629
|
-
);
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
// (a2) Link git-ignored principal entries (round-3 S3/L4). One shared
|
|
634
|
-
// policy for both creation routes: `.agents`, `.pi`, `.claude` and `odd`
|
|
635
|
-
// become symlinks to the principal ONLY when git ignores them there. A
|
|
636
|
-
// tracked entry arrives with the checkout, an untracked-but-not-ignored
|
|
637
|
-
// one is not eligible, and an occupied target is preserved — this step
|
|
638
|
-
// never copies, never deletes and never aborts the creation.
|
|
639
|
-
{
|
|
640
|
-
let applied = null;
|
|
641
|
-
let linkError = null;
|
|
642
|
-
try {
|
|
643
|
-
applied = symlinksLib.applyWorktreeSymlinks({
|
|
644
|
-
principalRoot, worktreePath, env, timeoutMs, run,
|
|
645
|
-
});
|
|
646
|
-
} catch (err) {
|
|
647
|
-
linkError = err;
|
|
648
|
-
}
|
|
649
|
-
const outcome = formatLinkOutcome(applied, linkError);
|
|
650
|
-
if (outcome.warning) warnings.push(outcome.warning);
|
|
651
|
-
addStep('link-principal-entries', outcome.status, outcome.detail);
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
// (b) Dependency install by detected stack. Every detected stack runs in
|
|
655
|
-
// table order. A missing binary is skipped; a non-zero exit is failed
|
|
656
|
-
// plus a warning and never aborts the remaining steps. `npm ci` falls
|
|
657
|
-
// back to `npm install` only when `ci` fails.
|
|
658
|
-
{
|
|
659
|
-
const detected = table.filter((entry) => Array.isArray(entry.markers)
|
|
660
|
-
&& entry.markers.some((marker) => pathExists(path.join(worktreePath, marker))));
|
|
661
|
-
if (detected.length === 0) {
|
|
662
|
-
addStep('install', 'skipped', 'no lockfile detected');
|
|
663
|
-
}
|
|
664
|
-
for (const entry of detected) {
|
|
665
|
-
const name = `install:${entry.key}`;
|
|
666
|
-
const argv = entry.argv || [];
|
|
667
|
-
const bin = argv[0];
|
|
668
|
-
const args = argv.slice(1);
|
|
669
|
-
if (typeof bin !== 'string' || bin === '') {
|
|
670
|
-
addStep(name, 'skipped', 'no command configured');
|
|
671
|
-
continue;
|
|
672
|
-
}
|
|
673
|
-
if (!hasBinary(bin)) {
|
|
674
|
-
addStep(name, 'skipped', `${bin} not on PATH`);
|
|
675
|
-
continue;
|
|
676
|
-
}
|
|
677
|
-
let res;
|
|
678
|
-
try {
|
|
679
|
-
res = run(bin, args, { cwd: worktreePath, timeoutMs, env });
|
|
680
|
-
} catch (err) {
|
|
681
|
-
res = { status: null, stdout: '', stderr: '', error: String((err && err.message) || err) };
|
|
682
|
-
}
|
|
683
|
-
if (res && res.status === 0) {
|
|
684
|
-
addStep(name, 'done', `${bin} ${args.join(' ')} exited 0`);
|
|
685
|
-
continue;
|
|
686
|
-
}
|
|
687
|
-
// Non-zero: npm alone gets exactly one fallback to `npm install`.
|
|
688
|
-
if (Array.isArray(entry.fallbackArgv) && entry.fallbackArgv.length > 0) {
|
|
689
|
-
const fbBin = entry.fallbackArgv[0];
|
|
690
|
-
const fbArgs = entry.fallbackArgv.slice(1);
|
|
691
|
-
if (!hasBinary(fbBin)) {
|
|
692
|
-
const message = `${name}: ${bin} ${args.join(' ')} failed and fallback ${fbBin} not on PATH`;
|
|
693
|
-
warnings.push(message);
|
|
694
|
-
addStep(name, 'failed', message);
|
|
695
|
-
continue;
|
|
696
|
-
}
|
|
697
|
-
let fb;
|
|
698
|
-
try {
|
|
699
|
-
fb = run(fbBin, fbArgs, { cwd: worktreePath, timeoutMs, env });
|
|
700
|
-
} catch (err) {
|
|
701
|
-
fb = { status: null, stdout: '', stderr: '', error: String((err && err.message) || err) };
|
|
702
|
-
}
|
|
703
|
-
if (fb && fb.status === 0) {
|
|
704
|
-
addStep(name, 'done', `${bin} ${args.join(' ')} failed, ${fbBin} ${fbArgs.join(' ')} exited 0`);
|
|
705
|
-
continue;
|
|
706
|
-
}
|
|
707
|
-
const message = `${name}: ${bin} ${args.join(' ')} failed (${((res && (res.stderr || res.error)) || 'exit non-zero').trim()}), `
|
|
708
|
-
+ `fallback ${fbBin} ${fbArgs.join(' ')} failed (${((fb && (fb.stderr || fb.error)) || 'exit non-zero').trim()})`;
|
|
709
|
-
warnings.push(message);
|
|
710
|
-
addStep(name, 'failed', message);
|
|
711
|
-
continue;
|
|
712
|
-
}
|
|
713
|
-
const cause = res && (res.stderr || res.error)
|
|
714
|
-
? String(res.stderr || res.error).trim()
|
|
715
|
-
: `exit ${res && res.status !== null && res.status !== undefined ? res.status : 'non-zero'}`;
|
|
716
|
-
const message = `${name}: ${bin} ${args.join(' ')} failed (${cause})`;
|
|
717
|
-
warnings.push(message);
|
|
718
|
-
addStep(name, 'failed', message);
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
// (c) codegraph init when the CLI is available. Skipped with a reason
|
|
723
|
-
// when absent; a non-zero exit is failed plus a warning. Never fatal.
|
|
724
|
-
{
|
|
725
|
-
const codegraph = codegraphDecision(opts, env, hasBinary);
|
|
726
|
-
if (!codegraph.available) {
|
|
727
|
-
addStep('codegraph-init', 'skipped', codegraph.detail);
|
|
728
|
-
} else {
|
|
729
|
-
let res;
|
|
730
|
-
try {
|
|
731
|
-
res = run('codegraph', ['init'], { cwd: worktreePath, timeoutMs, env });
|
|
732
|
-
} catch (err) {
|
|
733
|
-
res = { status: null, stdout: '', stderr: '', error: String((err && err.message) || err) };
|
|
734
|
-
}
|
|
735
|
-
if (res && res.status === 0) {
|
|
736
|
-
addStep('codegraph-init', 'done', 'codegraph init exited 0');
|
|
737
|
-
} else {
|
|
738
|
-
const cause = res && (res.stderr || res.error)
|
|
739
|
-
? String(res.stderr || res.error).trim()
|
|
740
|
-
: `exit ${res && res.status !== null && res.status !== undefined ? res.status : 'non-zero'}`;
|
|
741
|
-
const message = `codegraph-init: codegraph init failed (${cause})`;
|
|
742
|
-
warnings.push(message);
|
|
743
|
-
addStep('codegraph-init', 'failed', message);
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
return { ok: !failed, steps, warnings };
|
|
441
|
+
/** S7: the outcome as the record stores it, with the advice folded into its detail. */
|
|
442
|
+
function worktreeHookRecords(results) {
|
|
443
|
+
return results.map((result) => {
|
|
444
|
+
const detail = [result.detail, result.warning].filter((part) => part !== undefined).join('; ');
|
|
445
|
+
return detail.length === 0 ? { name: result.name, status: result.status } : { name: result.name, status: result.status, detail };
|
|
446
|
+
});
|
|
749
447
|
}
|
|
750
448
|
|
|
751
449
|
module.exports = {
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
450
|
+
WORKTREE_HOOKS_FILENAME,
|
|
451
|
+
worktreeHooksFilePath,
|
|
452
|
+
WORKTREE_NEVER_LINKED,
|
|
453
|
+
WORKTREE_BUILD_CACHE_LINKS,
|
|
454
|
+
WORKTREE_ASSET_LINKS,
|
|
455
|
+
WORKTREE_INPUT_PREFIX,
|
|
456
|
+
emptyWorktreeHooksConfig,
|
|
457
|
+
parseWorktreeHooksConfig,
|
|
458
|
+
runWorktreeHooks,
|
|
459
|
+
defaultWorktreeHooks,
|
|
460
|
+
submodulesHook,
|
|
461
|
+
inputsHook,
|
|
462
|
+
copyChangesHook,
|
|
463
|
+
symlinksHook,
|
|
464
|
+
worktreeHooksSummary,
|
|
465
|
+
worktreeCopyChangesLine,
|
|
466
|
+
worktreeHookRecords,
|
|
757
467
|
};
|