@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,155 @@
1
+ import type { ObjectId } from "@statewalker/vcs-core";
2
+
3
+ import { GitCommand } from "../git-command.js";
4
+
5
+ /**
6
+ * Result of garbage collection
7
+ */
8
+ export interface GCCommandResult {
9
+ /** Number of objects removed */
10
+ objectsRemoved: number;
11
+ /** Bytes freed by removing objects */
12
+ bytesFreed: number;
13
+ /** Whether refs were packed */
14
+ refsPacked: boolean;
15
+ /** Duration of the GC operation in milliseconds */
16
+ durationMs: number;
17
+ }
18
+
19
+ /**
20
+ * Run garbage collection on the repository.
21
+ *
22
+ * Equivalent to `git gc`.
23
+ *
24
+ * Garbage collection removes unreachable objects and optimizes
25
+ * repository storage. This includes:
26
+ * - Removing objects not reachable from any ref
27
+ * - Packing loose refs into packed-refs
28
+ * - Repacking objects into pack files (optional)
29
+ *
30
+ * Based on JGit's GarbageCollectCommand.
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * // Basic garbage collection
35
+ * const result = await git.gc().call();
36
+ * console.log(`Removed ${result.objectsRemoved} objects`);
37
+ *
38
+ * // Aggressive GC with repacking
39
+ * const result = await git.gc()
40
+ * .setAggressive(true)
41
+ * .call();
42
+ *
43
+ * // GC with expiration date (keep recent unreachable objects)
44
+ * const twoWeeksAgo = new Date(Date.now() - 14 * 24 * 60 * 60 * 1000);
45
+ * const result = await git.gc()
46
+ * .setExpire(twoWeeksAgo)
47
+ * .call();
48
+ * ```
49
+ */
50
+ export class GarbageCollectCommand extends GitCommand<GCCommandResult> {
51
+ private _packRefs = true;
52
+ private _aggressive = false;
53
+ private _expire?: Date;
54
+
55
+ /**
56
+ * Set aggressive mode.
57
+ *
58
+ * In aggressive mode, GC will also repack objects for better compression.
59
+ *
60
+ * @param aggressive Whether to run aggressive GC
61
+ */
62
+ setAggressive(aggressive: boolean): this {
63
+ this.checkCallable();
64
+ this._aggressive = aggressive;
65
+ return this;
66
+ }
67
+
68
+ /**
69
+ * Set whether to pack refs.
70
+ *
71
+ * When true (default), loose refs are packed into packed-refs.
72
+ *
73
+ * @param packRefs Whether to pack refs
74
+ */
75
+ setPackRefs(packRefs: boolean): this {
76
+ this.checkCallable();
77
+ this._packRefs = packRefs;
78
+ return this;
79
+ }
80
+
81
+ /**
82
+ * Set expiration date for unreachable objects.
83
+ *
84
+ * Objects unreachable but modified after this date will not be deleted.
85
+ * This provides a grace period to prevent deleting objects that might
86
+ * still be needed by in-progress operations.
87
+ *
88
+ * @param expire Expiration date (objects older than this may be deleted)
89
+ */
90
+ setExpire(expire: Date): this {
91
+ this.checkCallable();
92
+ this._expire = expire;
93
+ return this;
94
+ }
95
+
96
+ /**
97
+ * Execute the garbage collection.
98
+ *
99
+ * @returns GC result with statistics
100
+ */
101
+ async call(): Promise<GCCommandResult> {
102
+ this.checkCallable();
103
+ this.setCallable(false);
104
+
105
+ const startTime = Date.now();
106
+ const objectsRemoved = 0;
107
+ const bytesFreed = 0;
108
+ let refsPacked = false;
109
+
110
+ // Collect all ref roots
111
+ const roots: ObjectId[] = [];
112
+ for await (const ref of this.refsStore.list("refs/")) {
113
+ if ("objectId" in ref && ref.objectId) {
114
+ roots.push(ref.objectId);
115
+ }
116
+ }
117
+
118
+ // Also include HEAD if it points to a commit
119
+ const head = await this.refsStore.resolve("HEAD");
120
+ if (head?.objectId) {
121
+ roots.push(head.objectId);
122
+ }
123
+
124
+ // Run garbage collection if GCController is available
125
+ // Note: This requires access to the Repository's gc property
126
+ // For now, we'll check if the RefStore supports the gc operations
127
+
128
+ // Check if refs support packRefs
129
+ if (this._packRefs && this.refsStore.packRefs) {
130
+ await this.refsStore.packRefs([], { all: true, deleteLoose: true });
131
+ refsPacked = true;
132
+ }
133
+
134
+ // Note: Full GC requires Repository.gc which provides access to
135
+ // GCController with collectGarbage(). This command provides the
136
+ // interface but full implementation requires Repository access.
137
+ // The WorkingCopy interface doesn't expose gc, so we'd need to extend
138
+ // the interface or use a different pattern.
139
+
140
+ // For aggressive mode, we could trigger repack here
141
+ // But that also requires access to the GCController
142
+ // Reserved for future implementation:
143
+ // - this._aggressive: trigger full repack with better compression
144
+ // - this._expire: pass to collectGarbage() for expiration filtering
145
+ void this._aggressive;
146
+ void this._expire;
147
+
148
+ return {
149
+ objectsRemoved,
150
+ bytesFreed,
151
+ refsPacked,
152
+ durationMs: Date.now() - startTime,
153
+ };
154
+ }
155
+ }
@@ -0,0 +1,31 @@
1
+ export * from "./add-command.js";
2
+ export * from "./blame-command.js";
3
+ export * from "./branch-command.js";
4
+ export * from "./checkout-command.js";
5
+ export * from "./cherry-pick-command.js";
6
+ export * from "./clean-command.js";
7
+ export * from "./clone-command.js";
8
+ export * from "./commit-command.js";
9
+ export * from "./describe-command.js";
10
+ export * from "./diff-command.js";
11
+ export * from "./fetch-command.js";
12
+ export * from "./gc-command.js";
13
+ export * from "./init-command.js";
14
+ export * from "./log-command.js";
15
+ export * from "./ls-remote-command.js";
16
+ export * from "./merge-command.js";
17
+ export * from "./pack-refs-command.js";
18
+ export * from "./pull-command.js";
19
+ export * from "./push-command.js";
20
+ export * from "./rebase-command.js";
21
+ export * from "./reflog-command.js";
22
+ export * from "./remote-command.js";
23
+ export * from "./reset-command.js";
24
+ export * from "./revert-command.js";
25
+ export * from "./rm-command.js";
26
+ export * from "./stash-apply-command.js";
27
+ export * from "./stash-create-command.js";
28
+ export * from "./stash-drop-command.js";
29
+ export * from "./stash-list-command.js";
30
+ export * from "./status-command.js";
31
+ export * from "./tag-command.js";
@@ -0,0 +1,365 @@
1
+ import { createInMemoryFilesApi, createMemoryHistory, type FilesApi } from "@statewalker/vcs-core";
2
+ import { createFileWorktree } from "@statewalker/vcs-store-files";
3
+ import { MemoryStagingStore } from "@statewalker/vcs-store-mem";
4
+ import {
5
+ createMemoryCheckout,
6
+ type Staging,
7
+ type WorkingCopy,
8
+ type Worktree,
9
+ } from "@statewalker/vcs-working-tree";
10
+
11
+ import { Git } from "../git.js";
12
+ import type { InitResult } from "../results/init-result.js";
13
+
14
+ /**
15
+ * Command to initialize a new Git repository.
16
+ *
17
+ * Based on JGit's InitCommand pattern. This command creates a new
18
+ * Git repository with the specified configuration.
19
+ *
20
+ * Note: Unlike other commands, InitCommand does not extend GitCommand
21
+ * because it creates a new repository rather than operating on an existing one.
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * // Initialize in-memory repository
26
+ * const result = await new InitCommand().call();
27
+ * const git = result.git;
28
+ *
29
+ * // Initialize file-based repository
30
+ * import { createNodeFilesApi } from "@statewalker/vcs-utils-node/files";
31
+ * const files = createNodeFilesApi({ fs, rootDir: "/path/to/project" });
32
+ * const result = await new InitCommand()
33
+ * .setFilesApi(files)
34
+ * .setDirectory(".git")
35
+ * .call();
36
+ *
37
+ * // Initialize bare repository
38
+ * const result = await new InitCommand()
39
+ * .setBare(true)
40
+ * .setInitialBranch("main")
41
+ * .call();
42
+ *
43
+ * // Initialize with custom staging store and worktree support
44
+ * const result = await new InitCommand()
45
+ * .setFilesApi(files)
46
+ * .setStagingStore(new FileStagingStore(files, ".git/index"))
47
+ * .setWorktree(true)
48
+ * .call();
49
+ * ```
50
+ */
51
+ export class InitCommand {
52
+ private files?: FilesApi;
53
+ private directory?: string;
54
+ private gitDir?: string;
55
+ private bare = false;
56
+ private initialBranch = "main";
57
+ private stagingStore?: Staging;
58
+ private worktreeEnabled = false;
59
+ private worktreeStore?: Worktree;
60
+ private callable = true;
61
+
62
+ /**
63
+ * Set the FilesApi implementation for storage.
64
+ *
65
+ * If not set, an in-memory FilesApi will be used.
66
+ *
67
+ * @param files The FilesApi to use for repository storage
68
+ * @returns this for method chaining
69
+ */
70
+ setFilesApi(files: FilesApi): this {
71
+ this.checkCallable();
72
+ this.files = files;
73
+ return this;
74
+ }
75
+
76
+ /**
77
+ * Set the working directory for the repository.
78
+ *
79
+ * For non-bare repositories, this is the parent of the .git directory.
80
+ * If gitDir is not explicitly set, it will be `${directory}/.git`.
81
+ *
82
+ * @param directory The working directory path
83
+ * @returns this for method chaining
84
+ */
85
+ setDirectory(directory: string): this {
86
+ this.checkCallable();
87
+ this.directory = directory;
88
+ return this;
89
+ }
90
+
91
+ /**
92
+ * Set the git directory path.
93
+ *
94
+ * For non-bare repositories, this is typically `.git` inside the working directory.
95
+ * For bare repositories, this is the repository root.
96
+ *
97
+ * @param gitDir The .git directory path
98
+ * @returns this for method chaining
99
+ */
100
+ setGitDir(gitDir: string): this {
101
+ this.checkCallable();
102
+ this.gitDir = gitDir;
103
+ return this;
104
+ }
105
+
106
+ /**
107
+ * Set whether to create a bare repository.
108
+ *
109
+ * Bare repositories have no working tree and are typically used
110
+ * as central repositories for pushing/pulling.
111
+ *
112
+ * @param bare True for bare repository, false for normal
113
+ * @returns this for method chaining
114
+ */
115
+ setBare(bare: boolean): this {
116
+ this.checkCallable();
117
+ this.bare = bare;
118
+ return this;
119
+ }
120
+
121
+ /**
122
+ * Set the initial branch name.
123
+ *
124
+ * This is the branch that HEAD will point to after initialization.
125
+ * Defaults to "main".
126
+ *
127
+ * @param branch The initial branch name
128
+ * @returns this for method chaining
129
+ */
130
+ setInitialBranch(branch: string): this {
131
+ this.checkCallable();
132
+ this.initialBranch = branch;
133
+ return this;
134
+ }
135
+
136
+ /**
137
+ * Set a custom staging store.
138
+ *
139
+ * If not set, a MemoryStagingStore will be used.
140
+ * Use FileStagingStore for native git compatibility.
141
+ *
142
+ * @param stagingStore The staging store to use
143
+ * @returns this for method chaining
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * // For native git compatibility
148
+ * const fileStaging = new FileStagingStore(files, ".git/index");
149
+ * await fileStaging.read(); // Load existing index if present
150
+ *
151
+ * const result = await new InitCommand()
152
+ * .setFilesApi(files)
153
+ * .setStagingStore(fileStaging)
154
+ * .call();
155
+ * ```
156
+ */
157
+ setStagingStore(stagingStore: Staging): this {
158
+ this.checkCallable();
159
+ this.stagingStore = stagingStore;
160
+ return this;
161
+ }
162
+
163
+ /**
164
+ * Enable worktree support for porcelain commands like git.add().
165
+ *
166
+ * When enabled, creates a WorktreeStore from the FilesApi that allows
167
+ * commands like AddCommand to iterate and read files from the working tree.
168
+ *
169
+ * Ignored for bare repositories.
170
+ *
171
+ * @param enabled True to enable worktree support
172
+ * @returns this for method chaining
173
+ *
174
+ * @example
175
+ * ```typescript
176
+ * const result = await new InitCommand()
177
+ * .setFilesApi(files)
178
+ * .setWorktree(true)
179
+ * .call();
180
+ *
181
+ * // Now git.add() works
182
+ * await result.git.add().addFilepattern("src/").call();
183
+ * ```
184
+ */
185
+ setWorktree(enabled: boolean): this {
186
+ this.checkCallable();
187
+ this.worktreeEnabled = enabled;
188
+ return this;
189
+ }
190
+
191
+ /**
192
+ * Set a custom worktree store.
193
+ *
194
+ * Alternative to setWorktree(true) when you need custom worktree configuration.
195
+ *
196
+ * @param worktree The worktree store to use
197
+ * @returns this for method chaining
198
+ */
199
+ setWorktreeStore(worktree: Worktree): this {
200
+ this.checkCallable();
201
+ this.worktreeStore = worktree;
202
+ this.worktreeEnabled = true;
203
+ return this;
204
+ }
205
+
206
+ /**
207
+ * Execute the init command and create a new repository.
208
+ *
209
+ * @returns InitResult containing the Git facade, store, and repository
210
+ * @throws Error if the command has already been called
211
+ */
212
+ async call(): Promise<InitResult> {
213
+ this.checkCallable();
214
+ this.setCallable(false);
215
+
216
+ // Use in-memory FilesApi if none provided
217
+ const files = this.files ?? createInMemoryFilesApi();
218
+
219
+ // Determine git directory
220
+ const gitDir = this.resolveGitDir();
221
+
222
+ // Create the history (repository)
223
+ const history = createMemoryHistory();
224
+ await history.initialize();
225
+
226
+ // Set initial branch by creating symbolic ref for HEAD
227
+ await history.refs.setSymbolic("HEAD", `refs/heads/${this.initialBranch}`);
228
+
229
+ // Create staging store (use provided or default to MemoryStagingStore)
230
+ const staging = this.stagingStore ?? new MemoryStagingStore();
231
+
232
+ // Create worktree if enabled and not bare
233
+ let worktree: Worktree | undefined;
234
+ if (this.worktreeEnabled && !this.bare) {
235
+ worktree =
236
+ this.worktreeStore ??
237
+ createFileWorktree({
238
+ files,
239
+ rootPath: "",
240
+ blobs: history.blobs,
241
+ trees: history.trees,
242
+ gitDir,
243
+ });
244
+ }
245
+
246
+ // Wrap the staging area in a Checkout so `workingCopy.checkout.staging`
247
+ // resolves (matching the WorkingCopy interface). A freshly-init'd repo has
248
+ // no HEAD/stash/in-progress operation yet, so those default to unborn/empty.
249
+ // The Checkout shares the same `staging` instance as the legacy top-level
250
+ // `staging` field, so edits are visible through either path.
251
+ const checkout = createMemoryCheckout({ staging });
252
+
253
+ // Create WorkingCopy from components
254
+ // Note: Staging/Worktree implementations may be partial
255
+ // Full implementations are provided at runtime by workspace integration
256
+ // Use type assertion since init creates a minimal working copy
257
+ const workingCopy = {
258
+ history,
259
+ staging: staging as unknown as Staging,
260
+ worktree: worktree as unknown as Worktree,
261
+ stash: {} as never, // Stash not available for newly init'd repos
262
+ config: {} as never, // Config not set for newly init'd repos
263
+ checkout,
264
+ async getHead() {
265
+ const ref = await history.refs.resolve("HEAD");
266
+ return ref?.objectId;
267
+ },
268
+ async getCurrentBranch() {
269
+ const ref = await history.refs.get("HEAD");
270
+ if (ref && "target" in ref) {
271
+ return ref.target.replace("refs/heads/", "");
272
+ }
273
+ return undefined;
274
+ },
275
+ async setHead() {
276
+ // No-op for init command
277
+ },
278
+ async isDetachedHead() {
279
+ return false;
280
+ },
281
+ async getMergeState() {
282
+ return undefined;
283
+ },
284
+ async getRebaseState() {
285
+ return undefined;
286
+ },
287
+ async getCherryPickState() {
288
+ return undefined;
289
+ },
290
+ async getRevertState() {
291
+ return undefined;
292
+ },
293
+ async hasOperationInProgress() {
294
+ return false;
295
+ },
296
+ async getStatus() {
297
+ // Return a minimal RepositoryStatus compatible object
298
+ return {
299
+ files: [],
300
+ staged: [],
301
+ unstaged: [],
302
+ untracked: [],
303
+ isClean: true,
304
+ hasStaged: false,
305
+ hasUnstaged: false,
306
+ hasUntracked: false,
307
+ hasConflicts: false,
308
+ };
309
+ },
310
+ } as unknown as WorkingCopy;
311
+
312
+ // Create Git facade
313
+ const git = Git.fromWorkingCopy(workingCopy);
314
+
315
+ return {
316
+ git,
317
+ workingCopy,
318
+ repository: history,
319
+ initialBranch: this.initialBranch,
320
+ bare: this.bare,
321
+ gitDir,
322
+ };
323
+ }
324
+
325
+ /**
326
+ * Resolve the git directory path based on configuration.
327
+ */
328
+ private resolveGitDir(): string {
329
+ // If gitDir is explicitly set, use it
330
+ if (this.gitDir !== undefined) {
331
+ return this.gitDir;
332
+ }
333
+
334
+ // If directory is set, derive gitDir
335
+ if (this.directory !== undefined) {
336
+ if (this.bare) {
337
+ // Bare repositories: the directory IS the git directory
338
+ return this.directory;
339
+ } else {
340
+ // Normal repositories: .git inside the directory
341
+ return `${this.directory}/.git`;
342
+ }
343
+ }
344
+
345
+ // Default to .git
346
+ return ".git";
347
+ }
348
+
349
+ /**
350
+ * Check if the command is still callable.
351
+ * @throws Error if the command has already been executed
352
+ */
353
+ private checkCallable(): void {
354
+ if (!this.callable) {
355
+ throw new Error("InitCommand can only be called once");
356
+ }
357
+ }
358
+
359
+ /**
360
+ * Set the callable state.
361
+ */
362
+ private setCallable(value: boolean): void {
363
+ this.callable = value;
364
+ }
365
+ }