@narumitw/pi-worktree 0.51.7 → 0.51.9
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/CHANGELOG.md +78 -0
- package/README.md +19 -57
- package/dist/index.ts +121 -210
- package/dist/index.ts.map +2 -2
- package/package.json +50 -50
- package/src/command.ts +605 -693
- package/src/git.ts +720 -731
- package/src/session.ts +76 -84
- package/src/settings.ts +211 -223
- package/src/status.ts +296 -309
- package/src/worktree.ts +26 -33
package/src/git.ts
CHANGED
|
@@ -5,907 +5,896 @@ import type { ExecResult, ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
|
5
5
|
|
|
6
6
|
const GIT_TIMEOUT_MS = 15_000;
|
|
7
7
|
const GIT_MUTATION_TIMEOUT_MS = 60_000;
|
|
8
|
+
// Metadata is a path, ref, OID, or boolean; record queries need room for large repositories.
|
|
9
|
+
const GIT_METADATA_MAX_BUFFER = 64 * 1024;
|
|
10
|
+
const GIT_RECORDS_MAX_BUFFER = 8 * 1024 * 1024;
|
|
11
|
+
// Mutations can emit progress and hook diagnostics, but their output is not parsed as records.
|
|
12
|
+
const GIT_MUTATION_MAX_BUFFER = 1024 * 1024;
|
|
8
13
|
const LOCAL_BRANCH_PREFIX = "refs/heads/";
|
|
9
14
|
|
|
10
15
|
export interface WorktreeRecord {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
path: string;
|
|
17
|
+
head?: string;
|
|
18
|
+
branchRef?: string;
|
|
19
|
+
branch?: string;
|
|
20
|
+
isMain: boolean;
|
|
21
|
+
bare: boolean;
|
|
22
|
+
detached: boolean;
|
|
23
|
+
lockedReason?: string;
|
|
24
|
+
prunableReason?: string;
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
export interface AddArguments {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
path: string;
|
|
29
|
+
branch: string;
|
|
30
|
+
startOid?: string;
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
export interface AdministrativePruneCandidate {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
id: string;
|
|
35
|
+
administrativePath: string;
|
|
36
|
+
head?: string;
|
|
37
|
+
branchRef?: string;
|
|
38
|
+
indexDirty: boolean;
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
export interface GitClient {
|
|
37
|
-
|
|
42
|
+
exec(command: string, args: string[], options?: GitExecOptions): Promise<ExecResult>;
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
interface GitExecOptions {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
46
|
+
cwd?: string;
|
|
47
|
+
signal?: AbortSignal;
|
|
48
|
+
timeout?: number;
|
|
49
|
+
maxBuffer?: number;
|
|
44
50
|
}
|
|
45
51
|
|
|
46
52
|
export class GitWorktreeError extends Error {
|
|
47
|
-
|
|
53
|
+
readonly args?: readonly string[];
|
|
48
54
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
constructor(message: string, args?: readonly string[]) {
|
|
56
|
+
super(message);
|
|
57
|
+
this.name = "GitWorktreeError";
|
|
58
|
+
this.args = args;
|
|
59
|
+
}
|
|
54
60
|
}
|
|
55
61
|
|
|
56
62
|
export function parseWorktreePorcelain(output: string): WorktreeRecord[] {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
): WorktreeRecord | undefined {
|
|
119
|
-
const branchRef = `${LOCAL_BRANCH_PREFIX}${branch}`;
|
|
120
|
-
return records.find((record) => record.branchRef === branchRef);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export function defaultWorktreePath(
|
|
124
|
-
mainWorktreePath: string,
|
|
125
|
-
branch: string,
|
|
126
|
-
worktreeRoot: string,
|
|
127
|
-
): string {
|
|
128
|
-
return resolve(worktreeRoot, basename(mainWorktreePath), branch.replaceAll("/", "-"));
|
|
63
|
+
const records: WorktreeRecord[] = [];
|
|
64
|
+
let current: Omit<WorktreeRecord, "isMain"> | undefined;
|
|
65
|
+
|
|
66
|
+
const finish = () => {
|
|
67
|
+
if (!current) return;
|
|
68
|
+
records.push({ ...current, isMain: records.length === 0 });
|
|
69
|
+
current = undefined;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
for (const field of output.split("\0")) {
|
|
73
|
+
if (field === "") {
|
|
74
|
+
finish();
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const separator = field.indexOf(" ");
|
|
78
|
+
const key = separator < 0 ? field : field.slice(0, separator);
|
|
79
|
+
const value = separator < 0 ? "" : field.slice(separator + 1);
|
|
80
|
+
|
|
81
|
+
if (key === "worktree") {
|
|
82
|
+
finish();
|
|
83
|
+
if (!value) throw new GitWorktreeError("Worktree porcelain record is missing path.");
|
|
84
|
+
current = { path: value, bare: false, detached: false };
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (!current) {
|
|
88
|
+
throw new GitWorktreeError(`Worktree porcelain field ${JSON.stringify(key)} appears before worktree.`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
switch (key) {
|
|
92
|
+
case "HEAD":
|
|
93
|
+
current.head = value;
|
|
94
|
+
break;
|
|
95
|
+
case "branch":
|
|
96
|
+
current.branchRef = value;
|
|
97
|
+
current.branch = value.startsWith(LOCAL_BRANCH_PREFIX) ? value.slice(LOCAL_BRANCH_PREFIX.length) : undefined;
|
|
98
|
+
break;
|
|
99
|
+
case "bare":
|
|
100
|
+
current.bare = true;
|
|
101
|
+
break;
|
|
102
|
+
case "detached":
|
|
103
|
+
current.detached = true;
|
|
104
|
+
break;
|
|
105
|
+
case "locked":
|
|
106
|
+
current.lockedReason = value;
|
|
107
|
+
break;
|
|
108
|
+
case "prunable":
|
|
109
|
+
current.prunableReason = value;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
finish();
|
|
114
|
+
return records;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function worktreeForBranch(records: readonly WorktreeRecord[], branch: string): WorktreeRecord | undefined {
|
|
118
|
+
const branchRef = `${LOCAL_BRANCH_PREFIX}${branch}`;
|
|
119
|
+
return records.find((record) => record.branchRef === branchRef);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function defaultWorktreePath(mainWorktreePath: string, branch: string, worktreeRoot: string): string {
|
|
123
|
+
return resolve(worktreeRoot, basename(mainWorktreePath), branch.replaceAll("/", "-"));
|
|
129
124
|
}
|
|
130
125
|
|
|
131
126
|
export function buildAddArguments(input: AddArguments): string[] {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
127
|
+
return input.startOid
|
|
128
|
+
? ["worktree", "add", "-b", input.branch, input.path, input.startOid]
|
|
129
|
+
: ["worktree", "add", input.path, input.branch];
|
|
135
130
|
}
|
|
136
131
|
|
|
137
132
|
export function pathIdentity(path: string): string {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
133
|
+
const absolute = resolve(path);
|
|
134
|
+
if (!existsSync(absolute)) return absolute;
|
|
135
|
+
try {
|
|
136
|
+
return realpathSync.native(absolute);
|
|
137
|
+
} catch {
|
|
138
|
+
return absolute;
|
|
139
|
+
}
|
|
145
140
|
}
|
|
146
141
|
|
|
147
142
|
export function pathEntryExists(path: string): boolean {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
143
|
+
try {
|
|
144
|
+
lstatSync(path);
|
|
145
|
+
return true;
|
|
146
|
+
} catch (error) {
|
|
147
|
+
if (isNodeError(error) && error.code === "ENOENT") return false;
|
|
148
|
+
throw new GitWorktreeError(`Cannot inspect filesystem path ${path}: ${formatError(error)}`);
|
|
149
|
+
}
|
|
155
150
|
}
|
|
156
151
|
|
|
157
152
|
export function unresolvableSymlinkAncestor(path: string): string | undefined {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (parent === current) return undefined;
|
|
183
|
-
current = parent;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
153
|
+
let current = dirname(resolve(path));
|
|
154
|
+
while (true) {
|
|
155
|
+
try {
|
|
156
|
+
const stat = lstatSync(current);
|
|
157
|
+
if (!stat.isSymbolicLink()) return undefined;
|
|
158
|
+
try {
|
|
159
|
+
realpathSync.native(current);
|
|
160
|
+
return undefined;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
if (isNodeError(error) && (error.code === "ENOENT" || error.code === "ELOOP")) {
|
|
163
|
+
return current;
|
|
164
|
+
}
|
|
165
|
+
throw new GitWorktreeError(`Cannot resolve filesystem ancestor ${current}: ${formatError(error)}`);
|
|
166
|
+
}
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (!isNodeError(error) || error.code !== "ENOENT") {
|
|
169
|
+
if (error instanceof GitWorktreeError) throw error;
|
|
170
|
+
throw new GitWorktreeError(`Cannot inspect filesystem ancestor ${current}: ${formatError(error)}`);
|
|
171
|
+
}
|
|
172
|
+
const parent = dirname(current);
|
|
173
|
+
if (parent === current) return undefined;
|
|
174
|
+
current = parent;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
186
177
|
}
|
|
187
178
|
|
|
188
179
|
export function pathsEqual(left: string, right: string): boolean {
|
|
189
|
-
|
|
180
|
+
return pathIdentity(left) === pathIdentity(right);
|
|
190
181
|
}
|
|
191
182
|
|
|
192
183
|
export function sameWorktreeIdentity(left: WorktreeRecord, right: WorktreeRecord): boolean {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
184
|
+
return (
|
|
185
|
+
pathsEqual(left.path, right.path) &&
|
|
186
|
+
left.head === right.head &&
|
|
187
|
+
left.branchRef === right.branchRef &&
|
|
188
|
+
left.detached === right.detached &&
|
|
189
|
+
left.isMain === right.isMain &&
|
|
190
|
+
left.bare === right.bare
|
|
191
|
+
);
|
|
201
192
|
}
|
|
202
193
|
|
|
203
194
|
export async function listWorktrees(
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
195
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
196
|
+
cwd: string,
|
|
197
|
+
signal?: AbortSignal,
|
|
207
198
|
): Promise<WorktreeRecord[]> {
|
|
208
|
-
|
|
209
|
-
|
|
199
|
+
const result = await runGit(pi, ["worktree", "list", "--porcelain", "-z"], cwd, GIT_RECORDS_MAX_BUFFER, signal);
|
|
200
|
+
return parseWorktreePorcelain(result.stdout);
|
|
210
201
|
}
|
|
211
202
|
|
|
212
203
|
export async function currentWorktreePath(
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
204
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
205
|
+
cwd: string,
|
|
206
|
+
signal?: AbortSignal,
|
|
216
207
|
): Promise<string> {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
208
|
+
const result = await runGit(pi, ["rev-parse", "--show-toplevel"], cwd, GIT_METADATA_MAX_BUFFER, signal);
|
|
209
|
+
const path = removeLineEnding(result.stdout);
|
|
210
|
+
if (!path) throw new GitWorktreeError("Git did not return the current worktree path.");
|
|
211
|
+
return pathIdentity(path);
|
|
221
212
|
}
|
|
222
213
|
|
|
223
214
|
export async function symbolicBranch(
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
215
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
216
|
+
cwd: string,
|
|
217
|
+
signal?: AbortSignal,
|
|
227
218
|
): Promise<string | undefined> {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
219
|
+
const result = await runGitAllowFailure(
|
|
220
|
+
pi,
|
|
221
|
+
["symbolic-ref", "--quiet", "--short", "HEAD"],
|
|
222
|
+
cwd,
|
|
223
|
+
GIT_METADATA_MAX_BUFFER,
|
|
224
|
+
signal,
|
|
225
|
+
);
|
|
226
|
+
if (result.killed) throw killedError(["symbolic-ref", "--quiet", "--short", "HEAD"]);
|
|
227
|
+
if (result.code !== 0) return undefined;
|
|
228
|
+
return result.stdout.trim() || undefined;
|
|
237
229
|
}
|
|
238
230
|
|
|
239
231
|
export async function validateBranch(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
232
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
233
|
+
cwd: string,
|
|
234
|
+
branch: string,
|
|
235
|
+
signal?: AbortSignal,
|
|
244
236
|
): Promise<string> {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
237
|
+
const result = await runGit(pi, ["check-ref-format", "--branch", branch], cwd, GIT_METADATA_MAX_BUFFER, signal);
|
|
238
|
+
const normalized = result.stdout.trim();
|
|
239
|
+
if (!normalized) throw new GitWorktreeError("Git returned an empty branch name.");
|
|
240
|
+
return normalized;
|
|
249
241
|
}
|
|
250
242
|
|
|
251
243
|
export async function localBranchExists(
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
244
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
245
|
+
cwd: string,
|
|
246
|
+
branch: string,
|
|
247
|
+
signal?: AbortSignal,
|
|
256
248
|
): Promise<boolean> {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
249
|
+
const result = await runGitAllowFailure(
|
|
250
|
+
pi,
|
|
251
|
+
["show-ref", "--verify", "--quiet", `${LOCAL_BRANCH_PREFIX}${branch}`],
|
|
252
|
+
cwd,
|
|
253
|
+
GIT_METADATA_MAX_BUFFER,
|
|
254
|
+
signal,
|
|
255
|
+
);
|
|
256
|
+
if (result.killed) throw killedError(["show-ref", "--verify", "--quiet"]);
|
|
257
|
+
if (result.code === 0) return true;
|
|
258
|
+
if (result.code === 1) return false;
|
|
259
|
+
throw gitFailure(["show-ref", "--verify", "--quiet"], result);
|
|
267
260
|
}
|
|
268
261
|
|
|
269
262
|
export async function resolveCommit(
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
263
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
264
|
+
cwd: string,
|
|
265
|
+
startPoint: string,
|
|
266
|
+
signal?: AbortSignal,
|
|
274
267
|
): Promise<string> {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
268
|
+
const result = await runGit(
|
|
269
|
+
pi,
|
|
270
|
+
["rev-parse", "--verify", "--end-of-options", `${startPoint}^{commit}`],
|
|
271
|
+
cwd,
|
|
272
|
+
GIT_METADATA_MAX_BUFFER,
|
|
273
|
+
signal,
|
|
274
|
+
);
|
|
275
|
+
const oid = result.stdout.trim();
|
|
276
|
+
if (!/^[0-9a-fA-F]{40,64}$/u.test(oid)) {
|
|
277
|
+
throw new GitWorktreeError(`Git returned an invalid commit object for ${startPoint}.`);
|
|
278
|
+
}
|
|
279
|
+
return oid;
|
|
286
280
|
}
|
|
287
281
|
|
|
288
282
|
export async function addWorktree(
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
283
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
284
|
+
cwd: string,
|
|
285
|
+
input: AddArguments,
|
|
286
|
+
signal?: AbortSignal,
|
|
293
287
|
): Promise<void> {
|
|
294
|
-
|
|
288
|
+
await runGit(pi, buildAddArguments(input), cwd, GIT_MUTATION_MAX_BUFFER, signal, GIT_MUTATION_TIMEOUT_MS);
|
|
295
289
|
}
|
|
296
290
|
|
|
297
291
|
export async function removeWorktree(
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
292
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
293
|
+
cwd: string,
|
|
294
|
+
path: string,
|
|
295
|
+
signal?: AbortSignal,
|
|
302
296
|
): Promise<void> {
|
|
303
|
-
|
|
297
|
+
await runGit(pi, ["worktree", "remove", path], cwd, GIT_MUTATION_MAX_BUFFER, signal, GIT_MUTATION_TIMEOUT_MS);
|
|
304
298
|
}
|
|
305
299
|
|
|
306
300
|
export async function worktreeInventory(
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
301
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
302
|
+
path: string,
|
|
303
|
+
signal?: AbortSignal,
|
|
310
304
|
): Promise<string[]> {
|
|
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
|
-
|
|
305
|
+
const statusArgs = [
|
|
306
|
+
"status",
|
|
307
|
+
"--porcelain=v1",
|
|
308
|
+
"--untracked-files=all",
|
|
309
|
+
"--ignored=matching",
|
|
310
|
+
"--ignore-submodules=none",
|
|
311
|
+
];
|
|
312
|
+
const status = await runGit(pi, statusArgs, path, GIT_RECORDS_MAX_BUFFER, signal);
|
|
313
|
+
const indexFlags = await runGit(pi, ["ls-files", "-v", "-z"], path, GIT_RECORDS_MAX_BUFFER, signal);
|
|
314
|
+
const indexInventory = await indexFlagInventory(pi, indexFlags.stdout, path, signal);
|
|
315
|
+
const submoduleStatus = await runGit(
|
|
316
|
+
pi,
|
|
317
|
+
["submodule", "status", "--recursive"],
|
|
318
|
+
path,
|
|
319
|
+
GIT_RECORDS_MAX_BUFFER,
|
|
320
|
+
signal,
|
|
321
|
+
);
|
|
322
|
+
const initializedSubmodules = nonEmptyLines(submoduleStatus.stdout)
|
|
323
|
+
.filter((line) => !line.startsWith("-"))
|
|
324
|
+
.map((line) => `initialized submodule: ${line.slice(1).trimStart()}`);
|
|
325
|
+
const submodules = await runGit(
|
|
326
|
+
pi,
|
|
327
|
+
[
|
|
328
|
+
"submodule",
|
|
329
|
+
"foreach",
|
|
330
|
+
"--recursive",
|
|
331
|
+
"--quiet",
|
|
332
|
+
"git status --porcelain=v1 --untracked-files=all --ignored=matching --ignore-submodules=none",
|
|
333
|
+
],
|
|
334
|
+
path,
|
|
335
|
+
GIT_RECORDS_MAX_BUFFER,
|
|
336
|
+
signal,
|
|
337
|
+
);
|
|
338
|
+
return [
|
|
339
|
+
...nonEmptyLines(status.stdout),
|
|
340
|
+
...indexInventory,
|
|
341
|
+
...initializedSubmodules,
|
|
342
|
+
...nonEmptyLines(submodules.stdout),
|
|
343
|
+
];
|
|
343
344
|
}
|
|
344
345
|
|
|
345
346
|
export async function worktreeAdministrativeDirectory(
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
347
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
348
|
+
cwd: string,
|
|
349
|
+
signal?: AbortSignal,
|
|
349
350
|
): Promise<string> {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
351
|
+
const result = await runGit(
|
|
352
|
+
pi,
|
|
353
|
+
["rev-parse", "--path-format=absolute", "--git-dir"],
|
|
354
|
+
cwd,
|
|
355
|
+
GIT_METADATA_MAX_BUFFER,
|
|
356
|
+
signal,
|
|
357
|
+
);
|
|
358
|
+
const value = removeLineEnding(result.stdout);
|
|
359
|
+
if (!value) throw new GitWorktreeError("Git did not return its worktree administrative path.");
|
|
360
|
+
return resolve(cwd, value);
|
|
359
361
|
}
|
|
360
362
|
|
|
361
363
|
export async function administrativeHistoryOids(
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
364
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
365
|
+
cwd: string,
|
|
366
|
+
administrativePath: string,
|
|
367
|
+
signal?: AbortSignal,
|
|
366
368
|
): Promise<string[]> {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
}
|
|
390
|
-
const fetchHead = readAdministrativeFile(administrativePath, "FETCH_HEAD");
|
|
391
|
-
if (fetchHead !== undefined) {
|
|
392
|
-
values.push(...splitFetchHeadOids(fetchHead));
|
|
393
|
-
}
|
|
394
|
-
return [...new Set(values)];
|
|
369
|
+
const gitDirArgument = `--git-dir=${administrativePath}`;
|
|
370
|
+
const values = readAdministrativeReflogOids(resolve(administrativePath, "logs"));
|
|
371
|
+
|
|
372
|
+
const refs = await runGit(
|
|
373
|
+
pi,
|
|
374
|
+
[gitDirArgument, "for-each-ref", "--format=%(objectname)", "refs/worktree", "refs/bisect"],
|
|
375
|
+
cwd,
|
|
376
|
+
GIT_RECORDS_MAX_BUFFER,
|
|
377
|
+
signal,
|
|
378
|
+
);
|
|
379
|
+
values.push(...splitAdministrativeOids(refs.stdout, "per-worktree refs"));
|
|
380
|
+
|
|
381
|
+
for (const name of ["ORIG_HEAD", "MERGE_HEAD", "REBASE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "BISECT_HEAD"]) {
|
|
382
|
+
const contents = readAdministrativeFile(administrativePath, name);
|
|
383
|
+
if (contents === undefined) continue;
|
|
384
|
+
values.push(...splitAdministrativeOids(contents, name));
|
|
385
|
+
}
|
|
386
|
+
const fetchHead = readAdministrativeFile(administrativePath, "FETCH_HEAD");
|
|
387
|
+
if (fetchHead !== undefined) {
|
|
388
|
+
values.push(...splitFetchHeadOids(fetchHead));
|
|
389
|
+
}
|
|
390
|
+
return [...new Set(values)];
|
|
395
391
|
}
|
|
396
392
|
|
|
397
393
|
export async function administrativePruneCandidates(
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
394
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
395
|
+
cwd: string,
|
|
396
|
+
signal?: AbortSignal,
|
|
401
397
|
): Promise<AdministrativePruneCandidate[]> {
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
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
|
-
head: headValue,
|
|
472
|
-
indexDirty,
|
|
473
|
-
});
|
|
474
|
-
}
|
|
475
|
-
return candidates;
|
|
398
|
+
const commonResult = await runGit(
|
|
399
|
+
pi,
|
|
400
|
+
["rev-parse", "--path-format=absolute", "--git-common-dir"],
|
|
401
|
+
cwd,
|
|
402
|
+
GIT_METADATA_MAX_BUFFER,
|
|
403
|
+
signal,
|
|
404
|
+
);
|
|
405
|
+
const commonValue = removeLineEnding(commonResult.stdout);
|
|
406
|
+
if (!commonValue) throw new GitWorktreeError("Git did not return its common directory.");
|
|
407
|
+
const commonDirectory = resolve(cwd, commonValue);
|
|
408
|
+
const administrativeRoot = resolve(commonDirectory, "worktrees");
|
|
409
|
+
if (!existsSync(administrativeRoot)) return [];
|
|
410
|
+
|
|
411
|
+
const candidates: AdministrativePruneCandidate[] = [];
|
|
412
|
+
for (const entry of readdirSync(administrativeRoot, { withFileTypes: true })) {
|
|
413
|
+
const administrativePath = resolve(administrativeRoot, entry.name);
|
|
414
|
+
if (!entry.isDirectory() || entry.isSymbolicLink()) {
|
|
415
|
+
throw new GitWorktreeError(`Unexpected Git worktree administrative entry: ${administrativePath}.`);
|
|
416
|
+
}
|
|
417
|
+
if (existsSync(resolve(administrativePath, "locked"))) continue;
|
|
418
|
+
|
|
419
|
+
const gitdirPath = resolve(administrativePath, "gitdir");
|
|
420
|
+
let registeredGitFile: string | undefined;
|
|
421
|
+
try {
|
|
422
|
+
registeredGitFile = removeLineEnding(readFileSync(gitdirPath, "utf8"));
|
|
423
|
+
} catch (error) {
|
|
424
|
+
if (!isNodeError(error) || error.code !== "ENOENT") throw error;
|
|
425
|
+
}
|
|
426
|
+
if (registeredGitFile) {
|
|
427
|
+
const targetGitFile = resolve(administrativePath, registeredGitFile);
|
|
428
|
+
if (existsSync(targetGitFile)) continue;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const headPath = resolve(administrativePath, "HEAD");
|
|
432
|
+
let headValue: string;
|
|
433
|
+
try {
|
|
434
|
+
if (!lstatSync(headPath).isFile()) {
|
|
435
|
+
throw new GitWorktreeError(`Git worktree administrative HEAD is not a file: ${headPath}.`);
|
|
436
|
+
}
|
|
437
|
+
headValue = removeLineEnding(readFileSync(headPath, "utf8"));
|
|
438
|
+
} catch (error) {
|
|
439
|
+
if (error instanceof GitWorktreeError) throw error;
|
|
440
|
+
throw new GitWorktreeError(`Cannot inspect Git worktree administrative HEAD ${headPath}: ${formatError(error)}`);
|
|
441
|
+
}
|
|
442
|
+
const indexDirty = await administrativeIndexIsDirty(pi, cwd, administrativePath, signal);
|
|
443
|
+
if (headValue.startsWith("ref: ")) {
|
|
444
|
+
const branchRef = headValue.slice("ref: ".length);
|
|
445
|
+
if (!branchRef) {
|
|
446
|
+
throw new GitWorktreeError(`Git worktree administrative HEAD has an empty ref: ${headPath}.`);
|
|
447
|
+
}
|
|
448
|
+
candidates.push({
|
|
449
|
+
id: entry.name,
|
|
450
|
+
administrativePath,
|
|
451
|
+
branchRef,
|
|
452
|
+
indexDirty,
|
|
453
|
+
});
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
if (!/^[0-9a-fA-F]{40,64}$/u.test(headValue)) {
|
|
457
|
+
throw new GitWorktreeError(`Git worktree administrative HEAD is malformed: ${headPath}.`);
|
|
458
|
+
}
|
|
459
|
+
candidates.push({
|
|
460
|
+
id: entry.name,
|
|
461
|
+
administrativePath,
|
|
462
|
+
head: headValue,
|
|
463
|
+
indexDirty,
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
return candidates;
|
|
476
467
|
}
|
|
477
468
|
|
|
478
469
|
async function administrativeIndexIsDirty(
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
470
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
471
|
+
cwd: string,
|
|
472
|
+
administrativePath: string,
|
|
473
|
+
signal?: AbortSignal,
|
|
483
474
|
): Promise<boolean> {
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
475
|
+
const args = [
|
|
476
|
+
`--git-dir=${administrativePath}`,
|
|
477
|
+
"diff",
|
|
478
|
+
"--cached",
|
|
479
|
+
"--quiet",
|
|
480
|
+
"--no-ext-diff",
|
|
481
|
+
"--no-textconv",
|
|
482
|
+
"--ignore-submodules=none",
|
|
483
|
+
"--",
|
|
484
|
+
];
|
|
485
|
+
const result = await runGitAllowFailure(pi, args, cwd, GIT_METADATA_MAX_BUFFER, signal);
|
|
486
|
+
if (result.killed) throw killedError(args);
|
|
487
|
+
if (result.code === 0) return false;
|
|
488
|
+
if (result.code === 1) return true;
|
|
489
|
+
throw gitFailure(args, result);
|
|
499
490
|
}
|
|
500
491
|
|
|
501
492
|
export async function durableRefExists(
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
493
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
494
|
+
cwd: string,
|
|
495
|
+
ref: string,
|
|
496
|
+
signal?: AbortSignal,
|
|
506
497
|
): Promise<boolean> {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
498
|
+
if (!ref.startsWith("refs/") || ref.includes("\0")) {
|
|
499
|
+
throw new GitWorktreeError("Git worktree administrative HEAD contains an invalid ref.");
|
|
500
|
+
}
|
|
501
|
+
const result = await runGitAllowFailure(
|
|
502
|
+
pi,
|
|
503
|
+
["show-ref", "--verify", "--quiet", ref],
|
|
504
|
+
cwd,
|
|
505
|
+
GIT_METADATA_MAX_BUFFER,
|
|
506
|
+
signal,
|
|
507
|
+
);
|
|
508
|
+
if (result.killed) throw killedError(["show-ref", "--verify", "--quiet"]);
|
|
509
|
+
if (result.code === 0) return true;
|
|
510
|
+
if (result.code === 1) return false;
|
|
511
|
+
throw gitFailure(["show-ref", "--verify", "--quiet"], result);
|
|
520
512
|
}
|
|
521
513
|
|
|
522
514
|
export async function durableRefsContaining(
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
515
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
516
|
+
cwd: string,
|
|
517
|
+
head: string,
|
|
518
|
+
signal?: AbortSignal,
|
|
527
519
|
): Promise<string[]> {
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
return nonEmptyLines(result.stdout);
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
export async function prunePreview(
|
|
548
|
-
pi: Pick<ExtensionAPI, "exec">,
|
|
549
|
-
cwd: string,
|
|
550
|
-
signal?: AbortSignal,
|
|
551
|
-
): Promise<string> {
|
|
552
|
-
const result = await runGit(pi, ["worktree", "prune", "--dry-run", "--verbose"], cwd, signal);
|
|
553
|
-
return combineOutput(result);
|
|
520
|
+
if (!/^[0-9a-fA-F]{40,64}$/u.test(head)) {
|
|
521
|
+
throw new GitWorktreeError("Detached worktree has an invalid HEAD object.");
|
|
522
|
+
}
|
|
523
|
+
const result = await runGit(
|
|
524
|
+
pi,
|
|
525
|
+
["for-each-ref", "--format=%(refname)", `--contains=${head}`, "refs/heads", "refs/tags", "refs/remotes"],
|
|
526
|
+
cwd,
|
|
527
|
+
GIT_RECORDS_MAX_BUFFER,
|
|
528
|
+
signal,
|
|
529
|
+
);
|
|
530
|
+
return nonEmptyLines(result.stdout);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
export async function prunePreview(pi: Pick<ExtensionAPI, "exec">, cwd: string, signal?: AbortSignal): Promise<string> {
|
|
534
|
+
const result = await runGit(pi, ["worktree", "prune", "--dry-run", "--verbose"], cwd, GIT_RECORDS_MAX_BUFFER, signal);
|
|
535
|
+
return combineOutput(result);
|
|
554
536
|
}
|
|
555
537
|
|
|
556
538
|
export async function pruneWorktrees(
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
539
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
540
|
+
cwd: string,
|
|
541
|
+
signal?: AbortSignal,
|
|
560
542
|
): Promise<string> {
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
543
|
+
const result = await runGit(
|
|
544
|
+
pi,
|
|
545
|
+
["worktree", "prune", "--verbose"],
|
|
546
|
+
cwd,
|
|
547
|
+
GIT_MUTATION_MAX_BUFFER,
|
|
548
|
+
signal,
|
|
549
|
+
GIT_MUTATION_TIMEOUT_MS,
|
|
550
|
+
);
|
|
551
|
+
return combineOutput(result);
|
|
569
552
|
}
|
|
570
553
|
|
|
571
554
|
export function formatWorktree(record: WorktreeRecord, currentPath?: string): string {
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
const head = record.head ? record.head.slice(0, 8) : "no HEAD";
|
|
585
|
-
return stripTerminalControls(`${record.path} [${labels.join(", ") || "unknown"}] ${head}`);
|
|
555
|
+
const labels = [
|
|
556
|
+
currentPath && pathsEqual(record.path, currentPath) ? "current" : undefined,
|
|
557
|
+
record.isMain ? "main" : undefined,
|
|
558
|
+
record.bare ? "bare" : undefined,
|
|
559
|
+
record.detached ? "detached" : record.branch,
|
|
560
|
+
record.lockedReason !== undefined ? `locked${record.lockedReason ? `: ${record.lockedReason}` : ""}` : undefined,
|
|
561
|
+
record.prunableReason !== undefined
|
|
562
|
+
? `prunable${record.prunableReason ? `: ${record.prunableReason}` : ""}`
|
|
563
|
+
: undefined,
|
|
564
|
+
].filter((label): label is string => Boolean(label));
|
|
565
|
+
const head = record.head ? record.head.slice(0, 8) : "no HEAD";
|
|
566
|
+
return stripTerminalControls(`${record.path} [${labels.join(", ") || "unknown"}] ${head}`);
|
|
586
567
|
}
|
|
587
568
|
|
|
588
569
|
export function stripTerminalControls(value: string): string {
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
570
|
+
return [...value]
|
|
571
|
+
.filter((character) => {
|
|
572
|
+
const code = character.codePointAt(0) ?? 0;
|
|
573
|
+
return code > 0x1f && (code < 0x7f || code > 0x9f);
|
|
574
|
+
})
|
|
575
|
+
.join("");
|
|
595
576
|
}
|
|
596
577
|
|
|
597
578
|
async function runGit(
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
579
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
580
|
+
args: string[],
|
|
581
|
+
cwd: string,
|
|
582
|
+
maxBuffer: number,
|
|
583
|
+
signal?: AbortSignal,
|
|
584
|
+
timeout = GIT_TIMEOUT_MS,
|
|
603
585
|
): Promise<ExecResult> {
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
586
|
+
const result = await runGitAllowFailure(pi, args, cwd, maxBuffer, signal, timeout);
|
|
587
|
+
if (result.killed) throw killedError(args);
|
|
588
|
+
if (result.code !== 0) throw gitFailure(args, result);
|
|
589
|
+
return result;
|
|
608
590
|
}
|
|
609
591
|
|
|
610
592
|
async function runGitAllowFailure(
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
593
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
594
|
+
args: string[],
|
|
595
|
+
cwd: string,
|
|
596
|
+
maxBuffer: number,
|
|
597
|
+
signal?: AbortSignal,
|
|
598
|
+
timeout = GIT_TIMEOUT_MS,
|
|
616
599
|
): Promise<ExecResult> {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
600
|
+
let result: ExecResult;
|
|
601
|
+
try {
|
|
602
|
+
result = await pi.exec("git", args, { cwd, signal, timeout, maxBuffer });
|
|
603
|
+
} catch (error) {
|
|
604
|
+
const message = formatError(error);
|
|
605
|
+
if (/\bENOENT\b|not found/i.test(message)) {
|
|
606
|
+
throw new GitWorktreeError("Git executable was not found. Install Git and retry.", args);
|
|
607
|
+
}
|
|
608
|
+
throw new GitWorktreeError(`Could not start git ${args.slice(0, 2).join(" ")}: ${message}`, args);
|
|
609
|
+
}
|
|
610
|
+
// Even injected executors must not let incomplete data reach parsing or nonzero-exit fallbacks.
|
|
611
|
+
if (result.truncated) throw outputLimitError(args, maxBuffer);
|
|
612
|
+
return result;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function outputLimitError(args: string[], maxBuffer: number): GitWorktreeError {
|
|
616
|
+
return new GitWorktreeError(
|
|
617
|
+
`git ${args.slice(0, 2).join(" ")} output exceeded the ${maxBuffer}-byte limit. Partial output was not used; inspect Git state before retrying.`,
|
|
618
|
+
args,
|
|
619
|
+
);
|
|
629
620
|
}
|
|
630
621
|
|
|
631
622
|
function gitFailure(args: string[], result: ExecResult): GitWorktreeError {
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
: detail || `Git exited with code ${result.code}.`;
|
|
638
|
-
return new GitWorktreeError(`git ${args.slice(0, 2).join(" ")} failed: ${hint}`, args);
|
|
623
|
+
const detail = stripTerminalControls([result.stderr.trim(), result.stdout.trim()].filter(Boolean).join("\n"));
|
|
624
|
+
const hint = /not a git repository/i.test(detail)
|
|
625
|
+
? "The current Pi workspace is not inside a Git repository."
|
|
626
|
+
: detail || `Git exited with code ${result.code}.`;
|
|
627
|
+
return new GitWorktreeError(`git ${args.slice(0, 2).join(" ")} failed: ${hint}`, args);
|
|
639
628
|
}
|
|
640
629
|
|
|
641
630
|
function killedError(args: string[]): GitWorktreeError {
|
|
642
|
-
|
|
643
|
-
`git ${args.slice(0, 2).join(" ")} timed out or was cancelled.`,
|
|
644
|
-
args,
|
|
645
|
-
);
|
|
631
|
+
return new GitWorktreeError(`git ${args.slice(0, 2).join(" ")} timed out or was cancelled.`, args);
|
|
646
632
|
}
|
|
647
633
|
|
|
648
634
|
function nonEmptyLines(value: string): string[] {
|
|
649
|
-
|
|
635
|
+
return value.split(/\r?\n/u).filter((line) => line.length > 0);
|
|
650
636
|
}
|
|
651
637
|
|
|
652
638
|
interface IndexFlagEntry {
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
639
|
+
path: string;
|
|
640
|
+
skipWorktree: boolean;
|
|
641
|
+
assumeUnchanged: boolean;
|
|
656
642
|
}
|
|
657
643
|
|
|
658
644
|
async function indexFlagInventory(
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
645
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
646
|
+
value: string,
|
|
647
|
+
cwd: string,
|
|
648
|
+
signal?: AbortSignal,
|
|
663
649
|
): Promise<string[]> {
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
650
|
+
const entries = parseIndexFlagEntries(value);
|
|
651
|
+
const sparseManagedPaths = await sparseManagedSkipWorktreePaths(
|
|
652
|
+
pi,
|
|
653
|
+
cwd,
|
|
654
|
+
entries.filter((entry) => entry.skipWorktree).map((entry) => entry.path),
|
|
655
|
+
signal,
|
|
656
|
+
);
|
|
657
|
+
const inventory: string[] = [];
|
|
658
|
+
for (const entry of entries) {
|
|
659
|
+
const flags = [
|
|
660
|
+
entry.skipWorktree && !sparseManagedPaths.has(entry.path) ? "skip-worktree" : undefined,
|
|
661
|
+
entry.assumeUnchanged ? "assume-unchanged" : undefined,
|
|
662
|
+
].filter((flag): flag is string => flag !== undefined);
|
|
663
|
+
if (flags.length > 0) inventory.push(`index flag ${flags.join("+")}: ${entry.path}`);
|
|
664
|
+
}
|
|
665
|
+
return inventory;
|
|
680
666
|
}
|
|
681
667
|
|
|
682
668
|
function parseIndexFlagEntries(value: string): IndexFlagEntry[] {
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
669
|
+
const entries: IndexFlagEntry[] = [];
|
|
670
|
+
for (const entry of value.split("\0")) {
|
|
671
|
+
if (!entry) continue;
|
|
672
|
+
if (entry.length < 3 || entry[1] !== " ") {
|
|
673
|
+
throw new GitWorktreeError("Git returned malformed ls-files index-flag output.");
|
|
674
|
+
}
|
|
675
|
+
const tag = entry[0] ?? "";
|
|
676
|
+
const skipWorktree = tag.toUpperCase() === "S";
|
|
677
|
+
const assumeUnchanged = /[a-z]/u.test(tag);
|
|
678
|
+
if (skipWorktree || assumeUnchanged) {
|
|
679
|
+
entries.push({ path: entry.slice(2), skipWorktree, assumeUnchanged });
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
return entries;
|
|
697
683
|
}
|
|
698
684
|
|
|
699
685
|
async function sparseManagedSkipWorktreePaths(
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
686
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
687
|
+
cwd: string,
|
|
688
|
+
paths: readonly string[],
|
|
689
|
+
signal?: AbortSignal,
|
|
704
690
|
): Promise<ReadonlySet<string>> {
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
if ([...included].some((path) => !candidates.has(path))) {
|
|
725
|
-
throw new GitWorktreeError("Git returned an unexpected sparse-checkout path.");
|
|
726
|
-
}
|
|
727
|
-
return new Set([...candidates].filter((path) => !included.has(path)));
|
|
691
|
+
if (paths.length === 0) return new Set();
|
|
692
|
+
const configArgs = ["config", "--bool", "--get", "core.sparseCheckout"];
|
|
693
|
+
const config = await runGitAllowFailure(pi, configArgs, cwd, GIT_METADATA_MAX_BUFFER, signal);
|
|
694
|
+
if (config.killed) throw killedError(configArgs);
|
|
695
|
+
if (config.code !== 0 || config.stdout.trim() !== "true") return new Set();
|
|
696
|
+
|
|
697
|
+
const candidates = new Set(paths);
|
|
698
|
+
const checkArgs = ["sparse-checkout", "check-rules", "-z"];
|
|
699
|
+
const checked = await runGitWithInputAllowFailure(checkArgs, cwd, `${[...candidates].join("\0")}\0`, signal);
|
|
700
|
+
if (checked.truncated) throw outputLimitError(checkArgs, GIT_RECORDS_MAX_BUFFER);
|
|
701
|
+
if (checked.killed) throw killedError(checkArgs);
|
|
702
|
+
// Older Git versions lack check-rules; retain every flag rather than guessing.
|
|
703
|
+
if (checked.code !== 0) return new Set();
|
|
704
|
+
|
|
705
|
+
const included = new Set(nulSeparatedPaths(checked.stdout, "sparse-checkout rules"));
|
|
706
|
+
if ([...included].some((path) => !candidates.has(path))) {
|
|
707
|
+
throw new GitWorktreeError("Git returned an unexpected sparse-checkout path.");
|
|
708
|
+
}
|
|
709
|
+
return new Set([...candidates].filter((path) => !included.has(path)));
|
|
728
710
|
}
|
|
729
711
|
|
|
730
712
|
function runGitWithInputAllowFailure(
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
713
|
+
args: string[],
|
|
714
|
+
cwd: string,
|
|
715
|
+
input: string,
|
|
716
|
+
signal?: AbortSignal,
|
|
717
|
+
timeout = GIT_TIMEOUT_MS,
|
|
736
718
|
): Promise<ExecResult> {
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
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
|
-
|
|
719
|
+
if (signal?.aborted) {
|
|
720
|
+
return Promise.resolve({ stdout: "", stderr: "", code: 1, killed: true, truncated: false });
|
|
721
|
+
}
|
|
722
|
+
return new Promise((resolveResult, reject) => {
|
|
723
|
+
// ExtensionAPI.exec has no stdin channel, so this read-only check uses an argv-only child.
|
|
724
|
+
const child = spawn("git", args, { cwd, stdio: ["pipe", "pipe", "pipe"], windowsHide: true });
|
|
725
|
+
let stdout = "";
|
|
726
|
+
let stderr = "";
|
|
727
|
+
let killed = false;
|
|
728
|
+
let truncated = false;
|
|
729
|
+
let outputBytes = 0;
|
|
730
|
+
let settled = false;
|
|
731
|
+
const finish = (result: ExecResult) => {
|
|
732
|
+
if (settled) return;
|
|
733
|
+
settled = true;
|
|
734
|
+
clearTimeout(timeoutHandle);
|
|
735
|
+
signal?.removeEventListener("abort", stop);
|
|
736
|
+
resolveResult(result);
|
|
737
|
+
};
|
|
738
|
+
const fail = (error: unknown) => {
|
|
739
|
+
if (settled) return;
|
|
740
|
+
settled = true;
|
|
741
|
+
clearTimeout(timeoutHandle);
|
|
742
|
+
signal?.removeEventListener("abort", stop);
|
|
743
|
+
child.kill();
|
|
744
|
+
const message = formatError(error);
|
|
745
|
+
reject(
|
|
746
|
+
/\bENOENT\b|not found/i.test(message)
|
|
747
|
+
? new GitWorktreeError("Git executable was not found. Install Git and retry.", args)
|
|
748
|
+
: new GitWorktreeError(`Could not start git ${args.slice(0, 2).join(" ")}: ${message}`, args),
|
|
749
|
+
);
|
|
750
|
+
};
|
|
751
|
+
const stop = () => {
|
|
752
|
+
killed = true;
|
|
753
|
+
child.kill();
|
|
754
|
+
};
|
|
755
|
+
const timeoutHandle = setTimeout(stop, timeout);
|
|
756
|
+
const acceptChunk = (chunk: string): boolean => {
|
|
757
|
+
if (truncated) return false;
|
|
758
|
+
outputBytes += Buffer.byteLength(chunk);
|
|
759
|
+
if (outputBytes <= GIT_RECORDS_MAX_BUFFER) return true;
|
|
760
|
+
truncated = true;
|
|
761
|
+
child.stdout.destroy();
|
|
762
|
+
child.stderr.destroy();
|
|
763
|
+
stop();
|
|
764
|
+
return false;
|
|
765
|
+
};
|
|
766
|
+
child.stdout.setEncoding("utf8");
|
|
767
|
+
child.stderr.setEncoding("utf8");
|
|
768
|
+
child.stdout.on("data", (chunk: string) => {
|
|
769
|
+
if (acceptChunk(chunk)) stdout += chunk;
|
|
770
|
+
});
|
|
771
|
+
child.stderr.on("data", (chunk: string) => {
|
|
772
|
+
if (acceptChunk(chunk)) stderr += chunk;
|
|
773
|
+
});
|
|
774
|
+
child.stdin.on("error", (error) => {
|
|
775
|
+
if (!isNodeError(error) || error.code !== "EPIPE") fail(error);
|
|
776
|
+
});
|
|
777
|
+
child.once("error", fail);
|
|
778
|
+
child.once("close", (code, closeSignal) => {
|
|
779
|
+
finish({
|
|
780
|
+
stdout,
|
|
781
|
+
stderr,
|
|
782
|
+
code: truncated && code === 0 ? 1 : (code ?? 1),
|
|
783
|
+
killed: killed || closeSignal !== null,
|
|
784
|
+
truncated,
|
|
785
|
+
});
|
|
786
|
+
});
|
|
787
|
+
signal?.addEventListener("abort", stop, { once: true });
|
|
788
|
+
if (signal?.aborted) stop();
|
|
789
|
+
child.stdin.end(input);
|
|
790
|
+
});
|
|
794
791
|
}
|
|
795
792
|
|
|
796
793
|
function nulSeparatedPaths(value: string, source: string): string[] {
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
794
|
+
if (value && !value.endsWith("\0")) {
|
|
795
|
+
throw new GitWorktreeError(`Git returned malformed ${source} output.`);
|
|
796
|
+
}
|
|
797
|
+
return value.split("\0").filter(Boolean);
|
|
801
798
|
}
|
|
802
799
|
|
|
803
800
|
function combineOutput(result: ExecResult): string {
|
|
804
|
-
|
|
801
|
+
return [result.stdout.trimEnd(), result.stderr.trimEnd()].filter(Boolean).join("\n");
|
|
805
802
|
}
|
|
806
803
|
|
|
807
804
|
function removeLineEnding(value: string): string {
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
805
|
+
if (value.endsWith("\r\n")) return value.slice(0, -2);
|
|
806
|
+
if (value.endsWith("\n")) return value.slice(0, -1);
|
|
807
|
+
return value;
|
|
811
808
|
}
|
|
812
809
|
|
|
813
810
|
function splitAdministrativeOids(value: string, source: string): string[] {
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
}
|
|
822
|
-
return values;
|
|
811
|
+
const normalized = value.endsWith("\n") ? value.slice(0, -1) : value;
|
|
812
|
+
if (!normalized) return [];
|
|
813
|
+
const values = normalized.split("\n").map((line) => (line.endsWith("\r") ? line.slice(0, -1) : line));
|
|
814
|
+
if (values.some((oid) => !/^[0-9a-fA-F]{40,64}$/u.test(oid))) {
|
|
815
|
+
throw new GitWorktreeError(`Git returned malformed object IDs for ${source}.`);
|
|
816
|
+
}
|
|
817
|
+
return values;
|
|
823
818
|
}
|
|
824
819
|
|
|
825
820
|
function splitFetchHeadOids(value: string): string[] {
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
821
|
+
const normalized = value.endsWith("\n") ? value.slice(0, -1) : value;
|
|
822
|
+
if (!normalized) return [];
|
|
823
|
+
return normalized.split("\n").map((line) => {
|
|
824
|
+
const match = /^([0-9a-fA-F]{40,64})\t/u.exec(line);
|
|
825
|
+
if (!match?.[1]) {
|
|
826
|
+
throw new GitWorktreeError("Git worktree administrative FETCH_HEAD is malformed.");
|
|
827
|
+
}
|
|
828
|
+
return match[1];
|
|
829
|
+
});
|
|
835
830
|
}
|
|
836
831
|
|
|
837
832
|
function readAdministrativeFile(administrativePath: string, name: string): string | undefined {
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
return readFileSync(path, "utf8");
|
|
855
|
-
} catch (error) {
|
|
856
|
-
throw new GitWorktreeError(
|
|
857
|
-
`Cannot inspect Git worktree administrative ${name}: ${formatError(error)}`,
|
|
858
|
-
);
|
|
859
|
-
}
|
|
833
|
+
const path = resolve(administrativePath, name);
|
|
834
|
+
let stat: ReturnType<typeof lstatSync>;
|
|
835
|
+
try {
|
|
836
|
+
stat = lstatSync(path);
|
|
837
|
+
} catch (error) {
|
|
838
|
+
if (isNodeError(error) && error.code === "ENOENT") return undefined;
|
|
839
|
+
throw new GitWorktreeError(`Cannot inspect Git worktree administrative ${name}: ${formatError(error)}`);
|
|
840
|
+
}
|
|
841
|
+
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
842
|
+
throw new GitWorktreeError(`Git worktree administrative ${name} must be a regular file: ${path}.`);
|
|
843
|
+
}
|
|
844
|
+
try {
|
|
845
|
+
return readFileSync(path, "utf8");
|
|
846
|
+
} catch (error) {
|
|
847
|
+
throw new GitWorktreeError(`Cannot inspect Git worktree administrative ${name}: ${formatError(error)}`);
|
|
848
|
+
}
|
|
860
849
|
}
|
|
861
850
|
|
|
862
851
|
function readAdministrativeReflogOids(logPath: string): string[] {
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
852
|
+
if (!existsSync(logPath)) return [];
|
|
853
|
+
let stat: ReturnType<typeof lstatSync>;
|
|
854
|
+
try {
|
|
855
|
+
stat = lstatSync(logPath);
|
|
856
|
+
} catch (error) {
|
|
857
|
+
throw new GitWorktreeError(`Cannot inspect Git reflog path ${logPath}: ${formatError(error)}`);
|
|
858
|
+
}
|
|
859
|
+
if (stat.isSymbolicLink()) {
|
|
860
|
+
throw new GitWorktreeError(`Git reflog path must not be a symbolic link: ${logPath}.`);
|
|
861
|
+
}
|
|
862
|
+
if (stat.isDirectory()) {
|
|
863
|
+
const values: string[] = [];
|
|
864
|
+
for (const entry of readdirSync(logPath, { withFileTypes: true })) {
|
|
865
|
+
values.push(...readAdministrativeReflogOids(resolve(logPath, entry.name)));
|
|
866
|
+
}
|
|
867
|
+
return values;
|
|
868
|
+
}
|
|
869
|
+
if (!stat.isFile()) {
|
|
870
|
+
throw new GitWorktreeError(`Unexpected Git reflog entry type: ${logPath}.`);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
let contents: string;
|
|
874
|
+
try {
|
|
875
|
+
contents = readFileSync(logPath, "utf8");
|
|
876
|
+
} catch (error) {
|
|
877
|
+
throw new GitWorktreeError(`Cannot read Git reflog ${logPath}: ${formatError(error)}`);
|
|
878
|
+
}
|
|
879
|
+
const normalized = contents.endsWith("\n") ? contents.slice(0, -1) : contents;
|
|
880
|
+
if (!normalized) return [];
|
|
881
|
+
const values: string[] = [];
|
|
882
|
+
for (const line of normalized.split("\n")) {
|
|
883
|
+
const match = /^([0-9a-fA-F]{40,64}) ([0-9a-fA-F]{40,64}) /u.exec(line);
|
|
884
|
+
if (!match?.[1] || !match[2] || match[1].length !== match[2].length) {
|
|
885
|
+
throw new GitWorktreeError(`Git worktree reflog is malformed: ${logPath}.`);
|
|
886
|
+
}
|
|
887
|
+
for (const oid of [match[1], match[2]]) {
|
|
888
|
+
if (!/^0+$/u.test(oid)) values.push(oid);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
return values;
|
|
903
892
|
}
|
|
904
893
|
|
|
905
894
|
function isNodeError(error: unknown): error is NodeJS.ErrnoException {
|
|
906
|
-
|
|
895
|
+
return error instanceof Error && "code" in error;
|
|
907
896
|
}
|
|
908
897
|
|
|
909
898
|
function formatError(error: unknown): string {
|
|
910
|
-
|
|
899
|
+
return error instanceof Error ? error.message : String(error);
|
|
911
900
|
}
|