@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
@@ -0,0 +1,532 @@
1
+ import type { ObjectId } from "@statewalker/vcs-core";
2
+ import {
3
+ type CloneOptions as TransportCloneOptions,
4
+ clone as transportClone,
5
+ } from "@statewalker/vcs-transport";
6
+ import { bytesToHex } from "@statewalker/vcs-utils/hash/utils";
7
+
8
+ import { InvalidArgumentError, InvalidRemoteError } from "../errors/index.js";
9
+ import { importPackIntoHistory } from "../pack-import/index.js";
10
+ import type { CloneResult } from "../results/clone-result.js";
11
+ import {
12
+ type FetchResult,
13
+ RefUpdateStatus,
14
+ type TrackingRefUpdate,
15
+ } from "../results/fetch-result.js";
16
+ import { TransportCommand } from "../transport-command.js";
17
+ import { TagOption } from "./fetch-command.js";
18
+
19
+ /**
20
+ * Clone a remote repository.
21
+ *
22
+ * Equivalent to `git clone`.
23
+ *
24
+ * Based on JGit's CloneCommand.
25
+ *
26
+ * Note: Unlike other commands, CloneCommand initializes a new repository
27
+ * rather than operating on an existing one. The store passed to the
28
+ * constructor should be an empty/new store that will be populated.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * // Basic clone
33
+ * const result = await git.clone()
34
+ * .setURI("https://github.com/user/repo")
35
+ * .call();
36
+ *
37
+ * // Clone specific branch
38
+ * const result = await git.clone()
39
+ * .setURI("https://github.com/user/repo")
40
+ * .setBranch("develop")
41
+ * .call();
42
+ *
43
+ * // Shallow clone
44
+ * const result = await git.clone()
45
+ * .setURI("https://github.com/user/repo")
46
+ * .setDepth(1)
47
+ * .call();
48
+ *
49
+ * // Bare clone
50
+ * const result = await git.clone()
51
+ * .setURI("https://github.com/user/repo")
52
+ * .setBare(true)
53
+ * .call();
54
+ * ```
55
+ */
56
+ export class CloneCommand extends TransportCommand<CloneResult> {
57
+ private uri?: string;
58
+ private branch?: string;
59
+ private depth?: number;
60
+ private bare = false;
61
+ private noCheckout = false;
62
+ private remoteName = "origin";
63
+ private cloneAllBranches = true;
64
+ private mirror = false;
65
+ private branchesToClone: string[] = [];
66
+ private tagOption: TagOption = TagOption.AUTO_FOLLOW;
67
+ private shallowSince?: Date;
68
+ private shallowExcludes: string[] = [];
69
+
70
+ /**
71
+ * Set the URI to clone from.
72
+ *
73
+ * @param uri Repository URI (HTTPS, SSH, or Git protocol)
74
+ */
75
+ setURI(uri: string): this {
76
+ this.checkCallable();
77
+ this.uri = uri;
78
+ return this;
79
+ }
80
+
81
+ /**
82
+ * Get the URI being cloned.
83
+ */
84
+ getURI(): string | undefined {
85
+ return this.uri;
86
+ }
87
+
88
+ /**
89
+ * Set the branch to clone.
90
+ *
91
+ * If set, only this branch will be fetched.
92
+ * Otherwise, all branches are fetched.
93
+ *
94
+ * @param branch Branch name (e.g., "main" or "refs/heads/main")
95
+ */
96
+ setBranch(branch: string): this {
97
+ this.checkCallable();
98
+ this.branch = branch;
99
+ return this;
100
+ }
101
+
102
+ /**
103
+ * Get the branch being cloned.
104
+ */
105
+ getBranch(): string | undefined {
106
+ return this.branch;
107
+ }
108
+
109
+ /**
110
+ * Set shallow clone depth.
111
+ *
112
+ * @param depth Number of commits to fetch
113
+ */
114
+ setDepth(depth: number): this {
115
+ this.checkCallable();
116
+ if (depth < 1) {
117
+ throw new InvalidArgumentError("depth", depth, "Depth must be at least 1");
118
+ }
119
+ this.depth = depth;
120
+ return this;
121
+ }
122
+
123
+ /**
124
+ * Set whether to create a bare repository.
125
+ *
126
+ * @param bare Whether to create bare repository
127
+ */
128
+ setBare(bare: boolean): this {
129
+ this.checkCallable();
130
+ this.bare = bare;
131
+ return this;
132
+ }
133
+
134
+ /**
135
+ * Whether bare clone is enabled.
136
+ */
137
+ isBare(): boolean {
138
+ return this.bare;
139
+ }
140
+
141
+ /**
142
+ * Set whether to skip checkout.
143
+ *
144
+ * @param noCheckout Whether to skip checkout
145
+ */
146
+ setNoCheckout(noCheckout: boolean): this {
147
+ this.checkCallable();
148
+ this.noCheckout = noCheckout;
149
+ return this;
150
+ }
151
+
152
+ /**
153
+ * Whether checkout is skipped.
154
+ */
155
+ isNoCheckout(): boolean {
156
+ return this.noCheckout;
157
+ }
158
+
159
+ /**
160
+ * Set the remote name.
161
+ *
162
+ * Default is "origin".
163
+ *
164
+ * @param remoteName Remote name
165
+ */
166
+ setRemote(remoteName: string): this {
167
+ this.checkCallable();
168
+ this.remoteName = remoteName;
169
+ return this;
170
+ }
171
+
172
+ /**
173
+ * Get the remote name.
174
+ */
175
+ getRemote(): string {
176
+ return this.remoteName;
177
+ }
178
+
179
+ /**
180
+ * Set whether to clone all branches.
181
+ *
182
+ * Default is true. If set to false and no branch is specified,
183
+ * only the default branch will be cloned.
184
+ *
185
+ * @param cloneAllBranches Whether to clone all branches
186
+ */
187
+ setCloneAllBranches(cloneAllBranches: boolean): this {
188
+ this.checkCallable();
189
+ this.cloneAllBranches = cloneAllBranches;
190
+ return this;
191
+ }
192
+
193
+ /**
194
+ * Whether clone all branches is enabled.
195
+ */
196
+ isCloneAllBranches(): boolean {
197
+ return this.cloneAllBranches;
198
+ }
199
+
200
+ /**
201
+ * Set up a mirror of the source repository.
202
+ *
203
+ * This implies that a bare repository will be created.
204
+ * Compared to setBare, setMirror not only maps local branches
205
+ * of the source to local branches of the target, it maps all refs
206
+ * (including remote-tracking branches, notes, etc.).
207
+ *
208
+ * Equivalent to `git clone --mirror`.
209
+ *
210
+ * @param mirror Whether to mirror all refs
211
+ */
212
+ setMirror(mirror: boolean): this {
213
+ this.checkCallable();
214
+ this.mirror = mirror;
215
+ if (mirror) {
216
+ this.bare = true;
217
+ }
218
+ return this;
219
+ }
220
+
221
+ /**
222
+ * Whether mirror mode is enabled.
223
+ */
224
+ isMirror(): boolean {
225
+ return this.mirror;
226
+ }
227
+
228
+ /**
229
+ * Set branches or tags to clone.
230
+ *
231
+ * This is ignored if setCloneAllBranches(true) or setMirror(true) is used.
232
+ * If branchesToClone is empty, it's also ignored.
233
+ *
234
+ * @param branchesToClone Collection of branches to clone (full ref names)
235
+ */
236
+ setBranchesToClone(branchesToClone: string[]): this {
237
+ this.checkCallable();
238
+ this.branchesToClone = [...branchesToClone];
239
+ return this;
240
+ }
241
+
242
+ /**
243
+ * Get branches to clone.
244
+ */
245
+ getBranchesToClone(): string[] {
246
+ return [...this.branchesToClone];
247
+ }
248
+
249
+ /**
250
+ * Set the tag option for the remote configuration.
251
+ *
252
+ * @param tagOption Tag option
253
+ */
254
+ setTagOption(tagOption: TagOption): this {
255
+ this.checkCallable();
256
+ this.tagOption = tagOption;
257
+ return this;
258
+ }
259
+
260
+ /**
261
+ * Get the tag option.
262
+ */
263
+ getTagOption(): TagOption {
264
+ return this.tagOption;
265
+ }
266
+
267
+ /**
268
+ * Set the --no-tags option.
269
+ *
270
+ * Tags are not cloned and the remote configuration is initialized
271
+ * with the --no-tags option as well.
272
+ *
273
+ * Equivalent to `git clone --no-tags`.
274
+ */
275
+ setNoTags(): this {
276
+ return this.setTagOption(TagOption.NO_TAGS);
277
+ }
278
+
279
+ /**
280
+ * Create a shallow clone with a history after the specified time.
281
+ *
282
+ * Equivalent to `git clone --shallow-since=<date>`.
283
+ *
284
+ * @param shallowSince The timestamp
285
+ */
286
+ setShallowSince(shallowSince: Date): this {
287
+ this.checkCallable();
288
+ this.shallowSince = shallowSince;
289
+ return this;
290
+ }
291
+
292
+ /**
293
+ * Get the shallow-since date.
294
+ */
295
+ getShallowSince(): Date | undefined {
296
+ return this.shallowSince;
297
+ }
298
+
299
+ /**
300
+ * Create a shallow clone excluding commits reachable from a specified
301
+ * remote branch or tag.
302
+ *
303
+ * Equivalent to `git clone --shallow-exclude=<ref>`.
304
+ *
305
+ * @param shallowExclude The ref or commit to exclude
306
+ */
307
+ addShallowExclude(shallowExclude: string): this {
308
+ this.checkCallable();
309
+ this.shallowExcludes.push(shallowExclude);
310
+ return this;
311
+ }
312
+
313
+ /**
314
+ * Get the shallow excludes.
315
+ */
316
+ getShallowExcludes(): string[] {
317
+ return [...this.shallowExcludes];
318
+ }
319
+
320
+ /**
321
+ * Execute the clone operation.
322
+ *
323
+ * @returns Clone result
324
+ * @throws InvalidRemoteError if URI is not specified
325
+ */
326
+ async call(): Promise<CloneResult> {
327
+ this.checkCallable();
328
+ this.setCallable(false);
329
+
330
+ if (!this.uri) {
331
+ throw new InvalidRemoteError("", "URI must be specified for clone");
332
+ }
333
+
334
+ // Determine clone branch strategy
335
+ const branchToClone = this.determineBranchToClone();
336
+
337
+ // Execute clone via transport
338
+ const options: TransportCloneOptions = {
339
+ url: this.uri,
340
+ branch: branchToClone,
341
+ depth: this.depth,
342
+ bare: this.bare,
343
+ remoteName: this.remoteName,
344
+ auth: this.credentials,
345
+ headers: this.headers,
346
+ timeout: this.timeout,
347
+ onProgress: this.progressCallback,
348
+ onProgressMessage: this.progressMessageCallback,
349
+ };
350
+
351
+ const transportResult = await transportClone(options);
352
+
353
+ // Store pack data
354
+ if (transportResult.packData.length > 0) {
355
+ await this.storePack(transportResult.packData);
356
+ }
357
+
358
+ // Update refs based on clone mode
359
+ const trackingUpdates: TrackingRefUpdate[] = [];
360
+ for (const [refName, objectId] of transportResult.refs) {
361
+ const objectIdHex = bytesToHex(objectId);
362
+
363
+ // Filter refs based on tag option
364
+ if (this.shouldSkipRef(refName)) {
365
+ continue;
366
+ }
367
+
368
+ // For mirror mode, refs are stored directly without remotes prefix
369
+ const localRefName = this.mirror ? this.toMirrorRefName(refName) : refName;
370
+
371
+ await this.refsStore.set(localRefName, objectIdHex);
372
+ trackingUpdates.push({
373
+ localRef: localRefName,
374
+ remoteRef: refName,
375
+ newObjectId: objectIdHex,
376
+ status: RefUpdateStatus.NEW,
377
+ });
378
+ }
379
+
380
+ // Set up HEAD and default branch.
381
+ //
382
+ // Two forms, deliberately kept apart: the advertised form goes out on
383
+ // FetchResult unchanged (it is what the remote said), while the ref work
384
+ // below and CloneResult use the bare branch name.
385
+ const advertisedDefaultBranch = transportResult.defaultBranch;
386
+ const defaultBranch = toBranchName(advertisedDefaultBranch);
387
+ let headCommit: ObjectId | undefined;
388
+
389
+ if (defaultBranch && !this.bare) {
390
+ // Create local branch from the tip the clone just wrote. Look under the
391
+ // remote-tracking name first, then under the local branch name: the ref
392
+ // loop above stores each ref under its advertised name, so which of the
393
+ // two exists depends on the refspec the remote advertised through.
394
+ const localRef = `refs/heads/${defaultBranch}`;
395
+ const tip =
396
+ (await this.refsStore.resolve(`refs/remotes/${this.remoteName}/${defaultBranch}`)) ??
397
+ (await this.refsStore.resolve(localRef));
398
+
399
+ if (tip?.objectId) {
400
+ await this.refsStore.set(localRef, tip.objectId);
401
+ headCommit = tip.objectId;
402
+
403
+ // Set HEAD to point to local branch
404
+ await this.refsStore.setSymbolic("HEAD", localRef);
405
+
406
+ // Set up staging area from HEAD tree
407
+ if (!this.noCheckout) {
408
+ await this.checkoutHead(tip.objectId);
409
+ }
410
+ }
411
+ } else if (this.bare || this.mirror) {
412
+ // For bare/mirror repos, HEAD points to default branch
413
+ if (defaultBranch) {
414
+ await this.refsStore.setSymbolic("HEAD", `refs/heads/${defaultBranch}`);
415
+ }
416
+ }
417
+
418
+ // Build fetch result
419
+ const advertisedRefs = new Map<string, ObjectId>();
420
+ for (const [refName, objectId] of transportResult.refs) {
421
+ advertisedRefs.set(refName, bytesToHex(objectId));
422
+ }
423
+
424
+ const fetchResult: FetchResult = {
425
+ uri: this.uri,
426
+ advertisedRefs,
427
+ trackingRefUpdates: trackingUpdates,
428
+ defaultBranch: advertisedDefaultBranch,
429
+ bytesReceived: transportResult.bytesReceived,
430
+ isEmpty: transportResult.isEmpty,
431
+ messages: [],
432
+ };
433
+
434
+ return {
435
+ fetchResult,
436
+ defaultBranch: defaultBranch ?? "main",
437
+ remoteName: this.remoteName,
438
+ bare: this.bare,
439
+ headCommit,
440
+ };
441
+ }
442
+
443
+ /**
444
+ * Determine which branch to clone based on options.
445
+ */
446
+ private determineBranchToClone(): string | undefined {
447
+ // Explicit branch takes precedence
448
+ if (this.branch) {
449
+ return this.branch;
450
+ }
451
+
452
+ // Mirror or cloneAllBranches means fetch all
453
+ if (this.mirror || this.cloneAllBranches) {
454
+ return undefined;
455
+ }
456
+
457
+ // If specific branches are set, use the first one
458
+ if (this.branchesToClone.length > 0) {
459
+ // Extract branch name from full ref if provided
460
+ const first = this.branchesToClone[0];
461
+ if (first.startsWith("refs/heads/")) {
462
+ return first.slice("refs/heads/".length);
463
+ }
464
+ return first;
465
+ }
466
+
467
+ return undefined;
468
+ }
469
+
470
+ /**
471
+ * Check if a ref should be skipped based on tag option.
472
+ */
473
+ private shouldSkipRef(refName: string): boolean {
474
+ // Skip tags if NO_TAGS option is set
475
+ if (this.tagOption === TagOption.NO_TAGS && refName.startsWith("refs/tags/")) {
476
+ return true;
477
+ }
478
+ return false;
479
+ }
480
+
481
+ /**
482
+ * Convert a remote ref name to mirror ref name.
483
+ */
484
+ private toMirrorRefName(refName: string): string {
485
+ // For mirror clones, refs/remotes/origin/* becomes refs/heads/*
486
+ const remotePrefix = `refs/remotes/${this.remoteName}/`;
487
+ if (refName.startsWith(remotePrefix)) {
488
+ return `refs/heads/${refName.slice(remotePrefix.length)}`;
489
+ }
490
+ return refName;
491
+ }
492
+
493
+ /**
494
+ * Store received pack data.
495
+ *
496
+ * Unpacks the pack into the working copy's history, so the objects the
497
+ * cloned refs point at are actually available locally.
498
+ */
499
+ private async storePack(packData: Uint8Array): Promise<void> {
500
+ await importPackIntoHistory(this._workingCopy.history, packData);
501
+ }
502
+
503
+ /**
504
+ * Checkout HEAD commit to staging area.
505
+ */
506
+ private async checkoutHead(commitId: ObjectId): Promise<void> {
507
+ try {
508
+ const commit = await this.commits.load(commitId);
509
+ if (!commit) {
510
+ return;
511
+ }
512
+ await this.staging.readTree(this.trees, commit.tree);
513
+ await this.staging.write();
514
+ } catch {
515
+ // Commit not available yet (pack not unpacked)
516
+ }
517
+ }
518
+ }
519
+
520
+ /**
521
+ * The bare branch name of a remote's default branch.
522
+ *
523
+ * The advertisement reports it as the target of HEAD's symref capability, so
524
+ * it arrives fully qualified (`refs/heads/main`); a transport that reports the
525
+ * bare name (`main`) is equally legitimate. Both are accepted, and both yield
526
+ * the bare name the ref-building code below prefixes.
527
+ */
528
+ function toBranchName(defaultBranch: string | undefined): string | undefined {
529
+ if (defaultBranch === undefined) return undefined;
530
+ const prefix = "refs/heads/";
531
+ return defaultBranch.startsWith(prefix) ? defaultBranch.slice(prefix.length) : defaultBranch;
532
+ }