@statewalker/vcs-commands 0.1.1 → 0.3.2

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 (86) hide show
  1. package/README.md +180 -466
  2. package/dist/commands/commit-command.d.ts.map +1 -1
  3. package/dist/commands/init-command.d.ts.map +1 -1
  4. package/dist/commands/push-command.d.ts.map +1 -1
  5. package/dist/core-commands/checkout.command.impl.d.ts +1 -1
  6. package/dist/core-commands/checkout.command.impl.d.ts.map +1 -1
  7. package/dist/git-command.d.ts.map +1 -1
  8. package/package.json +25 -16
  9. package/src/commands/add-command.ts +494 -0
  10. package/src/commands/blame-command.ts +1241 -0
  11. package/src/commands/branch-command.ts +487 -0
  12. package/src/commands/checkout-command.ts +681 -0
  13. package/src/commands/cherry-pick-command.ts +544 -0
  14. package/src/commands/clean-command.ts +218 -0
  15. package/src/commands/clone-command.ts +532 -0
  16. package/src/commands/commit-command.ts +529 -0
  17. package/src/commands/describe-command.ts +454 -0
  18. package/src/commands/diff-command.ts +257 -0
  19. package/src/commands/fetch-command.ts +615 -0
  20. package/src/commands/gc-command.ts +155 -0
  21. package/src/commands/index.ts +31 -0
  22. package/src/commands/init-command.ts +365 -0
  23. package/src/commands/log-command.ts +549 -0
  24. package/src/commands/ls-remote-command.ts +237 -0
  25. package/src/commands/merge-command.ts +750 -0
  26. package/src/commands/pack-refs-command.ts +101 -0
  27. package/src/commands/pull-command.ts +258 -0
  28. package/src/commands/push-command.ts +682 -0
  29. package/src/commands/rebase-command.ts +665 -0
  30. package/src/commands/reflog-command.ts +68 -0
  31. package/src/commands/remote-command.ts +385 -0
  32. package/src/commands/reset-command.ts +292 -0
  33. package/src/commands/revert-command.ts +552 -0
  34. package/src/commands/rm-command.ts +178 -0
  35. package/src/commands/stash-apply-command.ts +402 -0
  36. package/src/commands/stash-create-command.ts +339 -0
  37. package/src/commands/stash-drop-command.ts +115 -0
  38. package/src/commands/stash-list-command.ts +116 -0
  39. package/src/commands/status-command.ts +173 -0
  40. package/src/commands/tag-command.ts +378 -0
  41. package/src/commands/tree-restore.ts +126 -0
  42. package/src/core-commands/add.command.impl.ts +274 -0
  43. package/src/core-commands/add.command.ts +81 -0
  44. package/src/core-commands/checkout.command.impl.ts +586 -0
  45. package/src/core-commands/checkout.command.ts +125 -0
  46. package/src/core-commands/index.ts +13 -0
  47. package/src/errors/add-errors.ts +20 -0
  48. package/src/errors/checkout-errors.ts +42 -0
  49. package/src/errors/command-errors.ts +68 -0
  50. package/src/errors/commit-errors.ts +40 -0
  51. package/src/errors/git-api-error.ts +11 -0
  52. package/src/errors/index.ts +12 -0
  53. package/src/errors/merge-errors.ts +94 -0
  54. package/src/errors/rebase-errors.ts +34 -0
  55. package/src/errors/ref-errors.ts +100 -0
  56. package/src/errors/remote-errors.ts +27 -0
  57. package/src/errors/stash-errors.ts +42 -0
  58. package/src/errors/tag-errors.ts +16 -0
  59. package/src/errors/transport-errors.ts +68 -0
  60. package/src/git-command.ts +421 -0
  61. package/src/git.ts +910 -0
  62. package/src/index.ts +18 -0
  63. package/src/pack-import/index.ts +8 -0
  64. package/src/pack-import/pack-import.ts +24 -0
  65. package/src/remote-config/index.ts +12 -0
  66. package/src/remote-config/remote-config-store.ts +285 -0
  67. package/src/rename/index.ts +5 -0
  68. package/src/rename/similarity-index.ts +185 -0
  69. package/src/results/cherry-pick-result.ts +35 -0
  70. package/src/results/clone-result.ts +25 -0
  71. package/src/results/diff-entry.ts +162 -0
  72. package/src/results/diff-formatter.ts +416 -0
  73. package/src/results/fetch-result.ts +91 -0
  74. package/src/results/index.ts +13 -0
  75. package/src/results/init-result.ts +29 -0
  76. package/src/results/merge-result.ts +135 -0
  77. package/src/results/pull-result.ts +27 -0
  78. package/src/results/push-result.ts +86 -0
  79. package/src/results/rebase-result.ts +140 -0
  80. package/src/results/revert-result.ts +35 -0
  81. package/src/results/stash-result.ts +60 -0
  82. package/src/results/status-result.ts +65 -0
  83. package/src/transport-command.ts +125 -0
  84. package/src/tree-merge/index.ts +8 -0
  85. package/src/tree-merge/tree-merge.ts +249 -0
  86. package/src/types.ts +70 -0
