@narumitw/pi-worktree 0.51.7 → 0.51.8
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 +19 -57
- package/dist/index.ts +52 -226
- package/dist/index.ts.map +2 -2
- package/package.json +49 -50
- package/src/command.ts +605 -693
- package/src/git.ts +635 -731
- package/src/session.ts +76 -84
- package/src/settings.ts +211 -223
- package/src/status.ts +287 -309
- package/src/worktree.ts +26 -33
package/src/command.ts
CHANGED
|
@@ -2,32 +2,32 @@ import { existsSync } from "node:fs";
|
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
3
|
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
5
|
+
addWorktree,
|
|
6
|
+
administrativeHistoryOids,
|
|
7
|
+
administrativePruneCandidates,
|
|
8
|
+
currentWorktreePath,
|
|
9
|
+
defaultWorktreePath,
|
|
10
|
+
durableRefExists,
|
|
11
|
+
durableRefsContaining,
|
|
12
|
+
formatWorktree,
|
|
13
|
+
listWorktrees,
|
|
14
|
+
localBranchExists,
|
|
15
|
+
pathEntryExists,
|
|
16
|
+
pathIdentity,
|
|
17
|
+
pathsEqual,
|
|
18
|
+
prunePreview,
|
|
19
|
+
pruneWorktrees,
|
|
20
|
+
removeWorktree,
|
|
21
|
+
resolveCommit,
|
|
22
|
+
sameWorktreeIdentity,
|
|
23
|
+
stripTerminalControls,
|
|
24
|
+
symbolicBranch,
|
|
25
|
+
unresolvableSymlinkAncestor,
|
|
26
|
+
validateBranch,
|
|
27
|
+
type WorktreeRecord,
|
|
28
|
+
worktreeAdministrativeDirectory,
|
|
29
|
+
worktreeForBranch,
|
|
30
|
+
worktreeInventory,
|
|
31
31
|
} from "./git.js";
|
|
32
32
|
import { switchToWorktree } from "./session.js";
|
|
33
33
|
import type { WorktreeSettingsRuntime } from "./settings.js";
|
|
@@ -40,771 +40,683 @@ const ACTION_REMOVE = "Remove worktree";
|
|
|
40
40
|
const ACTION_PRUNE = "Prune stale metadata";
|
|
41
41
|
const ACTION_CONFIGURE_ROOT = "Configure worktree root";
|
|
42
42
|
const ACTIONS = {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
status: ACTION_STATUS,
|
|
44
|
+
add: ACTION_ADD,
|
|
45
|
+
switch: ACTION_SWITCH,
|
|
46
|
+
remove: ACTION_REMOVE,
|
|
47
|
+
prune: ACTION_PRUNE,
|
|
48
|
+
configure: ACTION_CONFIGURE_ROOT,
|
|
49
49
|
} as const;
|
|
50
50
|
|
|
51
51
|
interface WorktreeMenuState {
|
|
52
|
-
|
|
52
|
+
statusCards: WorktreeStatusCard[];
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
interface WorktreeMenuOwner {
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
signal: AbortSignal;
|
|
57
|
+
isCurrent(): boolean;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
interface AdministrativeHistoryRisk {
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
label: string;
|
|
62
|
+
oids: string[];
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
interface AddBaseProvenance {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
kind: "existing-local-branch" | "current-branch" | "explicit-commit-ish";
|
|
67
|
+
label: string;
|
|
68
|
+
oid: string;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
export function registerWorktreeCommand(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
pi: ExtensionAPI,
|
|
73
|
+
settings: WorktreeSettingsRuntime,
|
|
74
|
+
getMenuOwner: () => WorktreeMenuOwner,
|
|
75
75
|
): void {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
});
|
|
167
|
-
await runMenu(ctx, menu, {
|
|
168
|
-
getState: () => state,
|
|
169
|
-
signal: owner.signal,
|
|
170
|
-
isCurrent: owner.isCurrent,
|
|
171
|
-
});
|
|
172
|
-
} catch (error) {
|
|
173
|
-
safeNotify(ctx, formatError(error), "error");
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
|
-
});
|
|
76
|
+
pi.registerCommand("worktree", {
|
|
77
|
+
description: "Interactively manage Git worktrees and their default root",
|
|
78
|
+
handler: async (args, ctx) => {
|
|
79
|
+
if (args.trim()) {
|
|
80
|
+
safeNotify(ctx, "/worktree does not accept arguments; run it without arguments to open the menu.", "warning");
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (!ctx.hasUI) {
|
|
84
|
+
safeNotify(ctx, "/worktree requires TUI or RPC mode.", "error");
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
await ctx.waitForIdle();
|
|
90
|
+
const records = await listWorktrees(pi, ctx.cwd, ctx.signal);
|
|
91
|
+
const currentPath = await currentWorktreePath(pi, ctx.cwd, ctx.signal);
|
|
92
|
+
const root = settings.get();
|
|
93
|
+
const warning = root.warning ? " — settings warning" : "";
|
|
94
|
+
const owner = getMenuOwner();
|
|
95
|
+
const { defineMenu, runMenu } = await import("@narumitw/pi-tui-kit");
|
|
96
|
+
if (owner.signal.aborted || !owner.isCurrent()) return;
|
|
97
|
+
const runFlow = async (flow: () => Promise<void>) => {
|
|
98
|
+
try {
|
|
99
|
+
await flow();
|
|
100
|
+
} catch (error) {
|
|
101
|
+
safeNotify(ctx, formatError(error), "error");
|
|
102
|
+
}
|
|
103
|
+
return { kind: "close" } as const;
|
|
104
|
+
};
|
|
105
|
+
const state: WorktreeMenuState = { statusCards: [] };
|
|
106
|
+
type Screen = "main" | "status";
|
|
107
|
+
type Action = keyof typeof ACTIONS;
|
|
108
|
+
const menu = defineMenu<WorktreeMenuState, Screen, Action, ExtensionCommandContext>({
|
|
109
|
+
start: "main",
|
|
110
|
+
screens: {
|
|
111
|
+
main: () => ({
|
|
112
|
+
kind: "actions",
|
|
113
|
+
title: "Git worktrees",
|
|
114
|
+
lines: [
|
|
115
|
+
`Registered: ${records.length}`,
|
|
116
|
+
`Current: ${currentPath}`,
|
|
117
|
+
`Worktree root: ${root.effectiveRoot} (${root.source})${warning}`,
|
|
118
|
+
],
|
|
119
|
+
items: Object.entries(ACTIONS).map(([id, label]) => ({
|
|
120
|
+
id,
|
|
121
|
+
label,
|
|
122
|
+
action: id as Action,
|
|
123
|
+
busyLabel: id === "status" ? "Inspecting worktrees…" : undefined,
|
|
124
|
+
})),
|
|
125
|
+
hint: "close",
|
|
126
|
+
}),
|
|
127
|
+
status: ({ state: currentState }) => ({
|
|
128
|
+
kind: "browse",
|
|
129
|
+
title: "Worktree status",
|
|
130
|
+
lines: ["Local Git snapshot; no fetch performed."],
|
|
131
|
+
items: currentState.statusCards,
|
|
132
|
+
viewportSize: "adaptive",
|
|
133
|
+
hint: "back",
|
|
134
|
+
}),
|
|
135
|
+
},
|
|
136
|
+
actions: {
|
|
137
|
+
status: async ({ signal }) => {
|
|
138
|
+
try {
|
|
139
|
+
const statusRecords = await listWorktrees(pi, ctx.cwd, signal);
|
|
140
|
+
state.statusCards = await loadWorktreeStatusCards(pi, statusRecords, currentPath, signal);
|
|
141
|
+
if (signal.aborted || !owner.isCurrent()) return { kind: "close" };
|
|
142
|
+
return { kind: "to", screen: "status" };
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (signal.aborted || !owner.isCurrent()) return { kind: "close" };
|
|
145
|
+
safeNotify(ctx, formatError(error), "error");
|
|
146
|
+
return { kind: "stay" };
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
add: async () => runFlow(() => addFlow(pi, ctx, records, root.effectiveRoot)),
|
|
150
|
+
switch: async ({ signal }) => runFlow(() => switchFlow(pi, ctx, records, currentPath, signal)),
|
|
151
|
+
remove: async ({ signal }) => runFlow(() => removeFlow(pi, ctx, records, currentPath, signal)),
|
|
152
|
+
prune: async () => runFlow(() => pruneFlow(pi, ctx, records)),
|
|
153
|
+
configure: async () => runFlow(() => configureRootFlow(ctx, settings)),
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
await runMenu(ctx, menu, {
|
|
157
|
+
getState: () => state,
|
|
158
|
+
signal: owner.signal,
|
|
159
|
+
isCurrent: owner.isCurrent,
|
|
160
|
+
});
|
|
161
|
+
} catch (error) {
|
|
162
|
+
safeNotify(ctx, formatError(error), "error");
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
});
|
|
177
166
|
}
|
|
178
167
|
|
|
179
|
-
async function configureRootFlow(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
)
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
configuredRoot === undefined
|
|
199
|
-
? `Worktree root reset to ${updated.effectiveRoot}.`
|
|
200
|
-
: `Worktree root saved as ${updated.effectiveRoot}.`,
|
|
201
|
-
"info",
|
|
202
|
-
);
|
|
168
|
+
async function configureRootFlow(ctx: ExtensionCommandContext, settings: WorktreeSettingsRuntime): Promise<void> {
|
|
169
|
+
const current = await settings.reload();
|
|
170
|
+
if (!current.canSave) {
|
|
171
|
+
throw new Error(current.warning ?? `Fix ${settings.getPath()} before changing pi-worktree settings.`);
|
|
172
|
+
}
|
|
173
|
+
const requested = await ctx.ui.input(
|
|
174
|
+
"Worktree root (blank restores ~/.worktrees)",
|
|
175
|
+
stripTerminalControls(current.configuredRoot ?? current.effectiveRoot),
|
|
176
|
+
);
|
|
177
|
+
if (requested === undefined) return;
|
|
178
|
+
const configuredRoot = requested.trim() || undefined;
|
|
179
|
+
const updated = await settings.save(configuredRoot);
|
|
180
|
+
safeNotify(
|
|
181
|
+
ctx,
|
|
182
|
+
configuredRoot === undefined
|
|
183
|
+
? `Worktree root reset to ${updated.effectiveRoot}.`
|
|
184
|
+
: `Worktree root saved as ${updated.effectiveRoot}.`,
|
|
185
|
+
"info",
|
|
186
|
+
);
|
|
203
187
|
}
|
|
204
188
|
|
|
205
189
|
async function addFlow(
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
190
|
+
pi: ExtensionAPI,
|
|
191
|
+
ctx: ExtensionCommandContext,
|
|
192
|
+
records: readonly WorktreeRecord[],
|
|
193
|
+
worktreeRoot: string,
|
|
210
194
|
): Promise<void> {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
-
if (
|
|
326
|
-
await ctx.ui.confirm(
|
|
327
|
-
"Switch Pi workspace?",
|
|
328
|
-
stripTerminalControls(`Continue this conversation in ${targetPath}?`),
|
|
329
|
-
)
|
|
330
|
-
) {
|
|
331
|
-
const latest = await revalidateWorktreeIdentity(pi, ctx, created);
|
|
332
|
-
if (latest.prunableReason !== undefined || !existsSync(latest.path)) {
|
|
333
|
-
throw new Error("The newly created worktree became unavailable; select it again.");
|
|
334
|
-
}
|
|
335
|
-
await switchToWorktree(ctx, latest.path);
|
|
336
|
-
}
|
|
195
|
+
const main = records[0];
|
|
196
|
+
if (!main) throw new Error("Git returned no registered worktrees.");
|
|
197
|
+
if (main.bare) {
|
|
198
|
+
throw new Error("The main worktree is bare; pi-worktree cannot derive a safe default path.");
|
|
199
|
+
}
|
|
200
|
+
if (!existsSync(main.path)) {
|
|
201
|
+
throw new Error(`The registered main worktree path is stale: ${main.path}. Repair it with Git first.`);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const requestedBranch = await ctx.ui.input("Branch for the new worktree", "feat/my-change");
|
|
205
|
+
if (requestedBranch === undefined) return;
|
|
206
|
+
const branchInput = requestedBranch.trim();
|
|
207
|
+
if (!branchInput) throw new Error("Branch name is required.");
|
|
208
|
+
const branch = await validateBranch(pi, ctx.cwd, branchInput, ctx.signal);
|
|
209
|
+
const branchExists = await localBranchExists(pi, ctx.cwd, branch, ctx.signal);
|
|
210
|
+
const occupied = worktreeForBranch(records, branch);
|
|
211
|
+
if (occupied) {
|
|
212
|
+
throw new Error(`Branch ${branch} is already checked out at ${occupied.path}.`);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
let startOid: string | undefined;
|
|
216
|
+
let provenance: AddBaseProvenance;
|
|
217
|
+
if (branchExists) {
|
|
218
|
+
provenance = {
|
|
219
|
+
kind: "existing-local-branch",
|
|
220
|
+
label: branch,
|
|
221
|
+
oid: await resolveCommit(pi, ctx.cwd, `refs/heads/${branch}`, ctx.signal),
|
|
222
|
+
};
|
|
223
|
+
} else {
|
|
224
|
+
const defaultStart = await symbolicBranch(pi, ctx.cwd, ctx.signal);
|
|
225
|
+
const requestedStart = await ctx.ui.input(
|
|
226
|
+
stripTerminalControls(
|
|
227
|
+
defaultStart
|
|
228
|
+
? `Start point for ${branch} (blank uses ${defaultStart})`
|
|
229
|
+
: `Start point for ${branch} (required because HEAD is detached)`,
|
|
230
|
+
),
|
|
231
|
+
stripTerminalControls(defaultStart ?? "commit-ish"),
|
|
232
|
+
);
|
|
233
|
+
if (requestedStart === undefined) return;
|
|
234
|
+
const explicitStart = requestedStart.trim();
|
|
235
|
+
const startLabel = explicitStart || defaultStart;
|
|
236
|
+
if (!startLabel) throw new Error("An explicit start point is required from detached HEAD.");
|
|
237
|
+
startOid = await resolveCommit(pi, ctx.cwd, startLabel, ctx.signal);
|
|
238
|
+
provenance = {
|
|
239
|
+
kind: explicitStart ? "explicit-commit-ish" : "current-branch",
|
|
240
|
+
label: startLabel,
|
|
241
|
+
oid: startOid,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const suggestedPath = defaultWorktreePath(main.path, branch, worktreeRoot);
|
|
246
|
+
const requestedPath = await ctx.ui.input(
|
|
247
|
+
stripTerminalControls(`Worktree path (blank uses ${suggestedPath})`),
|
|
248
|
+
stripTerminalControls(suggestedPath),
|
|
249
|
+
);
|
|
250
|
+
if (requestedPath === undefined) return;
|
|
251
|
+
const targetPath = pathIdentity(requestedPath.trim() ? resolve(ctx.cwd, requestedPath.trim()) : suggestedPath);
|
|
252
|
+
assertTargetFilesystemAvailable(targetPath);
|
|
253
|
+
const pathCollision = records.find((record) => pathsEqual(record.path, targetPath));
|
|
254
|
+
if (pathCollision) {
|
|
255
|
+
throw new Error(`The target path is already registered as a worktree: ${pathCollision.path}.`);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const summary = formatAddPreview(branch, branchExists, provenance, targetPath);
|
|
259
|
+
if (!(await ctx.ui.confirm("Create Git worktree", summary))) return;
|
|
260
|
+
|
|
261
|
+
assertTargetFilesystemAvailable(targetPath);
|
|
262
|
+
const latestRecords = await listWorktrees(pi, ctx.cwd, ctx.signal);
|
|
263
|
+
const latestOccupied = worktreeForBranch(latestRecords, branch);
|
|
264
|
+
if (latestOccupied) {
|
|
265
|
+
throw new Error(`Branch ${branch} is now checked out at ${latestOccupied.path}; select it again.`);
|
|
266
|
+
}
|
|
267
|
+
const latestPathCollision = latestRecords.find((record) => pathsEqual(record.path, targetPath));
|
|
268
|
+
if (latestPathCollision) {
|
|
269
|
+
throw new Error(`The target path is now registered as a worktree: ${latestPathCollision.path}. Select it again.`);
|
|
270
|
+
}
|
|
271
|
+
const branchStillExists = await localBranchExists(pi, ctx.cwd, branch, ctx.signal);
|
|
272
|
+
if (branchStillExists !== branchExists) {
|
|
273
|
+
throw new Error(`Branch ${branch} changed after confirmation; select it again.`);
|
|
274
|
+
}
|
|
275
|
+
if (branchExists) {
|
|
276
|
+
const latestOid = await resolveCommit(pi, ctx.cwd, `refs/heads/${branch}`, ctx.signal);
|
|
277
|
+
if (latestOid !== provenance.oid) {
|
|
278
|
+
throw new Error(`Branch ${branch} moved after confirmation; select it again.`);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
assertTargetFilesystemAvailable(targetPath);
|
|
283
|
+
await addWorktree(pi, ctx.cwd, { path: targetPath, branch, startOid }, ctx.signal);
|
|
284
|
+
let created: WorktreeRecord;
|
|
285
|
+
try {
|
|
286
|
+
const updated = await listWorktrees(pi, ctx.cwd, ctx.signal);
|
|
287
|
+
const verified = updated.find((record) => pathsEqual(record.path, targetPath));
|
|
288
|
+
if (!verified || verified.branch !== branch || verified.head !== provenance.oid) {
|
|
289
|
+
throw new Error("the expected path, branch, and approved HEAD were not present in Git porcelain output");
|
|
290
|
+
}
|
|
291
|
+
created = verified;
|
|
292
|
+
} catch (error) {
|
|
293
|
+
throw new Error(
|
|
294
|
+
`Git add completed, so the worktree was retained at ${targetPath}, but verification failed: ${formatError(error)}. Inspect git worktree list before retrying.`,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
safeNotify(ctx, `Created worktree ${targetPath} on branch ${branch}.`, "info");
|
|
298
|
+
|
|
299
|
+
if (
|
|
300
|
+
await ctx.ui.confirm("Switch Pi workspace?", stripTerminalControls(`Continue this conversation in ${targetPath}?`))
|
|
301
|
+
) {
|
|
302
|
+
const latest = await revalidateWorktreeIdentity(pi, ctx, created);
|
|
303
|
+
if (latest.prunableReason !== undefined || !existsSync(latest.path)) {
|
|
304
|
+
throw new Error("The newly created worktree became unavailable; select it again.");
|
|
305
|
+
}
|
|
306
|
+
await switchToWorktree(ctx, latest.path);
|
|
307
|
+
}
|
|
337
308
|
}
|
|
338
309
|
|
|
339
310
|
function formatAddPreview(
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
311
|
+
branch: string,
|
|
312
|
+
branchExists: boolean,
|
|
313
|
+
provenance: AddBaseProvenance,
|
|
314
|
+
targetPath: string,
|
|
344
315
|
): string {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
316
|
+
const base =
|
|
317
|
+
provenance.kind === "existing-local-branch"
|
|
318
|
+
? `existing local branch ${quoteTerminalValue(provenance.label)}`
|
|
319
|
+
: provenance.kind === "current-branch"
|
|
320
|
+
? `current branch ${quoteTerminalValue(provenance.label)}`
|
|
321
|
+
: `explicit commit-ish ${quoteTerminalValue(provenance.label)}`;
|
|
322
|
+
return [
|
|
323
|
+
`Branch: ${quoteTerminalValue(branch)} (${branchExists ? "existing" : "new"} local branch)`,
|
|
324
|
+
`Base: ${base}`,
|
|
325
|
+
`Base commit: ${provenance.oid}`,
|
|
326
|
+
`Path: ${quoteTerminalValue(targetPath)}`,
|
|
327
|
+
].join("; ");
|
|
357
328
|
}
|
|
358
329
|
|
|
359
330
|
function quoteTerminalValue(value: string): string {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
331
|
+
let quoted = "";
|
|
332
|
+
for (const character of value) {
|
|
333
|
+
const code = character.codePointAt(0) ?? 0;
|
|
334
|
+
if (code <= 0x1f || (code >= 0x7f && code <= 0x9f)) {
|
|
335
|
+
quoted += `\\u${code.toString(16).padStart(4, "0")}`;
|
|
336
|
+
} else if (character === "\\" || character === '"') {
|
|
337
|
+
quoted += `\\${character}`;
|
|
338
|
+
} else {
|
|
339
|
+
quoted += character;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return `"${quoted}"`;
|
|
372
343
|
}
|
|
373
344
|
|
|
374
345
|
function assertTargetFilesystemAvailable(targetPath: string): void {
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
);
|
|
383
|
-
}
|
|
346
|
+
if (pathEntryExists(targetPath)) {
|
|
347
|
+
throw new Error(`The target path already exists: ${targetPath}.`);
|
|
348
|
+
}
|
|
349
|
+
const unsafeAncestor = unresolvableSymlinkAncestor(targetPath);
|
|
350
|
+
if (unsafeAncestor) {
|
|
351
|
+
throw new Error(`The target path has an unresolvable symbolic-link ancestor: ${unsafeAncestor}.`);
|
|
352
|
+
}
|
|
384
353
|
}
|
|
385
354
|
|
|
386
355
|
async function switchFlow(
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
356
|
+
pi: ExtensionAPI,
|
|
357
|
+
ctx: ExtensionCommandContext,
|
|
358
|
+
records: readonly WorktreeRecord[],
|
|
359
|
+
currentPath: string,
|
|
360
|
+
signal?: AbortSignal,
|
|
392
361
|
): Promise<void> {
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
362
|
+
const candidates = records.filter(
|
|
363
|
+
(record) =>
|
|
364
|
+
!record.bare &&
|
|
365
|
+
record.prunableReason === undefined &&
|
|
366
|
+
existsSync(record.path) &&
|
|
367
|
+
!pathsEqual(record.path, currentPath),
|
|
368
|
+
);
|
|
369
|
+
const selected = await selectWorktree(ctx, "Switch to worktree", candidates, currentPath, signal);
|
|
370
|
+
if (!selected) return;
|
|
371
|
+
const latest = await revalidateWorktreeIdentity(pi, ctx, selected);
|
|
372
|
+
if (
|
|
373
|
+
latest.bare ||
|
|
374
|
+
latest.prunableReason !== undefined ||
|
|
375
|
+
!existsSync(latest.path) ||
|
|
376
|
+
pathsEqual(latest.path, currentPath)
|
|
377
|
+
) {
|
|
378
|
+
throw new Error("The selected worktree changed state; select it again.");
|
|
379
|
+
}
|
|
380
|
+
await switchToWorktree(ctx, latest.path);
|
|
412
381
|
}
|
|
413
382
|
|
|
414
383
|
async function removeFlow(
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
384
|
+
pi: ExtensionAPI,
|
|
385
|
+
ctx: ExtensionCommandContext,
|
|
386
|
+
records: readonly WorktreeRecord[],
|
|
387
|
+
currentPath: string,
|
|
388
|
+
signal?: AbortSignal,
|
|
420
389
|
): Promise<void> {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
if (latestInventory.protected.length > 0) {
|
|
501
|
-
throw new Error(
|
|
502
|
-
`Removal refused because new protected local data appeared after confirmation:\n${latestInventory.protected.join("\n")}`,
|
|
503
|
-
);
|
|
504
|
-
}
|
|
505
|
-
if (!sameInventory(inventory.ignored, latestInventory.ignored)) {
|
|
506
|
-
throw new Error(
|
|
507
|
-
`Removal refused because ignored data changed after confirmation:\n${latestInventory.ignored.join("\n") || "(none)"}`,
|
|
508
|
-
);
|
|
509
|
-
}
|
|
510
|
-
await assertDetachedHeadIsDurable(pi, ctx, latest);
|
|
511
|
-
await assertAdministrativeHistoryUnchanged(
|
|
512
|
-
pi,
|
|
513
|
-
ctx,
|
|
514
|
-
latest.path,
|
|
515
|
-
administrativePath,
|
|
516
|
-
approvedHistoryRisks,
|
|
517
|
-
);
|
|
518
|
-
await removeWorktree(pi, ctx.cwd, latest.path, ctx.signal);
|
|
519
|
-
const updated = await listWorktrees(pi, ctx.cwd, ctx.signal);
|
|
520
|
-
if (updated.some((record) => pathsEqual(record.path, selected.path))) {
|
|
521
|
-
throw new Error(`Git remove returned success, but ${selected.path} is still registered.`);
|
|
522
|
-
}
|
|
523
|
-
safeNotify(ctx, `Removed worktree ${selected.path}. Its branch was preserved.`, "info");
|
|
390
|
+
const candidates = records.filter(
|
|
391
|
+
(record) => !record.isMain && !record.bare && !pathsEqual(record.path, currentPath),
|
|
392
|
+
);
|
|
393
|
+
const selected = await selectWorktree(ctx, "Remove linked worktree", candidates, currentPath, signal);
|
|
394
|
+
if (!selected) return;
|
|
395
|
+
if (selected.lockedReason !== undefined) {
|
|
396
|
+
throw new Error(
|
|
397
|
+
`Worktree is locked${selected.lockedReason ? `: ${selected.lockedReason}` : "."} Unlock it explicitly with Git before removal.`,
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
if (selected.prunableReason !== undefined || !existsSync(selected.path)) {
|
|
401
|
+
throw new Error("The selected worktree path is stale. Use prune instead of remove.");
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const inventory = classifyRemovalInventory(await worktreeInventory(pi, selected.path, ctx.signal));
|
|
405
|
+
if (inventory.protected.length > 0) {
|
|
406
|
+
throw new Error(
|
|
407
|
+
`Removal refused because ${selected.path} contains tracked, untracked, index-flagged, or submodule data:\n${inventory.protected.join("\n")}`,
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
await assertDetachedHeadIsDurable(pi, ctx, selected);
|
|
411
|
+
const administrativePath = await worktreeAdministrativeDirectory(pi, selected.path, ctx.signal);
|
|
412
|
+
const approvedHistoryRisks = historyRisks(
|
|
413
|
+
selected.path,
|
|
414
|
+
await unreachableAdministrativeHistoryOids(pi, ctx, administrativePath),
|
|
415
|
+
);
|
|
416
|
+
const recoveryWarning = formatAdministrativeRecoveryWarning(approvedHistoryRisks);
|
|
417
|
+
const ignoredWarning = formatIgnoredDataWarning(inventory.ignored);
|
|
418
|
+
const removalWarning =
|
|
419
|
+
ignoredWarning && recoveryWarning
|
|
420
|
+
? `${ignoredWarning}\n${recoveryWarning.trimStart()}`
|
|
421
|
+
: `${ignoredWarning}${recoveryWarning}`;
|
|
422
|
+
const confirmationTitle =
|
|
423
|
+
inventory.ignored.length > 0
|
|
424
|
+
? recoveryWarning
|
|
425
|
+
? "Remove worktree and discard local/recovery data"
|
|
426
|
+
: "Remove worktree and delete ignored files"
|
|
427
|
+
: recoveryWarning
|
|
428
|
+
? "Remove worktree and discard recovery history"
|
|
429
|
+
: "Remove Git worktree";
|
|
430
|
+
if (
|
|
431
|
+
!(await ctx.ui.confirm(
|
|
432
|
+
confirmationTitle,
|
|
433
|
+
`Delete the worktree directory ${stripTerminalControls(selected.path)}? The branch will be preserved.${removalWarning}`,
|
|
434
|
+
))
|
|
435
|
+
) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
await assertAdministrativeHistoryUnchanged(pi, ctx, selected.path, administrativePath, approvedHistoryRisks);
|
|
440
|
+
|
|
441
|
+
const beforeRemoval = await listWorktrees(pi, ctx.cwd, ctx.signal);
|
|
442
|
+
const latest = beforeRemoval.find((record) => pathsEqual(record.path, selected.path));
|
|
443
|
+
if (!latest) throw new Error(`Worktree ${selected.path} is no longer registered.`);
|
|
444
|
+
if (!sameWorktreeIdentity(selected, latest)) {
|
|
445
|
+
throw new Error(`Worktree ${selected.path} changed identity; select it again.`);
|
|
446
|
+
}
|
|
447
|
+
if (latest.isMain || latest.lockedReason !== undefined || latest.prunableReason !== undefined) {
|
|
448
|
+
throw new Error(`Worktree ${selected.path} changed state after confirmation; removal was refused.`);
|
|
449
|
+
}
|
|
450
|
+
const latestInventory = classifyRemovalInventory(await worktreeInventory(pi, latest.path, ctx.signal));
|
|
451
|
+
if (latestInventory.protected.length > 0) {
|
|
452
|
+
throw new Error(
|
|
453
|
+
`Removal refused because new protected local data appeared after confirmation:\n${latestInventory.protected.join("\n")}`,
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
if (!sameInventory(inventory.ignored, latestInventory.ignored)) {
|
|
457
|
+
throw new Error(
|
|
458
|
+
`Removal refused because ignored data changed after confirmation:\n${latestInventory.ignored.join("\n") || "(none)"}`,
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
await assertDetachedHeadIsDurable(pi, ctx, latest);
|
|
462
|
+
await assertAdministrativeHistoryUnchanged(pi, ctx, latest.path, administrativePath, approvedHistoryRisks);
|
|
463
|
+
await removeWorktree(pi, ctx.cwd, latest.path, ctx.signal);
|
|
464
|
+
const updated = await listWorktrees(pi, ctx.cwd, ctx.signal);
|
|
465
|
+
if (updated.some((record) => pathsEqual(record.path, selected.path))) {
|
|
466
|
+
throw new Error(`Git remove returned success, but ${selected.path} is still registered.`);
|
|
467
|
+
}
|
|
468
|
+
safeNotify(ctx, `Removed worktree ${selected.path}. Its branch was preserved.`, "info");
|
|
524
469
|
}
|
|
525
470
|
|
|
526
471
|
async function pruneFlow(
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
472
|
+
pi: ExtensionAPI,
|
|
473
|
+
ctx: ExtensionCommandContext,
|
|
474
|
+
records: readonly WorktreeRecord[],
|
|
530
475
|
): Promise<void> {
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
const finalHistoryRisks = await inspectAdministrativePruneCandidates(pi, ctx);
|
|
567
|
-
if (
|
|
568
|
-
latestPreview !== preview ||
|
|
569
|
-
!sameAdministrativeHistoryRisks(approvedHistoryRisks, finalHistoryRisks)
|
|
570
|
-
) {
|
|
571
|
-
throw new Error("Stale worktree metadata changed after confirmation; run prune again.");
|
|
572
|
-
}
|
|
573
|
-
const output = await pruneWorktrees(pi, ctx.cwd, ctx.signal);
|
|
574
|
-
safeNotify(
|
|
575
|
-
ctx,
|
|
576
|
-
output ? `Pruned stale worktree metadata:\n${output}` : "Pruned stale worktree metadata.",
|
|
577
|
-
"info",
|
|
578
|
-
);
|
|
476
|
+
for (const record of records.filter((candidate) => candidate.prunableReason !== undefined && candidate.detached)) {
|
|
477
|
+
await assertDetachedHeadIsDurable(pi, ctx, record);
|
|
478
|
+
}
|
|
479
|
+
const preview = await prunePreview(pi, ctx.cwd, ctx.signal);
|
|
480
|
+
if (!preview) {
|
|
481
|
+
ctx.ui.notify("Git found no stale worktree metadata to prune.", "info");
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const approvedHistoryRisks = await inspectAdministrativePruneCandidates(pi, ctx);
|
|
485
|
+
const safePreview = stripTerminalControls(preview);
|
|
486
|
+
const recoveryWarning = formatAdministrativeRecoveryWarning(approvedHistoryRisks);
|
|
487
|
+
ctx.ui.notify(`git worktree prune --dry-run --verbose\n${safePreview}`, "warning");
|
|
488
|
+
if (
|
|
489
|
+
!(await ctx.ui.confirm(
|
|
490
|
+
recoveryWarning ? "Prune metadata and discard recovery history" : "Prune stale worktree metadata",
|
|
491
|
+
stripTerminalControls(`${safePreview}${recoveryWarning}`),
|
|
492
|
+
))
|
|
493
|
+
) {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
const latest = await listWorktrees(pi, ctx.cwd, ctx.signal);
|
|
497
|
+
for (const record of latest.filter((candidate) => candidate.prunableReason !== undefined && candidate.detached)) {
|
|
498
|
+
await assertDetachedHeadIsDurable(pi, ctx, record);
|
|
499
|
+
}
|
|
500
|
+
const beforePreviewHistoryRisks = await inspectAdministrativePruneCandidates(pi, ctx);
|
|
501
|
+
if (!sameAdministrativeHistoryRisks(approvedHistoryRisks, beforePreviewHistoryRisks)) {
|
|
502
|
+
throw new Error("Stale worktree metadata changed after confirmation; run prune again.");
|
|
503
|
+
}
|
|
504
|
+
const latestPreview = await prunePreview(pi, ctx.cwd, ctx.signal);
|
|
505
|
+
const finalHistoryRisks = await inspectAdministrativePruneCandidates(pi, ctx);
|
|
506
|
+
if (latestPreview !== preview || !sameAdministrativeHistoryRisks(approvedHistoryRisks, finalHistoryRisks)) {
|
|
507
|
+
throw new Error("Stale worktree metadata changed after confirmation; run prune again.");
|
|
508
|
+
}
|
|
509
|
+
const output = await pruneWorktrees(pi, ctx.cwd, ctx.signal);
|
|
510
|
+
safeNotify(ctx, output ? `Pruned stale worktree metadata:\n${output}` : "Pruned stale worktree metadata.", "info");
|
|
579
511
|
}
|
|
580
512
|
|
|
581
513
|
async function assertAdministrativeHistoryUnchanged(
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
514
|
+
pi: ExtensionAPI,
|
|
515
|
+
ctx: ExtensionCommandContext,
|
|
516
|
+
selectedPath: string,
|
|
517
|
+
approvedAdministrativePath: string,
|
|
518
|
+
approvedHistoryRisks: readonly AdministrativeHistoryRisk[],
|
|
587
519
|
): Promise<void> {
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
throw new Error(
|
|
602
|
-
`Worktree ${selectedPath} administrative recovery history changed after confirmation; select it again.`,
|
|
603
|
-
);
|
|
604
|
-
}
|
|
520
|
+
const latestAdministrativePath = await worktreeAdministrativeDirectory(pi, selectedPath, ctx.signal);
|
|
521
|
+
const latestHistoryRisks = historyRisks(
|
|
522
|
+
selectedPath,
|
|
523
|
+
await unreachableAdministrativeHistoryOids(pi, ctx, latestAdministrativePath),
|
|
524
|
+
);
|
|
525
|
+
if (
|
|
526
|
+
!pathsEqual(approvedAdministrativePath, latestAdministrativePath) ||
|
|
527
|
+
!sameAdministrativeHistoryRisks(approvedHistoryRisks, latestHistoryRisks)
|
|
528
|
+
) {
|
|
529
|
+
throw new Error(
|
|
530
|
+
`Worktree ${selectedPath} administrative recovery history changed after confirmation; select it again.`,
|
|
531
|
+
);
|
|
532
|
+
}
|
|
605
533
|
}
|
|
606
534
|
|
|
607
535
|
async function inspectAdministrativePruneCandidates(
|
|
608
|
-
|
|
609
|
-
|
|
536
|
+
pi: ExtensionAPI,
|
|
537
|
+
ctx: ExtensionCommandContext,
|
|
610
538
|
): Promise<AdministrativeHistoryRisk[]> {
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
candidate.id,
|
|
636
|
-
await unreachableAdministrativeHistoryOids(pi, ctx, candidate.administrativePath),
|
|
637
|
-
),
|
|
638
|
-
);
|
|
639
|
-
}
|
|
640
|
-
return normalizeAdministrativeHistoryRisks(risks);
|
|
539
|
+
const risks: AdministrativeHistoryRisk[] = [];
|
|
540
|
+
for (const candidate of await administrativePruneCandidates(pi, ctx.cwd, ctx.signal)) {
|
|
541
|
+
if (candidate.indexDirty) {
|
|
542
|
+
throw new Error(
|
|
543
|
+
`Prune refused because administrative worktree ${candidate.id} contains staged-only index changes.`,
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
if (candidate.head) {
|
|
547
|
+
const refs = await durableRefsContaining(pi, ctx.cwd, candidate.head, ctx.signal);
|
|
548
|
+
if (refs.length === 0) {
|
|
549
|
+
throw new Error(
|
|
550
|
+
`Prune refused because administrative worktree ${candidate.id} has detached HEAD ${candidate.head}, which is not reachable from a durable ref.`,
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
} else if (!candidate.branchRef || !(await durableRefExists(pi, ctx.cwd, candidate.branchRef, ctx.signal))) {
|
|
554
|
+
throw new Error(
|
|
555
|
+
`Prune refused because administrative worktree ${candidate.id} does not resolve to a durable ref.`,
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
risks.push(
|
|
559
|
+
...historyRisks(candidate.id, await unreachableAdministrativeHistoryOids(pi, ctx, candidate.administrativePath)),
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
return normalizeAdministrativeHistoryRisks(risks);
|
|
641
563
|
}
|
|
642
564
|
|
|
643
565
|
async function unreachableAdministrativeHistoryOids(
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
566
|
+
pi: ExtensionAPI,
|
|
567
|
+
ctx: ExtensionCommandContext,
|
|
568
|
+
administrativePath: string,
|
|
647
569
|
): Promise<string[]> {
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
570
|
+
const unreachable: string[] = [];
|
|
571
|
+
for (const oid of await administrativeHistoryOids(pi, ctx.cwd, administrativePath, ctx.signal)) {
|
|
572
|
+
const refs = await durableRefsContaining(pi, ctx.cwd, oid, ctx.signal);
|
|
573
|
+
if (refs.length === 0) unreachable.push(oid);
|
|
574
|
+
}
|
|
575
|
+
return [...new Set(unreachable)].sort();
|
|
654
576
|
}
|
|
655
577
|
|
|
656
578
|
function historyRisks(label: string, oids: string[]): AdministrativeHistoryRisk[] {
|
|
657
|
-
|
|
579
|
+
return oids.length > 0 ? [{ label, oids }] : [];
|
|
658
580
|
}
|
|
659
581
|
|
|
660
|
-
function normalizeAdministrativeHistoryRisks(
|
|
661
|
-
|
|
662
|
-
):
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
.filter((risk) => risk.oids.length > 0)
|
|
666
|
-
.sort((left, right) => left.label.localeCompare(right.label));
|
|
582
|
+
function normalizeAdministrativeHistoryRisks(risks: readonly AdministrativeHistoryRisk[]): AdministrativeHistoryRisk[] {
|
|
583
|
+
return risks
|
|
584
|
+
.map((risk) => ({ label: risk.label, oids: [...new Set(risk.oids)].sort() }))
|
|
585
|
+
.filter((risk) => risk.oids.length > 0)
|
|
586
|
+
.sort((left, right) => left.label.localeCompare(right.label));
|
|
667
587
|
}
|
|
668
588
|
|
|
669
589
|
function sameAdministrativeHistoryRisks(
|
|
670
|
-
|
|
671
|
-
|
|
590
|
+
left: readonly AdministrativeHistoryRisk[],
|
|
591
|
+
right: readonly AdministrativeHistoryRisk[],
|
|
672
592
|
): boolean {
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
593
|
+
return (
|
|
594
|
+
JSON.stringify(normalizeAdministrativeHistoryRisks(left)) ===
|
|
595
|
+
JSON.stringify(normalizeAdministrativeHistoryRisks(right))
|
|
596
|
+
);
|
|
677
597
|
}
|
|
678
598
|
|
|
679
599
|
function formatAdministrativeRecoveryWarning(risks: readonly AdministrativeHistoryRisk[]): string {
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
)
|
|
686
|
-
.join("; ");
|
|
687
|
-
return ` Administrative recovery warning: these commits are not reachable from a branch, tag, or remote ref: ${entries}. Discarding their recovery pointers means they may later be garbage-collected.`;
|
|
600
|
+
if (risks.length === 0) return "";
|
|
601
|
+
const entries = risks
|
|
602
|
+
.map((risk) => `${stripTerminalControls(risk.label)}: ${risk.oids.map(stripTerminalControls).join(", ")}`)
|
|
603
|
+
.join("; ");
|
|
604
|
+
return ` Administrative recovery warning: these commits are not reachable from a branch, tag, or remote ref: ${entries}. Discarding their recovery pointers means they may later be garbage-collected.`;
|
|
688
605
|
}
|
|
689
606
|
|
|
690
607
|
interface RemovalInventory {
|
|
691
|
-
|
|
692
|
-
|
|
608
|
+
ignored: string[];
|
|
609
|
+
protected: string[];
|
|
693
610
|
}
|
|
694
611
|
|
|
695
612
|
function classifyRemovalInventory(lines: readonly string[]): RemovalInventory {
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
613
|
+
const ignored: string[] = [];
|
|
614
|
+
const protectedData: string[] = [];
|
|
615
|
+
for (const line of lines) {
|
|
616
|
+
(line.startsWith("!! ") ? ignored : protectedData).push(line);
|
|
617
|
+
}
|
|
618
|
+
return {
|
|
619
|
+
ignored: normalizeInventory(ignored),
|
|
620
|
+
protected: normalizeInventory(protectedData),
|
|
621
|
+
};
|
|
705
622
|
}
|
|
706
623
|
|
|
707
624
|
function normalizeInventory(lines: readonly string[]): string[] {
|
|
708
|
-
|
|
625
|
+
return [...new Set(lines)].sort();
|
|
709
626
|
}
|
|
710
627
|
|
|
711
628
|
function sameInventory(left: readonly string[], right: readonly string[]): boolean {
|
|
712
|
-
|
|
629
|
+
return JSON.stringify(normalizeInventory(left)) === JSON.stringify(normalizeInventory(right));
|
|
713
630
|
}
|
|
714
631
|
|
|
715
632
|
function formatIgnoredDataWarning(ignored: readonly string[]): string {
|
|
716
|
-
|
|
717
|
-
|
|
633
|
+
if (ignored.length === 0) return "";
|
|
634
|
+
return ` Ignored files and directories that will be deleted:\n${ignored.map(stripTerminalControls).join("\n")}`;
|
|
718
635
|
}
|
|
719
636
|
|
|
720
637
|
async function assertDetachedHeadIsDurable(
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
638
|
+
pi: ExtensionAPI,
|
|
639
|
+
ctx: ExtensionCommandContext,
|
|
640
|
+
record: WorktreeRecord,
|
|
724
641
|
): Promise<void> {
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
}
|
|
642
|
+
if (!record.detached) return;
|
|
643
|
+
if (!record.head) throw new Error(`Detached worktree ${record.path} has no HEAD object; refusing.`);
|
|
644
|
+
const refs = await durableRefsContaining(pi, ctx.cwd, record.head, ctx.signal);
|
|
645
|
+
if (refs.length === 0) {
|
|
646
|
+
throw new Error(
|
|
647
|
+
`Detached HEAD ${record.head} at ${record.path} is not reachable from a local branch, tag, or remote ref. Preserve it before continuing.`,
|
|
648
|
+
);
|
|
649
|
+
}
|
|
734
650
|
}
|
|
735
651
|
|
|
736
652
|
async function revalidateWorktreeIdentity(
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
653
|
+
pi: ExtensionAPI,
|
|
654
|
+
ctx: ExtensionCommandContext,
|
|
655
|
+
selected: WorktreeRecord,
|
|
740
656
|
): Promise<WorktreeRecord> {
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
657
|
+
const latest = (await listWorktrees(pi, ctx.cwd, ctx.signal)).find((record) =>
|
|
658
|
+
pathsEqual(record.path, selected.path),
|
|
659
|
+
);
|
|
660
|
+
if (!latest) throw new Error(`Worktree ${selected.path} is no longer registered.`);
|
|
661
|
+
if (!sameWorktreeIdentity(selected, latest)) {
|
|
662
|
+
throw new Error(`Worktree ${selected.path} changed identity; select it again.`);
|
|
663
|
+
}
|
|
664
|
+
return latest;
|
|
749
665
|
}
|
|
750
666
|
|
|
751
667
|
async function selectWorktree(
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
668
|
+
ctx: ExtensionCommandContext,
|
|
669
|
+
title: string,
|
|
670
|
+
records: readonly WorktreeRecord[],
|
|
671
|
+
currentPath: string,
|
|
672
|
+
signal?: AbortSignal,
|
|
757
673
|
): Promise<WorktreeRecord | undefined> {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
674
|
+
if (records.length === 0) {
|
|
675
|
+
ctx.ui.notify("No eligible worktrees are available for this action.", "info");
|
|
676
|
+
return undefined;
|
|
677
|
+
}
|
|
678
|
+
const { defineMenu, runMenu } = await import("@narumitw/pi-tui-kit");
|
|
679
|
+
if (signal?.aborted || ctx.signal?.aborted) return undefined;
|
|
680
|
+
let selected: WorktreeRecord | undefined;
|
|
681
|
+
const menu = defineMenu<undefined, "worktrees", "choose", ExtensionCommandContext>({
|
|
682
|
+
start: "worktrees",
|
|
683
|
+
screens: {
|
|
684
|
+
worktrees: () => ({
|
|
685
|
+
kind: "choice",
|
|
686
|
+
title,
|
|
687
|
+
enableSearch: true,
|
|
688
|
+
items: records.map((record, index) => ({
|
|
689
|
+
id: record.path,
|
|
690
|
+
label: `${index + 1}. ${formatWorktree(record, currentPath)}`,
|
|
691
|
+
searchText: [record.path, record.branch, record.head].filter(Boolean).join(" "),
|
|
692
|
+
})),
|
|
693
|
+
action: "choose",
|
|
694
|
+
hint: "close",
|
|
695
|
+
}),
|
|
696
|
+
},
|
|
697
|
+
actions: {
|
|
698
|
+
choose: async ({ itemId }) => {
|
|
699
|
+
selected = records.find((record) => record.path === itemId);
|
|
700
|
+
return selected ? { kind: "close" } : { kind: "rejected" };
|
|
701
|
+
},
|
|
702
|
+
},
|
|
703
|
+
});
|
|
704
|
+
await runMenu(ctx, menu, {
|
|
705
|
+
getState: () => undefined,
|
|
706
|
+
signal,
|
|
707
|
+
isCurrent: () => !signal?.aborted,
|
|
708
|
+
});
|
|
709
|
+
return selected;
|
|
794
710
|
}
|
|
795
711
|
|
|
796
|
-
function safeNotify(
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
)
|
|
801
|
-
|
|
802
|
-
ctx.ui.notify(stripTerminalControls(message), level);
|
|
803
|
-
} catch {
|
|
804
|
-
console.error(message);
|
|
805
|
-
}
|
|
712
|
+
function safeNotify(ctx: ExtensionCommandContext, message: string, level: "info" | "warning" | "error"): void {
|
|
713
|
+
try {
|
|
714
|
+
ctx.ui.notify(stripTerminalControls(message), level);
|
|
715
|
+
} catch {
|
|
716
|
+
console.error(message);
|
|
717
|
+
}
|
|
806
718
|
}
|
|
807
719
|
|
|
808
720
|
function formatError(error: unknown): string {
|
|
809
|
-
|
|
721
|
+
return error instanceof Error ? error.message : String(error);
|
|
810
722
|
}
|