@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.
- package/README.md +180 -466
- 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 +25 -16
- 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,750 @@
|
|
|
1
|
+
import type { Commit, ObjectId, TreeEntry } from "@statewalker/vcs-core";
|
|
2
|
+
import { FileMode, isSymbolicRef } from "@statewalker/vcs-core";
|
|
3
|
+
import {
|
|
4
|
+
merge3Way,
|
|
5
|
+
MergeContentStrategy as UtilsMergeContentStrategy,
|
|
6
|
+
} from "@statewalker/vcs-utils";
|
|
7
|
+
import { MergeStage } from "@statewalker/vcs-working-tree";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
InvalidMergeHeadsError,
|
|
11
|
+
NoMergeBaseError,
|
|
12
|
+
NotFastForwardError,
|
|
13
|
+
} from "../errors/merge-errors.js";
|
|
14
|
+
import { NoHeadError, RefNotFoundError } from "../errors/ref-errors.js";
|
|
15
|
+
import { GitCommand } from "../git-command.js";
|
|
16
|
+
import {
|
|
17
|
+
ContentMergeStrategy,
|
|
18
|
+
FastForwardMode,
|
|
19
|
+
type MergeResult,
|
|
20
|
+
MergeStatus,
|
|
21
|
+
MergeStrategy,
|
|
22
|
+
} from "../results/merge-result.js";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Merge branches.
|
|
26
|
+
*
|
|
27
|
+
* Equivalent to `git merge`.
|
|
28
|
+
*
|
|
29
|
+
* Based on JGit's MergeCommand.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* // Fast-forward merge
|
|
34
|
+
* const result = await git.merge()
|
|
35
|
+
* .include("feature-branch")
|
|
36
|
+
* .call();
|
|
37
|
+
*
|
|
38
|
+
* // Force merge commit (no fast-forward)
|
|
39
|
+
* const result = await git.merge()
|
|
40
|
+
* .include("feature-branch")
|
|
41
|
+
* .setFastForwardMode(FastForwardMode.NO_FF)
|
|
42
|
+
* .call();
|
|
43
|
+
*
|
|
44
|
+
* // Fail if fast-forward not possible
|
|
45
|
+
* const result = await git.merge()
|
|
46
|
+
* .include("feature-branch")
|
|
47
|
+
* .setFastForwardMode(FastForwardMode.FF_ONLY)
|
|
48
|
+
* .call();
|
|
49
|
+
*
|
|
50
|
+
* // Use "ours" strategy (keep our tree, ignore theirs)
|
|
51
|
+
* const result = await git.merge()
|
|
52
|
+
* .include("feature-branch")
|
|
53
|
+
* .setStrategy(MergeStrategy.OURS)
|
|
54
|
+
* .call();
|
|
55
|
+
*
|
|
56
|
+
* // Use "theirs" strategy (replace our tree with theirs)
|
|
57
|
+
* const result = await git.merge()
|
|
58
|
+
* .include("feature-branch")
|
|
59
|
+
* .setStrategy(MergeStrategy.THEIRS)
|
|
60
|
+
* .call();
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export class MergeCommand extends GitCommand<MergeResult> {
|
|
64
|
+
private includes: string[] = [];
|
|
65
|
+
private fastForwardMode = FastForwardMode.FF;
|
|
66
|
+
private strategy = MergeStrategy.RECURSIVE;
|
|
67
|
+
private contentStrategy?: ContentMergeStrategy;
|
|
68
|
+
private squash = false;
|
|
69
|
+
private commit = true;
|
|
70
|
+
private message?: string;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Add a commit/branch to merge.
|
|
74
|
+
*
|
|
75
|
+
* @param refOrId Commit ID, branch name, or tag name to merge
|
|
76
|
+
*/
|
|
77
|
+
include(refOrId: string): this {
|
|
78
|
+
this.checkCallable();
|
|
79
|
+
this.includes.push(refOrId);
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Set fast-forward mode.
|
|
85
|
+
*
|
|
86
|
+
* - FF (default): Fast-forward when possible
|
|
87
|
+
* - NO_FF: Always create merge commit
|
|
88
|
+
* - FF_ONLY: Abort if fast-forward not possible
|
|
89
|
+
*
|
|
90
|
+
* @param mode Fast-forward mode
|
|
91
|
+
*/
|
|
92
|
+
setFastForwardMode(mode: FastForwardMode): this {
|
|
93
|
+
this.checkCallable();
|
|
94
|
+
this.fastForwardMode = mode;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Set merge strategy.
|
|
100
|
+
*
|
|
101
|
+
* - RECURSIVE (default): Standard three-way merge
|
|
102
|
+
* - OURS: Keep our tree, ignore their changes (creates merge commit)
|
|
103
|
+
* - THEIRS: Replace our tree with theirs (creates merge commit)
|
|
104
|
+
* - RESOLVE: Simple resolve strategy
|
|
105
|
+
*
|
|
106
|
+
* @param strategy Merge strategy
|
|
107
|
+
*/
|
|
108
|
+
setStrategy(strategy: MergeStrategy): this {
|
|
109
|
+
this.checkCallable();
|
|
110
|
+
this.strategy = strategy;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Set content merge strategy for handling file-level conflicts.
|
|
116
|
+
*
|
|
117
|
+
* - OURS: Take our version for conflicts
|
|
118
|
+
* - THEIRS: Take their version for conflicts
|
|
119
|
+
* - UNION: Concatenate both sides (ours first, then theirs)
|
|
120
|
+
*
|
|
121
|
+
* When set, conflicts are resolved automatically using this strategy
|
|
122
|
+
* instead of producing conflict markers.
|
|
123
|
+
*
|
|
124
|
+
* @param contentStrategy Content merge strategy
|
|
125
|
+
*/
|
|
126
|
+
setContentMergeStrategy(contentStrategy: ContentMergeStrategy): this {
|
|
127
|
+
this.checkCallable();
|
|
128
|
+
this.contentStrategy = contentStrategy;
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Set squash mode.
|
|
134
|
+
*
|
|
135
|
+
* When true, changes are staged but not committed, and HEAD is not updated.
|
|
136
|
+
*
|
|
137
|
+
* @param squash Whether to squash
|
|
138
|
+
*/
|
|
139
|
+
setSquash(squash: boolean): this {
|
|
140
|
+
this.checkCallable();
|
|
141
|
+
this.squash = squash;
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Set whether to commit after merge.
|
|
147
|
+
*
|
|
148
|
+
* @param commit Whether to commit (default: true)
|
|
149
|
+
*/
|
|
150
|
+
setCommit(commit: boolean): this {
|
|
151
|
+
this.checkCallable();
|
|
152
|
+
this.commit = commit;
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Set the merge commit message.
|
|
158
|
+
*
|
|
159
|
+
* @param message Commit message
|
|
160
|
+
*/
|
|
161
|
+
setMessage(message: string): this {
|
|
162
|
+
this.checkCallable();
|
|
163
|
+
this.message = message;
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Execute the merge.
|
|
169
|
+
*
|
|
170
|
+
* @returns MergeResult with status and details
|
|
171
|
+
*/
|
|
172
|
+
async call(): Promise<MergeResult> {
|
|
173
|
+
this.checkCallable();
|
|
174
|
+
this.setCallable(false);
|
|
175
|
+
|
|
176
|
+
// Validate parameters
|
|
177
|
+
if (this.includes.length === 0) {
|
|
178
|
+
throw new InvalidMergeHeadsError("No merge head specified");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (this.includes.length > 1) {
|
|
182
|
+
throw new InvalidMergeHeadsError("Only single-head merges are currently supported");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Get HEAD
|
|
186
|
+
const head = await this.refsStore.get("HEAD");
|
|
187
|
+
if (!head) {
|
|
188
|
+
throw new NoHeadError("Cannot merge without HEAD");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const headId = await this.refsStore.resolve("HEAD");
|
|
192
|
+
if (!headId?.objectId) {
|
|
193
|
+
throw new NoHeadError("HEAD does not point to a commit");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Resolve merge source
|
|
197
|
+
const srcRef = this.includes[0];
|
|
198
|
+
const srcId = await this.resolveRef(srcRef);
|
|
199
|
+
|
|
200
|
+
// Check if already up to date (src is ancestor of HEAD)
|
|
201
|
+
if (await this.isAncestor(srcId, headId.objectId)) {
|
|
202
|
+
return {
|
|
203
|
+
status: MergeStatus.ALREADY_UP_TO_DATE,
|
|
204
|
+
newHead: headId.objectId,
|
|
205
|
+
mergedCommits: [srcId],
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Check if fast-forward is possible (HEAD is ancestor of src)
|
|
210
|
+
const canFastForward = await this.isAncestor(headId.objectId, srcId);
|
|
211
|
+
|
|
212
|
+
if (canFastForward && this.fastForwardMode !== FastForwardMode.NO_FF) {
|
|
213
|
+
// Fast-forward merge
|
|
214
|
+
return this.doFastForward(headId.objectId, srcId, head);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (this.fastForwardMode === FastForwardMode.FF_ONLY) {
|
|
218
|
+
// Cannot fast-forward and FF_ONLY was requested
|
|
219
|
+
throw new NotFastForwardError();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Need to do a real merge
|
|
223
|
+
return this.doMerge(headId.objectId, srcId, head);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Perform a fast-forward merge.
|
|
228
|
+
*/
|
|
229
|
+
private async doFastForward(
|
|
230
|
+
headId: ObjectId,
|
|
231
|
+
srcId: ObjectId,
|
|
232
|
+
head: Awaited<ReturnType<typeof this.refsStore.get>>,
|
|
233
|
+
): Promise<MergeResult> {
|
|
234
|
+
if (!this.squash) {
|
|
235
|
+
// Update HEAD/branch to point to source commit
|
|
236
|
+
if (head && isSymbolicRef(head)) {
|
|
237
|
+
await this.refsStore.set(head.target, srcId);
|
|
238
|
+
} else {
|
|
239
|
+
await this.refsStore.set("HEAD", srcId);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Update staging to match new HEAD
|
|
243
|
+
const srcCommitFF = await this.commits.load(srcId);
|
|
244
|
+
if (srcCommitFF) {
|
|
245
|
+
await this.staging.readTree(this.trees, srcCommitFF.tree);
|
|
246
|
+
await this.staging.write();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
status: MergeStatus.FAST_FORWARD,
|
|
251
|
+
newHead: srcId,
|
|
252
|
+
mergedCommits: [headId, srcId],
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Squash: stage changes but don't update HEAD
|
|
257
|
+
const srcCommitSquash = await this.commits.load(srcId);
|
|
258
|
+
if (srcCommitSquash) {
|
|
259
|
+
await this.staging.readTree(this.trees, srcCommitSquash.tree);
|
|
260
|
+
await this.staging.write();
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
status: MergeStatus.FAST_FORWARD_SQUASHED,
|
|
265
|
+
newHead: headId,
|
|
266
|
+
mergedCommits: [headId, srcId],
|
|
267
|
+
message: "Squashed commit of merged changes",
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Perform a three-way merge.
|
|
273
|
+
*/
|
|
274
|
+
private async doMerge(
|
|
275
|
+
headId: ObjectId,
|
|
276
|
+
srcId: ObjectId,
|
|
277
|
+
head: Awaited<ReturnType<typeof this.refsStore.get>>,
|
|
278
|
+
): Promise<MergeResult> {
|
|
279
|
+
// Find merge base
|
|
280
|
+
const mergeBases = await this.commits.findMergeBase(headId, srcId);
|
|
281
|
+
if (mergeBases.length === 0) {
|
|
282
|
+
// No common ancestor - shouldn't happen with connected histories
|
|
283
|
+
throw new NoMergeBaseError([headId, srcId]);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const baseId = mergeBases[0]; // Use first merge base
|
|
287
|
+
|
|
288
|
+
// Load commits
|
|
289
|
+
const headCommit = await this.commits.load(headId);
|
|
290
|
+
const srcCommit = await this.commits.load(srcId);
|
|
291
|
+
|
|
292
|
+
if (!headCommit || !srcCommit) {
|
|
293
|
+
throw new RefNotFoundError(headId, "Failed to load commits for merge");
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Handle strategy-specific merge
|
|
297
|
+
let treeId: ObjectId;
|
|
298
|
+
|
|
299
|
+
if (this.strategy === MergeStrategy.OURS) {
|
|
300
|
+
// OURS strategy: keep our tree unchanged, completely ignore theirs
|
|
301
|
+
treeId = headCommit.tree;
|
|
302
|
+
await this.staging.readTree(this.trees, treeId);
|
|
303
|
+
await this.staging.write();
|
|
304
|
+
} else if (this.strategy === MergeStrategy.THEIRS) {
|
|
305
|
+
// THEIRS strategy: replace our tree with theirs
|
|
306
|
+
treeId = srcCommit.tree;
|
|
307
|
+
await this.staging.readTree(this.trees, treeId);
|
|
308
|
+
await this.staging.write();
|
|
309
|
+
} else {
|
|
310
|
+
// RECURSIVE or RESOLVE strategy: do three-way merge
|
|
311
|
+
const baseCommit = await this.commits.load(baseId);
|
|
312
|
+
if (!baseCommit) {
|
|
313
|
+
throw new RefNotFoundError(baseId, "Failed to load merge base commit");
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Perform tree-level merge
|
|
317
|
+
const mergeResult = await this.mergeTreesThreeWay(
|
|
318
|
+
baseCommit.tree,
|
|
319
|
+
headCommit.tree,
|
|
320
|
+
srcCommit.tree,
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
if (mergeResult.conflicts.length > 0) {
|
|
324
|
+
// Has conflicts - write conflict state to staging
|
|
325
|
+
await this.writeConflictStaging(
|
|
326
|
+
baseCommit.tree,
|
|
327
|
+
headCommit.tree,
|
|
328
|
+
srcCommit.tree,
|
|
329
|
+
mergeResult,
|
|
330
|
+
);
|
|
331
|
+
await this.staging.write();
|
|
332
|
+
|
|
333
|
+
return {
|
|
334
|
+
status: MergeStatus.CONFLICTING,
|
|
335
|
+
mergeBase: baseId,
|
|
336
|
+
mergedCommits: [headId, srcId],
|
|
337
|
+
conflicts: mergeResult.conflicts,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// No conflicts - write merged tree to staging
|
|
342
|
+
await this.writeMergedStaging(mergeResult);
|
|
343
|
+
await this.staging.write();
|
|
344
|
+
treeId = await this.staging.writeTree(this.trees);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (!this.commit || this.squash) {
|
|
348
|
+
// Don't commit
|
|
349
|
+
const status = this.squash ? MergeStatus.MERGED_SQUASHED : MergeStatus.MERGED_NOT_COMMITTED;
|
|
350
|
+
return {
|
|
351
|
+
status,
|
|
352
|
+
newHead: headId,
|
|
353
|
+
mergeBase: baseId,
|
|
354
|
+
mergedCommits: [headId, srcId],
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Create merge commit
|
|
359
|
+
const mergeMessage = this.message ?? `Merge commit '${srcId.slice(0, 7)}'`;
|
|
360
|
+
|
|
361
|
+
const now = Math.floor(Date.now() / 1000); // Unix timestamp in seconds
|
|
362
|
+
const mergeCommit: Commit = {
|
|
363
|
+
tree: treeId,
|
|
364
|
+
parents: [headId, srcId],
|
|
365
|
+
author: {
|
|
366
|
+
name: "Author",
|
|
367
|
+
email: "author@example.com",
|
|
368
|
+
timestamp: now,
|
|
369
|
+
tzOffset: "+0000",
|
|
370
|
+
},
|
|
371
|
+
committer: {
|
|
372
|
+
name: "Committer",
|
|
373
|
+
email: "committer@example.com",
|
|
374
|
+
timestamp: now,
|
|
375
|
+
tzOffset: "+0000",
|
|
376
|
+
},
|
|
377
|
+
message: mergeMessage,
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const newCommitId = await this.commits.store(mergeCommit);
|
|
381
|
+
|
|
382
|
+
// Update HEAD/branch
|
|
383
|
+
if (head && isSymbolicRef(head)) {
|
|
384
|
+
await this.refsStore.set(head.target, newCommitId);
|
|
385
|
+
} else {
|
|
386
|
+
await this.refsStore.set("HEAD", newCommitId);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return {
|
|
390
|
+
status: MergeStatus.MERGED,
|
|
391
|
+
newHead: newCommitId,
|
|
392
|
+
mergeBase: baseId,
|
|
393
|
+
mergedCommits: [headId, srcId],
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Check if commitA is an ancestor of commitB.
|
|
399
|
+
*/
|
|
400
|
+
private async isAncestor(commitA: ObjectId, commitB: ObjectId): Promise<boolean> {
|
|
401
|
+
if (commitA === commitB) return true;
|
|
402
|
+
|
|
403
|
+
// Walk ancestors of B looking for A
|
|
404
|
+
const visited = new Set<ObjectId>();
|
|
405
|
+
const queue: ObjectId[] = [commitB];
|
|
406
|
+
|
|
407
|
+
while (queue.length > 0) {
|
|
408
|
+
const current = queue.shift();
|
|
409
|
+
if (current === undefined) break;
|
|
410
|
+
|
|
411
|
+
if (visited.has(current)) continue;
|
|
412
|
+
visited.add(current);
|
|
413
|
+
|
|
414
|
+
if (current === commitA) return true;
|
|
415
|
+
|
|
416
|
+
try {
|
|
417
|
+
const commit = await this.commits.load(current);
|
|
418
|
+
if (commit) {
|
|
419
|
+
for (const parent of commit.parents) {
|
|
420
|
+
if (!visited.has(parent)) {
|
|
421
|
+
queue.push(parent);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
} catch {
|
|
426
|
+
// Commit not found - continue
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Perform three-way tree merge.
|
|
435
|
+
*
|
|
436
|
+
* Returns merged entries and conflicts.
|
|
437
|
+
*/
|
|
438
|
+
private async mergeTreesThreeWay(
|
|
439
|
+
baseTreeId: ObjectId,
|
|
440
|
+
oursTreeId: ObjectId,
|
|
441
|
+
theirsTreeId: ObjectId,
|
|
442
|
+
): Promise<TreeMergeResult> {
|
|
443
|
+
const result: TreeMergeResult = {
|
|
444
|
+
merged: [],
|
|
445
|
+
conflicts: [],
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
// Collect all paths from all three trees
|
|
449
|
+
const allPaths = new Set<string>();
|
|
450
|
+
const basePaths = new Map<string, TreeEntry>();
|
|
451
|
+
const oursPaths = new Map<string, TreeEntry>();
|
|
452
|
+
const theirsPaths = new Map<string, TreeEntry>();
|
|
453
|
+
|
|
454
|
+
await this.collectTreeEntries(baseTreeId, "", basePaths, allPaths);
|
|
455
|
+
await this.collectTreeEntries(oursTreeId, "", oursPaths, allPaths);
|
|
456
|
+
await this.collectTreeEntries(theirsTreeId, "", theirsPaths, allPaths);
|
|
457
|
+
|
|
458
|
+
// Process each unique path
|
|
459
|
+
for (const path of allPaths) {
|
|
460
|
+
const base = basePaths.get(path);
|
|
461
|
+
const ours = oursPaths.get(path);
|
|
462
|
+
const theirs = theirsPaths.get(path);
|
|
463
|
+
|
|
464
|
+
const mergeEntry = this.mergeEntry(path, base, ours, theirs);
|
|
465
|
+
|
|
466
|
+
if (mergeEntry.conflict) {
|
|
467
|
+
// Try content-level merge if contentStrategy is set
|
|
468
|
+
if (this.contentStrategy && ours && theirs) {
|
|
469
|
+
const contentMergeResult = await this.tryContentMerge(path, base, ours, theirs);
|
|
470
|
+
if (contentMergeResult) {
|
|
471
|
+
result.merged.push({
|
|
472
|
+
path,
|
|
473
|
+
entry: contentMergeResult,
|
|
474
|
+
});
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// No content strategy or content merge not possible - mark as conflict
|
|
480
|
+
result.conflicts.push(path);
|
|
481
|
+
result.merged.push({
|
|
482
|
+
path,
|
|
483
|
+
base,
|
|
484
|
+
ours,
|
|
485
|
+
theirs,
|
|
486
|
+
});
|
|
487
|
+
} else if (mergeEntry.entry) {
|
|
488
|
+
result.merged.push({
|
|
489
|
+
path,
|
|
490
|
+
entry: mergeEntry.entry,
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
// If entry is undefined and no conflict, the file was deleted
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
return result;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Try to perform content-level merge using the configured strategy.
|
|
501
|
+
*
|
|
502
|
+
* @returns Merged tree entry if successful, undefined if content merge not applicable
|
|
503
|
+
*/
|
|
504
|
+
private async tryContentMerge(
|
|
505
|
+
_path: string,
|
|
506
|
+
base: TreeEntry | undefined,
|
|
507
|
+
ours: TreeEntry,
|
|
508
|
+
theirs: TreeEntry,
|
|
509
|
+
): Promise<TreeEntry | undefined> {
|
|
510
|
+
// Only merge regular files (not symlinks, executables, etc. for now)
|
|
511
|
+
if (ours.mode !== FileMode.REGULAR_FILE || theirs.mode !== FileMode.REGULAR_FILE) {
|
|
512
|
+
return undefined;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// Content strategy must be set (caller should verify)
|
|
516
|
+
if (!this.contentStrategy) {
|
|
517
|
+
return undefined;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
try {
|
|
521
|
+
// Load content from all three versions
|
|
522
|
+
const baseContent = base ? await this.collectBlobContent(base.id) : new Uint8Array(0);
|
|
523
|
+
const oursContent = await this.collectBlobContent(ours.id);
|
|
524
|
+
const theirsContent = await this.collectBlobContent(theirs.id);
|
|
525
|
+
|
|
526
|
+
// Convert ContentMergeStrategy to utils MergeContentStrategy
|
|
527
|
+
const strategy = this.convertContentStrategy(this.contentStrategy);
|
|
528
|
+
|
|
529
|
+
// Perform content-level merge
|
|
530
|
+
const mergeResult = merge3Way(baseContent, oursContent, theirsContent, { strategy });
|
|
531
|
+
|
|
532
|
+
// If using OURS/THEIRS/UNION, conflicts are auto-resolved
|
|
533
|
+
// Store the merged content
|
|
534
|
+
const mergedBlobId = await this.blobs.store([mergeResult.content]);
|
|
535
|
+
|
|
536
|
+
return {
|
|
537
|
+
name: ours.name,
|
|
538
|
+
mode: ours.mode,
|
|
539
|
+
id: mergedBlobId,
|
|
540
|
+
};
|
|
541
|
+
} catch {
|
|
542
|
+
// Content merge failed - fall back to conflict
|
|
543
|
+
return undefined;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Collect all bytes from a blob into a single Uint8Array.
|
|
549
|
+
*/
|
|
550
|
+
private async collectBlobContent(blobId: ObjectId): Promise<Uint8Array> {
|
|
551
|
+
const chunks: Uint8Array[] = [];
|
|
552
|
+
const blobContent = await this.blobs.load(blobId);
|
|
553
|
+
if (blobContent) {
|
|
554
|
+
for await (const chunk of blobContent) {
|
|
555
|
+
chunks.push(chunk);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
if (chunks.length === 0) {
|
|
559
|
+
return new Uint8Array(0);
|
|
560
|
+
}
|
|
561
|
+
if (chunks.length === 1) {
|
|
562
|
+
return chunks[0];
|
|
563
|
+
}
|
|
564
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
565
|
+
const result = new Uint8Array(totalLength);
|
|
566
|
+
let offset = 0;
|
|
567
|
+
for (const chunk of chunks) {
|
|
568
|
+
result.set(chunk, offset);
|
|
569
|
+
offset += chunk.length;
|
|
570
|
+
}
|
|
571
|
+
return result;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Convert ContentMergeStrategy to utils MergeContentStrategy.
|
|
576
|
+
*/
|
|
577
|
+
private convertContentStrategy(strategy: ContentMergeStrategy): UtilsMergeContentStrategy {
|
|
578
|
+
switch (strategy) {
|
|
579
|
+
case ContentMergeStrategy.OURS:
|
|
580
|
+
return UtilsMergeContentStrategy.OURS;
|
|
581
|
+
case ContentMergeStrategy.THEIRS:
|
|
582
|
+
return UtilsMergeContentStrategy.THEIRS;
|
|
583
|
+
case ContentMergeStrategy.UNION:
|
|
584
|
+
return UtilsMergeContentStrategy.UNION;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Collect all blob entries from a tree recursively.
|
|
590
|
+
*/
|
|
591
|
+
private async collectTreeEntries(
|
|
592
|
+
treeId: ObjectId,
|
|
593
|
+
prefix: string,
|
|
594
|
+
entries: Map<string, TreeEntry>,
|
|
595
|
+
allPaths: Set<string>,
|
|
596
|
+
): Promise<void> {
|
|
597
|
+
for await (const entry of this.trees.loadTree(treeId)) {
|
|
598
|
+
const path = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
599
|
+
|
|
600
|
+
if (entry.mode === FileMode.TREE) {
|
|
601
|
+
// Recurse into subtree
|
|
602
|
+
await this.collectTreeEntries(entry.id, path, entries, allPaths);
|
|
603
|
+
} else {
|
|
604
|
+
// Blob entry
|
|
605
|
+
entries.set(path, entry);
|
|
606
|
+
allPaths.add(path);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Merge a single entry using three-way merge logic.
|
|
613
|
+
*/
|
|
614
|
+
private mergeEntry(
|
|
615
|
+
_path: string,
|
|
616
|
+
base: TreeEntry | undefined,
|
|
617
|
+
ours: TreeEntry | undefined,
|
|
618
|
+
theirs: TreeEntry | undefined,
|
|
619
|
+
): { entry?: TreeEntry; conflict?: boolean } {
|
|
620
|
+
// Case 1: All same (or all undefined)
|
|
621
|
+
if (this.sameEntry(base, ours) && this.sameEntry(base, theirs)) {
|
|
622
|
+
return { entry: ours };
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
// Case 2: Only we changed
|
|
626
|
+
if (this.sameEntry(base, theirs)) {
|
|
627
|
+
return { entry: ours };
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// Case 3: Only they changed
|
|
631
|
+
if (this.sameEntry(base, ours)) {
|
|
632
|
+
return { entry: theirs };
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// Case 4: Both changed - check if same change
|
|
636
|
+
if (this.sameEntry(ours, theirs)) {
|
|
637
|
+
return { entry: ours };
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// Case 5: Both changed differently - conflict
|
|
641
|
+
return { conflict: true };
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Check if two tree entries are the same.
|
|
646
|
+
*/
|
|
647
|
+
private sameEntry(a: TreeEntry | undefined, b: TreeEntry | undefined): boolean {
|
|
648
|
+
if (a === undefined && b === undefined) return true;
|
|
649
|
+
if (a === undefined || b === undefined) return false;
|
|
650
|
+
return a.id === b.id && a.mode === b.mode;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* Write conflict state to staging area.
|
|
655
|
+
*/
|
|
656
|
+
private async writeConflictStaging(
|
|
657
|
+
_baseTreeId: ObjectId,
|
|
658
|
+
_oursTreeId: ObjectId,
|
|
659
|
+
_theirsTreeId: ObjectId,
|
|
660
|
+
mergeResult: TreeMergeResult,
|
|
661
|
+
): Promise<void> {
|
|
662
|
+
const builder = this.staging.createBuilder();
|
|
663
|
+
|
|
664
|
+
// Add merged entries (stage 0)
|
|
665
|
+
for (const item of mergeResult.merged) {
|
|
666
|
+
if (item.entry) {
|
|
667
|
+
builder.add({
|
|
668
|
+
path: item.path,
|
|
669
|
+
mode: item.entry.mode,
|
|
670
|
+
objectId: item.entry.id,
|
|
671
|
+
stage: MergeStage.MERGED,
|
|
672
|
+
});
|
|
673
|
+
} else if (item.base || item.ours || item.theirs) {
|
|
674
|
+
// Conflict entry - add all stages
|
|
675
|
+
if (item.base) {
|
|
676
|
+
builder.add({
|
|
677
|
+
path: item.path,
|
|
678
|
+
mode: item.base.mode,
|
|
679
|
+
objectId: item.base.id,
|
|
680
|
+
stage: MergeStage.BASE,
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
if (item.ours) {
|
|
684
|
+
builder.add({
|
|
685
|
+
path: item.path,
|
|
686
|
+
mode: item.ours.mode,
|
|
687
|
+
objectId: item.ours.id,
|
|
688
|
+
stage: MergeStage.OURS,
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
if (item.theirs) {
|
|
692
|
+
builder.add({
|
|
693
|
+
path: item.path,
|
|
694
|
+
mode: item.theirs.mode,
|
|
695
|
+
objectId: item.theirs.id,
|
|
696
|
+
stage: MergeStage.THEIRS,
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
await builder.finish();
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* Write successfully merged entries to staging.
|
|
707
|
+
*/
|
|
708
|
+
private async writeMergedStaging(mergeResult: TreeMergeResult): Promise<void> {
|
|
709
|
+
const builder = this.staging.createBuilder();
|
|
710
|
+
|
|
711
|
+
for (const item of mergeResult.merged) {
|
|
712
|
+
if (item.entry) {
|
|
713
|
+
builder.add({
|
|
714
|
+
path: item.path,
|
|
715
|
+
mode: item.entry.mode,
|
|
716
|
+
objectId: item.entry.id,
|
|
717
|
+
stage: MergeStage.MERGED,
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
await builder.finish();
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Result of tree-level merge.
|
|
728
|
+
*/
|
|
729
|
+
interface TreeMergeResult {
|
|
730
|
+
/** Merged entries (both clean merges and conflicts) */
|
|
731
|
+
merged: MergedEntry[];
|
|
732
|
+
/** Paths with conflicts */
|
|
733
|
+
conflicts: string[];
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* A merged entry from three-way merge.
|
|
738
|
+
*/
|
|
739
|
+
interface MergedEntry {
|
|
740
|
+
/** File path */
|
|
741
|
+
path: string;
|
|
742
|
+
/** Merged entry (for clean merges) */
|
|
743
|
+
entry?: TreeEntry;
|
|
744
|
+
/** Base entry (for conflicts) */
|
|
745
|
+
base?: TreeEntry;
|
|
746
|
+
/** Our entry (for conflicts) */
|
|
747
|
+
ours?: TreeEntry;
|
|
748
|
+
/** Their entry (for conflicts) */
|
|
749
|
+
theirs?: TreeEntry;
|
|
750
|
+
}
|