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