@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,665 @@
1
+ import type { ObjectId, PersonIdent } from "@statewalker/vcs-core";
2
+
3
+ import {
4
+ CommitNotFoundError,
5
+ InvalidArgumentError,
6
+ NoHeadError,
7
+ NoRebaseInProgressError,
8
+ UpstreamRequiredError,
9
+ } from "../errors/index.js";
10
+ import { GitCommand } from "../git-command.js";
11
+ import { type ContentMergeStrategy, MergeStrategy } from "../results/merge-result.js";
12
+ import type { RebaseResult, RebaseTodoLine } from "../results/rebase-result.js";
13
+ import { RebaseAction, RebaseStatus } from "../results/rebase-result.js";
14
+ import { mergeTreesThreeWay } from "../tree-merge/index.js";
15
+
16
+ /**
17
+ * Get timezone offset string in Git format (+HHMM or -HHMM).
18
+ */
19
+ function getTimezoneOffset(): string {
20
+ const offset = new Date().getTimezoneOffset();
21
+ const sign = offset <= 0 ? "+" : "-";
22
+ const absOffset = Math.abs(offset);
23
+ const hours = Math.floor(absOffset / 60)
24
+ .toString()
25
+ .padStart(2, "0");
26
+ const minutes = (absOffset % 60).toString().padStart(2, "0");
27
+ return `${sign}${hours}${minutes}`;
28
+ }
29
+
30
+ /**
31
+ * The available rebase operations.
32
+ *
33
+ * Based on JGit's RebaseCommand.Operation.
34
+ */
35
+ export enum RebaseOperation {
36
+ /** Initiates rebase */
37
+ BEGIN = "begin",
38
+
39
+ /** Continues after conflict resolution */
40
+ CONTINUE = "continue",
41
+
42
+ /** Skips the current commit */
43
+ SKIP = "skip",
44
+
45
+ /** Aborts and resets the current rebase */
46
+ ABORT = "abort",
47
+
48
+ /** Process steps (internal) */
49
+ PROCESS_STEPS = "process-steps",
50
+ }
51
+
52
+ /**
53
+ * Interactive rebase handler interface.
54
+ *
55
+ * Based on JGit's RebaseCommand.InteractiveHandler.
56
+ */
57
+ export interface InteractiveHandler {
58
+ /**
59
+ * Prepare the list of steps before rebase starts.
60
+ * Can reorder, remove, or change actions.
61
+ */
62
+ prepareSteps(steps: RebaseTodoLine[]): void;
63
+
64
+ /**
65
+ * Modify commit message during reword/squash.
66
+ */
67
+ modifyCommitMessage(message: string): string;
68
+ }
69
+
70
+ /**
71
+ * In-memory rebase state.
72
+ */
73
+ interface RebaseState {
74
+ /** Original HEAD ref name */
75
+ headName: string;
76
+
77
+ /** Original HEAD commit ID */
78
+ originalHead: ObjectId;
79
+
80
+ /** Onto commit (upstream) */
81
+ onto: ObjectId;
82
+
83
+ /** Onto name for messages */
84
+ ontoName: string;
85
+
86
+ /** Commits to cherry-pick in order */
87
+ todoSteps: RebaseTodoLine[];
88
+
89
+ /** Completed steps */
90
+ doneSteps: RebaseTodoLine[];
91
+
92
+ /** Whether rebase is in progress */
93
+ inProgress: boolean;
94
+
95
+ /** Current commit being processed (for stop/conflict) */
96
+ currentCommit?: ObjectId;
97
+ }
98
+
99
+ /**
100
+ * Rebase command for replaying commits on top of another branch.
101
+ *
102
+ * Equivalent to `git rebase`.
103
+ *
104
+ * Based on JGit's RebaseCommand.
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * // Basic rebase onto main
109
+ * const result = await git.rebase()
110
+ * .setUpstream("main")
111
+ * .call();
112
+ *
113
+ * // Abort ongoing rebase
114
+ * const result = await git.rebase()
115
+ * .setOperation(RebaseOperation.ABORT)
116
+ * .call();
117
+ *
118
+ * // Rebase with merge strategy
119
+ * const result = await git.rebase()
120
+ * .setUpstream("main")
121
+ * .setStrategy(MergeStrategy.OURS)
122
+ * .call();
123
+ * ```
124
+ */
125
+ export class RebaseCommand extends GitCommand<RebaseResult> {
126
+ private operation = RebaseOperation.BEGIN;
127
+ private upstreamCommit?: ObjectId;
128
+ private upstreamCommitName?: string;
129
+ private strategy = MergeStrategy.RECURSIVE;
130
+ private contentStrategy?: ContentMergeStrategy;
131
+ private preserveMerges = false;
132
+ private interactiveHandler?: InteractiveHandler;
133
+
134
+ // In-memory state for tracking rebase progress
135
+ private static rebaseStates = new WeakMap<object, RebaseState>();
136
+
137
+ /**
138
+ * Set the upstream branch or commit to rebase onto.
139
+ *
140
+ * @param upstream The upstream commit ID
141
+ */
142
+ setUpstream(upstream: ObjectId): this {
143
+ this.checkCallable();
144
+ this.upstreamCommit = upstream;
145
+ this.upstreamCommitName = upstream;
146
+ return this;
147
+ }
148
+
149
+ /**
150
+ * Set the upstream branch name to rebase onto.
151
+ *
152
+ * @param upstream The upstream branch name
153
+ */
154
+ async setUpstreamBranch(upstream: string): Promise<this> {
155
+ this.checkCallable();
156
+ this.upstreamCommit = await this.resolveRef(upstream);
157
+ this.upstreamCommitName = upstream;
158
+ return this;
159
+ }
160
+
161
+ /**
162
+ * Override the upstream name for conflict messages.
163
+ *
164
+ * @param name The name to use for upstream in messages
165
+ */
166
+ setUpstreamName(name: string): this {
167
+ this.checkCallable();
168
+ if (!this.upstreamCommit) {
169
+ throw new InvalidArgumentError(
170
+ "upstreamName",
171
+ name,
172
+ "setUpstreamName must be called after setUpstream",
173
+ );
174
+ }
175
+ this.upstreamCommitName = name;
176
+ return this;
177
+ }
178
+
179
+ /**
180
+ * Set the operation to perform.
181
+ *
182
+ * @param operation BEGIN, CONTINUE, SKIP, or ABORT
183
+ */
184
+ setOperation(operation: RebaseOperation): this {
185
+ this.checkCallable();
186
+ this.operation = operation;
187
+ return this;
188
+ }
189
+
190
+ /**
191
+ * Get the current operation.
192
+ */
193
+ getOperation(): RebaseOperation {
194
+ return this.operation;
195
+ }
196
+
197
+ /**
198
+ * Set the merge strategy to use.
199
+ *
200
+ * @param strategy The merge strategy
201
+ */
202
+ setStrategy(strategy: MergeStrategy): this {
203
+ this.checkCallable();
204
+ this.strategy = strategy;
205
+ return this;
206
+ }
207
+
208
+ /**
209
+ * Get the merge strategy.
210
+ */
211
+ getStrategy(): MergeStrategy {
212
+ return this.strategy;
213
+ }
214
+
215
+ /**
216
+ * Set the content merge strategy for file-level conflicts.
217
+ *
218
+ * @param strategy The content merge strategy
219
+ */
220
+ setContentMergeStrategy(strategy: ContentMergeStrategy): this {
221
+ this.checkCallable();
222
+ this.contentStrategy = strategy;
223
+ return this;
224
+ }
225
+
226
+ /**
227
+ * Get the content merge strategy.
228
+ */
229
+ getContentMergeStrategy(): ContentMergeStrategy | undefined {
230
+ return this.contentStrategy;
231
+ }
232
+
233
+ /**
234
+ * Set whether to preserve merges during rebase.
235
+ *
236
+ * @param preserve true to re-create merges
237
+ */
238
+ setPreserveMerges(preserve: boolean): this {
239
+ this.checkCallable();
240
+ this.preserveMerges = preserve;
241
+ return this;
242
+ }
243
+
244
+ /**
245
+ * Get whether merges are preserved.
246
+ */
247
+ getPreserveMerges(): boolean {
248
+ return this.preserveMerges;
249
+ }
250
+
251
+ /**
252
+ * Enable interactive rebase with the given handler.
253
+ *
254
+ * @param handler The interactive handler
255
+ */
256
+ runInteractively(handler: InteractiveHandler): this {
257
+ this.checkCallable();
258
+ this.interactiveHandler = handler;
259
+ return this;
260
+ }
261
+
262
+ /**
263
+ * Execute the rebase command.
264
+ */
265
+ async call(): Promise<RebaseResult> {
266
+ this.checkCallable();
267
+ this.setCallable(false);
268
+
269
+ switch (this.operation) {
270
+ case RebaseOperation.ABORT:
271
+ return this.abort();
272
+
273
+ case RebaseOperation.CONTINUE:
274
+ return this.continueRebase();
275
+
276
+ case RebaseOperation.SKIP:
277
+ return this.skip();
278
+
279
+ case RebaseOperation.BEGIN:
280
+ case RebaseOperation.PROCESS_STEPS:
281
+ return this.begin();
282
+ }
283
+ }
284
+
285
+ /**
286
+ * Begin the rebase operation.
287
+ */
288
+ private async begin(): Promise<RebaseResult> {
289
+ if (!this.upstreamCommit) {
290
+ throw new UpstreamRequiredError("Upstream commit is required for rebase BEGIN operation");
291
+ }
292
+
293
+ // Get current HEAD
294
+ const headResolved = await this.refsStore.resolve("HEAD");
295
+ if (!headResolved?.objectId) {
296
+ throw new NoHeadError("HEAD cannot be resolved");
297
+ }
298
+
299
+ const headCommit = headResolved.objectId;
300
+
301
+ // Get HEAD's target branch name (if symbolic)
302
+ const headRef = await this.refsStore.get("HEAD");
303
+ const headName = headRef && "target" in headRef ? headRef.target : headCommit;
304
+
305
+ // Check if already up to date
306
+ const isAncestor = await this.isAncestor(this.upstreamCommit, headCommit);
307
+ if (isAncestor) {
308
+ return {
309
+ status: RebaseStatus.UP_TO_DATE,
310
+ newHead: headCommit,
311
+ };
312
+ }
313
+
314
+ // Check if can fast-forward
315
+ const canFastForward = await this.isAncestor(headCommit, this.upstreamCommit);
316
+ if (canFastForward) {
317
+ // Fast-forward HEAD to upstream
318
+ await this.updateHead(headName, this.upstreamCommit);
319
+ return {
320
+ status: RebaseStatus.FAST_FORWARD,
321
+ newHead: this.upstreamCommit,
322
+ };
323
+ }
324
+
325
+ // Calculate commits to cherry-pick
326
+ const commitsToRebase = await this.calculateCommitsToRebase(headCommit, this.upstreamCommit);
327
+
328
+ if (commitsToRebase.length === 0) {
329
+ return {
330
+ status: RebaseStatus.UP_TO_DATE,
331
+ newHead: headCommit,
332
+ };
333
+ }
334
+
335
+ // Build todo list
336
+ const todoSteps: RebaseTodoLine[] = [];
337
+ for (const commitId of commitsToRebase) {
338
+ const commit = await this.commits.load(commitId);
339
+ if (!commit) continue;
340
+ const shortMessage = commit.message.split("\n")[0].substring(0, 50);
341
+ todoSteps.push({
342
+ action: RebaseAction.PICK,
343
+ commit: commitId.substring(0, 7),
344
+ shortMessage,
345
+ });
346
+ }
347
+
348
+ // Let interactive handler modify steps
349
+ if (this.interactiveHandler) {
350
+ this.interactiveHandler.prepareSteps(todoSteps);
351
+ }
352
+
353
+ // Save rebase state
354
+ const state: RebaseState = {
355
+ headName,
356
+ originalHead: headCommit,
357
+ onto: this.upstreamCommit,
358
+ ontoName: this.upstreamCommitName ?? this.upstreamCommit,
359
+ todoSteps,
360
+ doneSteps: [],
361
+ inProgress: true,
362
+ };
363
+ RebaseCommand.rebaseStates.set(this._workingCopy, state);
364
+
365
+ // Reset HEAD to onto
366
+ await this.updateHead(headName, this.upstreamCommit);
367
+
368
+ // Process steps
369
+ return this.processSteps(state, commitsToRebase);
370
+ }
371
+
372
+ /**
373
+ * Process the rebase steps (cherry-pick each commit).
374
+ */
375
+ private async processSteps(
376
+ state: RebaseState,
377
+ commitsToRebase: ObjectId[],
378
+ ): Promise<RebaseResult> {
379
+ let newHead = state.onto;
380
+
381
+ for (let i = 0; i < state.todoSteps.length; i++) {
382
+ const step = state.todoSteps[i];
383
+
384
+ if (step.action === RebaseAction.COMMENT) {
385
+ continue;
386
+ }
387
+
388
+ // Find the full commit ID
389
+ const commitId = commitsToRebase.find((c) => c.startsWith(step.commit));
390
+ if (!commitId) {
391
+ throw new CommitNotFoundError(step.commit);
392
+ }
393
+
394
+ // Cherry-pick the commit
395
+ const result = await this.cherryPickCommit(commitId, newHead, state);
396
+
397
+ if (result.status === RebaseStatus.STOPPED) {
398
+ state.currentCommit = commitId;
399
+ state.doneSteps = state.todoSteps.slice(0, i);
400
+ state.todoSteps = state.todoSteps.slice(i);
401
+ return result;
402
+ }
403
+
404
+ if (result.newHead) {
405
+ newHead = result.newHead;
406
+ }
407
+
408
+ // Handle EDIT action - stop after successful pick
409
+ if (step.action === RebaseAction.EDIT) {
410
+ state.currentCommit = commitId;
411
+ state.doneSteps = state.todoSteps.slice(0, i + 1);
412
+ state.todoSteps = state.todoSteps.slice(i + 1);
413
+ return {
414
+ status: RebaseStatus.EDIT,
415
+ newHead,
416
+ currentCommit: commitId,
417
+ };
418
+ }
419
+
420
+ state.doneSteps.push(step);
421
+ }
422
+
423
+ // Rebase complete - clean up state
424
+ state.inProgress = false;
425
+ RebaseCommand.rebaseStates.delete(this._workingCopy);
426
+
427
+ // Update branch ref to point to new head
428
+ await this.updateHead(state.headName, newHead);
429
+
430
+ return {
431
+ status: RebaseStatus.OK,
432
+ newHead,
433
+ };
434
+ }
435
+
436
+ /**
437
+ * Cherry-pick a single commit.
438
+ */
439
+ private async cherryPickCommit(
440
+ commitId: ObjectId,
441
+ ontoCommit: ObjectId,
442
+ _state: RebaseState,
443
+ ): Promise<RebaseResult> {
444
+ const commit = await this.commits.load(commitId);
445
+ if (!commit) {
446
+ throw new CommitNotFoundError(commitId);
447
+ }
448
+
449
+ // Skip merge commits unless preserveMerges is set
450
+ if (commit.parents.length > 1 && !this.preserveMerges) {
451
+ // Return success without creating a new commit
452
+ return {
453
+ status: RebaseStatus.OK,
454
+ newHead: ontoCommit,
455
+ };
456
+ }
457
+
458
+ // Get parent commit for diff base
459
+ const parentId = commit.parents[0];
460
+ if (!parentId) {
461
+ // Root commit - just use its tree
462
+ const newCommitId = await this.createRebasedCommit(commit, ontoCommit, commit.tree);
463
+ return {
464
+ status: RebaseStatus.OK,
465
+ newHead: newCommitId,
466
+ };
467
+ }
468
+
469
+ // Three-way merge: parent -> commit changes applied to onto
470
+ const parentCommit = await this.commits.load(parentId);
471
+ const ontoCommitObj = await this.commits.load(ontoCommit);
472
+ if (!parentCommit || !ontoCommitObj) {
473
+ throw new CommitNotFoundError(parentId);
474
+ }
475
+
476
+ // Replay this commit's changes onto `onto`: base is the commit's own
477
+ // parent, so base->commit is exactly the change being replayed. The merge
478
+ // is path-level; a file both sides edited conflicts even if the edits are
479
+ // to different regions.
480
+ const mergeResult = await mergeTreesThreeWay(
481
+ this.trees,
482
+ parentCommit.tree,
483
+ commit.tree,
484
+ ontoCommitObj.tree,
485
+ );
486
+
487
+ if (mergeResult.conflicts && mergeResult.conflicts.length > 0) {
488
+ return {
489
+ status: RebaseStatus.STOPPED,
490
+ currentCommit: commitId,
491
+ conflicts: mergeResult.conflicts,
492
+ };
493
+ }
494
+
495
+ // Create new commit with merged tree
496
+ const newCommitId = await this.createRebasedCommit(commit, ontoCommit, mergeResult.tree);
497
+
498
+ return {
499
+ status: RebaseStatus.OK,
500
+ newHead: newCommitId,
501
+ };
502
+ }
503
+
504
+ /**
505
+ * Create a new commit preserving the original author info.
506
+ */
507
+ private async createRebasedCommit(
508
+ original: {
509
+ author: PersonIdent;
510
+ message: string;
511
+ },
512
+ parentId: ObjectId,
513
+ treeId: ObjectId,
514
+ ): Promise<ObjectId> {
515
+ const committer = await this.getCommitter();
516
+
517
+ return this.commits.store({
518
+ tree: treeId,
519
+ parents: [parentId],
520
+ author: original.author,
521
+ committer,
522
+ message: original.message,
523
+ });
524
+ }
525
+
526
+ /**
527
+ * Get default committer identity.
528
+ */
529
+ private async getCommitter(): Promise<PersonIdent> {
530
+ return {
531
+ name: "Unknown",
532
+ email: "unknown@example.com",
533
+ timestamp: Math.floor(Date.now() / 1000),
534
+ tzOffset: getTimezoneOffset(),
535
+ };
536
+ }
537
+
538
+ /**
539
+ * Abort the rebase and restore original HEAD.
540
+ */
541
+ private async abort(): Promise<RebaseResult> {
542
+ const state = RebaseCommand.rebaseStates.get(this._workingCopy);
543
+ if (!state) {
544
+ return {
545
+ status: RebaseStatus.ABORTED,
546
+ };
547
+ }
548
+
549
+ // Restore HEAD to original
550
+ await this.updateHead(state.headName, state.originalHead);
551
+
552
+ // Clean up state
553
+ RebaseCommand.rebaseStates.delete(this._workingCopy);
554
+
555
+ return {
556
+ status: RebaseStatus.ABORTED,
557
+ newHead: state.originalHead,
558
+ };
559
+ }
560
+
561
+ /**
562
+ * Continue rebase after conflict resolution.
563
+ */
564
+ private async continueRebase(): Promise<RebaseResult> {
565
+ const state = RebaseCommand.rebaseStates.get(this._workingCopy);
566
+ if (!state || !state.inProgress) {
567
+ throw new NoRebaseInProgressError();
568
+ }
569
+
570
+ // Get current HEAD
571
+ const headRef = await this.refsStore.resolve("HEAD");
572
+ if (!headRef?.objectId) {
573
+ throw new NoHeadError("HEAD cannot be resolved");
574
+ }
575
+
576
+ // Calculate remaining commits to process
577
+ const remaining: ObjectId[] = [];
578
+ for (const step of state.todoSteps) {
579
+ if (step.action === RebaseAction.COMMENT) continue;
580
+ // Find full commit ID from original commits
581
+ const commitId = state.originalHead; // Simplified - would need proper tracking
582
+ remaining.push(commitId);
583
+ }
584
+
585
+ return this.processSteps(state, remaining);
586
+ }
587
+
588
+ /**
589
+ * Skip current commit and continue.
590
+ */
591
+ private async skip(): Promise<RebaseResult> {
592
+ const state = RebaseCommand.rebaseStates.get(this._workingCopy);
593
+ if (!state || !state.inProgress) {
594
+ throw new NoRebaseInProgressError();
595
+ }
596
+
597
+ // Skip first step
598
+ if (state.todoSteps.length > 0) {
599
+ const skipped = state.todoSteps.shift();
600
+ if (skipped) {
601
+ state.doneSteps.push(skipped);
602
+ }
603
+ }
604
+
605
+ state.currentCommit = undefined;
606
+
607
+ // Calculate remaining commits
608
+ const remaining: ObjectId[] = [];
609
+ return this.processSteps(state, remaining);
610
+ }
611
+
612
+ /**
613
+ * Calculate which commits need to be rebased.
614
+ */
615
+ private async calculateCommitsToRebase(head: ObjectId, upstream: ObjectId): Promise<ObjectId[]> {
616
+ const commits: ObjectId[] = [];
617
+ const seen = new Set<ObjectId>();
618
+
619
+ // Get all commits reachable from upstream
620
+ const upstreamCommits = new Set<ObjectId>();
621
+ for await (const commitId of this.commits.walkAncestry([upstream])) {
622
+ upstreamCommits.add(commitId);
623
+ }
624
+
625
+ // Walk from head and collect commits not in upstream
626
+ for await (const commitId of this.commits.walkAncestry([head])) {
627
+ if (upstreamCommits.has(commitId)) {
628
+ break;
629
+ }
630
+ if (!seen.has(commitId)) {
631
+ seen.add(commitId);
632
+ commits.push(commitId);
633
+ }
634
+ }
635
+
636
+ // Reverse to get oldest first
637
+ return commits.reverse();
638
+ }
639
+
640
+ /**
641
+ * Check if commit A is an ancestor of commit B.
642
+ */
643
+ private async isAncestor(a: ObjectId, b: ObjectId): Promise<boolean> {
644
+ if (a === b) return true;
645
+
646
+ for await (const commitId of this.commits.walkAncestry([b])) {
647
+ if (commitId === a) {
648
+ return true;
649
+ }
650
+ }
651
+ return false;
652
+ }
653
+
654
+ /**
655
+ * Update HEAD and optionally the branch it points to.
656
+ */
657
+ private async updateHead(headName: string, commitId: ObjectId): Promise<void> {
658
+ if (headName.startsWith("refs/")) {
659
+ // Update the branch ref
660
+ await this.refsStore.set(headName, commitId);
661
+ }
662
+ // HEAD is symbolic, so it should follow
663
+ await this.refsStore.set("HEAD", commitId);
664
+ }
665
+ }