@statewalker/vcs-commands 0.1.1 → 0.3.1

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.
Files changed (85) hide show
  1. package/dist/commands/commit-command.d.ts.map +1 -1
  2. package/dist/commands/init-command.d.ts.map +1 -1
  3. package/dist/commands/push-command.d.ts.map +1 -1
  4. package/dist/core-commands/checkout.command.impl.d.ts +1 -1
  5. package/dist/core-commands/checkout.command.impl.d.ts.map +1 -1
  6. package/dist/git-command.d.ts.map +1 -1
  7. package/package.json +19 -14
  8. package/src/commands/add-command.ts +494 -0
  9. package/src/commands/blame-command.ts +1241 -0
  10. package/src/commands/branch-command.ts +487 -0
  11. package/src/commands/checkout-command.ts +681 -0
  12. package/src/commands/cherry-pick-command.ts +544 -0
  13. package/src/commands/clean-command.ts +218 -0
  14. package/src/commands/clone-command.ts +532 -0
  15. package/src/commands/commit-command.ts +529 -0
  16. package/src/commands/describe-command.ts +454 -0
  17. package/src/commands/diff-command.ts +257 -0
  18. package/src/commands/fetch-command.ts +615 -0
  19. package/src/commands/gc-command.ts +155 -0
  20. package/src/commands/index.ts +31 -0
  21. package/src/commands/init-command.ts +365 -0
  22. package/src/commands/log-command.ts +549 -0
  23. package/src/commands/ls-remote-command.ts +237 -0
  24. package/src/commands/merge-command.ts +750 -0
  25. package/src/commands/pack-refs-command.ts +101 -0
  26. package/src/commands/pull-command.ts +258 -0
  27. package/src/commands/push-command.ts +682 -0
  28. package/src/commands/rebase-command.ts +665 -0
  29. package/src/commands/reflog-command.ts +68 -0
  30. package/src/commands/remote-command.ts +385 -0
  31. package/src/commands/reset-command.ts +292 -0
  32. package/src/commands/revert-command.ts +552 -0
  33. package/src/commands/rm-command.ts +178 -0
  34. package/src/commands/stash-apply-command.ts +402 -0
  35. package/src/commands/stash-create-command.ts +339 -0
  36. package/src/commands/stash-drop-command.ts +115 -0
  37. package/src/commands/stash-list-command.ts +116 -0
  38. package/src/commands/status-command.ts +173 -0
  39. package/src/commands/tag-command.ts +378 -0
  40. package/src/commands/tree-restore.ts +126 -0
  41. package/src/core-commands/add.command.impl.ts +274 -0
  42. package/src/core-commands/add.command.ts +81 -0
  43. package/src/core-commands/checkout.command.impl.ts +586 -0
  44. package/src/core-commands/checkout.command.ts +125 -0
  45. package/src/core-commands/index.ts +13 -0
  46. package/src/errors/add-errors.ts +20 -0
  47. package/src/errors/checkout-errors.ts +42 -0
  48. package/src/errors/command-errors.ts +68 -0
  49. package/src/errors/commit-errors.ts +40 -0
  50. package/src/errors/git-api-error.ts +11 -0
  51. package/src/errors/index.ts +12 -0
  52. package/src/errors/merge-errors.ts +94 -0
  53. package/src/errors/rebase-errors.ts +34 -0
  54. package/src/errors/ref-errors.ts +100 -0
  55. package/src/errors/remote-errors.ts +27 -0
  56. package/src/errors/stash-errors.ts +42 -0
  57. package/src/errors/tag-errors.ts +16 -0
  58. package/src/errors/transport-errors.ts +68 -0
  59. package/src/git-command.ts +421 -0
  60. package/src/git.ts +910 -0
  61. package/src/index.ts +18 -0
  62. package/src/pack-import/index.ts +8 -0
  63. package/src/pack-import/pack-import.ts +24 -0
  64. package/src/remote-config/index.ts +12 -0
  65. package/src/remote-config/remote-config-store.ts +285 -0
  66. package/src/rename/index.ts +5 -0
  67. package/src/rename/similarity-index.ts +185 -0
  68. package/src/results/cherry-pick-result.ts +35 -0
  69. package/src/results/clone-result.ts +25 -0
  70. package/src/results/diff-entry.ts +162 -0
  71. package/src/results/diff-formatter.ts +416 -0
  72. package/src/results/fetch-result.ts +91 -0
  73. package/src/results/index.ts +13 -0
  74. package/src/results/init-result.ts +29 -0
  75. package/src/results/merge-result.ts +135 -0
  76. package/src/results/pull-result.ts +27 -0
  77. package/src/results/push-result.ts +86 -0
  78. package/src/results/rebase-result.ts +140 -0
  79. package/src/results/revert-result.ts +35 -0
  80. package/src/results/stash-result.ts +60 -0
  81. package/src/results/status-result.ts +65 -0
  82. package/src/transport-command.ts +125 -0
  83. package/src/tree-merge/index.ts +8 -0
  84. package/src/tree-merge/tree-merge.ts +249 -0
  85. package/src/types.ts +70 -0
