@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/status.ts
CHANGED
|
@@ -3,382 +3,369 @@ import type { ExecResult, ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
|
3
3
|
import { pathsEqual, stripTerminalControls, type WorktreeRecord } from "./git.js";
|
|
4
4
|
|
|
5
5
|
const GIT_STATUS_TIMEOUT_MS = 15_000;
|
|
6
|
+
// Status can contain every repository path; the last-commit query returns only a date and subject.
|
|
7
|
+
const GIT_STATUS_MAX_BUFFER = 8 * 1024 * 1024;
|
|
8
|
+
const GIT_LAST_COMMIT_MAX_BUFFER = 64 * 1024;
|
|
6
9
|
const DEFAULT_STATUS_CONCURRENCY = 4;
|
|
7
10
|
const OID_PATTERN = /^[0-9a-fA-F]{40,64}$/u;
|
|
8
11
|
const STATUS_XY_PATTERN = /^[.MADRCUT]{2}$/u;
|
|
9
12
|
|
|
10
13
|
export interface WorktreeStatusSnapshot {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
headOid?: string;
|
|
15
|
+
branch?: string;
|
|
16
|
+
detached: boolean;
|
|
17
|
+
upstream?: string;
|
|
18
|
+
ahead?: number;
|
|
19
|
+
behind?: number;
|
|
20
|
+
staged: number;
|
|
21
|
+
unstaged: number;
|
|
22
|
+
untracked: number;
|
|
23
|
+
conflicts: number;
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
export interface LastCommitSummary {
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
committedAt: string;
|
|
28
|
+
subject: string;
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
export type WorktreeStatusResult =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
| {
|
|
33
|
+
kind: "available";
|
|
34
|
+
snapshot: WorktreeStatusSnapshot;
|
|
35
|
+
lastCommit?: LastCommitSummary;
|
|
36
|
+
}
|
|
37
|
+
| { kind: "unavailable"; reason: string };
|
|
35
38
|
|
|
36
39
|
export interface WorktreeStatusCard {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
id: string;
|
|
41
|
+
label: string;
|
|
42
|
+
description: string;
|
|
43
|
+
statusText: string;
|
|
44
|
+
searchText: string;
|
|
45
|
+
details: string[];
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
interface LoadStatusOptions {
|
|
46
|
-
|
|
49
|
+
concurrency?: number;
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
export function parseWorktreeStatusPorcelain(output: string): WorktreeStatusSnapshot {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
53
|
+
if (output && !output.endsWith("\0")) {
|
|
54
|
+
throw new Error("Git status porcelain output is not NUL-terminated.");
|
|
55
|
+
}
|
|
56
|
+
const entries = output ? output.slice(0, -1).split("\0") : [];
|
|
57
|
+
const snapshot: WorktreeStatusSnapshot = {
|
|
58
|
+
detached: false,
|
|
59
|
+
staged: 0,
|
|
60
|
+
unstaged: 0,
|
|
61
|
+
untracked: 0,
|
|
62
|
+
conflicts: 0,
|
|
63
|
+
};
|
|
61
64
|
|
|
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
|
-
|
|
65
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
66
|
+
const entry = entries[index] ?? "";
|
|
67
|
+
if (entry.startsWith("# ")) {
|
|
68
|
+
parseBranchHeader(snapshot, entry);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (entry.startsWith("1 ")) {
|
|
72
|
+
assertFieldCount(entry, 9, "ordinary");
|
|
73
|
+
countTrackedState(snapshot, statusCode(entry));
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (entry.startsWith("2 ")) {
|
|
77
|
+
assertFieldCount(entry, 10, "renamed");
|
|
78
|
+
countTrackedState(snapshot, statusCode(entry));
|
|
79
|
+
const originalPath = entries[index + 1];
|
|
80
|
+
if (!originalPath) throw new Error("Git status returned a malformed renamed record.");
|
|
81
|
+
index += 1;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (entry.startsWith("u ")) {
|
|
85
|
+
assertFieldCount(entry, 11, "unmerged");
|
|
86
|
+
statusCode(entry);
|
|
87
|
+
snapshot.conflicts += 1;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (entry.startsWith("? ")) {
|
|
91
|
+
if (entry.length === 2) throw new Error("Git status returned a malformed untracked record.");
|
|
92
|
+
snapshot.untracked += 1;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (entry.startsWith("! ")) continue;
|
|
96
|
+
throw new Error("Git status returned an unknown porcelain-v2 record.");
|
|
97
|
+
}
|
|
98
|
+
return snapshot;
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
export function formatWorktreeStatusCard(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
+
record: WorktreeRecord,
|
|
103
|
+
currentPath: string,
|
|
104
|
+
result: WorktreeStatusResult,
|
|
102
105
|
): WorktreeStatusCard {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
? `Prunable: ${safeDisplay(record.prunableReason)}`
|
|
135
|
-
: "Prunable: no reason provided",
|
|
136
|
-
);
|
|
137
|
-
}
|
|
106
|
+
const path = safeDisplay(record.path);
|
|
107
|
+
const identity = [
|
|
108
|
+
pathsEqual(record.path, currentPath) ? "current" : undefined,
|
|
109
|
+
record.isMain ? "main" : undefined,
|
|
110
|
+
record.bare ? "bare" : undefined,
|
|
111
|
+
record.detached ? "detached" : undefined,
|
|
112
|
+
].filter((value): value is string => value !== undefined);
|
|
113
|
+
const label = safeDisplay(
|
|
114
|
+
record.branch ??
|
|
115
|
+
(record.detached
|
|
116
|
+
? `Detached ${record.head?.slice(0, 8) ?? "HEAD"}`
|
|
117
|
+
: record.bare
|
|
118
|
+
? "Bare worktree"
|
|
119
|
+
: "Unknown worktree"),
|
|
120
|
+
);
|
|
121
|
+
const statePrefix = identity.length > 0 ? `${identity.join(" · ")} · ` : "";
|
|
122
|
+
const baseDetails = [
|
|
123
|
+
`Path: ${path}`,
|
|
124
|
+
`Identity: ${identity.join(" · ") || "linked"}`,
|
|
125
|
+
`Registered HEAD: ${safeDisplay(record.head ?? "unavailable")}`,
|
|
126
|
+
];
|
|
127
|
+
if (record.lockedReason !== undefined) {
|
|
128
|
+
baseDetails.push(
|
|
129
|
+
record.lockedReason ? `Locked: ${safeDisplay(record.lockedReason)}` : "Locked: no reason provided",
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
if (record.prunableReason !== undefined) {
|
|
133
|
+
baseDetails.push(
|
|
134
|
+
record.prunableReason ? `Prunable: ${safeDisplay(record.prunableReason)}` : "Prunable: no reason provided",
|
|
135
|
+
);
|
|
136
|
+
}
|
|
138
137
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
"Snapshot: local Git state; no fetch performed.",
|
|
151
|
-
],
|
|
152
|
-
};
|
|
153
|
-
}
|
|
138
|
+
if (result.kind === "unavailable") {
|
|
139
|
+
const reason = safeDisplay(result.reason);
|
|
140
|
+
return {
|
|
141
|
+
id: record.path,
|
|
142
|
+
label,
|
|
143
|
+
description: path,
|
|
144
|
+
statusText: `${statePrefix}unavailable`,
|
|
145
|
+
searchText: `${label} ${path} ${identity.join(" ")} unavailable ${reason}`,
|
|
146
|
+
details: [...baseDetails, `Status: ${reason}`, "Snapshot: local Git state; no fetch performed."],
|
|
147
|
+
};
|
|
148
|
+
}
|
|
154
149
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
150
|
+
const { snapshot, lastCommit } = result;
|
|
151
|
+
const workingState = formatWorkingState(snapshot);
|
|
152
|
+
const upstream = snapshot.upstream
|
|
153
|
+
? snapshot.ahead !== undefined && snapshot.behind !== undefined
|
|
154
|
+
? `${safeDisplay(snapshot.upstream)} · ahead ${snapshot.ahead} · behind ${snapshot.behind}`
|
|
155
|
+
: `${safeDisplay(snapshot.upstream)} · ahead/behind unavailable`
|
|
156
|
+
: "not configured; ahead/behind unavailable";
|
|
157
|
+
const details = [
|
|
158
|
+
...baseDetails,
|
|
159
|
+
`Snapshot HEAD: ${safeDisplay(snapshot.headOid ?? "unborn")}`,
|
|
160
|
+
`Working tree: ${workingState}`,
|
|
161
|
+
`Upstream: ${upstream}`,
|
|
162
|
+
lastCommit
|
|
163
|
+
? `Last commit: ${safeDisplay(lastCommit.committedAt)} · ${safeDisplay(lastCommit.subject || "(no subject)")}`
|
|
164
|
+
: "Last commit: unavailable",
|
|
165
|
+
"Snapshot: local Git state; no fetch performed; removal uses stricter checks.",
|
|
166
|
+
];
|
|
167
|
+
return {
|
|
168
|
+
id: record.path,
|
|
169
|
+
label,
|
|
170
|
+
description: path,
|
|
171
|
+
statusText: `${statePrefix}${workingState}`,
|
|
172
|
+
searchText: `${label} ${path} ${identity.join(" ")} ${workingState} ${safeDisplay(snapshot.upstream ?? "")}`,
|
|
173
|
+
details,
|
|
174
|
+
};
|
|
180
175
|
}
|
|
181
176
|
|
|
182
177
|
export async function loadWorktreeStatusCards(
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
178
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
179
|
+
records: readonly WorktreeRecord[],
|
|
180
|
+
currentPath: string,
|
|
181
|
+
signal?: AbortSignal,
|
|
182
|
+
options: LoadStatusOptions = {},
|
|
188
183
|
): Promise<WorktreeStatusCard[]> {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
);
|
|
193
|
-
const cards = new Array<WorktreeStatusCard>(records.length);
|
|
194
|
-
let nextIndex = 0;
|
|
184
|
+
const concurrency = Math.max(1, Math.min(options.concurrency ?? DEFAULT_STATUS_CONCURRENCY, records.length || 1));
|
|
185
|
+
const cards = new Array<WorktreeStatusCard>(records.length);
|
|
186
|
+
let nextIndex = 0;
|
|
195
187
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
reason: `Status failed: ${formatError(error)}`,
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
};
|
|
188
|
+
const worker = async () => {
|
|
189
|
+
while (true) {
|
|
190
|
+
throwIfAborted(signal);
|
|
191
|
+
const index = nextIndex;
|
|
192
|
+
if (index >= records.length) return;
|
|
193
|
+
nextIndex += 1;
|
|
194
|
+
const record = records[index];
|
|
195
|
+
if (!record) return;
|
|
196
|
+
const unavailable = unavailableReason(record);
|
|
197
|
+
if (unavailable) {
|
|
198
|
+
cards[index] = formatWorktreeStatusCard(record, currentPath, {
|
|
199
|
+
kind: "unavailable",
|
|
200
|
+
reason: unavailable,
|
|
201
|
+
});
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
const status = await runGit(
|
|
206
|
+
pi,
|
|
207
|
+
["status", "--porcelain=v2", "--branch", "-z", "--untracked-files=all", "--ignore-submodules=none"],
|
|
208
|
+
record.path,
|
|
209
|
+
GIT_STATUS_MAX_BUFFER,
|
|
210
|
+
signal,
|
|
211
|
+
);
|
|
212
|
+
const snapshot = parseWorktreeStatusPorcelain(status.stdout);
|
|
213
|
+
const lastCommit = snapshot.headOid
|
|
214
|
+
? parseLastCommit(
|
|
215
|
+
(
|
|
216
|
+
await runGit(
|
|
217
|
+
pi,
|
|
218
|
+
["show", "-s", "--format=%aI%x00%s", "--end-of-options", snapshot.headOid],
|
|
219
|
+
record.path,
|
|
220
|
+
GIT_LAST_COMMIT_MAX_BUFFER,
|
|
221
|
+
signal,
|
|
222
|
+
)
|
|
223
|
+
).stdout,
|
|
224
|
+
)
|
|
225
|
+
: undefined;
|
|
226
|
+
cards[index] = formatWorktreeStatusCard(record, currentPath, {
|
|
227
|
+
kind: "available",
|
|
228
|
+
snapshot,
|
|
229
|
+
lastCommit,
|
|
230
|
+
});
|
|
231
|
+
} catch (error) {
|
|
232
|
+
throwIfAborted(signal);
|
|
233
|
+
cards[index] = formatWorktreeStatusCard(record, currentPath, {
|
|
234
|
+
kind: "unavailable",
|
|
235
|
+
reason: `Status failed: ${formatError(error)}`,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
};
|
|
253
240
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
241
|
+
await Promise.all(Array.from({ length: concurrency }, worker));
|
|
242
|
+
throwIfAborted(signal);
|
|
243
|
+
return cards;
|
|
257
244
|
}
|
|
258
245
|
|
|
259
246
|
function parseBranchHeader(snapshot: WorktreeStatusSnapshot, entry: string): void {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
247
|
+
if (entry.startsWith("# branch.oid ")) {
|
|
248
|
+
const oid = entry.slice("# branch.oid ".length);
|
|
249
|
+
if (oid === "(initial)") {
|
|
250
|
+
snapshot.headOid = undefined;
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (!OID_PATTERN.test(oid)) throw new Error("Git status returned a malformed branch OID.");
|
|
254
|
+
snapshot.headOid = oid;
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (entry.startsWith("# branch.head ")) {
|
|
258
|
+
const branch = entry.slice("# branch.head ".length);
|
|
259
|
+
if (!branch) throw new Error("Git status returned a malformed branch head.");
|
|
260
|
+
snapshot.detached = branch === "(detached)";
|
|
261
|
+
snapshot.branch = snapshot.detached || branch === "(unknown)" ? undefined : branch;
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (entry.startsWith("# branch.upstream ")) {
|
|
265
|
+
const upstream = entry.slice("# branch.upstream ".length);
|
|
266
|
+
if (!upstream) throw new Error("Git status returned a malformed upstream.");
|
|
267
|
+
snapshot.upstream = upstream;
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (entry.startsWith("# branch.ab ")) {
|
|
271
|
+
const match = /^# branch\.ab \+(\d+) -(\d+)$/u.exec(entry);
|
|
272
|
+
if (!match?.[1] || !match[2]) {
|
|
273
|
+
throw new Error("Git status returned malformed ahead/behind counts.");
|
|
274
|
+
}
|
|
275
|
+
snapshot.ahead = Number.parseInt(match[1], 10);
|
|
276
|
+
snapshot.behind = Number.parseInt(match[2], 10);
|
|
277
|
+
}
|
|
291
278
|
}
|
|
292
279
|
|
|
293
280
|
function statusCode(entry: string): string {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
281
|
+
const code = entry.slice(2, 4);
|
|
282
|
+
if (!STATUS_XY_PATTERN.test(code)) {
|
|
283
|
+
throw new Error("Git status returned a malformed tracked status code.");
|
|
284
|
+
}
|
|
285
|
+
return code;
|
|
299
286
|
}
|
|
300
287
|
|
|
301
288
|
function countTrackedState(snapshot: WorktreeStatusSnapshot, code: string): void {
|
|
302
|
-
|
|
303
|
-
|
|
289
|
+
if (code[0] !== ".") snapshot.staged += 1;
|
|
290
|
+
if (code[1] !== ".") snapshot.unstaged += 1;
|
|
304
291
|
}
|
|
305
292
|
|
|
306
293
|
function assertFieldCount(entry: string, minimum: number, kind: string): void {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
294
|
+
if (entry.split(" ").length < minimum) {
|
|
295
|
+
throw new Error(`Git status returned a malformed ${kind} record.`);
|
|
296
|
+
}
|
|
310
297
|
}
|
|
311
298
|
|
|
312
299
|
function formatWorkingState(snapshot: WorktreeStatusSnapshot): string {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
].filter((value): value is string => value !== undefined);
|
|
321
|
-
return values.join(" · ") || "clean";
|
|
300
|
+
const values = [
|
|
301
|
+
snapshot.conflicts > 0 ? `${snapshot.conflicts} conflict${snapshot.conflicts === 1 ? "" : "s"}` : undefined,
|
|
302
|
+
snapshot.staged > 0 ? `${snapshot.staged} staged` : undefined,
|
|
303
|
+
snapshot.unstaged > 0 ? `${snapshot.unstaged} unstaged` : undefined,
|
|
304
|
+
snapshot.untracked > 0 ? `${snapshot.untracked} untracked` : undefined,
|
|
305
|
+
].filter((value): value is string => value !== undefined);
|
|
306
|
+
return values.join(" · ") || "clean";
|
|
322
307
|
}
|
|
323
308
|
|
|
324
309
|
function unavailableReason(record: WorktreeRecord): string | undefined {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
if (!existsSync(record.path)) return "Worktree path is missing";
|
|
332
|
-
return undefined;
|
|
310
|
+
if (record.bare) return "Bare worktree cannot be inspected";
|
|
311
|
+
if (record.prunableReason !== undefined) {
|
|
312
|
+
return record.prunableReason ? `Prunable: ${record.prunableReason}` : "Prunable worktree metadata";
|
|
313
|
+
}
|
|
314
|
+
if (!existsSync(record.path)) return "Worktree path is missing";
|
|
315
|
+
return undefined;
|
|
333
316
|
}
|
|
334
317
|
|
|
335
318
|
async function runGit(
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
319
|
+
pi: Pick<ExtensionAPI, "exec">,
|
|
320
|
+
args: string[],
|
|
321
|
+
cwd: string,
|
|
322
|
+
maxBuffer: number,
|
|
323
|
+
signal?: AbortSignal,
|
|
340
324
|
): Promise<ExecResult> {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
325
|
+
throwIfAborted(signal);
|
|
326
|
+
const result = await pi.exec("git", args, { cwd, signal, timeout: GIT_STATUS_TIMEOUT_MS, maxBuffer });
|
|
327
|
+
if (result.truncated) {
|
|
328
|
+
throw new Error(`git ${args.slice(0, 2).join(" ")} output exceeded the ${maxBuffer}-byte limit.`);
|
|
329
|
+
}
|
|
330
|
+
if (result.killed) {
|
|
331
|
+
throwIfAborted(signal);
|
|
332
|
+
throw new Error(`git ${args.slice(0, 2).join(" ")} timed out.`);
|
|
333
|
+
}
|
|
334
|
+
if (result.code !== 0) {
|
|
335
|
+
const detail = [result.stderr.trim(), result.stdout.trim()].filter(Boolean).join(" ");
|
|
336
|
+
throw new Error(detail || `git ${args.slice(0, 2).join(" ")} exited with code ${result.code}.`);
|
|
337
|
+
}
|
|
338
|
+
return result;
|
|
352
339
|
}
|
|
353
340
|
|
|
354
341
|
function parseLastCommit(output: string): LastCommitSummary {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
342
|
+
const separator = output.indexOf("\0");
|
|
343
|
+
if (separator <= 0 || output.indexOf("\0", separator + 1) >= 0) {
|
|
344
|
+
throw new Error("Git returned malformed last-commit details.");
|
|
345
|
+
}
|
|
346
|
+
const committedAt = output.slice(0, separator);
|
|
347
|
+
const subject = removeLineEnding(output.slice(separator + 1));
|
|
348
|
+
if (!committedAt) throw new Error("Git returned an empty last-commit timestamp.");
|
|
349
|
+
return { committedAt, subject };
|
|
363
350
|
}
|
|
364
351
|
|
|
365
352
|
function safeDisplay(value: string): string {
|
|
366
|
-
|
|
353
|
+
return stripTerminalControls(value);
|
|
367
354
|
}
|
|
368
355
|
|
|
369
356
|
function throwIfAborted(signal?: AbortSignal): void {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
357
|
+
if (signal?.aborted) {
|
|
358
|
+
throw new DOMException("Worktree status loading was aborted.", "AbortError");
|
|
359
|
+
}
|
|
373
360
|
}
|
|
374
361
|
|
|
375
362
|
function removeLineEnding(value: string): string {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
363
|
+
if (value.endsWith("\r\n")) return value.slice(0, -2);
|
|
364
|
+
if (value.endsWith("\n")) return value.slice(0, -1);
|
|
365
|
+
return value;
|
|
379
366
|
}
|
|
380
367
|
|
|
381
368
|
function formatError(error: unknown): string {
|
|
382
|
-
|
|
383
|
-
|
|
369
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
370
|
+
return safeDisplay(message).slice(0, 500);
|
|
384
371
|
}
|