@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,544 @@
|
|
|
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 CherryPickResult, CherryPickStatus } from "../results/cherry-pick-result.js";
|
|
12
|
+
import { type ContentMergeStrategy, MergeStrategy } from "../results/merge-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
|
+
* Cherry-pick commits onto current branch.
|
|
51
|
+
*
|
|
52
|
+
* Equivalent to `git cherry-pick`.
|
|
53
|
+
*
|
|
54
|
+
* Based on JGit's CherryPickCommand.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // Cherry-pick a single commit
|
|
59
|
+
* const result = await git.cherryPick()
|
|
60
|
+
* .include(commitId)
|
|
61
|
+
* .call();
|
|
62
|
+
*
|
|
63
|
+
* // Cherry-pick without committing
|
|
64
|
+
* const result = await git.cherryPick()
|
|
65
|
+
* .include(commitId)
|
|
66
|
+
* .setNoCommit(true)
|
|
67
|
+
* .call();
|
|
68
|
+
*
|
|
69
|
+
* // Cherry-pick a merge commit
|
|
70
|
+
* const result = await git.cherryPick()
|
|
71
|
+
* .include(mergeCommitId)
|
|
72
|
+
* .setMainlineParentNumber(1)
|
|
73
|
+
* .call();
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export class CherryPickCommand extends GitCommand<CherryPickResult> {
|
|
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 = "cherry-pick:";
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Add a commit to cherry-pick.
|
|
87
|
+
*
|
|
88
|
+
* @param refOrId Commit ID or reference to cherry-pick
|
|
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 cherry-pick.
|
|
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 cherry-picking 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: "cherry-pick:")
|
|
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<CherryPickResult> {
|
|
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 cherryPickedRefs: ObjectId[] = [];
|
|
209
|
+
|
|
210
|
+
// Cherry-pick each commit in order
|
|
211
|
+
for (const refOrId of this.includes) {
|
|
212
|
+
const result = await this.cherryPickOne(refOrId, headId);
|
|
213
|
+
|
|
214
|
+
if (result.status !== CherryPickStatus.OK) {
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
headId = result.newHead as ObjectId;
|
|
219
|
+
cherryPickedRefs.push(refOrId);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return {
|
|
223
|
+
status: CherryPickStatus.OK,
|
|
224
|
+
newHead: headId,
|
|
225
|
+
cherryPickedRefs,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Cherry-pick a single commit.
|
|
231
|
+
*/
|
|
232
|
+
private async cherryPickOne(refOrId: string, headId: ObjectId): Promise<CherryPickResult> {
|
|
233
|
+
// Resolve the commit to cherry-pick
|
|
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 cherry-pick 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
|
+
const headCommit = await this.commits.load(headId);
|
|
265
|
+
if (!headCommit) {
|
|
266
|
+
throw new RefNotFoundError(`Cannot find head commit: ${headId}`);
|
|
267
|
+
}
|
|
268
|
+
const headTree = new Map<string, PathEntry>();
|
|
269
|
+
const commitTree = new Map<string, PathEntry>();
|
|
270
|
+
const parentTree = new Map<string, PathEntry>();
|
|
271
|
+
const allPaths = new Set<string>();
|
|
272
|
+
|
|
273
|
+
await this.collectTreeEntries(headCommit.tree, "", headTree, allPaths);
|
|
274
|
+
await this.collectTreeEntries(commit.tree, "", commitTree, allPaths);
|
|
275
|
+
if (parentId !== this.trees.getEmptyTreeId()) {
|
|
276
|
+
// parentId is a commit ID, so we need to load the commit to get its tree
|
|
277
|
+
const parentCommit = await this.commits.load(parentId);
|
|
278
|
+
if (!parentCommit) {
|
|
279
|
+
throw new RefNotFoundError(`Cannot find parent commit: ${parentId}`);
|
|
280
|
+
}
|
|
281
|
+
await this.collectTreeEntries(parentCommit.tree, "", parentTree, allPaths);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Perform three-way merge: parent -> commit applied to head
|
|
285
|
+
const { mergedEntries, conflicts } = this.threeWayMerge(
|
|
286
|
+
parentTree,
|
|
287
|
+
commitTree,
|
|
288
|
+
headTree,
|
|
289
|
+
allPaths,
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
if (conflicts.length > 0) {
|
|
293
|
+
// Write conflict markers to staging
|
|
294
|
+
await this.writeConflictStaging(parentTree, commitTree, headTree, conflicts);
|
|
295
|
+
|
|
296
|
+
return {
|
|
297
|
+
status: CherryPickStatus.CONFLICTING,
|
|
298
|
+
cherryPickedRefs: [],
|
|
299
|
+
conflicts,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Build merged tree
|
|
304
|
+
const builder = this.staging.createBuilder();
|
|
305
|
+
for (const entry of mergedEntries.values()) {
|
|
306
|
+
builder.add({
|
|
307
|
+
path: entry.path,
|
|
308
|
+
mode: entry.mode,
|
|
309
|
+
objectId: entry.id,
|
|
310
|
+
stage: MergeStage.MERGED,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
await builder.finish();
|
|
314
|
+
|
|
315
|
+
// Write tree
|
|
316
|
+
const newTreeId = await this.staging.writeTree(this.trees);
|
|
317
|
+
|
|
318
|
+
// Create commit if not noCommit mode
|
|
319
|
+
if (!this.noCommit) {
|
|
320
|
+
const newCommit: Commit = {
|
|
321
|
+
tree: newTreeId,
|
|
322
|
+
parents: [headId],
|
|
323
|
+
author: commit.author,
|
|
324
|
+
committer: defaultCommitter(),
|
|
325
|
+
message: commit.message,
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const newCommitId = await this.commits.store(newCommit);
|
|
329
|
+
|
|
330
|
+
// Update HEAD
|
|
331
|
+
const head = await this.refsStore.get("HEAD");
|
|
332
|
+
if (head && "target" in head) {
|
|
333
|
+
await this.refsStore.set(head.target, newCommitId);
|
|
334
|
+
} else {
|
|
335
|
+
await this.refsStore.set("HEAD", newCommitId);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return {
|
|
339
|
+
status: CherryPickStatus.OK,
|
|
340
|
+
newHead: newCommitId,
|
|
341
|
+
cherryPickedRefs: [commitId],
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// noCommit mode - just return current HEAD
|
|
346
|
+
return {
|
|
347
|
+
status: CherryPickStatus.OK,
|
|
348
|
+
newHead: headId,
|
|
349
|
+
cherryPickedRefs: [commitId],
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Collect all blob entries from a tree recursively.
|
|
355
|
+
*/
|
|
356
|
+
private async collectTreeEntries(
|
|
357
|
+
treeId: ObjectId,
|
|
358
|
+
prefix: string,
|
|
359
|
+
entries: Map<string, PathEntry>,
|
|
360
|
+
allPaths: Set<string>,
|
|
361
|
+
): Promise<void> {
|
|
362
|
+
for await (const entry of this.trees.loadTree(treeId)) {
|
|
363
|
+
const path = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
364
|
+
|
|
365
|
+
if (entry.mode === FileMode.TREE) {
|
|
366
|
+
// Recurse into subtree
|
|
367
|
+
await this.collectTreeEntries(entry.id, path, entries, allPaths);
|
|
368
|
+
} else {
|
|
369
|
+
// Blob entry
|
|
370
|
+
entries.set(path, { path, mode: entry.mode, id: entry.id });
|
|
371
|
+
allPaths.add(path);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Perform three-way merge for cherry-pick.
|
|
378
|
+
*
|
|
379
|
+
* The cherry-pick merge is: apply changes from parent->commit onto head.
|
|
380
|
+
*/
|
|
381
|
+
private threeWayMerge(
|
|
382
|
+
parentTree: Map<string, PathEntry>,
|
|
383
|
+
commitTree: Map<string, PathEntry>,
|
|
384
|
+
headTree: Map<string, PathEntry>,
|
|
385
|
+
allPaths: Set<string>,
|
|
386
|
+
): { mergedEntries: Map<string, PathEntry>; conflicts: string[] } {
|
|
387
|
+
const mergedEntries = new Map<string, PathEntry>();
|
|
388
|
+
const conflicts: string[] = [];
|
|
389
|
+
|
|
390
|
+
for (const path of allPaths) {
|
|
391
|
+
const parentEntry = parentTree.get(path);
|
|
392
|
+
const commitEntry = commitTree.get(path);
|
|
393
|
+
const headEntry = headTree.get(path);
|
|
394
|
+
|
|
395
|
+
const result = this.mergeEntry(path, parentEntry, commitEntry, headEntry);
|
|
396
|
+
|
|
397
|
+
if (result === "conflict") {
|
|
398
|
+
conflicts.push(path);
|
|
399
|
+
} else if (result !== "deleted") {
|
|
400
|
+
mergedEntries.set(path, result);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return { mergedEntries, conflicts };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Merge a single entry in three-way merge.
|
|
409
|
+
*/
|
|
410
|
+
private mergeEntry(
|
|
411
|
+
_path: string,
|
|
412
|
+
parent: PathEntry | undefined,
|
|
413
|
+
commit: PathEntry | undefined,
|
|
414
|
+
head: PathEntry | undefined,
|
|
415
|
+
): PathEntry | "deleted" | "conflict" {
|
|
416
|
+
const parentId = parent?.id;
|
|
417
|
+
const commitId = commit?.id;
|
|
418
|
+
const headId = head?.id;
|
|
419
|
+
|
|
420
|
+
// No change in cherry-picked commit
|
|
421
|
+
if (parentId === commitId) {
|
|
422
|
+
// Cherry-pick didn't change this file, keep head's version
|
|
423
|
+
if (head) {
|
|
424
|
+
return head;
|
|
425
|
+
}
|
|
426
|
+
return "deleted";
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// File added in cherry-picked commit
|
|
430
|
+
if (!parentId && commitId) {
|
|
431
|
+
if (!headId) {
|
|
432
|
+
// Added in commit, not in head - take commit's version
|
|
433
|
+
return commit as PathEntry;
|
|
434
|
+
}
|
|
435
|
+
if (commitId === headId) {
|
|
436
|
+
// Same content added in both
|
|
437
|
+
return head as PathEntry;
|
|
438
|
+
}
|
|
439
|
+
// Different content added - conflict
|
|
440
|
+
return "conflict";
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// File deleted in cherry-picked commit
|
|
444
|
+
if (parentId && !commitId) {
|
|
445
|
+
if (!headId) {
|
|
446
|
+
// Already deleted in head
|
|
447
|
+
return "deleted";
|
|
448
|
+
}
|
|
449
|
+
if (parentId === headId) {
|
|
450
|
+
// Head unchanged from parent, delete is clean
|
|
451
|
+
return "deleted";
|
|
452
|
+
}
|
|
453
|
+
// Head modified, commit deleted - conflict
|
|
454
|
+
return "conflict";
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// File modified in cherry-picked commit
|
|
458
|
+
if (parentId && commitId && parentId !== commitId) {
|
|
459
|
+
if (!headId) {
|
|
460
|
+
// Head deleted, commit modified - conflict
|
|
461
|
+
return "conflict";
|
|
462
|
+
}
|
|
463
|
+
if (parentId === headId) {
|
|
464
|
+
// Head unchanged, take commit's changes
|
|
465
|
+
return commit as PathEntry;
|
|
466
|
+
}
|
|
467
|
+
if (commitId === headId) {
|
|
468
|
+
// Same changes in both
|
|
469
|
+
return head as PathEntry;
|
|
470
|
+
}
|
|
471
|
+
// Both modified differently - conflict
|
|
472
|
+
return "conflict";
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Fallback: keep head
|
|
476
|
+
return head ?? "deleted";
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Write conflict entries to staging.
|
|
481
|
+
*/
|
|
482
|
+
private async writeConflictStaging(
|
|
483
|
+
parentTree: Map<string, PathEntry>,
|
|
484
|
+
commitTree: Map<string, PathEntry>,
|
|
485
|
+
headTree: Map<string, PathEntry>,
|
|
486
|
+
conflicts: string[],
|
|
487
|
+
): Promise<void> {
|
|
488
|
+
const builder = this.staging.createBuilder();
|
|
489
|
+
|
|
490
|
+
// Add all non-conflicting entries at stage 0
|
|
491
|
+
const conflictSet = new Set(conflicts);
|
|
492
|
+
|
|
493
|
+
// Add all entries from head that aren't conflicting
|
|
494
|
+
for (const [path, entry] of headTree) {
|
|
495
|
+
if (!conflictSet.has(path)) {
|
|
496
|
+
builder.add({
|
|
497
|
+
path,
|
|
498
|
+
mode: entry.mode,
|
|
499
|
+
objectId: entry.id,
|
|
500
|
+
stage: MergeStage.MERGED,
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// For conflicting paths, add stages 1, 2, 3
|
|
506
|
+
for (const path of conflicts) {
|
|
507
|
+
const parent = parentTree.get(path);
|
|
508
|
+
const commit = commitTree.get(path);
|
|
509
|
+
const head = headTree.get(path);
|
|
510
|
+
|
|
511
|
+
// Stage 1: base (parent)
|
|
512
|
+
if (parent) {
|
|
513
|
+
builder.add({
|
|
514
|
+
path,
|
|
515
|
+
mode: parent.mode,
|
|
516
|
+
objectId: parent.id,
|
|
517
|
+
stage: MergeStage.BASE,
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Stage 2: ours (head)
|
|
522
|
+
if (head) {
|
|
523
|
+
builder.add({
|
|
524
|
+
path,
|
|
525
|
+
mode: head.mode,
|
|
526
|
+
objectId: head.id,
|
|
527
|
+
stage: MergeStage.OURS,
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// Stage 3: theirs (commit being cherry-picked)
|
|
532
|
+
if (commit) {
|
|
533
|
+
builder.add({
|
|
534
|
+
path,
|
|
535
|
+
mode: commit.mode,
|
|
536
|
+
objectId: commit.id,
|
|
537
|
+
stage: MergeStage.THEIRS,
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
await builder.finish();
|
|
543
|
+
}
|
|
544
|
+
}
|