@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,421 @@
1
+ import type {
2
+ Blobs,
3
+ Commit,
4
+ Commits,
5
+ ObjectId,
6
+ Ref,
7
+ Refs,
8
+ SymbolicRef,
9
+ Tag,
10
+ Tags,
11
+ Tree,
12
+ TreeEntry,
13
+ Trees,
14
+ } from "@statewalker/vcs-core";
15
+ import { isSymbolicRef } from "@statewalker/vcs-core";
16
+ import type { Staging, WorkingCopy, Worktree } from "@statewalker/vcs-working-tree";
17
+
18
+ import { NoHeadError, RefNotFoundError } from "./errors/index.js";
19
+
20
+ /**
21
+ * Extended Trees interface with legacy method names for command compatibility.
22
+ */
23
+ export interface CommandTrees extends Trees {
24
+ /** Load tree entries - alias for load() */
25
+ loadTree(id: ObjectId): AsyncIterable<TreeEntry>;
26
+ /** Store tree - alias for store() */
27
+ storeTree(entries: Tree): Promise<ObjectId>;
28
+ }
29
+
30
+ /**
31
+ * Extended Refs interface with legacy method names for command compatibility.
32
+ */
33
+ export interface CommandRefs extends Refs {
34
+ /** Delete a reference - alias for remove() */
35
+ delete(name: string): Promise<boolean>;
36
+ }
37
+
38
+ /**
39
+ * Extended Tags interface with legacy method names for command compatibility.
40
+ */
41
+ export interface CommandTags extends Tags {
42
+ /** Store tag - alias for store() */
43
+ storeTag(tag: Tag): Promise<ObjectId>;
44
+ /** Load tag - alias for load() */
45
+ loadTag(id: ObjectId): Promise<Tag | undefined>;
46
+ }
47
+
48
+ /**
49
+ * Extended Commits interface with legacy method names for command compatibility.
50
+ */
51
+ export interface CommandCommits extends Commits {
52
+ /** Load commit - alias for legacy loadCommit() */
53
+ load(id: ObjectId): Promise<Commit | undefined>;
54
+ /** Store commit - alias for legacy storeCommit() */
55
+ store(commit: Commit): Promise<ObjectId>;
56
+ }
57
+
58
+ /**
59
+ * Create a CommandTrees adapter wrapping a Trees instance.
60
+ */
61
+ function wrapTrees(trees: Trees): CommandTrees {
62
+ const wrapper = trees as CommandTrees;
63
+ if (!wrapper.loadTree) {
64
+ wrapper.loadTree = async function* (id: ObjectId): AsyncIterable<TreeEntry> {
65
+ const result = await trees.load(id);
66
+ if (result) {
67
+ yield* result;
68
+ }
69
+ };
70
+ }
71
+ if (!wrapper.storeTree) {
72
+ wrapper.storeTree = (entries: Tree) => trees.store(entries);
73
+ }
74
+ return wrapper;
75
+ }
76
+
77
+ /**
78
+ * Create a CommandRefs adapter wrapping a Refs instance.
79
+ */
80
+ function wrapRefs(refs: Refs): CommandRefs {
81
+ const wrapper = refs as CommandRefs;
82
+ if (!wrapper.delete) {
83
+ wrapper.delete = (name: string) => refs.remove(name);
84
+ }
85
+ return wrapper;
86
+ }
87
+
88
+ /**
89
+ * Create a CommandTags adapter wrapping a Tags instance.
90
+ * Handles both new Tags interface (load/store) and legacy TagStore (loadTag/storeTag).
91
+ */
92
+ function wrapTags(tags: Tags): CommandTags {
93
+ const wrapper = tags as CommandTags & {
94
+ loadTag?: (id: ObjectId) => Promise<Tag | undefined>;
95
+ storeTag?: (tag: Tag) => Promise<ObjectId>;
96
+ };
97
+ // Add load/store if missing (old TagStore uses loadTag/storeTag)
98
+ if (!wrapper.load && wrapper.loadTag) {
99
+ wrapper.load = wrapper.loadTag;
100
+ }
101
+ if (!wrapper.store && wrapper.storeTag) {
102
+ wrapper.store = wrapper.storeTag;
103
+ }
104
+ // Add loadTag/storeTag as aliases for new interface
105
+ // Bind methods to preserve 'this' context
106
+ if (!wrapper.loadTag) {
107
+ const loadFn = wrapper.load;
108
+ if (loadFn) {
109
+ wrapper.loadTag = (id: ObjectId) => loadFn.call(wrapper, id);
110
+ }
111
+ }
112
+ if (!wrapper.storeTag) {
113
+ const storeFn = wrapper.store;
114
+ if (storeFn) {
115
+ wrapper.storeTag = (tag: Tag) => storeFn.call(wrapper, tag);
116
+ }
117
+ }
118
+ return wrapper as CommandTags;
119
+ }
120
+
121
+ /**
122
+ * Create a CommandCommits adapter wrapping a Commits instance.
123
+ * Handles legacy CommitStore interface that uses loadCommit/storeCommit.
124
+ */
125
+ function wrapCommits(commits: Commits): CommandCommits {
126
+ const wrapper = commits as CommandCommits & {
127
+ loadCommit?: (id: ObjectId) => Promise<Commit | undefined>;
128
+ storeCommit?: (commit: Commit) => Promise<ObjectId>;
129
+ };
130
+ // Add load/store if missing (old CommitStore uses loadCommit/storeCommit)
131
+ if (!wrapper.load && wrapper.loadCommit) {
132
+ wrapper.load = wrapper.loadCommit;
133
+ }
134
+ if (!wrapper.store && wrapper.storeCommit) {
135
+ wrapper.store = wrapper.storeCommit;
136
+ }
137
+ return wrapper as CommandCommits;
138
+ }
139
+
140
+ /**
141
+ * Abstract base class for all Git commands.
142
+ *
143
+ * Implements the Command pattern with single-use semantics.
144
+ * Each command instance can only be called once.
145
+ *
146
+ * Commands are constructed with a WorkingCopy that provides access to all repository components.
147
+ *
148
+ * Based on JGit's GitCommand<T> class.
149
+ *
150
+ * @typeParam T - The return type of the command's call() method
151
+ */
152
+ export abstract class GitCommand<T> {
153
+ protected readonly _workingCopy: WorkingCopy;
154
+ private callable = true;
155
+
156
+ constructor(workingCopy: WorkingCopy) {
157
+ this._workingCopy = workingCopy;
158
+ }
159
+
160
+ // ============ Store Accessors ============
161
+
162
+ /**
163
+ * Access blob storage.
164
+ */
165
+ protected get blobs(): Blobs {
166
+ if (!this._workingCopy.history) {
167
+ throw new Error("WorkingCopy.history is required for commands");
168
+ }
169
+ return this._workingCopy.history.blobs as unknown as Blobs;
170
+ }
171
+
172
+ /**
173
+ * Access tree storage.
174
+ */
175
+ protected get trees(): CommandTrees {
176
+ if (!this._workingCopy.history) {
177
+ throw new Error("WorkingCopy.history is required for commands");
178
+ }
179
+ return wrapTrees(this._workingCopy.history.trees as unknown as Trees);
180
+ }
181
+
182
+ /**
183
+ * Access commit storage.
184
+ */
185
+ protected get commits(): CommandCommits {
186
+ if (!this._workingCopy.history) {
187
+ throw new Error("WorkingCopy.history is required for commands");
188
+ }
189
+ return wrapCommits(this._workingCopy.history.commits as unknown as Commits);
190
+ }
191
+
192
+ /**
193
+ * Access tag storage.
194
+ * Named tagsStore to avoid conflict with command properties.
195
+ */
196
+ protected get tagsStore(): CommandTags | undefined {
197
+ if (!this._workingCopy.history) {
198
+ throw new Error("WorkingCopy.history is required for commands");
199
+ }
200
+ const tags = this._workingCopy.history.tags as unknown as Tags | undefined;
201
+ return tags ? wrapTags(tags) : undefined;
202
+ }
203
+
204
+ /**
205
+ * Access refs storage.
206
+ * Named refsStore to avoid conflict with command properties.
207
+ */
208
+ protected get refsStore(): CommandRefs {
209
+ if (!this._workingCopy.history) {
210
+ throw new Error("WorkingCopy.history is required for commands");
211
+ }
212
+ return wrapRefs(this._workingCopy.history.refs as unknown as Refs);
213
+ }
214
+
215
+ /**
216
+ * Access staging area.
217
+ * Supports both new architecture (checkout.staging) and legacy/init (staging directly).
218
+ */
219
+ protected get staging(): Staging {
220
+ // Try new architecture first (checkout.staging)
221
+ if (this._workingCopy.checkout?.staging) {
222
+ return this._workingCopy.checkout.staging as unknown as Staging;
223
+ }
224
+ // Fall back to legacy pattern (staging directly on workingCopy)
225
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
226
+ const wcAny = this._workingCopy as any;
227
+ if (wcAny.staging) {
228
+ return wcAny.staging as unknown as Staging;
229
+ }
230
+ throw new Error("WorkingCopy.checkout.staging is required for commands");
231
+ }
232
+
233
+ /**
234
+ * Access worktree interface.
235
+ */
236
+ protected get worktreeAccess(): Worktree | undefined {
237
+ return this._workingCopy.worktree;
238
+ }
239
+
240
+ /**
241
+ * Get the WorkingCopy.
242
+ */
243
+ protected get workingCopy(): WorkingCopy {
244
+ return this._workingCopy;
245
+ }
246
+
247
+ /**
248
+ * Execute the command.
249
+ * Can only be called once per instance.
250
+ */
251
+ abstract call(): Promise<T>;
252
+
253
+ /**
254
+ * Verify the command hasn't been called yet.
255
+ * @throws Error if command was already executed
256
+ */
257
+ protected checkCallable(): void {
258
+ if (!this.callable) {
259
+ throw new Error(`Command ${this.constructor.name} has already been called`);
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Mark command as no longer callable.
265
+ */
266
+ protected setCallable(value: boolean): void {
267
+ this.callable = value;
268
+ }
269
+
270
+ /**
271
+ * Resolve HEAD to its target commit.
272
+ *
273
+ * @returns ObjectId of the commit HEAD points to
274
+ * @throws NoHeadError if HEAD doesn't exist or cannot be resolved
275
+ */
276
+ protected async resolveHead(): Promise<ObjectId> {
277
+ const ref = await this.refsStore.resolve("HEAD");
278
+ if (!ref?.objectId) {
279
+ throw new NoHeadError("HEAD cannot be resolved");
280
+ }
281
+ return ref.objectId;
282
+ }
283
+
284
+ /**
285
+ * Get the current branch name (if HEAD is not detached).
286
+ *
287
+ * @returns Branch name (e.g., "refs/heads/main") or undefined if detached
288
+ */
289
+ protected async getCurrentBranch(): Promise<string | undefined> {
290
+ const head = await this.refsStore.get("HEAD");
291
+ if (head && isSymbolicRef(head)) {
292
+ return head.target;
293
+ }
294
+ return undefined;
295
+ }
296
+
297
+ /**
298
+ * Resolve a ref name to its ObjectId.
299
+ *
300
+ * @param refName Ref name, commit ID, or commit-ish
301
+ * @returns ObjectId of the resolved ref
302
+ * @throws RefNotFoundError if ref cannot be resolved
303
+ */
304
+ protected async resolveRef(refName: string): Promise<ObjectId> {
305
+ // Handle relative refs like HEAD~1, HEAD^
306
+ if (refName.includes("~") || refName.includes("^")) {
307
+ return this.resolveRelativeRef(refName);
308
+ }
309
+
310
+ // Try as direct ref first
311
+ const ref = await this.refsStore.resolve(refName);
312
+ if (ref?.objectId) {
313
+ // Peel tag objects to commits
314
+ return this.peelToCommit(ref.objectId);
315
+ }
316
+
317
+ // Try as branch name
318
+ const branchRef = await this.refsStore.resolve(`refs/heads/${refName}`);
319
+ if (branchRef?.objectId) {
320
+ return branchRef.objectId;
321
+ }
322
+
323
+ // Try as tag name
324
+ const tagRef = await this.refsStore.resolve(`refs/tags/${refName}`);
325
+ if (tagRef?.objectId) {
326
+ // Peel tag objects to commits
327
+ return this.peelToCommit(tagRef.objectId);
328
+ }
329
+
330
+ // Try as direct commit ID
331
+ if (await this.commits.has(refName)) {
332
+ return refName;
333
+ }
334
+
335
+ throw new RefNotFoundError(refName);
336
+ }
337
+
338
+ /**
339
+ * Resolve a relative ref like HEAD~1 or HEAD^2.
340
+ */
341
+ private async resolveRelativeRef(ref: string): Promise<ObjectId> {
342
+ // Parse the ref
343
+ const match = ref.match(/^([^~^]+)((?:[~^]\d*)+)$/);
344
+ if (!match) {
345
+ throw new RefNotFoundError(ref);
346
+ }
347
+
348
+ const baseRef = match[1];
349
+ const modifiers = match[2];
350
+
351
+ let commitId = await this.resolveRef(baseRef);
352
+
353
+ // Process each modifier
354
+ const modifierMatch = modifiers.matchAll(/([~^])(\d*)/g);
355
+ for (const m of modifierMatch) {
356
+ const type = m[1];
357
+ const count = m[2] ? parseInt(m[2], 10) : 1;
358
+
359
+ if (type === "~") {
360
+ // ~N means follow first parent N times
361
+ for (let i = 0; i < count; i++) {
362
+ const commit = await this.commits.load(commitId);
363
+ if (!commit || commit.parents.length === 0) {
364
+ throw new RefNotFoundError(ref, `Cannot resolve ${ref}: no parent`);
365
+ }
366
+ commitId = commit.parents[0];
367
+ }
368
+ } else if (type === "^") {
369
+ // ^N means follow Nth parent
370
+ const commit = await this.commits.load(commitId);
371
+ if (!commit) {
372
+ throw new RefNotFoundError(ref, `Cannot resolve ${ref}: commit not found`);
373
+ }
374
+ const parentIndex = count > 0 ? count - 1 : 0;
375
+ if (commit.parents.length <= parentIndex) {
376
+ throw new RefNotFoundError(ref, `Cannot resolve ${ref}: no parent ${count}`);
377
+ }
378
+ commitId = commit.parents[parentIndex];
379
+ }
380
+ }
381
+
382
+ return commitId;
383
+ }
384
+
385
+ /**
386
+ * Peel an object ID to a commit (follows tag objects).
387
+ */
388
+ private async peelToCommit(objectId: ObjectId): Promise<ObjectId> {
389
+ // If it's already a commit, return it
390
+ if (await this.commits.has(objectId)) {
391
+ return objectId;
392
+ }
393
+
394
+ // Try to load as tag and get target
395
+ const tags = this.tagsStore;
396
+ if (tags) {
397
+ try {
398
+ const tag = await tags.load(objectId);
399
+ if (tag) {
400
+ // Recursively peel
401
+ return this.peelToCommit(tag.object);
402
+ }
403
+ } catch {
404
+ // Not a tag
405
+ }
406
+ }
407
+
408
+ // Return as-is (might be a tree or blob in some contexts)
409
+ return objectId;
410
+ }
411
+
412
+ /**
413
+ * Get a ref by name.
414
+ *
415
+ * @param refName Full ref name
416
+ * @returns Ref object or undefined
417
+ */
418
+ protected async getRef(refName: string): Promise<Ref | SymbolicRef | undefined> {
419
+ return this.refsStore.get(refName);
420
+ }
421
+ }