@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,549 @@
1
+ import type { Commit, ObjectId } from "@statewalker/vcs-core";
2
+
3
+ import { GitCommand } from "../git-command.js";
4
+
5
+ /**
6
+ * Result of a log command iteration.
7
+ *
8
+ * Extends Commit with the object ID.
9
+ */
10
+ export interface LogResult extends Commit {
11
+ /** The SHA-1 object ID of the commit */
12
+ id: ObjectId;
13
+ }
14
+
15
+ /**
16
+ * Revision filter for LogCommand.
17
+ *
18
+ * Based on JGit's RevFilter.
19
+ */
20
+ export enum RevFilter {
21
+ /** Include all commits (default) */
22
+ ALL = "all",
23
+ /** Only include merge commits (commits with more than one parent) */
24
+ ONLY_MERGES = "only-merges",
25
+ /** Exclude merge commits */
26
+ NO_MERGES = "no-merges",
27
+ }
28
+
29
+ /**
30
+ * Show commit history.
31
+ *
32
+ * Equivalent to `git log`.
33
+ *
34
+ * Based on JGit's LogCommand.
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * // Get all commits from HEAD
39
+ * for await (const commit of await git.log().call()) {
40
+ * console.log(commit.message);
41
+ * }
42
+ *
43
+ * // Get commits from a specific ref
44
+ * const commits = await git.log().add(commitId).call();
45
+ *
46
+ * // Limit number of commits
47
+ * const recent = await git.log().setMaxCount(10).call();
48
+ *
49
+ * // Skip first N commits
50
+ * const older = await git.log().setSkip(10).setMaxCount(10).call();
51
+ *
52
+ * // Filter by date range
53
+ * const thisWeek = await git.log()
54
+ * .setSince(Date.now() - 7 * 24 * 60 * 60 * 1000)
55
+ * .call();
56
+ *
57
+ * // Filter by author
58
+ * const myCommits = await git.log()
59
+ * .setAuthorFilter("john@example.com")
60
+ * .call();
61
+ *
62
+ * // Exclude commits
63
+ * const newCommits = await git.log()
64
+ * .not(oldBranchHead)
65
+ * .call();
66
+ *
67
+ * // Only merge commits
68
+ * const merges = await git.log()
69
+ * .setRevFilter(RevFilter.ONLY_MERGES)
70
+ * .call();
71
+ *
72
+ * // Exclude merge commits
73
+ * const nonMerges = await git.log()
74
+ * .setRevFilter(RevFilter.NO_MERGES)
75
+ * .call();
76
+ *
77
+ * // Range syntax (commits in feature not in main)
78
+ * const newInFeature = await git.log()
79
+ * .addRange(mainHead, featureHead)
80
+ * .call();
81
+ * ```
82
+ */
83
+ export class LogCommand extends GitCommand<AsyncIterable<LogResult>> {
84
+ private startCommits: ObjectId[] = [];
85
+ private excludeCommits: ObjectId[] = [];
86
+ private paths: string[] = [];
87
+ private excludePaths: string[] = [];
88
+ private maxCount?: number;
89
+ private skip = 0;
90
+ private includeAll = false;
91
+ private firstParentOnly = false;
92
+ private sinceTime?: number;
93
+ private untilTime?: number;
94
+ private authorPattern?: string;
95
+ private committerPattern?: string;
96
+ private revFilter = RevFilter.ALL;
97
+
98
+ /**
99
+ * Add a starting commit for the log.
100
+ *
101
+ * Multiple start commits can be added - the log will include
102
+ * ancestors of all of them.
103
+ *
104
+ * @param commit ObjectId of commit to start from
105
+ */
106
+ add(commit: ObjectId): this {
107
+ this.checkCallable();
108
+ this.startCommits.push(commit);
109
+ return this;
110
+ }
111
+
112
+ /**
113
+ * Filter commits to only those affecting the given path.
114
+ *
115
+ * @param path Repository-relative path to filter by
116
+ */
117
+ addPath(path: string): this {
118
+ this.checkCallable();
119
+ this.paths.push(path);
120
+ return this;
121
+ }
122
+
123
+ /**
124
+ * Exclude commits that only affect the given path.
125
+ *
126
+ * If a path is both added and excluded, the exclusion wins.
127
+ *
128
+ * @param path Repository-relative path to exclude
129
+ */
130
+ excludePath(path: string): this {
131
+ this.checkCallable();
132
+ this.excludePaths.push(path);
133
+ return this;
134
+ }
135
+
136
+ /**
137
+ * Limit the number of commits returned.
138
+ *
139
+ * @param maxCount Maximum number of commits to return
140
+ */
141
+ setMaxCount(maxCount: number): this {
142
+ this.checkCallable();
143
+ this.maxCount = maxCount;
144
+ return this;
145
+ }
146
+
147
+ /**
148
+ * Skip the first N commits.
149
+ *
150
+ * @param skip Number of commits to skip
151
+ */
152
+ setSkip(skip: number): this {
153
+ this.checkCallable();
154
+ this.skip = skip;
155
+ return this;
156
+ }
157
+
158
+ /**
159
+ * Include commits from all refs.
160
+ *
161
+ * When set, starts from all refs instead of just HEAD.
162
+ */
163
+ all(): this {
164
+ this.checkCallable();
165
+ this.includeAll = true;
166
+ return this;
167
+ }
168
+
169
+ /**
170
+ * Follow only the first parent on merges.
171
+ *
172
+ * Useful for seeing the linear history of a branch.
173
+ */
174
+ setFirstParent(firstParent: boolean): this {
175
+ this.checkCallable();
176
+ this.firstParentOnly = firstParent;
177
+ return this;
178
+ }
179
+
180
+ /**
181
+ * Exclude commits reachable from the given commit.
182
+ *
183
+ * Equivalent to `git log ^commit` or `A..B` syntax.
184
+ * Useful for finding commits in one branch but not another.
185
+ *
186
+ * @param commit ObjectId to exclude along with its ancestors
187
+ */
188
+ not(commit: ObjectId): this {
189
+ this.checkCallable();
190
+ this.excludeCommits.push(commit);
191
+ return this;
192
+ }
193
+
194
+ /**
195
+ * Set minimum commit time (--since/--after).
196
+ *
197
+ * Only include commits after this timestamp.
198
+ *
199
+ * @param timestamp Unix timestamp in milliseconds or seconds
200
+ */
201
+ setSince(timestamp: number): this {
202
+ this.checkCallable();
203
+ // Normalize to seconds (Git uses seconds)
204
+ this.sinceTime = timestamp > 1e12 ? Math.floor(timestamp / 1000) : timestamp;
205
+ return this;
206
+ }
207
+
208
+ /**
209
+ * Set maximum commit time (--until/--before).
210
+ *
211
+ * Only include commits before this timestamp.
212
+ *
213
+ * @param timestamp Unix timestamp in milliseconds or seconds
214
+ */
215
+ setUntil(timestamp: number): this {
216
+ this.checkCallable();
217
+ // Normalize to seconds (Git uses seconds)
218
+ this.untilTime = timestamp > 1e12 ? Math.floor(timestamp / 1000) : timestamp;
219
+ return this;
220
+ }
221
+
222
+ /**
223
+ * Filter by author name or email (case-insensitive).
224
+ *
225
+ * Matches against "Author Name <email>" string.
226
+ *
227
+ * @param pattern Substring to match in author field
228
+ */
229
+ setAuthorFilter(pattern: string): this {
230
+ this.checkCallable();
231
+ this.authorPattern = pattern.toLowerCase();
232
+ return this;
233
+ }
234
+
235
+ /**
236
+ * Filter by committer name or email (case-insensitive).
237
+ *
238
+ * Matches against "Committer Name <email>" string.
239
+ *
240
+ * @param pattern Substring to match in committer field
241
+ */
242
+ setCommitterFilter(pattern: string): this {
243
+ this.checkCallable();
244
+ this.committerPattern = pattern.toLowerCase();
245
+ return this;
246
+ }
247
+
248
+ /**
249
+ * Set revision filter.
250
+ *
251
+ * - ALL: Include all commits (default)
252
+ * - ONLY_MERGES: Only include merge commits
253
+ * - NO_MERGES: Exclude merge commits
254
+ *
255
+ * @param filter Revision filter
256
+ */
257
+ setRevFilter(filter: RevFilter): this {
258
+ this.checkCallable();
259
+ this.revFilter = filter;
260
+ return this;
261
+ }
262
+
263
+ /**
264
+ * Add a commit range.
265
+ *
266
+ * Equivalent to `git log since..until` - includes commits reachable
267
+ * from `until` but not from `since`.
268
+ *
269
+ * @param since Commits reachable from here are excluded
270
+ * @param until Commits reachable from here are included (start point)
271
+ */
272
+ addRange(since: ObjectId, until: ObjectId): this {
273
+ this.checkCallable();
274
+ this.excludeCommits.push(since);
275
+ this.startCommits.push(until);
276
+ return this;
277
+ }
278
+
279
+ /**
280
+ * Execute the log command.
281
+ *
282
+ * @returns AsyncIterable of commits in reverse chronological order
283
+ */
284
+ async call(): Promise<AsyncIterable<LogResult>> {
285
+ this.checkCallable();
286
+ this.setCallable(false);
287
+
288
+ // Determine starting commits
289
+ let starts: ObjectId[];
290
+ if (this.startCommits.length > 0) {
291
+ starts = this.startCommits;
292
+ } else if (this.includeAll) {
293
+ starts = await this.getAllRefs();
294
+ } else {
295
+ const headId = await this.resolveHead();
296
+ starts = [headId];
297
+ }
298
+
299
+ return this.walkCommits(starts);
300
+ }
301
+
302
+ /**
303
+ * Get all commit IDs from all refs.
304
+ */
305
+ private async getAllRefs(): Promise<ObjectId[]> {
306
+ const ids: ObjectId[] = [];
307
+ for await (const ref of this.refsStore.list()) {
308
+ if ("objectId" in ref && ref.objectId) {
309
+ ids.push(ref.objectId);
310
+ }
311
+ }
312
+ return ids;
313
+ }
314
+
315
+ /**
316
+ * Walk commits and yield them.
317
+ */
318
+ private async *walkCommits(starts: ObjectId[]): AsyncIterable<LogResult> {
319
+ let count = 0;
320
+ let skipped = 0;
321
+
322
+ // Build set of excluded commits (including their ancestors)
323
+ const excludedSet = await this.buildExcludedSet();
324
+
325
+ for await (const commitId of this.commits.walkAncestry(starts, {
326
+ firstParentOnly: this.firstParentOnly,
327
+ })) {
328
+ // Skip excluded commits
329
+ if (excludedSet.has(commitId)) {
330
+ continue;
331
+ }
332
+
333
+ const commit = await this.commits.load(commitId);
334
+ if (!commit) {
335
+ continue;
336
+ }
337
+
338
+ // RevFilter - ONLY_MERGES or NO_MERGES
339
+ if (this.revFilter === RevFilter.ONLY_MERGES) {
340
+ // Only include merge commits (more than one parent)
341
+ if (commit.parents.length <= 1) {
342
+ continue;
343
+ }
344
+ } else if (this.revFilter === RevFilter.NO_MERGES) {
345
+ // Exclude merge commits
346
+ if (commit.parents.length > 1) {
347
+ continue;
348
+ }
349
+ }
350
+
351
+ // Date filtering - since (after)
352
+ if (this.sinceTime !== undefined && commit.committer.timestamp < this.sinceTime) {
353
+ // Commits are in reverse chronological order, so we can stop early
354
+ break;
355
+ }
356
+
357
+ // Date filtering - until (before)
358
+ if (this.untilTime !== undefined && commit.committer.timestamp > this.untilTime) {
359
+ continue;
360
+ }
361
+
362
+ // Author filtering
363
+ if (this.authorPattern !== undefined && !this.matchesAuthor(commit)) {
364
+ continue;
365
+ }
366
+
367
+ // Committer filtering
368
+ if (this.committerPattern !== undefined && !this.matchesCommitter(commit)) {
369
+ continue;
370
+ }
371
+
372
+ // Path filtering (include paths)
373
+ if (this.paths.length > 0 && !(await this.affectsPath(commit, commitId))) {
374
+ continue;
375
+ }
376
+
377
+ // Path filtering (exclude paths) - if commit only affects excluded paths, skip it
378
+ if (this.excludePaths.length > 0 && (await this.onlyAffectsExcludedPaths(commit))) {
379
+ continue;
380
+ }
381
+
382
+ // Skip first N commits
383
+ if (skipped < this.skip) {
384
+ skipped++;
385
+ continue;
386
+ }
387
+
388
+ // Check max count
389
+ if (this.maxCount !== undefined && count >= this.maxCount) {
390
+ break;
391
+ }
392
+
393
+ count++;
394
+ yield { ...commit, id: commitId };
395
+ }
396
+ }
397
+
398
+ /**
399
+ * Build set of commits to exclude.
400
+ */
401
+ private async buildExcludedSet(): Promise<Set<ObjectId>> {
402
+ if (this.excludeCommits.length === 0) {
403
+ return new Set();
404
+ }
405
+
406
+ const excluded = new Set<ObjectId>();
407
+ for await (const commitId of this.commits.walkAncestry(this.excludeCommits, {
408
+ firstParentOnly: false,
409
+ })) {
410
+ excluded.add(commitId);
411
+ }
412
+ return excluded;
413
+ }
414
+
415
+ /**
416
+ * Check if commit author matches the filter pattern.
417
+ */
418
+ private matchesAuthor(commit: Commit): boolean {
419
+ if (!this.authorPattern) return true;
420
+ const authorStr = `${commit.author.name} <${commit.author.email}>`.toLowerCase();
421
+ return authorStr.includes(this.authorPattern);
422
+ }
423
+
424
+ /**
425
+ * Check if commit committer matches the filter pattern.
426
+ */
427
+ private matchesCommitter(commit: Commit): boolean {
428
+ if (!this.committerPattern) return true;
429
+ const committerStr = `${commit.committer.name} <${commit.committer.email}>`.toLowerCase();
430
+ return committerStr.includes(this.committerPattern);
431
+ }
432
+
433
+ /**
434
+ * Check if a commit affects any of the filtered paths.
435
+ */
436
+ private async affectsPath(commit: Commit, _commitId: ObjectId): Promise<boolean> {
437
+ // If no parents, this is the initial commit - include if tree has the path
438
+ if (commit.parents.length === 0) {
439
+ return this.treeHasPath(commit.tree);
440
+ }
441
+
442
+ // Compare with first parent's tree
443
+ const parentCommit = await this.commits.load(commit.parents[0]);
444
+ if (!parentCommit) {
445
+ return true; // No parent found, consider it affects paths
446
+ }
447
+ const parentTree = parentCommit.tree;
448
+
449
+ // Check if any path differs between trees
450
+ for (const path of this.paths) {
451
+ const entryInCurrent = await this.getTreeEntryForPath(commit.tree, path);
452
+ const entryInParent = await this.getTreeEntryForPath(parentTree, path);
453
+
454
+ // Different if one exists and other doesn't, or if object IDs differ
455
+ const currentId = entryInCurrent?.objectId;
456
+ const parentId = entryInParent?.objectId;
457
+
458
+ if (currentId !== parentId) {
459
+ return true;
460
+ }
461
+ }
462
+
463
+ return false;
464
+ }
465
+
466
+ /**
467
+ * Check if a commit only affects excluded paths.
468
+ *
469
+ * If the commit only changes files in excludePaths, it should be filtered out.
470
+ */
471
+ private async onlyAffectsExcludedPaths(commit: Commit): Promise<boolean> {
472
+ if (this.excludePaths.length === 0) {
473
+ return false;
474
+ }
475
+
476
+ // For initial commit, check if all paths in tree are excluded
477
+ if (commit.parents.length === 0) {
478
+ // For simplicity, don't exclude initial commits based on path
479
+ return false;
480
+ }
481
+
482
+ // Compare with first parent's tree
483
+ const parentCommit = await this.commits.load(commit.parents[0]);
484
+ if (!parentCommit) {
485
+ return false; // No parent found, don't filter out
486
+ }
487
+
488
+ // Check each excluded path - if ALL changes are to excluded paths, return true
489
+ for (const path of this.excludePaths) {
490
+ const entryInCurrent = await this.getTreeEntryForPath(commit.tree, path);
491
+ const entryInParent = await this.getTreeEntryForPath(parentCommit.tree, path);
492
+
493
+ const currentId = entryInCurrent?.objectId;
494
+ const parentId = entryInParent?.objectId;
495
+
496
+ if (currentId !== parentId) {
497
+ // This excluded path was modified - check if there are non-excluded changes
498
+ // For simplicity, we filter out commits that touch any excluded path
499
+ return true;
500
+ }
501
+ }
502
+
503
+ return false;
504
+ }
505
+
506
+ /**
507
+ * Check if tree contains any of the filtered paths.
508
+ */
509
+ private async treeHasPath(treeId: ObjectId): Promise<boolean> {
510
+ for (const path of this.paths) {
511
+ const entry = await this.getTreeEntryForPath(treeId, path);
512
+ if (entry) {
513
+ return true;
514
+ }
515
+ }
516
+ return false;
517
+ }
518
+
519
+ /**
520
+ * Get tree entry for a path (supports nested paths).
521
+ */
522
+ private async getTreeEntryForPath(
523
+ treeId: ObjectId,
524
+ path: string,
525
+ ): Promise<{ objectId: ObjectId; mode: number } | undefined> {
526
+ const parts = path.split("/").filter((p) => p.length > 0);
527
+ let currentTreeId = treeId;
528
+
529
+ for (let i = 0; i < parts.length; i++) {
530
+ const name = parts[i];
531
+ const isLast = i === parts.length - 1;
532
+
533
+ const entry = await this.trees.getEntry(currentTreeId, name);
534
+
535
+ if (!entry) {
536
+ return undefined;
537
+ }
538
+
539
+ if (isLast) {
540
+ return { objectId: entry.id, mode: entry.mode };
541
+ }
542
+
543
+ // Navigate into subtree
544
+ currentTreeId = entry.id;
545
+ }
546
+
547
+ return undefined;
548
+ }
549
+ }