@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.
- package/dist/commands/commit-command.d.ts.map +1 -1
- package/dist/commands/init-command.d.ts.map +1 -1
- package/dist/commands/push-command.d.ts.map +1 -1
- package/dist/core-commands/checkout.command.impl.d.ts +1 -1
- package/dist/core-commands/checkout.command.impl.d.ts.map +1 -1
- package/dist/git-command.d.ts.map +1 -1
- package/package.json +19 -14
- package/src/commands/add-command.ts +494 -0
- package/src/commands/blame-command.ts +1241 -0
- package/src/commands/branch-command.ts +487 -0
- package/src/commands/checkout-command.ts +681 -0
- package/src/commands/cherry-pick-command.ts +544 -0
- package/src/commands/clean-command.ts +218 -0
- package/src/commands/clone-command.ts +532 -0
- package/src/commands/commit-command.ts +529 -0
- package/src/commands/describe-command.ts +454 -0
- package/src/commands/diff-command.ts +257 -0
- package/src/commands/fetch-command.ts +615 -0
- package/src/commands/gc-command.ts +155 -0
- package/src/commands/index.ts +31 -0
- package/src/commands/init-command.ts +365 -0
- package/src/commands/log-command.ts +549 -0
- package/src/commands/ls-remote-command.ts +237 -0
- package/src/commands/merge-command.ts +750 -0
- package/src/commands/pack-refs-command.ts +101 -0
- package/src/commands/pull-command.ts +258 -0
- package/src/commands/push-command.ts +682 -0
- package/src/commands/rebase-command.ts +665 -0
- package/src/commands/reflog-command.ts +68 -0
- package/src/commands/remote-command.ts +385 -0
- package/src/commands/reset-command.ts +292 -0
- package/src/commands/revert-command.ts +552 -0
- package/src/commands/rm-command.ts +178 -0
- package/src/commands/stash-apply-command.ts +402 -0
- package/src/commands/stash-create-command.ts +339 -0
- package/src/commands/stash-drop-command.ts +115 -0
- package/src/commands/stash-list-command.ts +116 -0
- package/src/commands/status-command.ts +173 -0
- package/src/commands/tag-command.ts +378 -0
- package/src/commands/tree-restore.ts +126 -0
- package/src/core-commands/add.command.impl.ts +274 -0
- package/src/core-commands/add.command.ts +81 -0
- package/src/core-commands/checkout.command.impl.ts +586 -0
- package/src/core-commands/checkout.command.ts +125 -0
- package/src/core-commands/index.ts +13 -0
- package/src/errors/add-errors.ts +20 -0
- package/src/errors/checkout-errors.ts +42 -0
- package/src/errors/command-errors.ts +68 -0
- package/src/errors/commit-errors.ts +40 -0
- package/src/errors/git-api-error.ts +11 -0
- package/src/errors/index.ts +12 -0
- package/src/errors/merge-errors.ts +94 -0
- package/src/errors/rebase-errors.ts +34 -0
- package/src/errors/ref-errors.ts +100 -0
- package/src/errors/remote-errors.ts +27 -0
- package/src/errors/stash-errors.ts +42 -0
- package/src/errors/tag-errors.ts +16 -0
- package/src/errors/transport-errors.ts +68 -0
- package/src/git-command.ts +421 -0
- package/src/git.ts +910 -0
- package/src/index.ts +18 -0
- package/src/pack-import/index.ts +8 -0
- package/src/pack-import/pack-import.ts +24 -0
- package/src/remote-config/index.ts +12 -0
- package/src/remote-config/remote-config-store.ts +285 -0
- package/src/rename/index.ts +5 -0
- package/src/rename/similarity-index.ts +185 -0
- package/src/results/cherry-pick-result.ts +35 -0
- package/src/results/clone-result.ts +25 -0
- package/src/results/diff-entry.ts +162 -0
- package/src/results/diff-formatter.ts +416 -0
- package/src/results/fetch-result.ts +91 -0
- package/src/results/index.ts +13 -0
- package/src/results/init-result.ts +29 -0
- package/src/results/merge-result.ts +135 -0
- package/src/results/pull-result.ts +27 -0
- package/src/results/push-result.ts +86 -0
- package/src/results/rebase-result.ts +140 -0
- package/src/results/revert-result.ts +35 -0
- package/src/results/stash-result.ts +60 -0
- package/src/results/status-result.ts +65 -0
- package/src/transport-command.ts +125 -0
- package/src/tree-merge/index.ts +8 -0
- package/src/tree-merge/tree-merge.ts +249 -0
- package/src/types.ts +70 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
import type { Commit, ObjectId, PersonIdent } from "@statewalker/vcs-core";
|
|
2
|
+
import { FileMode } from "@statewalker/vcs-core";
|
|
3
|
+
import { MergeStage } from "@statewalker/vcs-working-tree";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
InvalidMainlineParentError,
|
|
7
|
+
MultipleParentsNotAllowedError,
|
|
8
|
+
} from "../errors/merge-errors.js";
|
|
9
|
+
import { NoHeadError, RefNotFoundError } from "../errors/ref-errors.js";
|
|
10
|
+
import { GitCommand } from "../git-command.js";
|
|
11
|
+
import { type ContentMergeStrategy, MergeStrategy } from "../results/merge-result.js";
|
|
12
|
+
import { type RevertResult, RevertStatus } from "../results/revert-result.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Entry with full path for internal use during merge.
|
|
16
|
+
*/
|
|
17
|
+
interface PathEntry {
|
|
18
|
+
path: string;
|
|
19
|
+
mode: number;
|
|
20
|
+
id: ObjectId;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Get current timezone offset as string (+HHMM or -HHMM).
|
|
25
|
+
*/
|
|
26
|
+
function getTimezoneOffset(): string {
|
|
27
|
+
const offset = new Date().getTimezoneOffset();
|
|
28
|
+
const sign = offset <= 0 ? "+" : "-";
|
|
29
|
+
const absOffset = Math.abs(offset);
|
|
30
|
+
const hours = Math.floor(absOffset / 60)
|
|
31
|
+
.toString()
|
|
32
|
+
.padStart(2, "0");
|
|
33
|
+
const minutes = (absOffset % 60).toString().padStart(2, "0");
|
|
34
|
+
return `${sign}${hours}${minutes}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Create a default committer identity.
|
|
39
|
+
*/
|
|
40
|
+
function defaultCommitter(): PersonIdent {
|
|
41
|
+
return {
|
|
42
|
+
name: "Unknown",
|
|
43
|
+
email: "unknown@example.com",
|
|
44
|
+
timestamp: Math.floor(Date.now() / 1000),
|
|
45
|
+
tzOffset: getTimezoneOffset(),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Revert commits on current branch.
|
|
51
|
+
*
|
|
52
|
+
* Equivalent to `git revert`.
|
|
53
|
+
*
|
|
54
|
+
* Based on JGit's RevertCommand.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // Revert a single commit
|
|
59
|
+
* const result = await git.revert()
|
|
60
|
+
* .include(commitId)
|
|
61
|
+
* .call();
|
|
62
|
+
*
|
|
63
|
+
* // Revert without committing
|
|
64
|
+
* const result = await git.revert()
|
|
65
|
+
* .include(commitId)
|
|
66
|
+
* .setNoCommit(true)
|
|
67
|
+
* .call();
|
|
68
|
+
*
|
|
69
|
+
* // Revert a merge commit
|
|
70
|
+
* const result = await git.revert()
|
|
71
|
+
* .include(mergeCommitId)
|
|
72
|
+
* .setMainlineParentNumber(1)
|
|
73
|
+
* .call();
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export class RevertCommand extends GitCommand<RevertResult> {
|
|
77
|
+
private includes: string[] = [];
|
|
78
|
+
private noCommit = false;
|
|
79
|
+
private mainlineParent?: number;
|
|
80
|
+
private strategy: MergeStrategy = MergeStrategy.RECURSIVE;
|
|
81
|
+
private contentStrategy?: ContentMergeStrategy;
|
|
82
|
+
private ourCommitName?: string;
|
|
83
|
+
private reflogPrefix = "revert:";
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Add a commit to revert.
|
|
87
|
+
*
|
|
88
|
+
* @param refOrId Commit ID or reference to revert
|
|
89
|
+
*/
|
|
90
|
+
include(refOrId: string): this {
|
|
91
|
+
this.checkCallable();
|
|
92
|
+
this.includes.push(refOrId);
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Set whether to commit after revert.
|
|
98
|
+
*
|
|
99
|
+
* When true, changes are staged but not committed.
|
|
100
|
+
*
|
|
101
|
+
* @param noCommit Whether to skip committing (default: false)
|
|
102
|
+
*/
|
|
103
|
+
setNoCommit(noCommit: boolean): this {
|
|
104
|
+
this.checkCallable();
|
|
105
|
+
this.noCommit = noCommit;
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Set mainline parent number for reverting merge commits.
|
|
111
|
+
*
|
|
112
|
+
* For regular commits, this is not needed. For merge commits,
|
|
113
|
+
* this specifies which parent to diff against (1-indexed).
|
|
114
|
+
*
|
|
115
|
+
* @param parent Parent number (1 = first parent, 2 = second parent)
|
|
116
|
+
*/
|
|
117
|
+
setMainlineParentNumber(parent: number): this {
|
|
118
|
+
this.checkCallable();
|
|
119
|
+
this.mainlineParent = parent;
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Set the merge strategy.
|
|
125
|
+
*
|
|
126
|
+
* Default is "recursive".
|
|
127
|
+
*
|
|
128
|
+
* @param strategy Merge strategy to use
|
|
129
|
+
*/
|
|
130
|
+
setStrategy(strategy: MergeStrategy): this {
|
|
131
|
+
this.checkCallable();
|
|
132
|
+
this.strategy = strategy;
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Get the merge strategy.
|
|
138
|
+
*/
|
|
139
|
+
getStrategy(): MergeStrategy {
|
|
140
|
+
return this.strategy;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Set the content merge strategy for handling conflicts.
|
|
145
|
+
*
|
|
146
|
+
* @param contentStrategy Content merge strategy
|
|
147
|
+
*/
|
|
148
|
+
setContentMergeStrategy(contentStrategy: ContentMergeStrategy): this {
|
|
149
|
+
this.checkCallable();
|
|
150
|
+
this.contentStrategy = contentStrategy;
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Get the content merge strategy.
|
|
156
|
+
*/
|
|
157
|
+
getContentMergeStrategy(): ContentMergeStrategy | undefined {
|
|
158
|
+
return this.contentStrategy;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Set the name to be used for "ours" in conflict markers.
|
|
163
|
+
*
|
|
164
|
+
* @param name Name for ours side (default: HEAD)
|
|
165
|
+
*/
|
|
166
|
+
setOurCommitName(name: string): this {
|
|
167
|
+
this.checkCallable();
|
|
168
|
+
this.ourCommitName = name;
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Get the name used for "ours" in conflict markers.
|
|
174
|
+
*/
|
|
175
|
+
getOurCommitName(): string | undefined {
|
|
176
|
+
return this.ourCommitName;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Set the reflog prefix.
|
|
181
|
+
*
|
|
182
|
+
* @param prefix Reflog message prefix (default: "revert:")
|
|
183
|
+
*/
|
|
184
|
+
setReflogPrefix(prefix: string): this {
|
|
185
|
+
this.checkCallable();
|
|
186
|
+
this.reflogPrefix = prefix;
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Get the reflog prefix.
|
|
192
|
+
*/
|
|
193
|
+
getReflogPrefix(): string {
|
|
194
|
+
return this.reflogPrefix;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async call(): Promise<RevertResult> {
|
|
198
|
+
this.checkCallable();
|
|
199
|
+
this.setCallable(false);
|
|
200
|
+
|
|
201
|
+
// Get current HEAD
|
|
202
|
+
const headRef = await this.refsStore.resolve("HEAD");
|
|
203
|
+
if (!headRef?.objectId) {
|
|
204
|
+
throw new NoHeadError("Repository has no HEAD");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let headId = headRef.objectId;
|
|
208
|
+
const revertedRefs: ObjectId[] = [];
|
|
209
|
+
|
|
210
|
+
// Revert each commit in order
|
|
211
|
+
for (const refOrId of this.includes) {
|
|
212
|
+
const result = await this.revertOne(refOrId, headId);
|
|
213
|
+
|
|
214
|
+
if (result.status !== RevertStatus.OK) {
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
headId = result.newHead as ObjectId;
|
|
219
|
+
revertedRefs.push(refOrId);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return {
|
|
223
|
+
status: RevertStatus.OK,
|
|
224
|
+
newHead: headId,
|
|
225
|
+
revertedRefs,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Revert a single commit.
|
|
231
|
+
*/
|
|
232
|
+
private async revertOne(refOrId: string, headId: ObjectId): Promise<RevertResult> {
|
|
233
|
+
// Resolve the commit to revert
|
|
234
|
+
const resolved = await this.refsStore.resolve(refOrId);
|
|
235
|
+
const commitId = resolved?.objectId ?? refOrId;
|
|
236
|
+
|
|
237
|
+
// Load the commit
|
|
238
|
+
const commit = await this.commits.load(commitId);
|
|
239
|
+
if (!commit) {
|
|
240
|
+
throw new RefNotFoundError(`Cannot find commit: ${refOrId}`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Determine the parent to diff against
|
|
244
|
+
let parentId: ObjectId;
|
|
245
|
+
if (commit.parents.length === 0) {
|
|
246
|
+
// Root commit - diff against empty tree
|
|
247
|
+
parentId = this.trees.getEmptyTreeId();
|
|
248
|
+
} else if (commit.parents.length === 1) {
|
|
249
|
+
parentId = commit.parents[0];
|
|
250
|
+
} else {
|
|
251
|
+
// Merge commit - need mainlineParent
|
|
252
|
+
if (this.mainlineParent === undefined) {
|
|
253
|
+
throw new MultipleParentsNotAllowedError(
|
|
254
|
+
"Cannot revert merge commit without specifying mainline parent",
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
if (this.mainlineParent < 1 || this.mainlineParent > commit.parents.length) {
|
|
258
|
+
throw new InvalidMainlineParentError(this.mainlineParent, commit.parents.length);
|
|
259
|
+
}
|
|
260
|
+
parentId = commit.parents[this.mainlineParent - 1];
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Collect tree entries into Maps by path
|
|
264
|
+
// For revert: base=commit, ours=head, theirs=parent
|
|
265
|
+
// This is the OPPOSITE of cherry-pick
|
|
266
|
+
const headCommit = await this.commits.load(headId);
|
|
267
|
+
if (!headCommit) {
|
|
268
|
+
throw new RefNotFoundError(`Cannot find head commit: ${headId}`);
|
|
269
|
+
}
|
|
270
|
+
const headTree = new Map<string, PathEntry>();
|
|
271
|
+
const commitTree = new Map<string, PathEntry>();
|
|
272
|
+
const parentTree = new Map<string, PathEntry>();
|
|
273
|
+
const allPaths = new Set<string>();
|
|
274
|
+
|
|
275
|
+
await this.collectTreeEntries(headCommit.tree, "", headTree, allPaths);
|
|
276
|
+
await this.collectTreeEntries(commit.tree, "", commitTree, allPaths);
|
|
277
|
+
if (parentId !== this.trees.getEmptyTreeId()) {
|
|
278
|
+
// parentId is a commit ID, so we need to load the commit to get its tree
|
|
279
|
+
const parentCommit = await this.commits.load(parentId);
|
|
280
|
+
if (!parentCommit) {
|
|
281
|
+
throw new RefNotFoundError(`Cannot find parent commit: ${parentId}`);
|
|
282
|
+
}
|
|
283
|
+
await this.collectTreeEntries(parentCommit.tree, "", parentTree, allPaths);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Perform three-way merge: commit -> parent applied to head
|
|
287
|
+
// This is the REVERSE of cherry-pick: we apply the inverse changes
|
|
288
|
+
const { mergedEntries, conflicts } = this.threeWayMerge(
|
|
289
|
+
commitTree, // base = the commit being reverted
|
|
290
|
+
parentTree, // theirs = the parent (what we want to go back to)
|
|
291
|
+
headTree, // ours = current HEAD
|
|
292
|
+
allPaths,
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
if (conflicts.length > 0) {
|
|
296
|
+
// Write conflict markers to staging
|
|
297
|
+
await this.writeConflictStaging(commitTree, parentTree, headTree, conflicts);
|
|
298
|
+
|
|
299
|
+
return {
|
|
300
|
+
status: RevertStatus.CONFLICTING,
|
|
301
|
+
revertedRefs: [],
|
|
302
|
+
conflicts,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Build merged tree
|
|
307
|
+
const builder = this.staging.createBuilder();
|
|
308
|
+
for (const entry of mergedEntries.values()) {
|
|
309
|
+
builder.add({
|
|
310
|
+
path: entry.path,
|
|
311
|
+
mode: entry.mode,
|
|
312
|
+
objectId: entry.id,
|
|
313
|
+
stage: MergeStage.MERGED,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
await builder.finish();
|
|
317
|
+
|
|
318
|
+
// Write tree
|
|
319
|
+
const newTreeId = await this.staging.writeTree(this.trees);
|
|
320
|
+
|
|
321
|
+
// Create commit if not noCommit mode
|
|
322
|
+
if (!this.noCommit) {
|
|
323
|
+
// Generate revert commit message
|
|
324
|
+
const originalMessage = commit.message.split("\n")[0];
|
|
325
|
+
const revertMessage = `Revert "${originalMessage}"\n\nThis reverts commit ${commitId}.`;
|
|
326
|
+
|
|
327
|
+
const newCommit: Commit = {
|
|
328
|
+
tree: newTreeId,
|
|
329
|
+
parents: [headId],
|
|
330
|
+
author: defaultCommitter(),
|
|
331
|
+
committer: defaultCommitter(),
|
|
332
|
+
message: revertMessage,
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
const newCommitId = await this.commits.store(newCommit);
|
|
336
|
+
|
|
337
|
+
// Update HEAD
|
|
338
|
+
const head = await this.refsStore.get("HEAD");
|
|
339
|
+
if (head && "target" in head) {
|
|
340
|
+
await this.refsStore.set(head.target, newCommitId);
|
|
341
|
+
} else {
|
|
342
|
+
await this.refsStore.set("HEAD", newCommitId);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return {
|
|
346
|
+
status: RevertStatus.OK,
|
|
347
|
+
newHead: newCommitId,
|
|
348
|
+
revertedRefs: [commitId],
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// noCommit mode - just return current HEAD
|
|
353
|
+
return {
|
|
354
|
+
status: RevertStatus.OK,
|
|
355
|
+
newHead: headId,
|
|
356
|
+
revertedRefs: [commitId],
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Collect all blob entries from a tree recursively.
|
|
362
|
+
*/
|
|
363
|
+
private async collectTreeEntries(
|
|
364
|
+
treeId: ObjectId,
|
|
365
|
+
prefix: string,
|
|
366
|
+
entries: Map<string, PathEntry>,
|
|
367
|
+
allPaths: Set<string>,
|
|
368
|
+
): Promise<void> {
|
|
369
|
+
for await (const entry of this.trees.loadTree(treeId)) {
|
|
370
|
+
const path = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
371
|
+
|
|
372
|
+
if (entry.mode === FileMode.TREE) {
|
|
373
|
+
// Recurse into subtree
|
|
374
|
+
await this.collectTreeEntries(entry.id, path, entries, allPaths);
|
|
375
|
+
} else {
|
|
376
|
+
// Blob entry
|
|
377
|
+
entries.set(path, { path, mode: entry.mode, id: entry.id });
|
|
378
|
+
allPaths.add(path);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Perform three-way merge for revert.
|
|
385
|
+
*
|
|
386
|
+
* The revert merge is: apply changes from commit->parent onto head.
|
|
387
|
+
* This is the INVERSE of cherry-pick.
|
|
388
|
+
*/
|
|
389
|
+
private threeWayMerge(
|
|
390
|
+
baseTree: Map<string, PathEntry>,
|
|
391
|
+
theirsTree: Map<string, PathEntry>,
|
|
392
|
+
oursTree: Map<string, PathEntry>,
|
|
393
|
+
allPaths: Set<string>,
|
|
394
|
+
): { mergedEntries: Map<string, PathEntry>; conflicts: string[] } {
|
|
395
|
+
const mergedEntries = new Map<string, PathEntry>();
|
|
396
|
+
const conflicts: string[] = [];
|
|
397
|
+
|
|
398
|
+
for (const path of allPaths) {
|
|
399
|
+
const baseEntry = baseTree.get(path);
|
|
400
|
+
const theirsEntry = theirsTree.get(path);
|
|
401
|
+
const oursEntry = oursTree.get(path);
|
|
402
|
+
|
|
403
|
+
const result = this.mergeEntry(path, baseEntry, theirsEntry, oursEntry);
|
|
404
|
+
|
|
405
|
+
if (result === "conflict") {
|
|
406
|
+
conflicts.push(path);
|
|
407
|
+
} else if (result !== "deleted") {
|
|
408
|
+
mergedEntries.set(path, result);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
return { mergedEntries, conflicts };
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Merge a single entry in three-way merge.
|
|
417
|
+
*/
|
|
418
|
+
private mergeEntry(
|
|
419
|
+
_path: string,
|
|
420
|
+
base: PathEntry | undefined,
|
|
421
|
+
theirs: PathEntry | undefined,
|
|
422
|
+
ours: PathEntry | undefined,
|
|
423
|
+
): PathEntry | "deleted" | "conflict" {
|
|
424
|
+
const baseId = base?.id;
|
|
425
|
+
const theirsId = theirs?.id;
|
|
426
|
+
const oursId = ours?.id;
|
|
427
|
+
|
|
428
|
+
// No change in reverted commit (base == theirs means no change to revert)
|
|
429
|
+
if (baseId === theirsId) {
|
|
430
|
+
// Revert didn't change this file, keep ours' version
|
|
431
|
+
if (ours) {
|
|
432
|
+
return ours;
|
|
433
|
+
}
|
|
434
|
+
return "deleted";
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// File added in reverted commit (will be deleted in revert)
|
|
438
|
+
if (baseId && !theirsId) {
|
|
439
|
+
if (!oursId) {
|
|
440
|
+
// Already deleted in ours
|
|
441
|
+
return "deleted";
|
|
442
|
+
}
|
|
443
|
+
if (baseId === oursId) {
|
|
444
|
+
// Ours unchanged from base, delete is clean
|
|
445
|
+
return "deleted";
|
|
446
|
+
}
|
|
447
|
+
// Ours modified, base deleted (revert wants to delete) - conflict
|
|
448
|
+
return "conflict";
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// File deleted in reverted commit (will be added back in revert)
|
|
452
|
+
if (!baseId && theirsId) {
|
|
453
|
+
if (!oursId) {
|
|
454
|
+
// Added in theirs (parent), not in ours - take theirs' version
|
|
455
|
+
return theirs as PathEntry;
|
|
456
|
+
}
|
|
457
|
+
if (theirsId === oursId) {
|
|
458
|
+
// Same content in both
|
|
459
|
+
return ours as PathEntry;
|
|
460
|
+
}
|
|
461
|
+
// Different content - conflict
|
|
462
|
+
return "conflict";
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// File modified in reverted commit (will be un-modified in revert)
|
|
466
|
+
if (baseId && theirsId && baseId !== theirsId) {
|
|
467
|
+
if (!oursId) {
|
|
468
|
+
// Ours deleted, revert wants to change - conflict
|
|
469
|
+
return "conflict";
|
|
470
|
+
}
|
|
471
|
+
if (baseId === oursId) {
|
|
472
|
+
// Ours unchanged from base, take theirs' changes (parent version)
|
|
473
|
+
return theirs as PathEntry;
|
|
474
|
+
}
|
|
475
|
+
if (theirsId === oursId) {
|
|
476
|
+
// Same changes in both
|
|
477
|
+
return ours as PathEntry;
|
|
478
|
+
}
|
|
479
|
+
// Both modified differently - conflict
|
|
480
|
+
return "conflict";
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Fallback: keep ours
|
|
484
|
+
return ours ?? "deleted";
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Write conflict entries to staging.
|
|
489
|
+
*/
|
|
490
|
+
private async writeConflictStaging(
|
|
491
|
+
baseTree: Map<string, PathEntry>,
|
|
492
|
+
theirsTree: Map<string, PathEntry>,
|
|
493
|
+
oursTree: Map<string, PathEntry>,
|
|
494
|
+
conflicts: string[],
|
|
495
|
+
): Promise<void> {
|
|
496
|
+
const builder = this.staging.createBuilder();
|
|
497
|
+
|
|
498
|
+
// Add all non-conflicting entries at stage 0
|
|
499
|
+
const conflictSet = new Set(conflicts);
|
|
500
|
+
|
|
501
|
+
// Add all entries from ours that aren't conflicting
|
|
502
|
+
for (const [path, entry] of oursTree) {
|
|
503
|
+
if (!conflictSet.has(path)) {
|
|
504
|
+
builder.add({
|
|
505
|
+
path,
|
|
506
|
+
mode: entry.mode,
|
|
507
|
+
objectId: entry.id,
|
|
508
|
+
stage: MergeStage.MERGED,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// For conflicting paths, add stages 1, 2, 3
|
|
514
|
+
for (const path of conflicts) {
|
|
515
|
+
const base = baseTree.get(path);
|
|
516
|
+
const theirs = theirsTree.get(path);
|
|
517
|
+
const ours = oursTree.get(path);
|
|
518
|
+
|
|
519
|
+
// Stage 1: base (the commit being reverted)
|
|
520
|
+
if (base) {
|
|
521
|
+
builder.add({
|
|
522
|
+
path,
|
|
523
|
+
mode: base.mode,
|
|
524
|
+
objectId: base.id,
|
|
525
|
+
stage: MergeStage.BASE,
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// Stage 2: ours (current HEAD)
|
|
530
|
+
if (ours) {
|
|
531
|
+
builder.add({
|
|
532
|
+
path,
|
|
533
|
+
mode: ours.mode,
|
|
534
|
+
objectId: ours.id,
|
|
535
|
+
stage: MergeStage.OURS,
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// Stage 3: theirs (parent of commit being reverted)
|
|
540
|
+
if (theirs) {
|
|
541
|
+
builder.add({
|
|
542
|
+
path,
|
|
543
|
+
mode: theirs.mode,
|
|
544
|
+
objectId: theirs.id,
|
|
545
|
+
stage: MergeStage.THEIRS,
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
await builder.finish();
|
|
551
|
+
}
|
|
552
|
+
}
|