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