@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,68 @@
1
+ import type { ReflogEntry } from "@statewalker/vcs-core";
2
+
3
+ import { GitCommand } from "../git-command.js";
4
+
5
+ /**
6
+ * Read reflog entries for a ref.
7
+ *
8
+ * Equivalent to `git reflog`.
9
+ *
10
+ * The reflog records when the tip of branches and other references
11
+ * were updated in the local repository.
12
+ *
13
+ * Based on JGit's ReflogCommand.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * // Get HEAD reflog entries
18
+ * const entries = await git.reflog().call();
19
+ *
20
+ * // Get reflog for specific branch
21
+ * const branchEntries = await git.reflog()
22
+ * .setRef("refs/heads/main")
23
+ * .call();
24
+ *
25
+ * // Iterate through entries
26
+ * for (const entry of entries) {
27
+ * console.log(`${entry.newId.slice(0, 7)} ${entry.comment}`);
28
+ * }
29
+ * ```
30
+ */
31
+ export class ReflogCommand extends GitCommand<ReflogEntry[]> {
32
+ private ref = "HEAD";
33
+
34
+ /**
35
+ * Set the ref to show reflog for.
36
+ *
37
+ * @param ref Ref name (default: "HEAD")
38
+ */
39
+ setRef(ref: string): this {
40
+ this.checkCallable();
41
+ this.ref = ref;
42
+ return this;
43
+ }
44
+
45
+ /**
46
+ * Execute the reflog command.
47
+ *
48
+ * @returns Array of reflog entries in reverse chronological order (most recent first)
49
+ * @throws Error if reflog doesn't exist for the ref
50
+ */
51
+ async call(): Promise<ReflogEntry[]> {
52
+ this.checkCallable();
53
+ this.setCallable(false);
54
+
55
+ // Check if the RefStore supports reflog
56
+ if (!this.refsStore.getReflog) {
57
+ throw new Error("RefStore does not support reflog");
58
+ }
59
+
60
+ const reader = await this.refsStore.getReflog(this.ref);
61
+ if (!reader) {
62
+ // Return empty array if no reflog exists (consistent with git behavior)
63
+ return [];
64
+ }
65
+
66
+ return reader.getReverseEntries();
67
+ }
68
+ }
@@ -0,0 +1,385 @@
1
+ import {
2
+ MissingArgumentError,
3
+ RemoteAlreadyExistsError,
4
+ RemoteNotFoundError,
5
+ } from "../errors/index.js";
6
+ import { GitCommand } from "../git-command.js";
7
+ import {
8
+ defaultFetchRefspec,
9
+ listRemoteNames,
10
+ type RemoteConfig,
11
+ RemoteConfigStore,
12
+ } from "../remote-config/index.js";
13
+
14
+ export type { RemoteConfig };
15
+
16
+ /**
17
+ * Add a remote to the repository.
18
+ *
19
+ * Equivalent to `git remote add`.
20
+ *
21
+ * Based on JGit's RemoteAddCommand.
22
+ *
23
+ * The remote is written to the working copy configuration as a
24
+ * `[remote "<name>"]` section, so it survives the command that created it.
25
+ * A remote already known from `refs/remotes/<name>/*` counts as existing even
26
+ * without a config section.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * await git.remoteAdd()
31
+ * .setName("upstream")
32
+ * .setUri("https://github.com/user/repo")
33
+ * .call();
34
+ * ```
35
+ */
36
+ export class RemoteAddCommand extends GitCommand<RemoteConfig> {
37
+ private name?: string;
38
+ private uri?: string;
39
+ private fetchRefspec?: string;
40
+
41
+ /**
42
+ * Set the remote name.
43
+ *
44
+ * @param name Remote name
45
+ */
46
+ setName(name: string): this {
47
+ this.checkCallable();
48
+ this.name = name;
49
+ return this;
50
+ }
51
+
52
+ /**
53
+ * Get the remote name.
54
+ */
55
+ getName(): string | undefined {
56
+ return this.name;
57
+ }
58
+
59
+ /**
60
+ * Set the remote URI.
61
+ *
62
+ * @param uri Remote URL
63
+ */
64
+ setUri(uri: string): this {
65
+ this.checkCallable();
66
+ this.uri = uri;
67
+ return this;
68
+ }
69
+
70
+ /**
71
+ * Get the remote URI.
72
+ */
73
+ getUri(): string | undefined {
74
+ return this.uri;
75
+ }
76
+
77
+ /**
78
+ * Set custom fetch refspec.
79
+ *
80
+ * If not set, defaults to +refs/heads/*:refs/remotes/{name}/*
81
+ *
82
+ * @param refspec Fetch refspec
83
+ */
84
+ setFetchRefspec(refspec: string): this {
85
+ this.checkCallable();
86
+ this.fetchRefspec = refspec;
87
+ return this;
88
+ }
89
+
90
+ /**
91
+ * Execute the remote add operation.
92
+ *
93
+ * @returns Remote configuration
94
+ * @throws Error if name or URI is not set
95
+ */
96
+ async call(): Promise<RemoteConfig> {
97
+ this.checkCallable();
98
+ this.setCallable(false);
99
+
100
+ if (!this.name) {
101
+ throw new MissingArgumentError("name", "Remote name must be specified");
102
+ }
103
+ if (!this.uri) {
104
+ throw new MissingArgumentError("uri", "Remote URI must be specified");
105
+ }
106
+
107
+ const store = RemoteConfigStore.from(this.workingCopy);
108
+
109
+ // A remote exists if it is configured OR if it already has tracking refs.
110
+ if (store.isConfigured(this.name) || (await this.hasTrackingRefs(this.name))) {
111
+ throw new RemoteAlreadyExistsError(this.name);
112
+ }
113
+
114
+ const config: RemoteConfig = {
115
+ name: this.name,
116
+ urls: [this.uri],
117
+ pushUrls: [],
118
+ fetchRefspecs: [this.fetchRefspec || defaultFetchRefspec(this.name)],
119
+ pushRefspecs: [],
120
+ };
121
+
122
+ store.write(config);
123
+ await store.save();
124
+
125
+ return config;
126
+ }
127
+
128
+ /** Whether any `refs/remotes/<name>/*` ref exists. */
129
+ private async hasTrackingRefs(name: string): Promise<boolean> {
130
+ for await (const _ref of this.refsStore.list(`refs/remotes/${name}/`)) {
131
+ return true;
132
+ }
133
+ return false;
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Remove a remote from the repository.
139
+ *
140
+ * Equivalent to `git remote remove`.
141
+ *
142
+ * Based on JGit's RemoteRemoveCommand.
143
+ *
144
+ * @example
145
+ * ```typescript
146
+ * await git.remoteRemove()
147
+ * .setRemoteName("upstream")
148
+ * .call();
149
+ * ```
150
+ */
151
+ export class RemoteRemoveCommand extends GitCommand<RemoteConfig | undefined> {
152
+ private remoteName?: string;
153
+
154
+ /**
155
+ * Set the remote name to remove.
156
+ *
157
+ * @param name Remote name
158
+ */
159
+ setRemoteName(name: string): this {
160
+ this.checkCallable();
161
+ this.remoteName = name;
162
+ return this;
163
+ }
164
+
165
+ /**
166
+ * Get the remote name.
167
+ */
168
+ getRemoteName(): string | undefined {
169
+ return this.remoteName;
170
+ }
171
+
172
+ /**
173
+ * Execute the remote remove operation.
174
+ *
175
+ * Drops both halves of a remote: its config section and its tracking refs.
176
+ * A remote that has only one of the two is still removable.
177
+ *
178
+ * @returns Removed remote config, or undefined if not found
179
+ */
180
+ async call(): Promise<RemoteConfig | undefined> {
181
+ this.checkCallable();
182
+ this.setCallable(false);
183
+
184
+ if (!this.remoteName) {
185
+ throw new MissingArgumentError("remoteName", "Remote name must be specified");
186
+ }
187
+
188
+ const store = RemoteConfigStore.from(this.workingCopy);
189
+ const configured = store.isConfigured(this.remoteName);
190
+
191
+ const refsToDelete: string[] = [];
192
+ for await (const ref of this.refsStore.list(`refs/remotes/${this.remoteName}/`)) {
193
+ refsToDelete.push(ref.name);
194
+ }
195
+
196
+ if (!configured && refsToDelete.length === 0) {
197
+ return undefined;
198
+ }
199
+
200
+ // Read before removing: this is what we report back.
201
+ const removed = store.read(this.remoteName);
202
+
203
+ for (const refName of refsToDelete) {
204
+ await this.refsStore.delete(refName);
205
+ }
206
+
207
+ if (configured) {
208
+ store.remove(this.remoteName);
209
+ await store.save();
210
+ }
211
+
212
+ return removed;
213
+ }
214
+ }
215
+
216
+ /**
217
+ * List remotes in the repository.
218
+ *
219
+ * Equivalent to `git remote -v`.
220
+ *
221
+ * Based on JGit's RemoteListCommand.
222
+ *
223
+ * @example
224
+ * ```typescript
225
+ * const remotes = await git.remoteList().call();
226
+ * for (const remote of remotes) {
227
+ * console.log(remote.name, remote.urls[0]);
228
+ * }
229
+ * ```
230
+ */
231
+ export class RemoteListCommand extends GitCommand<RemoteConfig[]> {
232
+ /**
233
+ * Execute the remote list operation.
234
+ *
235
+ * Lists the union of the configured remotes and the names appearing under
236
+ * `refs/remotes/`; a remote with no config section is reported with no URLs
237
+ * and the default fetch refspec.
238
+ *
239
+ * @returns List of remote configurations
240
+ */
241
+ async call(): Promise<RemoteConfig[]> {
242
+ this.checkCallable();
243
+ this.setCallable(false);
244
+
245
+ const store = RemoteConfigStore.from(this.workingCopy);
246
+ const names = await listRemoteNames(store, this.refsStore);
247
+ return names.map((name) => store.read(name));
248
+ }
249
+ }
250
+
251
+ /**
252
+ * Set URL for a remote.
253
+ *
254
+ * Equivalent to `git remote set-url`.
255
+ *
256
+ * Based on JGit's RemoteSetUrlCommand.
257
+ *
258
+ * @example
259
+ * ```typescript
260
+ * await git.remoteSetUrl()
261
+ * .setRemoteName("origin")
262
+ * .setRemoteUri("https://github.com/user/repo")
263
+ * .call();
264
+ * ```
265
+ */
266
+ export class RemoteSetUrlCommand extends GitCommand<RemoteConfig> {
267
+ private remoteName?: string;
268
+ private remoteUri?: string;
269
+ private pushUri = false;
270
+ private oldUri?: string;
271
+
272
+ /**
273
+ * Set the remote name.
274
+ *
275
+ * @param name Remote name
276
+ */
277
+ setRemoteName(name: string): this {
278
+ this.checkCallable();
279
+ this.remoteName = name;
280
+ return this;
281
+ }
282
+
283
+ /**
284
+ * Get the remote name.
285
+ */
286
+ getRemoteName(): string | undefined {
287
+ return this.remoteName;
288
+ }
289
+
290
+ /**
291
+ * Set the new remote URI.
292
+ *
293
+ * @param uri New remote URL
294
+ */
295
+ setRemoteUri(uri: string): this {
296
+ this.checkCallable();
297
+ this.remoteUri = uri;
298
+ return this;
299
+ }
300
+
301
+ /**
302
+ * Get the new remote URI.
303
+ */
304
+ getRemoteUri(): string | undefined {
305
+ return this.remoteUri;
306
+ }
307
+
308
+ /**
309
+ * Set whether this is a push URL.
310
+ *
311
+ * @param push Whether to set push URL
312
+ */
313
+ setPush(push: boolean): this {
314
+ this.checkCallable();
315
+ this.pushUri = push;
316
+ return this;
317
+ }
318
+
319
+ /**
320
+ * Whether this is a push URL.
321
+ */
322
+ isPush(): boolean {
323
+ return this.pushUri;
324
+ }
325
+
326
+ /**
327
+ * Set the URL to replace.
328
+ *
329
+ * Only this URL is replaced, leaving the remote's other URLs alone — the
330
+ * `<oldurl>` argument of `git remote set-url <name> <newurl> <oldurl>`.
331
+ * Without it, the new URL replaces all of them.
332
+ *
333
+ * @param uri Old URI to replace
334
+ */
335
+ setOldUri(uri: string): this {
336
+ this.checkCallable();
337
+ this.oldUri = uri;
338
+ return this;
339
+ }
340
+
341
+ /**
342
+ * Execute the remote set-url operation.
343
+ *
344
+ * The URL is written to the working copy configuration, so it survives the
345
+ * command. The remote may be known from its config section or from its
346
+ * tracking refs; in the latter case, this is what first gives it a URL.
347
+ *
348
+ * @returns Updated remote configuration
349
+ * @throws MissingArgumentError if remote name or URI is not set
350
+ * @throws RemoteNotFoundError if the remote is neither configured nor tracked
351
+ * @throws InvalidArgumentError if {@link setOldUri} names a URL the remote does not have
352
+ */
353
+ async call(): Promise<RemoteConfig> {
354
+ this.checkCallable();
355
+ this.setCallable(false);
356
+
357
+ if (!this.remoteName) {
358
+ throw new MissingArgumentError("remoteName", "Remote name must be specified");
359
+ }
360
+ if (!this.remoteUri) {
361
+ throw new MissingArgumentError("remoteUri", "Remote URI must be specified");
362
+ }
363
+
364
+ const store = RemoteConfigStore.from(this.workingCopy);
365
+ if (!store.isConfigured(this.remoteName) && !(await this.hasTrackingRefs(this.remoteName))) {
366
+ throw new RemoteNotFoundError(this.remoteName);
367
+ }
368
+
369
+ store.setUrl(this.remoteName, this.remoteUri, {
370
+ push: this.pushUri,
371
+ oldUrl: this.oldUri,
372
+ });
373
+ await store.save();
374
+
375
+ return store.read(this.remoteName);
376
+ }
377
+
378
+ /** Whether any `refs/remotes/<name>/*` ref exists. */
379
+ private async hasTrackingRefs(name: string): Promise<boolean> {
380
+ for await (const _ref of this.refsStore.list(`refs/remotes/${name}/`)) {
381
+ return true;
382
+ }
383
+ return false;
384
+ }
385
+ }