package/src/git.ts ADDED
@@ -0,0 +1,910 @@
1
+ import type { History } from "@statewalker/vcs-core";
2
+ import type { Checkout, WorkingCopy, Worktree } from "@statewalker/vcs-working-tree";
3
+
4
+ import {
5
+ AddCommand,
6
+ BlameCommand,
7
+ CheckoutCommand,
8
+ CherryPickCommand,
9
+ CleanCommand,
10
+ CloneCommand,
11
+ CommitCommand,
12
+ CreateBranchCommand,
13
+ DeleteBranchCommand,
14
+ DeleteTagCommand,
15
+ DescribeCommand,
16
+ DiffCommand,
17
+ FetchCommand,
18
+ GarbageCollectCommand,
19
+ InitCommand,
20
+ ListBranchCommand,
21
+ ListTagCommand,
22
+ LogCommand,
23
+ LsRemoteCommand,
24
+ MergeCommand,
25
+ PackRefsCommand,
26
+ PullCommand,
27
+ PushCommand,
28
+ RebaseCommand,
29
+ ReflogCommand,
30
+ RemoteAddCommand,
31
+ RemoteListCommand,
32
+ RemoteRemoveCommand,
33
+ RemoteSetUrlCommand,
34
+ RenameBranchCommand,
35
+ ResetCommand,
36
+ RevertCommand,
37
+ RmCommand,
38
+ StashApplyCommand,
39
+ StashCreateCommand,
40
+ StashDropCommand,
41
+ StashListCommand,
42
+ StatusCommand,
43
+ TagCommand,
44
+ } from "./commands/index.js";
45
+
46
+ /**
47
+ * Main entry point for high-level Git operations.
48
+ *
49
+ * The Git class is a facade that wraps a WorkingCopy and provides
50
+ * factory methods for all Git commands.
51
+ *
52
+ * Based on JGit's Git class.
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * // Create a WorkingCopy
57
+ * const workingCopy = await createWorkingCopy({ files, workDir: "/repo" });
58
+ *
59
+ * // Create Git facade
60
+ * const git = Git.fromWorkingCopy(workingCopy);
61
+ *
62
+ * // Use commands
63
+ * await git.commit().setMessage("Initial commit").call();
64
+ * const branches = await git.branchList().call();
65
+ *
66
+ * // Access components for advanced usage
67
+ * const history = git.history;
68
+ * const checkout = git.checkout;
69
+ *
70
+ * // Clean up
71
+ * git.close();
72
+ * ```
73
+ */
74
+ export class Git implements Disposable {
75
+ private readonly _workingCopy: WorkingCopy;
76
+ private closed = false;
77
+
78
+ private constructor(workingCopy: WorkingCopy) {
79
+ this._workingCopy = workingCopy;
80
+ }
81
+
82
+ // ============ Static Factory Methods ============
83
+
84
+ /**
85
+ * Create a Git facade from a WorkingCopy.
86
+ *
87
+ * This is the recommended way to create a Git instance. The WorkingCopy
88
+ * provides access to all components: History, Checkout, and Worktree.
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * const workingCopy = await createWorkingCopy({ files, workDir: "/repo" });
93
+ * const git = Git.fromWorkingCopy(workingCopy);
94
+ *
95
+ * // Use commands
96
+ * await git.add().addFilepattern(".").call();
97
+ * await git.commit().setMessage("Initial commit").call();
98
+ *
99
+ * // Access components
100
+ * const history = git.history;
101
+ * const checkout = git.checkout;
102
+ * ```
103
+ *
104
+ * @param workingCopy The WorkingCopy to wrap
105
+ * @returns A Git instance wrapping the working copy
106
+ */
107
+ static fromWorkingCopy(workingCopy: WorkingCopy): Git {
108
+ return new Git(workingCopy);
109
+ }
110
+
111
+ /**
112
+ * Create an InitCommand for initializing a new Git repository.
113
+ *
114
+ * This static factory method provides convenient access to repository
115
+ * initialization without needing to import InitCommand directly.
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * // Initialize in-memory repository
120
+ * const result = await Git.init().call();
121
+ * const git = result.git;
122
+ *
123
+ * // Initialize file-based repository with worktree support
124
+ * const result = await Git.init()
125
+ * .setFilesApi(files)
126
+ * .setWorktree(true)
127
+ * .call();
128
+ *
129
+ * // Now git.add() works
130
+ * await result.git.add().addFilepattern(".").call();
131
+ *
132
+ * // Initialize with custom staging for native git compatibility
133
+ * import { FileStagingStore } from "@statewalker/vcs-store-files";
134
+ * const staging = new FileStagingStore(files, ".git/index");
135
+ *
136
+ * const result = await Git.init()
137
+ * .setFilesApi(files)
138
+ * .setStagingStore(staging)
139
+ * .setWorktree(true)
140
+ * .call();
141
+ * ```
142
+ *
143
+ * @returns A new InitCommand instance
144
+ */
145
+ static init(): InitCommand {
146
+ return new InitCommand();
147
+ }
148
+
149
+ // ============ Add Operations ============
150
+
151
+ /**
152
+ * Create an AddCommand for staging files from working tree.
153
+ *
154
+ * Requires a WorkingCopy with worktree interface.
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * // Add specific files
159
+ * await git.add()
160
+ * .addFilepattern("src/")
161
+ * .addFilepattern("lib/")
162
+ * .call();
163
+ *
164
+ * // Update only tracked files (git add -u)
165
+ * await git.add()
166
+ * .addFilepattern(".")
167
+ * .setUpdate(true)
168
+ * .call();
169
+ * ```
170
+ */
171
+ add(): AddCommand {
172
+ this.checkClosed();
173
+ return new AddCommand(this._workingCopy);
174
+ }
175
+
176
+ // ============ Blame Operations ============
177
+
178
+ /**
179
+ * Create a BlameCommand for annotating file lines with authorship.
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * const result = await git.blame()
184
+ * .setFilePath("src/main.ts")
185
+ * .call();
186
+ *
187
+ * for (const entry of result.entries) {
188
+ * console.log(`${entry.commit.author.name}: lines ${entry.resultStart}-${entry.resultStart + entry.lineCount - 1}`);
189
+ * }
190
+ * ```
191
+ */
192
+ blame(): BlameCommand {
193
+ this.checkClosed();
194
+ return new BlameCommand(this._workingCopy);
195
+ }
196
+
197
+ // ============ Checkout Operations ============
198
+
199
+ /**
200
+ * Create a CheckoutCommand for switching branches or restoring files.
201
+ *
202
+ * @example
203
+ * ```typescript
204
+ * // Checkout existing branch
205
+ * await git.checkout().setName("feature").call();
206
+ *
207
+ * // Create and checkout new branch
208
+ * await git.checkout()
209
+ * .setCreateBranch(true)
210
+ * .setName("newbranch")
211
+ * .call();
212
+ *
213
+ * // Checkout paths from commit
214
+ * await git.checkout()
215
+ * .setStartPoint("HEAD~1")
216
+ * .addPath("file.txt")
217
+ * .call();
218
+ * ```
219
+ */
220
+ checkout(): CheckoutCommand {
221
+ this.checkClosed();
222
+ return new CheckoutCommand(this._workingCopy);
223
+ }
224
+
225
+ // ============ Commit Operations ============
226
+
227
+ /**
228
+ * Create a CommitCommand for creating commits.
229
+ *
230
+ * @example
231
+ * ```typescript
232
+ * await git.commit()
233
+ * .setMessage("Add feature")
234
+ * .setAuthor("John", "john@example.com")
235
+ * .call();
236
+ * ```
237
+ */
238
+ commit(): CommitCommand {
239
+ this.checkClosed();
240
+ return new CommitCommand(this._workingCopy);
241
+ }
242
+
243
+ // ============ Log Operations ============
244
+
245
+ /**
246
+ * Create a LogCommand for viewing commit history.
247
+ *
248
+ * @example
249
+ * ```typescript
250
+ * for await (const commit of await git.log().call()) {
251
+ * console.log(commit.message);
252
+ * }
253
+ * ```
254
+ */
255
+ log(): LogCommand {
256
+ this.checkClosed();
257
+ return new LogCommand(this._workingCopy);
258
+ }
259
+
260
+ /**
261
+ * Create a ReflogCommand for viewing reflog entries.
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * // Get HEAD reflog
266
+ * const entries = await git.reflog().call();
267
+ *
268
+ * // Get reflog for specific branch
269
+ * const branchEntries = await git.reflog()
270
+ * .setRef("refs/heads/main")
271
+ * .call();
272
+ * ```
273
+ */
274
+ reflog(): ReflogCommand {
275
+ this.checkClosed();
276
+ return new ReflogCommand(this._workingCopy);
277
+ }
278
+
279
+ // ============ Branch Operations ============
280
+
281
+ /**
282
+ * Create a CreateBranchCommand for creating branches.
283
+ *
284
+ * @example
285
+ * ```typescript
286
+ * await git.branchCreate().setName("feature").call();
287
+ * ```
288
+ */
289
+ branchCreate(): CreateBranchCommand {
290
+ this.checkClosed();
291
+ return new CreateBranchCommand(this._workingCopy);
292
+ }
293
+
294
+ /**
295
+ * Create a DeleteBranchCommand for deleting branches.
296
+ *
297
+ * @example
298
+ * ```typescript
299
+ * await git.branchDelete().setBranchNames("feature").call();
300
+ * ```
301
+ */
302
+ branchDelete(): DeleteBranchCommand {
303
+ this.checkClosed();
304
+ return new DeleteBranchCommand(this._workingCopy);
305
+ }
306
+
307
+ /**
308
+ * Create a ListBranchCommand for listing branches.
309
+ *
310
+ * @example
311
+ * ```typescript
312
+ * const branches = await git.branchList().call();
313
+ * ```
314
+ */
315
+ branchList(): ListBranchCommand {
316
+ this.checkClosed();
317
+ return new ListBranchCommand(this._workingCopy);
318
+ }
319
+
320
+ /**
321
+ * Create a RenameBranchCommand for renaming branches.
322
+ *
323
+ * @example
324
+ * ```typescript
325
+ * await git.branchRename()
326
+ * .setOldName("old")
327
+ * .setNewName("new")
328
+ * .call();
329
+ * ```
330
+ */
331
+ branchRename(): RenameBranchCommand {
332
+ this.checkClosed();
333
+ return new RenameBranchCommand(this._workingCopy);
334
+ }
335
+
336
+ // ============ Tag Operations ============
337
+
338
+ /**
339
+ * Create a TagCommand for creating tags.
340
+ *
341
+ * @example
342
+ * ```typescript
343
+ * await git.tag().setName("v1.0.0").call();
344
+ * ```
345
+ */
346
+ tag(): TagCommand {
347
+ this.checkClosed();
348
+ return new TagCommand(this._workingCopy);
349
+ }
350
+
351
+ /**
352
+ * Create a DeleteTagCommand for deleting tags.
353
+ *
354
+ * @example
355
+ * ```typescript
356
+ * await git.tagDelete().setTags("v1.0.0").call();
357
+ * ```
358
+ */
359
+ tagDelete(): DeleteTagCommand {
360
+ this.checkClosed();
361
+ return new DeleteTagCommand(this._workingCopy);
362
+ }
363
+
364
+ /**
365
+ * Create a ListTagCommand for listing tags.
366
+ *
367
+ * @example
368
+ * ```typescript
369
+ * const tags = await git.tagList().call();
370
+ * ```
371
+ */
372
+ tagList(): ListTagCommand {
373
+ this.checkClosed();
374
+ return new ListTagCommand(this._workingCopy);
375
+ }
376
+
377
+ // ============ Reset Operations ============
378
+
379
+ /**
380
+ * Create a ResetCommand for resetting HEAD.
381
+ *
382
+ * @example
383
+ * ```typescript
384
+ * await git.reset().setRef("HEAD~1").call();
385
+ * ```
386
+ */
387
+ reset(): ResetCommand {
388
+ this.checkClosed();
389
+ return new ResetCommand(this._workingCopy);
390
+ }
391
+
392
+ // ============ Merge Operations ============
393
+
394
+ /**
395
+ * Create a MergeCommand for merging branches.
396
+ *
397
+ * @example
398
+ * ```typescript
399
+ * await git.merge().include("feature").call();
400
+ * ```
401
+ */
402
+ merge(): MergeCommand {
403
+ this.checkClosed();
404
+ return new MergeCommand(this._workingCopy);
405
+ }
406
+
407
+ // ============ Cherry-pick Operations ============
408
+
409
+ /**
410
+ * Create a CherryPickCommand for cherry-picking commits.
411
+ *
412
+ * @example
413
+ * ```typescript
414
+ * await git.cherryPick().include(commitId).call();
415
+ * ```
416
+ */
417
+ cherryPick(): CherryPickCommand {
418
+ this.checkClosed();
419
+ return new CherryPickCommand(this._workingCopy);
420
+ }
421
+
422
+ // ============ Revert Operations ============
423
+
424
+ /**
425
+ * Create a RevertCommand for reverting commits.
426
+ *
427
+ * @example
428
+ * ```typescript
429
+ * await git.revert().include(commitId).call();
430
+ * ```
431
+ */
432
+ revert(): RevertCommand {
433
+ this.checkClosed();
434
+ return new RevertCommand(this._workingCopy);
435
+ }
436
+
437
+ // ============ Rm Operations ============
438
+
439
+ /**
440
+ * Create an RmCommand for removing files from the index.
441
+ *
442
+ * @example
443
+ * ```typescript
444
+ * // Remove file from index (and working tree if not cached)
445
+ * await git.rm()
446
+ * .addFilepattern("file.txt")
447
+ * .call();
448
+ *
449
+ * // Remove from index only (keep in working tree)
450
+ * await git.rm()
451
+ * .addFilepattern("file.txt")
452
+ * .setCached(true)
453
+ * .call();
454
+ * ```
455
+ */
456
+ rm(): RmCommand {
457
+ this.checkClosed();
458
+ return new RmCommand(this._workingCopy);
459
+ }
460
+
461
+ // ============ Describe Operations ============
462
+
463
+ /**
464
+ * Create a DescribeCommand for describing commits with tags.
465
+ *
466
+ * @example
467
+ * ```typescript
468
+ * // Describe HEAD
469
+ * const result = await git.describe().call();
470
+ * console.log(result.description); // e.g., "v1.0.0-5-gabcdef0"
471
+ *
472
+ * // Describe with all tags including lightweight
473
+ * const result = await git.describe().setTags(true).call();
474
+ *
475
+ * // Always output something
476
+ * const result = await git.describe().setAlways(true).call();
477
+ * ```
478
+ */
479
+ describe(): DescribeCommand {
480
+ this.checkClosed();
481
+ return new DescribeCommand(this._workingCopy);
482
+ }
483
+
484
+ // ============ Maintenance Operations ============
485
+
486
+ /**
487
+ * Create a GarbageCollectCommand for repository maintenance.
488
+ *
489
+ * @example
490
+ * ```typescript
491
+ * // Basic garbage collection
492
+ * const result = await git.gc().call();
493
+ *
494
+ * // Aggressive GC with repacking
495
+ * const result = await git.gc()
496
+ * .setAggressive(true)
497
+ * .call();
498
+ * ```
499
+ */
500
+ gc(): GarbageCollectCommand {
501
+ this.checkClosed();
502
+ return new GarbageCollectCommand(this._workingCopy);
503
+ }
504
+
505
+ /**
506
+ * Create a PackRefsCommand for packing loose refs.
507
+ *
508
+ * @example
509
+ * ```typescript
510
+ * // Pack all refs
511
+ * const result = await git.packRefs()
512
+ * .setAll(true)
513
+ * .call();
514
+ * ```
515
+ */
516
+ packRefs(): PackRefsCommand {
517
+ this.checkClosed();
518
+ return new PackRefsCommand(this._workingCopy);
519
+ }
520
+
521
+ /**
522
+ * Create a CleanCommand for removing untracked files.
523
+ *
524
+ * @example
525
+ * ```typescript
526
+ * // Preview what would be cleaned (dry run)
527
+ * const result = await git.clean()
528
+ * .setDryRun(true)
529
+ * .call();
530
+ * ```
531
+ */
532
+ clean(): CleanCommand {
533
+ this.checkClosed();
534
+ return new CleanCommand(this._workingCopy);
535
+ }
536
+
537
+ // ============ Status Operations ============
538
+
539
+ /**
540
+ * Create a StatusCommand for showing staging area status.
541
+ *
542
+ * @example
543
+ * ```typescript
544
+ * const status = await git.status().call();
545
+ * if (status.isClean()) {
546
+ * console.log("Nothing to commit");
547
+ * }
548
+ * ```
549
+ */
550
+ status(): StatusCommand {
551
+ this.checkClosed();
552
+ return new StatusCommand(this._workingCopy);
553
+ }
554
+
555
+ // ============ Diff Operations ============
556
+
557
+ /**
558
+ * Create a DiffCommand for comparing trees/commits.
559
+ *
560
+ * @example
561
+ * ```typescript
562
+ * const entries = await git.diff()
563
+ * .setOldTree("main")
564
+ * .setNewTree("feature")
565
+ * .call();
566
+ * ```
567
+ */
568
+ diff(): DiffCommand {
569
+ this.checkClosed();
570
+ return new DiffCommand(this._workingCopy);
571
+ }
572
+
573
+ // ============ Remote Operations ============
574
+
575
+ /**
576
+ * Create a FetchCommand for fetching from remotes.
577
+ *
578
+ * @example
579
+ * ```typescript
580
+ * await git.fetch()
581
+ * .setRemote("origin")
582
+ * .call();
583
+ * ```
584
+ */
585
+ fetch(): FetchCommand {
586
+ this.checkClosed();
587
+ return new FetchCommand(this._workingCopy);
588
+ }
589
+
590
+ /**
591
+ * Create a PushCommand for pushing to remotes.
592
+ *
593
+ * @example
594
+ * ```typescript
595
+ * await git.push()
596
+ * .setRemote("origin")
597
+ * .call();
598
+ * ```
599
+ */
600
+ push(): PushCommand {
601
+ this.checkClosed();
602
+ return new PushCommand(this._workingCopy);
603
+ }
604
+
605
+ /**
606
+ * Create a PullCommand for pulling from remotes.
607
+ *
608
+ * @example
609
+ * ```typescript
610
+ * await git.pull()
611
+ * .setRemote("origin")
612
+ * .call();
613
+ * ```
614
+ */
615
+ pull(): PullCommand {
616
+ this.checkClosed();
617
+ return new PullCommand(this._workingCopy);
618
+ }
619
+
620
+ /**
621
+ * Create a CloneCommand for cloning repositories.
622
+ *
623
+ * @example
624
+ * ```typescript
625
+ * await git.clone()
626
+ * .setURI("https://github.com/user/repo")
627
+ * .call();
628
+ * ```
629
+ */
630
+ clone(): CloneCommand {
631
+ this.checkClosed();
632
+ return new CloneCommand(this._workingCopy);
633
+ }
634
+
635
+ /**
636
+ * Create an LsRemoteCommand for listing remote refs.
637
+ *
638
+ * @example
639
+ * ```typescript
640
+ * const refs = await git.lsRemote()
641
+ * .setRemote("https://github.com/user/repo")
642
+ * .call();
643
+ * ```
644
+ */
645
+ lsRemote(): LsRemoteCommand {
646
+ this.checkClosed();
647
+ return new LsRemoteCommand(this._workingCopy);
648
+ }
649
+
650
+ // ============ Remote Configuration Operations ============
651
+
652
+ /**
653
+ * Create a RemoteAddCommand for adding remotes.
654
+ *
655
+ * @example
656
+ * ```typescript
657
+ * await git.remoteAdd()
658
+ * .setName("upstream")
659
+ * .setUri("https://github.com/user/repo")
660
+ * .call();
661
+ * ```
662
+ */
663
+ remoteAdd(): RemoteAddCommand {
664
+ this.checkClosed();
665
+ return new RemoteAddCommand(this._workingCopy);
666
+ }
667
+
668
+ /**
669
+ * Create a RemoteRemoveCommand for removing remotes.
670
+ *
671
+ * @example
672
+ * ```typescript
673
+ * await git.remoteRemove()
674
+ * .setRemoteName("upstream")
675
+ * .call();
676
+ * ```
677
+ */
678
+ remoteRemove(): RemoteRemoveCommand {
679
+ this.checkClosed();
680
+ return new RemoteRemoveCommand(this._workingCopy);
681
+ }
682
+
683
+ /**
684
+ * Create a RemoteListCommand for listing remotes.
685
+ *
686
+ * @example
687
+ * ```typescript
688
+ * const remotes = await git.remoteList().call();
689
+ * ```
690
+ */
691
+ remoteList(): RemoteListCommand {
692
+ this.checkClosed();
693
+ return new RemoteListCommand(this._workingCopy);
694
+ }
695
+
696
+ /**
697
+ * Create a RemoteSetUrlCommand for changing remote URLs.
698
+ *
699
+ * @example
700
+ * ```typescript
701
+ * await git.remoteSetUrl()
702
+ * .setRemoteName("origin")
703
+ * .setRemoteUri("https://github.com/user/repo")
704
+ * .call();
705
+ * ```
706
+ */
707
+ remoteSetUrl(): RemoteSetUrlCommand {
708
+ this.checkClosed();
709
+ return new RemoteSetUrlCommand(this._workingCopy);
710
+ }
711
+
712
+ // ============ Rebase Operations ============
713
+
714
+ /**
715
+ * Create a RebaseCommand for rebasing commits.
716
+ *
717
+ * @example
718
+ * ```typescript
719
+ * // Rebase current branch onto main
720
+ * const result = await git.rebase()
721
+ * .setUpstream(mainCommitId)
722
+ * .call();
723
+ *
724
+ * // Abort rebase in progress
725
+ * const result = await git.rebase()
726
+ * .setOperation(RebaseOperation.ABORT)
727
+ * .call();
728
+ * ```
729
+ */
730
+ rebase(): RebaseCommand {
731
+ this.checkClosed();
732
+ return new RebaseCommand(this._workingCopy);
733
+ }
734
+
735
+ // ============ Stash Operations ============
736
+
737
+ /**
738
+ * Create a StashCreateCommand for stashing changes.
739
+ *
740
+ * @example
741
+ * ```typescript
742
+ * const stashCommit = await git.stashCreate()
743
+ * .setWorkingTreeProvider(provider)
744
+ * .setMessage("WIP: feature work")
745
+ * .call();
746
+ * ```
747
+ */
748
+ stashCreate(): StashCreateCommand {
749
+ this.checkClosed();
750
+ return new StashCreateCommand(this._workingCopy);
751
+ }
752
+
753
+ /**
754
+ * Create a StashApplyCommand for applying stashed changes.
755
+ *
756
+ * @example
757
+ * ```typescript
758
+ * // Apply most recent stash
759
+ * const result = await git.stashApply().call();
760
+ *
761
+ * // Apply specific stash
762
+ * const result = await git.stashApply()
763
+ * .setStashRef("stash@{2}")
764
+ * .call();
765
+ * ```
766
+ */
767
+ stashApply(): StashApplyCommand {
768
+ this.checkClosed();
769
+ return new StashApplyCommand(this._workingCopy);
770
+ }
771
+
772
+ /**
773
+ * Create a StashDropCommand for dropping stashes.
774
+ *
775
+ * @example
776
+ * ```typescript
777
+ * // Drop most recent stash
778
+ * await git.stashDrop().call();
779
+ *
780
+ * // Drop all stashes
781
+ * await git.stashDrop().setAll(true).call();
782
+ * ```
783
+ */
784
+ stashDrop(): StashDropCommand {
785
+ this.checkClosed();
786
+ return new StashDropCommand(this._workingCopy);
787
+ }
788
+
789
+ /**
790
+ * Create a StashListCommand for listing stashes.
791
+ *
792
+ * @example
793
+ * ```typescript
794
+ * const stashes = await git.stashList().call();
795
+ * for (const stash of stashes) {
796
+ * console.log(`stash@{${stash.index}}: ${stash.message}`);
797
+ * }
798
+ * ```
799
+ */
800
+ stashList(): StashListCommand {
801
+ this.checkClosed();
802
+ return new StashListCommand(this._workingCopy);
803
+ }
804
+
805
+ // ============ Component Access ============
806
+
807
+ /**
808
+ * Get the underlying WorkingCopy.
809
+ *
810
+ * @example
811
+ * ```typescript
812
+ * const git = Git.fromWorkingCopy(workingCopy);
813
+ * const wc = git.workingCopy;
814
+ *
815
+ * // Access full WorkingCopy API
816
+ * const status = await wc.getStatus();
817
+ * const branch = await wc.getCurrentBranch();
818
+ * ```
819
+ */
820
+ get workingCopy(): WorkingCopy {
821
+ return this._workingCopy;
822
+ }
823
+
824
+ /**
825
+ * Get the History interface for accessing repository objects.
826
+ *
827
+ * Provides access to blobs, trees, commits, tags, and refs.
828
+ * Returns undefined if WorkingCopy doesn't have history.
829
+ *
830
+ * @example
831
+ * ```typescript
832
+ * const history = git.history;
833
+ * if (history) {
834
+ * const commit = await history.commits.load(commitId);
835
+ * const tree = await history.trees.load(commit.tree);
836
+ * }
837
+ * ```
838
+ */
839
+ get history(): History | undefined {
840
+ return this._workingCopy.history;
841
+ }
842
+
843
+ /**
844
+ * Get the Checkout interface for managing local state.
845
+ *
846
+ * Provides access to staging area, HEAD, and in-progress operations.
847
+ * Returns undefined if WorkingCopy doesn't have checkout.
848
+ *
849
+ * Note: Named `checkoutState` to avoid conflict with `checkout()` command method.
850
+ *
851
+ * @example
852
+ * ```typescript
853
+ * const checkoutState = git.checkoutState;
854
+ * if (checkoutState) {
855
+ * const staging = checkoutState.staging;
856
+ * const head = await checkoutState.getHeadCommit();
857
+ * }
858
+ * ```
859
+ */
860
+ get checkoutState(): Checkout | undefined {
861
+ return this._workingCopy.checkout;
862
+ }
863
+
864
+ /**
865
+ * Get the Worktree interface for filesystem operations.
866
+ *
867
+ * Provides access to working directory read/write operations.
868
+ * Returns undefined if WorkingCopy doesn't have worktree.
869
+ *
870
+ * @example
871
+ * ```typescript
872
+ * const worktree = git.worktree;
873
+ * if (worktree) {
874
+ * for await (const entry of worktree.walk()) {
875
+ * console.log(entry.path);
876
+ * }
877
+ * }
878
+ * ```
879
+ */
880
+ get worktree(): Worktree | undefined {
881
+ return this._workingCopy.worktree;
882
+ }
883
+
884
+ // ============ Lifecycle ============
885
+
886
+ /**
887
+ * Close the Git instance.
888
+ *
889
+ * After calling this method, no more commands can be created.
890
+ * This does NOT close the underlying store.
891
+ */
892
+ close(): void {
893
+ this.closed = true;
894
+ }
895
+
896
+ /**
897
+ * Disposable interface implementation.
898
+ */
899
+ [Symbol.dispose](): void {
900
+ this.close();
901
+ }
902
+
903
+ // ============ Private ============
904
+
905
+ private checkClosed(): void {
906
+ if (this.closed) {
907
+ throw new Error("Git instance is closed");
908
+ }
909
+ }
910
+ }