@@ -0,0 +1,681 @@
1
+ /**
2
+ * CheckoutCommand - Switch branches or restore files.
3
+ *
4
+ * Implements JGit-compatible fluent API for `git checkout` command:
5
+ * - Checkout branches, tags, or commits
6
+ * - Create new branches on checkout (-b flag)
7
+ * - Restore specific paths from index or commits
8
+ * - Detached HEAD mode for commit checkout
9
+ *
10
+ * Reference: jgit/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // Checkout existing branch
15
+ * await git.checkout().setName("feature").call();
16
+ *
17
+ * // Create and checkout new branch
18
+ * await git.checkout()
19
+ * .setCreateBranch(true)
20
+ * .setName("newbranch")
21
+ * .call();
22
+ *
23
+ * // Checkout paths from index
24
+ * await git.checkout()
25
+ * .addPath("file.txt")
26
+ * .call();
27
+ *
28
+ * // Checkout paths from specific commit
29
+ * await git.checkout()
30
+ * .setStartPoint("HEAD~1")
31
+ * .addPath("file.txt")
32
+ * .call();
33
+ * ```
34
+ */
35
+
36
+ import { FileMode, type ObjectId, type Ref } from "@statewalker/vcs-core";
37
+ import { UpdateStagingEntry } from "@statewalker/vcs-working-tree";
38
+
39
+ import {
40
+ MissingArgumentError,
41
+ NotADirectoryError,
42
+ PathNotFoundInTreeError,
43
+ PathNotInIndexError,
44
+ RefNotFoundError,
45
+ } from "../errors/index.js";
46
+ import { GitCommand } from "../git-command.js";
47
+
48
+ /**
49
+ * Stage to check out for conflicting files.
50
+ */
51
+ export enum CheckoutStage {
52
+ /** Base stage (ancestor) */
53
+ BASE = 1,
54
+ /** Ours stage (current branch) */
55
+ OURS = 2,
56
+ /** Theirs stage (merged branch) */
57
+ THEIRS = 3,
58
+ }
59
+
60
+ /**
61
+ * Status of checkout operation.
62
+ */
63
+ export enum CheckoutStatus {
64
+ /** Checkout completed successfully */
65
+ OK = "OK",
66
+ /** Checkout had conflicts */
67
+ CONFLICTS = "CONFLICTS",
68
+ /** Some files couldn't be deleted */
69
+ NONDELETED = "NONDELETED",
70
+ /** Checkout not attempted */
71
+ NOT_TRIED = "NOT_TRIED",
72
+ /** Checkout failed with error */
73
+ ERROR = "ERROR",
74
+ }
75
+
76
+ /**
77
+ * Result of CheckoutCommand execution.
78
+ */
79
+ export interface CheckoutResult {
80
+ /** Status of the operation */
81
+ readonly status: CheckoutStatus;
82
+
83
+ /** Files that were updated */
84
+ readonly updated: string[];
85
+
86
+ /** Files that were removed */
87
+ readonly removed: string[];
88
+
89
+ /** Files that couldn't be checked out (conflicts) */
90
+ readonly conflicts: string[];
91
+
92
+ /** The ref that was checked out (null for path checkout) */
93
+ readonly ref: Ref | null;
94
+ }
95
+
96
+ /**
97
+ * Command to checkout branches, commits, or paths.
98
+ *
99
+ * Based on JGit's CheckoutCommand. Supports both branch checkout
100
+ * (switching HEAD) and path checkout (restoring files).
101
+ */
102
+ export class CheckoutCommand extends GitCommand<CheckoutResult> {
103
+ private name: string | undefined;
104
+ private createBranch = false;
105
+ private orphan = false;
106
+ private force = false;
107
+ // biome-ignore lint/correctness/noUnusedPrivateClassMembers: JGit compatibility - will be used for branch reset
108
+ private forceRefUpdate = false;
109
+ private startPoint: string | undefined;
110
+ private paths: string[] = [];
111
+ private allPaths = false;
112
+ private stage: CheckoutStage | undefined;
113
+
114
+ /**
115
+ * Set the name of the branch or commit to check out.
116
+ *
117
+ * When checking out a branch, HEAD becomes a symbolic ref to that branch.
118
+ * When checking out a commit/tag, HEAD becomes detached.
119
+ *
120
+ * @param name Branch name, tag name, or commit ID
121
+ * @returns this for chaining
122
+ */
123
+ setName(name: string): this {
124
+ this.checkCallable();
125
+ this.name = name;
126
+ return this;
127
+ }
128
+
129
+ /**
130
+ * Set whether to create a new branch.
131
+ *
132
+ * If true, a new branch with setName() will be created at setStartPoint()
133
+ * (or HEAD if not specified) and checked out.
134
+ *
135
+ * Equivalent to `git checkout -b <name>`.
136
+ *
137
+ * @param createBranch Whether to create a new branch
138
+ * @returns this for chaining
139
+ */
140
+ setCreateBranch(createBranch: boolean): this {
141
+ this.checkCallable();
142
+ this.createBranch = createBranch;
143
+ return this;
144
+ }
145
+
146
+ /**
147
+ * Set whether to create an orphan branch.
148
+ *
149
+ * An orphan branch has no parent commits. The index and working tree
150
+ * are set to the start point, but no commit is made.
151
+ *
152
+ * Equivalent to `git checkout --orphan <name>`.
153
+ *
154
+ * @param orphan Whether to create orphan branch
155
+ * @returns this for chaining
156
+ */
157
+ setOrphan(orphan: boolean): this {
158
+ this.checkCallable();
159
+ this.orphan = orphan;
160
+ return this;
161
+ }
162
+
163
+ /**
164
+ * Set whether to force checkout.
165
+ *
166
+ * If true, checkout will proceed even if there are local changes
167
+ * that would be overwritten.
168
+ *
169
+ * Equivalent to `git checkout -f` or `git checkout --force`.
170
+ *
171
+ * @param force Whether to force checkout
172
+ * @returns this for chaining
173
+ */
174
+ setForced(force: boolean): this {
175
+ this.checkCallable();
176
+ this.force = force;
177
+ return this;
178
+ }
179
+
180
+ /**
181
+ * Set whether to force ref update.
182
+ *
183
+ * If true and the branch already exists, it will be reset to
184
+ * the start point.
185
+ *
186
+ * @param forceRefUpdate Whether to force ref update
187
+ * @returns this for chaining
188
+ */
189
+ setForceRefUpdate(forceRefUpdate: boolean): this {
190
+ this.checkCallable();
191
+ this.forceRefUpdate = forceRefUpdate;
192
+ return this;
193
+ }
194
+
195
+ /**
196
+ * Set the start point for branch creation or path checkout.
197
+ *
198
+ * For branch creation: the commit where the new branch starts.
199
+ * For path checkout: the commit to restore files from (default: index).
200
+ *
201
+ * @param startPoint Commit ID, branch name, or tag name
202
+ * @returns this for chaining
203
+ */
204
+ setStartPoint(startPoint: string): this {
205
+ this.checkCallable();
206
+ this.startPoint = startPoint;
207
+ return this;
208
+ }
209
+
210
+ /**
211
+ * Add a path to checkout.
212
+ *
213
+ * When paths are specified, this becomes a path checkout rather than
214
+ * a branch checkout. Files are restored from index or setStartPoint().
215
+ *
216
+ * @param path Path to checkout
217
+ * @returns this for chaining
218
+ */
219
+ addPath(path: string): this {
220
+ this.checkCallable();
221
+ this.paths.push(path);
222
+ return this;
223
+ }
224
+
225
+ /**
226
+ * Set whether to checkout all paths.
227
+ *
228
+ * When true, all files in the index (or start point) are checked out.
229
+ *
230
+ * @param allPaths Whether to checkout all paths
231
+ * @returns this for chaining
232
+ */
233
+ setAllPaths(allPaths: boolean): this {
234
+ this.checkCallable();
235
+ this.allPaths = allPaths;
236
+ return this;
237
+ }
238
+
239
+ /**
240
+ * Set the stage to checkout for conflicting files.
241
+ *
242
+ * Only applicable for path checkout from index when files have
243
+ * conflicts (multiple stages).
244
+ *
245
+ * @param stage Stage to checkout (OURS, THEIRS, or BASE)
246
+ * @returns this for chaining
247
+ */
248
+ setStage(stage: CheckoutStage): this {
249
+ this.checkCallable();
250
+ this.stage = stage;
251
+ return this;
252
+ }
253
+
254
+ /**
255
+ * Execute the checkout command.
256
+ *
257
+ * @returns Result with status and affected files
258
+ * @throws RefNotFoundError if target cannot be resolved
259
+ */
260
+ async call(): Promise<CheckoutResult> {
261
+ this.checkCallable();
262
+ this.setCallable(false);
263
+
264
+ // Path checkout mode
265
+ if (this.allPaths || this.paths.length > 0) {
266
+ return this.checkoutPaths();
267
+ }
268
+
269
+ // Branch checkout mode
270
+ return this.checkoutBranch();
271
+ }
272
+
273
+ /**
274
+ * Checkout paths from index or commit.
275
+ */
276
+ private async checkoutPaths(): Promise<CheckoutResult> {
277
+ const updated: string[] = [];
278
+ const conflicts: string[] = [];
279
+
280
+ // Determine source tree
281
+ let sourceTreeId: ObjectId | undefined;
282
+ if (this.startPoint) {
283
+ sourceTreeId = await this.resolveTreeId(this.startPoint);
284
+ if (!sourceTreeId) {
285
+ throw new RefNotFoundError(this.startPoint);
286
+ }
287
+ }
288
+
289
+ // Get paths to checkout
290
+ const pathsToCheckout = this.allPaths ? await this.getAllPaths(sourceTreeId) : this.paths;
291
+
292
+ // Checkout each path
293
+ for (const path of pathsToCheckout) {
294
+ try {
295
+ if (sourceTreeId) {
296
+ // Checkout from commit tree
297
+ await this.checkoutPathFromTree(path, sourceTreeId);
298
+ } else {
299
+ // Checkout from index
300
+ await this.checkoutPathFromIndex(path);
301
+ }
302
+ updated.push(path);
303
+ } catch {
304
+ conflicts.push(path);
305
+ }
306
+ }
307
+
308
+ return {
309
+ status: conflicts.length > 0 ? CheckoutStatus.CONFLICTS : CheckoutStatus.OK,
310
+ updated,
311
+ removed: [],
312
+ conflicts,
313
+ ref: null,
314
+ };
315
+ }
316
+
317
+ /**
318
+ * Detect conflicts between staged changes and checkout target.
319
+ *
320
+ * Returns list of paths that have uncommitted staged changes.
321
+ * Git prevents checkout when staged changes differ from HEAD,
322
+ * as they would be lost during checkout.
323
+ *
324
+ * @param _targetTreeId Target tree to checkout (unused for now, checks HEAD vs staging)
325
+ * @returns Array of conflicting paths
326
+ */
327
+ private async detectCheckoutConflicts(_targetTreeId?: ObjectId): Promise<string[]> {
328
+ const conflicts: string[] = [];
329
+
330
+ // Get current HEAD tree for comparison
331
+ const headId = await this.resolveCommitId("HEAD");
332
+ if (!headId) {
333
+ // No HEAD commit yet (empty repository) - no conflicts possible
334
+ return conflicts;
335
+ }
336
+
337
+ const headTreeId = await this.commits.getTree(headId);
338
+ if (!headTreeId) {
339
+ return conflicts;
340
+ }
341
+
342
+ // Collect HEAD tree entries
343
+ const headEntries = new Map<string, { objectId: ObjectId; mode: number }>();
344
+ await this.collectTreeEntries(headTreeId, "", headEntries);
345
+
346
+ // Compare staging with HEAD
347
+ for await (const entry of this.staging.entries()) {
348
+ // Skip conflict stages (1-3) - only check stage 0
349
+ if (entry.stage !== 0) continue;
350
+
351
+ const headEntry = headEntries.get(entry.path);
352
+
353
+ // Check if staged content differs from HEAD
354
+ if (!headEntry || headEntry.objectId !== entry.objectId) {
355
+ // Staged change differs from HEAD - this is an uncommitted change
356
+ conflicts.push(entry.path);
357
+ }
358
+ }
359
+
360
+ return conflicts;
361
+ }
362
+
363
+ /**
364
+ * Checkout a branch or commit.
365
+ */
366
+ private async checkoutBranch(): Promise<CheckoutResult> {
367
+ if (!this.name) {
368
+ throw new MissingArgumentError("name", "Branch name is required for checkout");
369
+ }
370
+
371
+ // Create branch if requested
372
+ if (this.createBranch) {
373
+ const startPointId = this.startPoint
374
+ ? await this.resolveCommitId(this.startPoint)
375
+ : await this.resolveCommitId("HEAD");
376
+
377
+ if (!startPointId) {
378
+ throw new RefNotFoundError(this.startPoint ?? "HEAD");
379
+ }
380
+
381
+ await this.refsStore.set(`refs/heads/${this.name}`, startPointId);
382
+ }
383
+
384
+ // Resolve target
385
+ const targetId = await this.resolveCommitId(this.name);
386
+ if (!targetId && !this.orphan) {
387
+ throw new RefNotFoundError(this.name);
388
+ }
389
+
390
+ // Get target tree for conflict detection and checkout
391
+ const targetTreeId = targetId ? await this.commits.getTree(targetId) : undefined;
392
+
393
+ // Detect conflicts with staged changes before checkout
394
+ if (!this.force && !this.orphan) {
395
+ const conflicts = await this.detectCheckoutConflicts(targetTreeId);
396
+ if (conflicts.length > 0) {
397
+ return {
398
+ status: CheckoutStatus.CONFLICTS,
399
+ updated: [],
400
+ removed: [],
401
+ conflicts,
402
+ ref: null,
403
+ };
404
+ }
405
+ }
406
+
407
+ // Determine if we should use symbolic ref (branch) or detached HEAD (commit/tag)
408
+ // Check if it's a local branch
409
+ const isLocalBranch = await this.refsStore.has(`refs/heads/${this.name}`);
410
+
411
+ // Decide whether to create symbolic ref or detached HEAD
412
+ if (this.orphan) {
413
+ // Orphan branch: symbolic ref to a branch that doesn't exist yet
414
+ await this.refsStore.setSymbolic("HEAD", `refs/heads/${this.name}`);
415
+ } else if (isLocalBranch) {
416
+ // Local branch exists: use symbolic ref to the branch
417
+ await this.refsStore.setSymbolic("HEAD", `refs/heads/${this.name}`);
418
+ } else if (targetId) {
419
+ // Not a local branch but resolves to a commit: detached HEAD
420
+ // This handles: commit IDs, tags, remote branches, etc.
421
+ await this.refsStore.set("HEAD", targetId);
422
+ }
423
+
424
+ // Sync staging to match target tree AFTER updating HEAD
425
+ const updated: string[] = [];
426
+ const removed: string[] = [];
427
+
428
+ if (targetTreeId) {
429
+ // Use this.trees which is CommandTrees (has loadTree method for TreeStore compatibility)
430
+ await this.staging.readTree(this.trees as any, targetTreeId);
431
+
432
+ // Persist staging to .git/index so native git sees the correct state
433
+ await this.staging.write();
434
+
435
+ // Write files to working directory if not a bare repository
436
+ if (this.hasFileWriteSupport()) {
437
+ await this.writeTreeToWorkdir(targetTreeId, "");
438
+ }
439
+ }
440
+
441
+ // Get the ref we checked out
442
+ const ref = await this.refsStore.resolve("HEAD");
443
+
444
+ return {
445
+ status: CheckoutStatus.OK,
446
+ updated,
447
+ removed,
448
+ conflicts: [],
449
+ ref: ref ?? null,
450
+ };
451
+ }
452
+
453
+ /**
454
+ * Check if the store supports file writes (not a bare repository).
455
+ *
456
+ * A repository is considered "bare" if it doesn't have a worktree interface.
457
+ */
458
+ private hasFileWriteSupport(): boolean {
459
+ return !!this.worktreeAccess;
460
+ }
461
+
462
+ /**
463
+ * Write all files from a tree to the working directory.
464
+ *
465
+ * @param treeId The tree ID to checkout
466
+ * @param basePath The base path prefix for entries in this tree
467
+ */
468
+ private async writeTreeToWorkdir(treeId: ObjectId, basePath: string): Promise<void> {
469
+ const worktree = this.worktreeAccess;
470
+ if (!worktree) {
471
+ throw new Error("Worktree not available for file writes");
472
+ }
473
+
474
+ for await (const entry of this.trees.loadTree(treeId)) {
475
+ const relativePath = basePath ? `${basePath}/${entry.name}` : entry.name;
476
+ const isDirectory = entry.mode === FileMode.TREE;
477
+
478
+ if (isDirectory) {
479
+ // Recursively process subdirectories
480
+ await this.writeTreeToWorkdir(entry.id, relativePath);
481
+ } else {
482
+ // Ensure parent directory exists
483
+ const lastSlash = relativePath.lastIndexOf("/");
484
+ if (lastSlash > 0) {
485
+ const parentDir = relativePath.substring(0, lastSlash);
486
+ await worktree.mkdir(parentDir);
487
+ }
488
+
489
+ // Load blob content
490
+ const chunks: Uint8Array[] = [];
491
+ const blobContent = await this.blobs.load(entry.id);
492
+ if (blobContent) {
493
+ for await (const chunk of blobContent) {
494
+ chunks.push(chunk);
495
+ }
496
+ }
497
+
498
+ // Write file using worktree interface
499
+ await worktree.writeContent(relativePath, chunks);
500
+ }
501
+ }
502
+ }
503
+
504
+ /**
505
+ * Checkout a path from the staging index.
506
+ */
507
+ private async checkoutPathFromIndex(path: string): Promise<void> {
508
+ // Find the entry in staging
509
+ let found = false;
510
+ for await (const entry of this.staging.entries()) {
511
+ if (entry.path !== path) continue;
512
+
513
+ // Handle conflict stages
514
+ if (entry.stage !== 0) {
515
+ if (this.stage && entry.stage === this.stage) {
516
+ // Replace with specified stage
517
+ const editor = this.staging.createEditor();
518
+ editor.add(
519
+ new UpdateStagingEntry(path, entry.objectId, entry.mode, {
520
+ size: entry.size,
521
+ mtime: Date.now(),
522
+ }),
523
+ );
524
+ await editor.finish();
525
+ found = true;
526
+ break;
527
+ }
528
+ continue;
529
+ }
530
+
531
+ found = true;
532
+ break;
533
+ }
534
+
535
+ if (!found) {
536
+ throw new PathNotInIndexError(path);
537
+ }
538
+ }
539
+
540
+ /**
541
+ * Checkout a path from a tree.
542
+ */
543
+ private async checkoutPathFromTree(path: string, treeId: ObjectId): Promise<void> {
544
+ // Navigate tree to find the entry
545
+ const parts = path.split("/");
546
+ let currentTreeId = treeId;
547
+
548
+ for (let i = 0; i < parts.length; i++) {
549
+ const name = parts[i];
550
+ const entry = await this.trees.getEntry(currentTreeId, name);
551
+
552
+ if (!entry) {
553
+ throw new PathNotFoundInTreeError(path);
554
+ }
555
+
556
+ if (i < parts.length - 1) {
557
+ if (entry.mode !== FileMode.TREE) {
558
+ throw new NotADirectoryError(parts.slice(0, i + 1).join("/"));
559
+ }
560
+ currentTreeId = entry.id;
561
+ } else {
562
+ // Found the file - update staging
563
+ const editor = this.staging.createEditor();
564
+ editor.add(
565
+ new UpdateStagingEntry(path, entry.id, entry.mode, {
566
+ size: 0,
567
+ mtime: Date.now(),
568
+ }),
569
+ );
570
+ await editor.finish();
571
+ }
572
+ }
573
+ }
574
+
575
+ /**
576
+ * Get all paths from staging or tree.
577
+ */
578
+ private async getAllPaths(sourceTreeId?: ObjectId): Promise<string[]> {
579
+ const paths: string[] = [];
580
+
581
+ if (sourceTreeId) {
582
+ // Get all paths from tree
583
+ await this.collectTreePaths(sourceTreeId, "", paths);
584
+ } else {
585
+ // Get all paths from staging
586
+ for await (const entry of this.staging.entries()) {
587
+ if (entry.stage === 0 && !paths.includes(entry.path)) {
588
+ paths.push(entry.path);
589
+ }
590
+ }
591
+ }
592
+
593
+ return paths;
594
+ }
595
+
596
+ /**
597
+ * Collect all paths from a tree recursively.
598
+ */
599
+ private async collectTreePaths(treeId: ObjectId, prefix: string, paths: string[]): Promise<void> {
600
+ for await (const entry of this.trees.loadTree(treeId)) {
601
+ const path = prefix ? `${prefix}/${entry.name}` : entry.name;
602
+
603
+ if (entry.mode === FileMode.TREE) {
604
+ await this.collectTreePaths(entry.id, path, paths);
605
+ } else {
606
+ paths.push(path);
607
+ }
608
+ }
609
+ }
610
+
611
+ /**
612
+ * Collect tree entries into a map.
613
+ */
614
+ private async collectTreeEntries(
615
+ treeId: ObjectId,
616
+ prefix: string,
617
+ entries: Map<string, { objectId: ObjectId; mode: number }>,
618
+ ): Promise<void> {
619
+ for await (const entry of this.trees.loadTree(treeId)) {
620
+ const path = prefix ? `${prefix}/${entry.name}` : entry.name;
621
+
622
+ if (entry.mode === FileMode.TREE) {
623
+ await this.collectTreeEntries(entry.id, path, entries);
624
+ } else {
625
+ entries.set(path, { objectId: entry.id, mode: entry.mode });
626
+ }
627
+ }
628
+ }
629
+
630
+ /**
631
+ * Resolve a ref to a tree ID.
632
+ */
633
+ private async resolveTreeId(refName: string): Promise<ObjectId | undefined> {
634
+ const commitId = await this.resolveCommitId(refName);
635
+ if (!commitId) return undefined;
636
+ return this.commits.getTree(commitId);
637
+ }
638
+
639
+ /**
640
+ * Resolve a ref to a commit ID.
641
+ */
642
+ private async resolveCommitId(refName: string): Promise<ObjectId | undefined> {
643
+ // Try as branch
644
+ let ref = await this.refsStore.resolve(`refs/heads/${refName}`);
645
+ if (ref?.objectId) return ref.objectId;
646
+
647
+ // Try as tag
648
+ ref = await this.refsStore.resolve(`refs/tags/${refName}`);
649
+ if (ref?.objectId) return ref.objectId;
650
+
651
+ // Try as direct ref (HEAD, etc.)
652
+ ref = await this.refsStore.resolve(refName);
653
+ if (ref?.objectId) return ref.objectId;
654
+
655
+ // Try as commit ID
656
+ if (await this.commits.has(refName)) {
657
+ return refName;
658
+ }
659
+
660
+ return undefined;
661
+ }
662
+
663
+ /**
664
+ * Collect object IDs from a tree recursively.
665
+ */
666
+ private async collectTreeObjectIds(
667
+ treeId: ObjectId,
668
+ prefix: string,
669
+ entries: Map<string, ObjectId>,
670
+ ): Promise<void> {
671
+ for await (const entry of this.trees.loadTree(treeId)) {
672
+ const path = prefix ? `${prefix}/${entry.name}` : entry.name;
673
+
674
+ if (entry.mode === FileMode.TREE) {
675
+ await this.collectTreeObjectIds(entry.id, path, entries);
676
+ } else {
677
+ entries.set(path, entry.id);
678
+ }
679
+ }
680
+ }
681
+ }