@statewalker/vcs-commands 0.3.2 → 0.5.0
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 +19 -0
- package/dist/commands/add-command.d.ts +0 -6
- package/dist/commands/add-command.d.ts.map +1 -1
- package/dist/commands/blame-command.d.ts +14 -0
- package/dist/commands/blame-command.d.ts.map +1 -1
- package/dist/commands/branch-command.d.ts.map +1 -1
- package/dist/commands/checkout-command.d.ts +7 -24
- package/dist/commands/checkout-command.d.ts.map +1 -1
- package/dist/commands/clean-command.d.ts +34 -4
- package/dist/commands/clean-command.d.ts.map +1 -1
- package/dist/commands/clone-command.d.ts +61 -21
- package/dist/commands/clone-command.d.ts.map +1 -1
- package/dist/commands/core-config.d.ts +16 -0
- package/dist/commands/core-config.d.ts.map +1 -0
- package/dist/commands/fetch-command.d.ts +104 -37
- package/dist/commands/fetch-command.d.ts.map +1 -1
- package/dist/commands/index.d.ts +1 -0
- package/dist/commands/index.d.ts.map +1 -1
- package/dist/commands/index.js +2 -2
- package/dist/commands/init-command.d.ts.map +1 -1
- package/dist/commands/merge-command.d.ts +15 -2
- package/dist/commands/merge-command.d.ts.map +1 -1
- package/dist/commands/push-command.d.ts +58 -25
- package/dist/commands/push-command.d.ts.map +1 -1
- package/dist/commands/remote-command.d.ts +115 -6
- package/dist/commands/remote-command.d.ts.map +1 -1
- package/dist/commands/reset-command.d.ts.map +1 -1
- package/dist/commands/stash-apply-command.d.ts +35 -13
- package/dist/commands/stash-apply-command.d.ts.map +1 -1
- package/dist/commands/stash-create-command.d.ts +59 -25
- package/dist/commands/stash-create-command.d.ts.map +1 -1
- package/dist/commands/stash-drop-command.d.ts +8 -2
- package/dist/commands/stash-drop-command.d.ts.map +1 -1
- package/dist/commands/stash-list-command.d.ts +2 -4
- package/dist/commands/stash-list-command.d.ts.map +1 -1
- package/dist/commands/stash-working-tree.d.ts +72 -0
- package/dist/commands/stash-working-tree.d.ts.map +1 -0
- package/dist/commands/status-command.d.ts +29 -22
- package/dist/commands/status-command.d.ts.map +1 -1
- package/dist/commands/tag-command.d.ts.map +1 -1
- package/dist/commands/tree-restore.d.ts +52 -5
- package/dist/commands/tree-restore.d.ts.map +1 -1
- package/dist/commands/worktree-overwrite-checks.d.ts +64 -0
- package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -0
- package/dist/{commands-COO4tJhv.js → commands-CsLNyvZ1.js} +3370 -1057
- package/dist/core-commands/add.command.impl.d.ts +0 -4
- package/dist/core-commands/add.command.impl.d.ts.map +1 -1
- package/dist/core-commands/index.d.ts +1 -3
- package/dist/core-commands/index.d.ts.map +1 -1
- package/dist/core-commands/store-worktree-file.d.ts +6 -0
- package/dist/core-commands/store-worktree-file.d.ts.map +1 -0
- package/dist/errors/checkout-errors.d.ts +13 -0
- package/dist/errors/checkout-errors.d.ts.map +1 -1
- package/dist/errors/index.js +18 -1
- package/dist/git-command.d.ts +10 -1
- package/dist/git-command.d.ts.map +1 -1
- package/dist/git.d.ts +21 -3
- package/dist/git.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/remote-config/index.d.ts +1 -0
- package/dist/remote-config/index.d.ts.map +1 -1
- package/dist/remote-config/remote-config-store.d.ts +22 -0
- package/dist/remote-config/remote-config-store.d.ts.map +1 -1
- package/dist/remote-config/remote-refs.d.ts +37 -0
- package/dist/remote-config/remote-refs.d.ts.map +1 -0
- package/dist/results/clone-result.d.ts +6 -0
- package/dist/results/clone-result.d.ts.map +1 -1
- package/dist/results/diff-formatter.d.ts +14 -7
- package/dist/results/diff-formatter.d.ts.map +1 -1
- package/dist/results/fetch-result.d.ts +16 -1
- package/dist/results/fetch-result.d.ts.map +1 -1
- package/dist/results/index.js +28 -23
- package/dist/results/push-result.d.ts +9 -2
- package/dist/results/push-result.d.ts.map +1 -1
- package/dist/results/status-result.d.ts +91 -20
- package/dist/results/status-result.d.ts.map +1 -1
- package/dist/{status-result-BwAyxq6R.js → status-result-BpyUK1Sg.js} +115 -5
- package/dist/transport-command.d.ts +12 -0
- package/dist/transport-command.d.ts.map +1 -1
- package/package.json +14 -12
- package/src/commands/add-command.ts +23 -30
- package/src/commands/blame-command.ts +41 -0
- package/src/commands/branch-command.ts +2 -53
- package/src/commands/checkout-command.ts +205 -127
- package/src/commands/clean-command.ts +187 -81
- package/src/commands/clone-command.ts +334 -137
- package/src/commands/core-config.ts +41 -0
- package/src/commands/fetch-command.ts +536 -211
- package/src/commands/gc-command.ts +1 -1
- package/src/commands/index.ts +1 -0
- package/src/commands/init-command.ts +108 -11
- package/src/commands/merge-command.ts +60 -27
- package/src/commands/pack-refs-command.ts +2 -2
- package/src/commands/push-command.ts +242 -308
- package/src/commands/reflog-command.ts +2 -2
- package/src/commands/remote-command.ts +429 -19
- package/src/commands/reset-command.ts +18 -5
- package/src/commands/stash-apply-command.ts +632 -97
- package/src/commands/stash-create-command.ts +142 -62
- package/src/commands/stash-drop-command.ts +53 -5
- package/src/commands/stash-list-command.ts +10 -4
- package/src/commands/stash-working-tree.ts +243 -0
- package/src/commands/status-command.ts +197 -91
- package/src/commands/tag-command.ts +2 -53
- package/src/commands/tree-restore.ts +143 -18
- package/src/commands/worktree-overwrite-checks.ts +215 -0
- package/src/core-commands/add.command.impl.ts +15 -20
- package/src/core-commands/index.ts +1 -8
- package/src/core-commands/store-worktree-file.ts +5 -0
- package/src/errors/checkout-errors.ts +20 -0
- package/src/git-command.ts +42 -1
- package/src/git.ts +30 -2
- package/src/remote-config/index.ts +1 -0
- package/src/remote-config/remote-config-store.ts +95 -8
- package/src/remote-config/remote-refs.ts +103 -0
- package/src/results/clone-result.ts +6 -0
- package/src/results/diff-formatter.ts +31 -38
- package/src/results/fetch-result.ts +16 -1
- package/src/results/push-result.ts +9 -2
- package/src/results/status-result.ts +212 -29
- package/src/transport-command.ts +18 -0
- package/dist/core-commands/checkout.command.d.ts +0 -103
- package/dist/core-commands/checkout.command.d.ts.map +0 -1
- package/dist/core-commands/checkout.command.impl.d.ts +0 -114
- package/dist/core-commands/checkout.command.impl.d.ts.map +0 -1
- package/dist/pack-import/index.d.ts +0 -8
- package/dist/pack-import/index.d.ts.map +0 -1
- package/dist/pack-import/pack-import.d.ts +0 -11
- package/dist/pack-import/pack-import.d.ts.map +0 -1
- package/src/core-commands/checkout.command.impl.ts +0 -586
- package/src/core-commands/checkout.command.ts +0 -125
- package/src/pack-import/index.ts +0 -8
- package/src/pack-import/pack-import.ts +0 -24
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { FileMode, type ObjectId } from "@statewalker/vcs-core";
|
|
2
|
+
import { mergeLines } from "@statewalker/vcs-utils";
|
|
3
|
+
import { entryStatOf, type Worktree } from "@statewalker/vcs-working-tree";
|
|
4
4
|
import {
|
|
5
5
|
InvalidArgumentError,
|
|
6
6
|
NoHeadError,
|
|
@@ -9,16 +9,64 @@ import {
|
|
|
9
9
|
StoreNotAvailableError,
|
|
10
10
|
} from "../errors/index.js";
|
|
11
11
|
import { GitCommand } from "../git-command.js";
|
|
12
|
+
import { DEFAULT_RENAME_SCORE, SimilarityIndex } from "../rename/index.js";
|
|
12
13
|
import { type ContentMergeStrategy, MergeStrategy } from "../results/merge-result.js";
|
|
13
14
|
import type { StashApplyResult } from "../results/stash-result.js";
|
|
14
15
|
import { StashApplyStatus } from "../results/stash-result.js";
|
|
15
|
-
import {
|
|
16
|
+
import { coreBool, worktreeModeTrust } from "./core-config.js";
|
|
16
17
|
import { STASH_REF } from "./stash-list-command.js";
|
|
17
18
|
import { planTreeRestore, type TreeRestorePlan, writeTreeRestorePlan } from "./tree-restore.js";
|
|
19
|
+
import {
|
|
20
|
+
type CheckedEntry,
|
|
21
|
+
findInTheWay,
|
|
22
|
+
overwriteMessage,
|
|
23
|
+
pruneEmptyParents,
|
|
24
|
+
removeDirectoryIfEmpty,
|
|
25
|
+
worktreeDiffers,
|
|
26
|
+
} from "./worktree-overwrite-checks.js";
|
|
27
|
+
|
|
28
|
+
/** A path the index and the stash changed beyond a clean merge, as git leaves it. */
|
|
29
|
+
interface MergeConflict {
|
|
30
|
+
base?: CheckedEntry;
|
|
31
|
+
ours?: CheckedEntry;
|
|
32
|
+
theirs?: CheckedEntry;
|
|
33
|
+
/** Where the stages (and the worktree file) go: the path, or `<path>~<side>` */
|
|
34
|
+
stagePath: string;
|
|
35
|
+
/** The file with conflict markers, for a text conflict */
|
|
36
|
+
content?: Uint8Array;
|
|
37
|
+
/** The blob the worktree gets at `stagePath` (a modify/delete's surviving stash side) */
|
|
38
|
+
write?: CheckedEntry;
|
|
39
|
+
/** The path a file/directory conflict moves ours away from */
|
|
40
|
+
moveFrom?: string;
|
|
41
|
+
mode: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function concatBytes(chunks: Uint8Array[]): Uint8Array {
|
|
45
|
+
const out = new Uint8Array(chunks.reduce((n, c) => n + c.length, 0));
|
|
46
|
+
let offset = 0;
|
|
47
|
+
for (const c of chunks) {
|
|
48
|
+
out.set(c, offset);
|
|
49
|
+
offset += c.length;
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const sameEntry = (a: CheckedEntry | undefined, b: CheckedEntry | undefined) =>
|
|
55
|
+
a === undefined || b === undefined ? a === b : a.objectId === b.objectId && a.mode === b.mode;
|
|
18
56
|
|
|
19
57
|
/** Names this operation in restore failure messages. */
|
|
20
58
|
const STASH_APPLY = "stash apply";
|
|
21
59
|
|
|
60
|
+
/** Removes the directory at `path` if nothing is in it (git's `rmdir` of a submodule). */
|
|
61
|
+
async function removeIfEmptyDirectory(worktree: Worktree, path: string): Promise<void> {
|
|
62
|
+
const entry = await worktree.getEntry(path);
|
|
63
|
+
if (!entry?.isDirectory) {
|
|
64
|
+
if (entry) await worktree.remove(path);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
await removeDirectoryIfEmpty(worktree, path);
|
|
68
|
+
}
|
|
69
|
+
|
|
22
70
|
/**
|
|
23
71
|
* Command to apply a stashed commit.
|
|
24
72
|
*
|
|
@@ -26,18 +74,16 @@ const STASH_APPLY = "stash apply";
|
|
|
26
74
|
*
|
|
27
75
|
* Based on JGit's StashApplyCommand.
|
|
28
76
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
77
|
+
* As git's `stash apply`: the stash's change is merged into the current index (staged
|
|
78
|
+
* content and newly staged files take part), line by line where both sides changed a
|
|
79
|
+
* file; the result goes to the worktree, refused up front where it would overwrite local
|
|
80
|
+
* changes or untracked files. A content conflict is written with conflict markers and
|
|
81
|
+
* staged at stages 1-3 (status CONFLICTS). Without `setRestoreIndex(true)` (`--index`)
|
|
82
|
+
* the index keeps its entries plus the files the stash adds; the stashed untracked files
|
|
83
|
+
* are restored first, refused (nothing written) where a file stands in their way.
|
|
33
84
|
*
|
|
34
|
-
* Known limits,
|
|
35
|
-
*
|
|
36
|
-
* has no primitive to create one - writing the target as a regular file
|
|
37
|
-
* would silently produce a working tree that does not match;
|
|
38
|
-
* - a gitlink's contents are not restored, as in git;
|
|
39
|
-
* - directories left empty by a deletion are not pruned;
|
|
40
|
-
* - the restore is not atomic across the working tree and the index.
|
|
85
|
+
* Known limits: a gitlink's contents are not restored, as in git; the restore is not
|
|
86
|
+
* atomic across the working tree and the index.
|
|
41
87
|
*
|
|
42
88
|
* @example
|
|
43
89
|
* ```typescript
|
|
@@ -57,7 +103,7 @@ const STASH_APPLY = "stash apply";
|
|
|
57
103
|
*/
|
|
58
104
|
export class StashApplyCommand extends GitCommand<StashApplyResult> {
|
|
59
105
|
private stashRef?: string;
|
|
60
|
-
private restoreIndex =
|
|
106
|
+
private restoreIndex = false;
|
|
61
107
|
private restoreUntracked = true;
|
|
62
108
|
private strategy = MergeStrategy.RECURSIVE;
|
|
63
109
|
private contentStrategy?: ContentMergeStrategy;
|
|
@@ -83,9 +129,12 @@ export class StashApplyCommand extends GitCommand<StashApplyResult> {
|
|
|
83
129
|
}
|
|
84
130
|
|
|
85
131
|
/**
|
|
86
|
-
* Set whether to restore the index state
|
|
132
|
+
* Set whether to restore the index state (`git stash apply --index`): the stash's index
|
|
133
|
+
* change is merged into the current index, and a conflict there refuses the apply
|
|
134
|
+
* ("conflicts in index. Try without --index."). Off by default, as in git: the index then
|
|
135
|
+
* keeps its entries, plus the files the stash adds.
|
|
87
136
|
*
|
|
88
|
-
* @param restoreIndex true to restore index (default)
|
|
137
|
+
* @param restoreIndex true to restore the index (default false)
|
|
89
138
|
*/
|
|
90
139
|
setRestoreIndex(restoreIndex: boolean): this {
|
|
91
140
|
this.checkCallable();
|
|
@@ -183,7 +232,11 @@ export class StashApplyCommand extends GitCommand<StashApplyResult> {
|
|
|
183
232
|
if (!worktree) {
|
|
184
233
|
throw new StoreNotAvailableError("worktree", "stash apply requires a working tree");
|
|
185
234
|
}
|
|
235
|
+
// one batch: the checks and the writes share one reading of the ignore rules
|
|
236
|
+
return worktree.batch ? worktree.batch(() => this.apply(worktree)) : this.apply(worktree);
|
|
237
|
+
}
|
|
186
238
|
|
|
239
|
+
private async apply(worktree: Worktree): Promise<StashApplyResult> {
|
|
187
240
|
// Get HEAD commit
|
|
188
241
|
const headRef = await this.refsStore.resolve("HEAD");
|
|
189
242
|
if (!headRef?.objectId) {
|
|
@@ -210,80 +263,108 @@ export class StashApplyCommand extends GitCommand<StashApplyResult> {
|
|
|
210
263
|
const stashIndexCommit = stashCommit.parents[1];
|
|
211
264
|
const stashUntrackedCommit = stashCommit.parents[2]; // Optional
|
|
212
265
|
|
|
213
|
-
//
|
|
266
|
+
// git's do_apply_stash: the stash's change (its base -> its worktree tree) merged into
|
|
267
|
+
// the current index ("ours" is the tree written from the index, so staged content and
|
|
268
|
+
// newly staged files take part), the result written to the worktree; with --index the
|
|
269
|
+
// stash's index change is merged into the index too, else the index keeps its entries
|
|
270
|
+
// plus the files the stash adds.
|
|
214
271
|
const headCommit = await this.commits.load(headRef.objectId);
|
|
215
272
|
const stashHeadCommitObj = await this.commits.load(stashHeadCommit);
|
|
216
273
|
if (!headCommit || !stashHeadCommitObj) {
|
|
217
274
|
throw new RefNotFoundError(headRef.objectId, "Head or stash commit not found");
|
|
218
275
|
}
|
|
219
|
-
const
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
// base->theirs is what the branch did since.
|
|
225
|
-
const mergeResult = await mergeTreesThreeWay(
|
|
226
|
-
this.trees,
|
|
227
|
-
stashHeadTree,
|
|
228
|
-
stashWorkingTree,
|
|
229
|
-
headCommit.tree,
|
|
230
|
-
);
|
|
231
|
-
|
|
232
|
-
if (mergeResult.conflicts && mergeResult.conflicts.length > 0) {
|
|
233
|
-
return {
|
|
234
|
-
status: StashApplyStatus.CONFLICTS,
|
|
235
|
-
stashCommit: stashId,
|
|
236
|
-
conflicts: mergeResult.conflicts,
|
|
237
|
-
};
|
|
276
|
+
const unmerged: string[] = [];
|
|
277
|
+
const index = new Map<string, CheckedEntry>();
|
|
278
|
+
for await (const entry of this.staging.entries()) {
|
|
279
|
+
if (entry.stage === 0) index.set(entry.path, entry);
|
|
280
|
+
else unmerged.push(entry.path);
|
|
238
281
|
}
|
|
282
|
+
if (unmerged.length > 0) {
|
|
283
|
+
throw new StashApplyFailedError(
|
|
284
|
+
stashId,
|
|
285
|
+
[...new Set(unmerged)],
|
|
286
|
+
"error: you need to resolve your current index first",
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
const ignoreCase = coreBool(this.workingCopy, "ignorecase", false);
|
|
290
|
+
const base = await this.flatTree(stashHeadCommitObj.tree);
|
|
291
|
+
const stashWorking = await this.flatTree(stashCommit.tree);
|
|
292
|
+
const ours = new Map(index);
|
|
239
293
|
|
|
240
|
-
//
|
|
241
|
-
|
|
242
|
-
// tree and the index exactly as they were.
|
|
243
|
-
let mergedIndexTree: ObjectId | undefined;
|
|
294
|
+
// --index: the stash's index change onto the current index, refused on a conflict
|
|
295
|
+
let restoredIndex: Map<string, CheckedEntry> | undefined;
|
|
244
296
|
if (this.restoreIndex) {
|
|
245
297
|
const stashIndexCommitObj = await this.commits.load(stashIndexCommit);
|
|
246
|
-
const
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
if (indexMergeResult.conflicts && indexMergeResult.conflicts.length > 0) {
|
|
255
|
-
return {
|
|
256
|
-
status: StashApplyStatus.CONFLICTS,
|
|
257
|
-
stashCommit: stashId,
|
|
258
|
-
conflicts: indexMergeResult.conflicts,
|
|
259
|
-
};
|
|
298
|
+
const stashIndex = stashIndexCommitObj ? await this.flatTree(stashIndexCommitObj.tree) : base;
|
|
299
|
+
const indexMerge = await this.mergeFlat(base, ours, stashIndex, ignoreCase);
|
|
300
|
+
if (indexMerge.conflicts.size > 0) {
|
|
301
|
+
throw new StashApplyFailedError(
|
|
302
|
+
stashId,
|
|
303
|
+
[...indexMerge.conflicts.keys()],
|
|
304
|
+
"error: conflicts in index. Try without --index.",
|
|
305
|
+
);
|
|
260
306
|
}
|
|
261
|
-
|
|
262
|
-
mergedIndexTree = indexMergeResult.tree;
|
|
307
|
+
restoredIndex = indexMerge.result;
|
|
263
308
|
}
|
|
264
309
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
//
|
|
268
|
-
//
|
|
269
|
-
const
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
310
|
+
const merge = await this.mergeFlat(base, ours, stashWorking, ignoreCase);
|
|
311
|
+
|
|
312
|
+
// symlinks are restored as links, or as files holding the target where the worktree
|
|
313
|
+
// or core.symlinks cannot have links (as clone writes them)
|
|
314
|
+
const trust = worktreeModeTrust(this.workingCopy, worktree);
|
|
315
|
+
// every path of the result verified (verify_path, collisions) before anything is written
|
|
316
|
+
const resultTree = await this.storeFlat(merge.result);
|
|
317
|
+
const worktreePlan = await planTreeRestore(this.trees, this.blobs, resultTree, STASH_APPLY, {
|
|
318
|
+
symlinks: true,
|
|
319
|
+
ignoreCase,
|
|
320
|
+
windows: worktree.capabilities?.windowsPaths === true,
|
|
321
|
+
reservedNames: worktree.capabilities?.reservedNames,
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
// Only what the merge changes is touched: the paths where its result differs from the
|
|
325
|
+
// index, and its conflicts, are written, removed or staged; an unrelated unstaged edit is
|
|
326
|
+
// left alone
|
|
327
|
+
const conflicts = merge.conflicts;
|
|
328
|
+
const changed = [...new Set([...ours.keys(), ...merge.result.keys(), ...conflicts.keys()])]
|
|
329
|
+
.filter((p) => conflicts.has(p) || !sameEntry(ours.get(p), merge.result.get(p)))
|
|
330
|
+
.sort();
|
|
331
|
+
const movedFrom = new Set(
|
|
332
|
+
[...conflicts.values()].flatMap((c) => (c.moveFrom ? [c.moveFrom] : [])),
|
|
274
333
|
);
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
334
|
+
const removes = changed.filter((p) => !merge.result.has(p) && ours.has(p) && !conflicts.has(p));
|
|
335
|
+
const conflictWrites = [...conflicts.values()].filter((c) => c.content || c.write);
|
|
336
|
+
const writes = [
|
|
337
|
+
...changed.filter((p) => merge.result.has(p) && !conflicts.has(p)),
|
|
338
|
+
...conflictWrites.map((c) => c.stagePath),
|
|
339
|
+
];
|
|
340
|
+
|
|
341
|
+
// git's merge refuses before writing anything (protecting untracked files): local
|
|
342
|
+
// changes on a path the merge changes, untracked files where it writes, untracked files
|
|
343
|
+
// in a directory a file of the stash replaces (ignored files are expendable)
|
|
344
|
+
const local: string[] = [];
|
|
345
|
+
for (const path of changed) {
|
|
346
|
+
// a conflict that leaves the worktree file as it is (rename/rename, rename/delete on
|
|
347
|
+
// our side, a binary conflict) only stages: git keeps an unstaged edit there
|
|
348
|
+
const conflict = conflicts.get(path);
|
|
349
|
+
if (conflict && !conflict.content && !conflict.write && !conflict.moveFrom) continue;
|
|
350
|
+
const i = index.get(path);
|
|
351
|
+
if (i && (await worktreeDiffers(worktree, path, i, trust))) local.push(path);
|
|
284
352
|
}
|
|
285
|
-
const
|
|
286
|
-
|
|
353
|
+
const found = await findInTheWay(worktree, {
|
|
354
|
+
writes,
|
|
355
|
+
removes: new Set([...removes, ...movedFrom]),
|
|
356
|
+
index,
|
|
357
|
+
ignoreCase,
|
|
358
|
+
});
|
|
359
|
+
const refused = overwriteMessage("merge", { local, ...found });
|
|
360
|
+
if (refused) {
|
|
361
|
+
throw new StashApplyFailedError(
|
|
362
|
+
stashId,
|
|
363
|
+
[...local, ...found.lostDirs, ...found.untracked],
|
|
364
|
+
refused,
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
// the stashed untracked files, checked (leading components too) before any write
|
|
287
368
|
const untrackedPlan = await this.planUntrackedRestore(
|
|
288
369
|
stashUntrackedCommit,
|
|
289
370
|
worktreePlan,
|
|
@@ -291,19 +372,128 @@ export class StashApplyCommand extends GitCommand<StashApplyResult> {
|
|
|
291
372
|
);
|
|
292
373
|
|
|
293
374
|
// Everything below this line writes.
|
|
294
|
-
|
|
295
|
-
|
|
375
|
+
if (untrackedPlan) {
|
|
376
|
+
// git restores the untracked files first; they stay untracked
|
|
377
|
+
await writeTreeRestorePlan(worktree, this.blobs, untrackedPlan, STASH_APPLY, {
|
|
378
|
+
symlinks: trust.symlinks,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
// a file a file/directory conflict moves aside goes to `<path>~<side>` first
|
|
382
|
+
for (const conflict of conflicts.values()) {
|
|
383
|
+
if (!conflict.moveFrom) continue;
|
|
384
|
+
if (conflict.write?.mode === FileMode.GITLINK) {
|
|
385
|
+
// a submodule moved aside: staged at stage 2, its new path an empty directory (as
|
|
386
|
+
// git checks out an unpopulated submodule); the old directory goes only if empty
|
|
387
|
+
if (!(await worktree.getEntry(conflict.stagePath))) {
|
|
388
|
+
await worktree.mkdir(conflict.stagePath);
|
|
389
|
+
}
|
|
390
|
+
await removeIfEmptyDirectory(worktree, conflict.moveFrom);
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
if (conflict.content) {
|
|
394
|
+
await worktree.writeContent(conflict.stagePath, conflict.content, {
|
|
395
|
+
mode: conflict.mode,
|
|
396
|
+
createParents: true,
|
|
397
|
+
replaceInTheWay: true,
|
|
398
|
+
});
|
|
399
|
+
} else if (conflict.write) {
|
|
400
|
+
await this.writeBlob(worktree, conflict.stagePath, conflict.write, trust.symlinks);
|
|
401
|
+
}
|
|
402
|
+
await worktree.remove(conflict.moveFrom);
|
|
403
|
+
}
|
|
404
|
+
// deletions next, as git's unpack-trees: a deleted file or link `d` makes way for `d/x`
|
|
405
|
+
for (const path of removes) {
|
|
296
406
|
await worktree.remove(path);
|
|
407
|
+
await pruneEmptyParents(worktree, path);
|
|
297
408
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
409
|
+
const writing = new Set(writes.filter((p) => merge.result.has(p)));
|
|
410
|
+
await writeTreeRestorePlan(
|
|
411
|
+
worktree,
|
|
412
|
+
this.blobs,
|
|
413
|
+
{
|
|
414
|
+
...worktreePlan,
|
|
415
|
+
entries: worktreePlan.entries.filter((e) => writing.has(e.path)),
|
|
416
|
+
gitlinks: [],
|
|
417
|
+
},
|
|
418
|
+
STASH_APPLY,
|
|
419
|
+
{ symlinks: trust.symlinks, replaceInTheWay: true },
|
|
420
|
+
);
|
|
421
|
+
// conflicted files: the text with git's markers, or the surviving side of a
|
|
422
|
+
// modify/delete; a binary conflict keeps ours as it is
|
|
423
|
+
for (const conflict of conflicts.values()) {
|
|
424
|
+
if (conflict.moveFrom) continue;
|
|
425
|
+
if (conflict.content) {
|
|
426
|
+
await worktree.writeContent(conflict.stagePath, conflict.content, {
|
|
427
|
+
mode: conflict.mode,
|
|
428
|
+
createParents: true,
|
|
429
|
+
replaceInTheWay: true,
|
|
430
|
+
});
|
|
431
|
+
} else if (conflict.write) {
|
|
432
|
+
await this.writeBlob(worktree, conflict.stagePath, conflict.write, trust.symlinks);
|
|
433
|
+
}
|
|
302
434
|
}
|
|
303
|
-
|
|
304
|
-
|
|
435
|
+
|
|
436
|
+
// the index
|
|
437
|
+
const editor = this.staging.createEditor();
|
|
438
|
+
const stage = async (path: string, entry: CheckedEntry, stageNumber: 0 | 1 | 2 | 3) => {
|
|
439
|
+
// the worktree's stat only for a file that holds the staged blob (else it is rehashed)
|
|
440
|
+
let stats: { size: number; mtime: number; ctime?: number } | undefined;
|
|
441
|
+
if (stageNumber === 0) {
|
|
442
|
+
const current = await worktree.getEntry(path);
|
|
443
|
+
if (
|
|
444
|
+
current &&
|
|
445
|
+
!current.isDirectory &&
|
|
446
|
+
(await worktree.computeHash(path)) === entry.objectId
|
|
447
|
+
) {
|
|
448
|
+
stats = current;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
editor.upsert({
|
|
452
|
+
path,
|
|
453
|
+
objectId: entry.objectId,
|
|
454
|
+
mode: entry.mode,
|
|
455
|
+
stage: stageNumber,
|
|
456
|
+
...entryStatOf(stats ?? {}),
|
|
457
|
+
});
|
|
458
|
+
};
|
|
459
|
+
if (conflicts.size > 0) {
|
|
460
|
+
// a conflicted merge leaves its result staged and the conflicts at stages 1-3
|
|
461
|
+
for (const path of changed) {
|
|
462
|
+
const result = merge.result.get(path);
|
|
463
|
+
const conflict = conflicts.get(path);
|
|
464
|
+
if (conflict) {
|
|
465
|
+
editor.remove(path);
|
|
466
|
+
if (conflict.base) await stage(conflict.stagePath, conflict.base, 1);
|
|
467
|
+
if (conflict.ours) await stage(conflict.stagePath, conflict.ours, 2);
|
|
468
|
+
if (conflict.theirs) await stage(conflict.stagePath, conflict.theirs, 3);
|
|
469
|
+
} else if (result) await stage(path, result, 0);
|
|
470
|
+
else editor.remove(path);
|
|
471
|
+
}
|
|
472
|
+
await editor.finish();
|
|
305
473
|
await this.staging.write();
|
|
474
|
+
return {
|
|
475
|
+
status: StashApplyStatus.CONFLICTS,
|
|
476
|
+
stashCommit: stashId,
|
|
477
|
+
conflicts: [...conflicts.keys()].sort(),
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
if (restoredIndex) {
|
|
481
|
+
// --index: the index is the stash's index change merged into it
|
|
482
|
+
for (const path of new Set([...index.keys(), ...restoredIndex.keys()])) {
|
|
483
|
+
const entry = restoredIndex.get(path);
|
|
484
|
+
if (sameEntry(index.get(path), entry)) continue;
|
|
485
|
+
if (entry) await stage(path, entry, 0);
|
|
486
|
+
else editor.remove(path);
|
|
487
|
+
}
|
|
488
|
+
} else {
|
|
489
|
+
// the index keeps its entries, plus the files the stash adds (git stages them)
|
|
490
|
+
for (const path of changed) {
|
|
491
|
+
const result = merge.result.get(path);
|
|
492
|
+
if (result && !ours.has(path)) await stage(path, result, 0);
|
|
493
|
+
}
|
|
306
494
|
}
|
|
495
|
+
await editor.finish();
|
|
496
|
+
await this.staging.write();
|
|
307
497
|
|
|
308
498
|
return {
|
|
309
499
|
status: StashApplyStatus.OK,
|
|
@@ -311,6 +501,321 @@ export class StashApplyCommand extends GitCommand<StashApplyResult> {
|
|
|
311
501
|
};
|
|
312
502
|
}
|
|
313
503
|
|
|
504
|
+
/** Writes a blob to the worktree (a link as a link where the worktree has them). */
|
|
505
|
+
private async writeBlob(
|
|
506
|
+
worktree: Worktree,
|
|
507
|
+
path: string,
|
|
508
|
+
entry: CheckedEntry,
|
|
509
|
+
symlinks: boolean,
|
|
510
|
+
): Promise<void> {
|
|
511
|
+
const content = await this.blobs.load(entry.objectId);
|
|
512
|
+
if (!content) throw new Error(`${STASH_APPLY} cannot restore ${path}: blob missing`);
|
|
513
|
+
const mode = entry.mode === FileMode.SYMLINK && !symlinks ? FileMode.REGULAR_FILE : entry.mode;
|
|
514
|
+
await worktree.writeContent(path, content, {
|
|
515
|
+
mode,
|
|
516
|
+
createParents: true,
|
|
517
|
+
replaceInTheWay: true,
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* git's ort merge of a stash: `base` (the stash's parent) to `ours` (the current index)
|
|
523
|
+
* and to `theirs` (the stash). Renames on one side (exact, then by similarity of at least
|
|
524
|
+
* {@link DEFAULT_RENAME_SCORE}%) carry the other side's change to the new path; files both
|
|
525
|
+
* sides changed are merged line by line (`Updated upstream` / `Stashed changes` markers);
|
|
526
|
+
* binary files, modify/delete and file/directory collisions are conflicts as in git.
|
|
527
|
+
*/
|
|
528
|
+
private async mergeFlat(
|
|
529
|
+
baseIn: Map<string, CheckedEntry>,
|
|
530
|
+
oursIn: Map<string, CheckedEntry>,
|
|
531
|
+
theirsIn: Map<string, CheckedEntry>,
|
|
532
|
+
ignoreCase = false,
|
|
533
|
+
): Promise<{
|
|
534
|
+
result: Map<string, CheckedEntry>;
|
|
535
|
+
conflicts: Map<string, MergeConflict>;
|
|
536
|
+
}> {
|
|
537
|
+
const base = new Map(baseIn);
|
|
538
|
+
const ours = new Map(oursIn);
|
|
539
|
+
const theirs = new Map(theirsIn);
|
|
540
|
+
const conflicts = new Map<string, MergeConflict>();
|
|
541
|
+
const oursRenames = await this.detectRenames(baseIn, oursIn);
|
|
542
|
+
const theirsRenames = await this.detectRenames(baseIn, theirsIn);
|
|
543
|
+
// both sides renamed the same path: to one path, it merges there; to two, git's
|
|
544
|
+
// CONFLICT (rename/rename): the original at stage 1 (DD), each side's at its path (AU/UA)
|
|
545
|
+
for (const [from, oursTo] of [...oursRenames]) {
|
|
546
|
+
const theirsTo = theirsRenames.get(from);
|
|
547
|
+
const original = base.get(from);
|
|
548
|
+
if (theirsTo === undefined || !original) continue;
|
|
549
|
+
oursRenames.delete(from);
|
|
550
|
+
theirsRenames.delete(from);
|
|
551
|
+
if (theirsTo === oursTo) {
|
|
552
|
+
base.delete(from);
|
|
553
|
+
base.set(oursTo, original);
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
const o = ours.get(oursTo);
|
|
557
|
+
const t = theirs.get(theirsTo);
|
|
558
|
+
if (!o || !t || theirs.has(oursTo) || ours.has(theirsTo)) continue;
|
|
559
|
+
base.delete(from);
|
|
560
|
+
ours.delete(oursTo);
|
|
561
|
+
theirs.delete(theirsTo);
|
|
562
|
+
conflicts.set(from, { base: original, stagePath: from, mode: original.mode });
|
|
563
|
+
conflicts.set(oursTo, { ours: o, stagePath: oursTo, mode: o.mode });
|
|
564
|
+
conflicts.set(theirsTo, { theirs: t, stagePath: theirsTo, mode: t.mode, write: t });
|
|
565
|
+
}
|
|
566
|
+
// a path one side renamed: the other side's change merges at the new path; deleted on
|
|
567
|
+
// the other side, git's CONFLICT (rename/delete): stage 1 and the renaming side's
|
|
568
|
+
for (const [renames, side, other, sideIsOurs] of [
|
|
569
|
+
[oursRenames, ours, theirs, true],
|
|
570
|
+
[theirsRenames, theirs, ours, false],
|
|
571
|
+
] as const) {
|
|
572
|
+
for (const [from, to] of renames) {
|
|
573
|
+
const original = base.get(from);
|
|
574
|
+
if (!original || other.has(to)) continue;
|
|
575
|
+
const moved = other.get(from);
|
|
576
|
+
base.delete(from);
|
|
577
|
+
if (moved) {
|
|
578
|
+
other.delete(from);
|
|
579
|
+
other.set(to, moved);
|
|
580
|
+
base.set(to, original);
|
|
581
|
+
continue;
|
|
582
|
+
}
|
|
583
|
+
const kept = side.get(to);
|
|
584
|
+
if (!kept) continue;
|
|
585
|
+
side.delete(to);
|
|
586
|
+
conflicts.set(to, {
|
|
587
|
+
base: original,
|
|
588
|
+
...(sideIsOurs ? { ours: kept } : { theirs: kept, write: kept }),
|
|
589
|
+
stagePath: to,
|
|
590
|
+
mode: kept.mode,
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const result = new Map<string, CheckedEntry>();
|
|
596
|
+
const read = async (e?: CheckedEntry) => {
|
|
597
|
+
if (!e) return new Uint8Array(0);
|
|
598
|
+
const chunks: Uint8Array[] = [];
|
|
599
|
+
for await (const chunk of (await this.blobs.load(e.objectId)) ?? []) chunks.push(chunk);
|
|
600
|
+
return concatBytes(chunks);
|
|
601
|
+
};
|
|
602
|
+
for (const path of new Set([...base.keys(), ...ours.keys(), ...theirs.keys()])) {
|
|
603
|
+
const b = base.get(path);
|
|
604
|
+
const o = ours.get(path);
|
|
605
|
+
const t = theirs.get(path);
|
|
606
|
+
if (sameEntry(o, t) || sameEntry(b, t)) {
|
|
607
|
+
if (o) result.set(path, o);
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
if (sameEntry(b, o)) {
|
|
611
|
+
if (t) result.set(path, t);
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
if (!o || !t) {
|
|
615
|
+
// modify/delete: the surviving side stays in the worktree, staged 1 and 2 or 3
|
|
616
|
+
conflicts.set(path, {
|
|
617
|
+
base: b,
|
|
618
|
+
ours: o,
|
|
619
|
+
theirs: t,
|
|
620
|
+
stagePath: path,
|
|
621
|
+
mode: (o ?? t)?.mode ?? 0,
|
|
622
|
+
...(t ? { write: t } : {}),
|
|
623
|
+
});
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
const textual = (e?: CheckedEntry) =>
|
|
627
|
+
e === undefined || e.mode === FileMode.REGULAR_FILE || e.mode === FileMode.EXECUTABLE_FILE;
|
|
628
|
+
const mode = o.mode === t.mode ? o.mode : b && o.mode === b.mode ? t.mode : o.mode;
|
|
629
|
+
if (textual(o) && textual(t) && textual(b)) {
|
|
630
|
+
const [bb, ob, tb] = [await read(b), await read(o), await read(t)];
|
|
631
|
+
if (![bb, ob, tb].some((bytes) => SimilarityIndex.isBinary(bytes))) {
|
|
632
|
+
const lines = mergeLines(bb, ob, tb, {
|
|
633
|
+
oursLabel: "Updated upstream",
|
|
634
|
+
theirsLabel: "Stashed changes",
|
|
635
|
+
});
|
|
636
|
+
if (lines.conflicts === 0) {
|
|
637
|
+
result.set(path, { objectId: await this.blobs.store([lines.content]), mode });
|
|
638
|
+
} else {
|
|
639
|
+
conflicts.set(path, {
|
|
640
|
+
base: b,
|
|
641
|
+
ours: o,
|
|
642
|
+
theirs: t,
|
|
643
|
+
stagePath: path,
|
|
644
|
+
content: lines.content,
|
|
645
|
+
mode,
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
// binary files (git: "Cannot merge binary files"), or a link or submodule changed on
|
|
652
|
+
// both sides: ours stays in the worktree, all three staged
|
|
653
|
+
conflicts.set(path, { base: b, ours: o, theirs: t, stagePath: path, mode });
|
|
654
|
+
}
|
|
655
|
+
// a path cannot be a file and a directory at once (CONFLICT (file/directory)): the
|
|
656
|
+
// directory keeps the name, the file (a merged one, or the side a conflict keeps) moves
|
|
657
|
+
// aside to `<path>~<side>`, made unique as git's unique_path: `_0`, `_1`, ... appended
|
|
658
|
+
// while the name is a path (or a directory) of any of the three trees, or already taken
|
|
659
|
+
const dirsOf = (paths: Iterable<string>) => {
|
|
660
|
+
const dirs = new Set<string>();
|
|
661
|
+
for (const path of paths) {
|
|
662
|
+
for (let k = path.indexOf("/"); k > 0; k = path.indexOf("/", k + 1)) {
|
|
663
|
+
dirs.add(path.slice(0, k));
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
return dirs;
|
|
667
|
+
};
|
|
668
|
+
// on a case-folding worktree (core.ignorecase) a name taken in another case is taken:
|
|
669
|
+
// the moved file would land on that path
|
|
670
|
+
const fold = (name: string) => (ignoreCase ? name.toLowerCase() : name);
|
|
671
|
+
const taken = new Set<string>();
|
|
672
|
+
for (const map of [baseIn, oursIn, theirsIn, base, ours, theirs]) {
|
|
673
|
+
for (const path of map.keys()) taken.add(fold(path));
|
|
674
|
+
for (const dir of dirsOf(map.keys())) taken.add(fold(dir));
|
|
675
|
+
}
|
|
676
|
+
const uniquePath = (path: string, label: string) => {
|
|
677
|
+
let name = `${path}~${label}`;
|
|
678
|
+
for (let n = 0; taken.has(fold(name)); n++) name = `${path}~${label}_${n}`;
|
|
679
|
+
taken.add(fold(name));
|
|
680
|
+
return name;
|
|
681
|
+
};
|
|
682
|
+
const outcomeDirs = dirsOf([
|
|
683
|
+
...result.keys(),
|
|
684
|
+
...[...conflicts.values()].map((c) => c.stagePath),
|
|
685
|
+
]);
|
|
686
|
+
const theirsDirs = dirsOf(theirs.keys());
|
|
687
|
+
const files = [
|
|
688
|
+
...result.keys(),
|
|
689
|
+
...[...conflicts.entries()].filter(([, c]) => c.ours || c.theirs).map(([p]) => p),
|
|
690
|
+
].sort();
|
|
691
|
+
for (const path of files) {
|
|
692
|
+
if (!outcomeDirs.has(path)) continue;
|
|
693
|
+
// the side whose tree has the directory keeps it; the file is the other side's
|
|
694
|
+
const oursFile = theirsDirs.has(path);
|
|
695
|
+
const label = oursFile ? "Updated upstream" : "Stashed changes";
|
|
696
|
+
const moveFrom = oursFile && ours.has(path) ? { moveFrom: path } : {};
|
|
697
|
+
const merged = result.get(path);
|
|
698
|
+
if (merged) {
|
|
699
|
+
result.delete(path);
|
|
700
|
+
conflicts.set(path, {
|
|
701
|
+
...(oursFile ? { ours: merged } : { theirs: merged }),
|
|
702
|
+
stagePath: uniquePath(path, label),
|
|
703
|
+
mode: merged.mode,
|
|
704
|
+
write: merged,
|
|
705
|
+
...moveFrom,
|
|
706
|
+
});
|
|
707
|
+
continue;
|
|
708
|
+
}
|
|
709
|
+
const conflict = conflicts.get(path) as MergeConflict;
|
|
710
|
+
const kept = oursFile ? conflict.ours : conflict.theirs;
|
|
711
|
+
conflicts.set(path, {
|
|
712
|
+
...conflict,
|
|
713
|
+
stagePath: uniquePath(path, label),
|
|
714
|
+
...(kept && !conflict.content ? { write: kept } : {}),
|
|
715
|
+
...moveFrom,
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
return { result, conflicts };
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* The paths `side` renamed from `base`: a deleted path paired with an added one holding
|
|
723
|
+
* the same blob, else (text files) the most similar added one scoring at least
|
|
724
|
+
* {@link DEFAULT_RENAME_SCORE}, as git's rename detection.
|
|
725
|
+
*/
|
|
726
|
+
private async detectRenames(
|
|
727
|
+
base: Map<string, CheckedEntry>,
|
|
728
|
+
side: Map<string, CheckedEntry>,
|
|
729
|
+
): Promise<Map<string, string>> {
|
|
730
|
+
const deleted = [...base.keys()].filter((p) => !side.has(p));
|
|
731
|
+
const added = new Set([...side.keys()].filter((p) => !base.has(p)));
|
|
732
|
+
const renames = new Map<string, string>();
|
|
733
|
+
if (deleted.length === 0 || added.size === 0) return renames;
|
|
734
|
+
for (const from of deleted) {
|
|
735
|
+
const blob = base.get(from)?.objectId;
|
|
736
|
+
const to = [...added].find((p) => side.get(p)?.objectId === blob);
|
|
737
|
+
if (to) {
|
|
738
|
+
renames.set(from, to);
|
|
739
|
+
added.delete(to);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
// inexact renames, bounded as git's rename limit bounds them
|
|
743
|
+
const left = deleted.filter((p) => !renames.has(p));
|
|
744
|
+
if (left.length === 0 || added.size === 0 || left.length * added.size > 1000) return renames;
|
|
745
|
+
const read = async (e?: CheckedEntry) => {
|
|
746
|
+
const chunks: Uint8Array[] = [];
|
|
747
|
+
for await (const chunk of (await this.blobs.load(e?.objectId ?? "")) ?? [])
|
|
748
|
+
chunks.push(chunk);
|
|
749
|
+
return concatBytes(chunks);
|
|
750
|
+
};
|
|
751
|
+
const candidates = new Map<string, SimilarityIndex>();
|
|
752
|
+
for (const p of added) {
|
|
753
|
+
const bytes = await read(side.get(p));
|
|
754
|
+
if (!SimilarityIndex.isBinary(bytes)) candidates.set(p, SimilarityIndex.create(bytes));
|
|
755
|
+
}
|
|
756
|
+
for (const from of left) {
|
|
757
|
+
const bytes = await read(base.get(from));
|
|
758
|
+
if (SimilarityIndex.isBinary(bytes)) continue;
|
|
759
|
+
const index = SimilarityIndex.create(bytes);
|
|
760
|
+
let best: string | undefined;
|
|
761
|
+
let bestScore = DEFAULT_RENAME_SCORE - 1;
|
|
762
|
+
for (const [to, other] of candidates) {
|
|
763
|
+
const score = index.score(other);
|
|
764
|
+
if (score > bestScore) {
|
|
765
|
+
best = to;
|
|
766
|
+
bestScore = score;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
if (best) {
|
|
770
|
+
renames.set(from, best);
|
|
771
|
+
candidates.delete(best);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
return renames;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/** Stores a flat path map as a tree. */
|
|
778
|
+
private async storeFlat(entries: Map<string, CheckedEntry>): Promise<ObjectId> {
|
|
779
|
+
type Node = Map<string, Node | CheckedEntry>;
|
|
780
|
+
const root: Node = new Map();
|
|
781
|
+
for (const [path, entry] of entries) {
|
|
782
|
+
const parts = path.split("/");
|
|
783
|
+
let node = root;
|
|
784
|
+
for (const part of parts.slice(0, -1)) {
|
|
785
|
+
let next = node.get(part);
|
|
786
|
+
if (!(next instanceof Map)) {
|
|
787
|
+
next = new Map();
|
|
788
|
+
node.set(part, next);
|
|
789
|
+
}
|
|
790
|
+
node = next;
|
|
791
|
+
}
|
|
792
|
+
node.set(parts[parts.length - 1], entry);
|
|
793
|
+
}
|
|
794
|
+
const store = async (node: Node): Promise<ObjectId> => {
|
|
795
|
+
const list: { name: string; mode: number; id: ObjectId }[] = [];
|
|
796
|
+
for (const [name, value] of node) {
|
|
797
|
+
if (value instanceof Map) list.push({ name, mode: FileMode.TREE, id: await store(value) });
|
|
798
|
+
else list.push({ name, mode: value.mode, id: value.objectId });
|
|
799
|
+
}
|
|
800
|
+
return this.trees.storeTree(list);
|
|
801
|
+
};
|
|
802
|
+
return store(root);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/** The blobs, links and gitlinks of a tree, by path. */
|
|
806
|
+
private async flatTree(treeId: ObjectId): Promise<Map<string, CheckedEntry>> {
|
|
807
|
+
const out = new Map<string, CheckedEntry>();
|
|
808
|
+
const walk = async (id: ObjectId, prefix: string): Promise<void> => {
|
|
809
|
+
for await (const entry of this.trees.loadTree(id)) {
|
|
810
|
+
const path = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
811
|
+
if (entry.mode === FileMode.TREE) await walk(entry.id, path);
|
|
812
|
+
else out.set(path, { objectId: entry.id, mode: entry.mode });
|
|
813
|
+
}
|
|
814
|
+
};
|
|
815
|
+
await walk(treeId, "");
|
|
816
|
+
return out;
|
|
817
|
+
}
|
|
818
|
+
|
|
314
819
|
/**
|
|
315
820
|
* Plan the restore of the stash's untracked files, if there are any to restore.
|
|
316
821
|
*
|
|
@@ -339,16 +844,42 @@ export class StashApplyCommand extends GitCommand<StashApplyResult> {
|
|
|
339
844
|
throw new RefNotFoundError(untrackedCommitId, "Stash untracked commit not found");
|
|
340
845
|
}
|
|
341
846
|
|
|
342
|
-
const plan = await planTreeRestore(this.trees, this.blobs, untrackedCommit.tree, STASH_APPLY
|
|
847
|
+
const plan = await planTreeRestore(this.trees, this.blobs, untrackedCommit.tree, STASH_APPLY, {
|
|
848
|
+
symlinks: true,
|
|
849
|
+
ignoreCase: coreBool(this.workingCopy, "ignorecase", false),
|
|
850
|
+
windows: worktree.capabilities?.windowsPaths === true,
|
|
851
|
+
reservedNames: worktree.capabilities?.reservedNames,
|
|
852
|
+
});
|
|
343
853
|
|
|
854
|
+
const taken: string[] = [];
|
|
344
855
|
for (const { path } of plan.entries) {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
);
|
|
856
|
+
// a file on a leading component: git cannot create the directory, and restores
|
|
857
|
+
// nothing ("cannot create directory at 'd': File exists")
|
|
858
|
+
const parts = path.split("/");
|
|
859
|
+
for (let k = 1; k < parts.length; k++) {
|
|
860
|
+
const prefix = parts.slice(0, k).join("/");
|
|
861
|
+
const entry = await worktree.getEntry(prefix);
|
|
862
|
+
if (entry && !entry.isDirectory) {
|
|
863
|
+
throw new StashApplyFailedError(
|
|
864
|
+
untrackedCommitId,
|
|
865
|
+
[prefix],
|
|
866
|
+
`fatal: cannot create directory at '${prefix}': File exists\nerror: could not restore untracked files from stash`,
|
|
867
|
+
);
|
|
868
|
+
}
|
|
351
869
|
}
|
|
870
|
+
// taken by a file on disk (untracked or tracked), or one the merge writes: git's
|
|
871
|
+
// `checkout-index --all` refuses each such path
|
|
872
|
+
if (worktreePlan.paths.has(path) || (await worktree.exists(path))) taken.push(path);
|
|
873
|
+
}
|
|
874
|
+
if (taken.length > 0) {
|
|
875
|
+
throw new StashApplyFailedError(
|
|
876
|
+
untrackedCommitId,
|
|
877
|
+
taken,
|
|
878
|
+
[
|
|
879
|
+
...taken.map((p) => `${p} already exists, no checkout`),
|
|
880
|
+
"error: could not restore untracked files from stash",
|
|
881
|
+
].join("\n"),
|
|
882
|
+
);
|
|
352
883
|
}
|
|
353
884
|
|
|
354
885
|
return plan;
|
|
@@ -380,10 +911,14 @@ export class StashApplyCommand extends GitCommand<StashApplyResult> {
|
|
|
380
911
|
return ref.objectId;
|
|
381
912
|
}
|
|
382
913
|
|
|
383
|
-
//
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
914
|
+
// Older entries are in the ref's reflog (stash@{n}), as git reads them
|
|
915
|
+
const reflog = await this.refsStore.getReflog?.(baseRef);
|
|
916
|
+
if (!reflog) {
|
|
917
|
+
throw new RefNotFoundError(refName, `Stash entry ${index} not found (no reflog)`);
|
|
918
|
+
}
|
|
919
|
+
const entry = await reflog.getReverseEntry(index);
|
|
920
|
+
if (!entry) throw new RefNotFoundError(refName, `Stash entry ${index} not found`);
|
|
921
|
+
return entry.newId;
|
|
387
922
|
}
|
|
388
923
|
|
|
389
924
|
// Try as direct ref
|