@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
|
@@ -33,10 +33,11 @@
|
|
|
33
33
|
* ```
|
|
34
34
|
*/
|
|
35
35
|
|
|
36
|
-
import { FileMode, type ObjectId, type Ref } from "@statewalker/vcs-core";
|
|
37
|
-
import { UpdateStagingEntry } from "@statewalker/vcs-working-tree";
|
|
36
|
+
import { FileMode, isValidBranchName, type ObjectId, type Ref } from "@statewalker/vcs-core";
|
|
37
|
+
import { entryStatOf, UpdateStagingEntry, type Worktree } from "@statewalker/vcs-working-tree";
|
|
38
38
|
|
|
39
39
|
import {
|
|
40
|
+
InvalidRefNameError,
|
|
40
41
|
MissingArgumentError,
|
|
41
42
|
NotADirectoryError,
|
|
42
43
|
PathNotFoundInTreeError,
|
|
@@ -44,6 +45,17 @@ import {
|
|
|
44
45
|
RefNotFoundError,
|
|
45
46
|
} from "../errors/index.js";
|
|
46
47
|
import { GitCommand } from "../git-command.js";
|
|
48
|
+
import { coreBool, worktreeModeTrust } from "./core-config.js";
|
|
49
|
+
import { planTreeRestore, writeTreeRestorePlan } from "./tree-restore.js";
|
|
50
|
+
import {
|
|
51
|
+
findInTheWay,
|
|
52
|
+
overwriteMessage,
|
|
53
|
+
pruneEmptyParents,
|
|
54
|
+
worktreeDiffers,
|
|
55
|
+
} from "./worktree-overwrite-checks.js";
|
|
56
|
+
|
|
57
|
+
/** Names a branch checkout in failure messages. */
|
|
58
|
+
const CHECKOUT = "checkout";
|
|
47
59
|
|
|
48
60
|
/**
|
|
49
61
|
* Stage to check out for conflicting files.
|
|
@@ -91,8 +103,26 @@ export interface CheckoutResult {
|
|
|
91
103
|
|
|
92
104
|
/** The ref that was checked out (null for path checkout) */
|
|
93
105
|
readonly ref: Ref | null;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Why a checkout was refused, in git's words (`status` CONFLICTS): local changes or
|
|
109
|
+
* untracked files the switch would overwrite, or an index with unmerged entries.
|
|
110
|
+
*/
|
|
111
|
+
readonly message?: string;
|
|
94
112
|
}
|
|
95
113
|
|
|
114
|
+
/** A tree or index entry, as the two-way merge compares them (index entries with stat). */
|
|
115
|
+
interface Side {
|
|
116
|
+
objectId: ObjectId;
|
|
117
|
+
mode: number;
|
|
118
|
+
size?: number;
|
|
119
|
+
mtime?: number;
|
|
120
|
+
ctime?: number;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const sameSide = (a: Side | undefined, b: Side | undefined) =>
|
|
124
|
+
a === undefined || b === undefined ? a === b : a.objectId === b.objectId && a.mode === b.mode;
|
|
125
|
+
|
|
96
126
|
/**
|
|
97
127
|
* Command to checkout branches, commits, or paths.
|
|
98
128
|
*
|
|
@@ -261,13 +291,12 @@ export class CheckoutCommand extends GitCommand<CheckoutResult> {
|
|
|
261
291
|
this.checkCallable();
|
|
262
292
|
this.setCallable(false);
|
|
263
293
|
|
|
264
|
-
//
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
return this.checkoutBranch();
|
|
294
|
+
// path checkout, or branch checkout; one worktree batch, so the checks and the writes
|
|
295
|
+
// share one reading of the ignore rules
|
|
296
|
+
const run = () =>
|
|
297
|
+
this.allPaths || this.paths.length > 0 ? this.checkoutPaths() : this.checkoutBranch();
|
|
298
|
+
const worktree = this.worktreeAccess;
|
|
299
|
+
return worktree?.batch ? worktree.batch(run) : run();
|
|
271
300
|
}
|
|
272
301
|
|
|
273
302
|
/**
|
|
@@ -314,50 +343,10 @@ export class CheckoutCommand extends GitCommand<CheckoutResult> {
|
|
|
314
343
|
};
|
|
315
344
|
}
|
|
316
345
|
|
|
317
|
-
/**
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
* Git prevents checkout when staged changes differ from HEAD,
|
|
322
|
-
* as they would be lost during checkout.
|
|
323
|
-
*
|
|
324
|
-
* @param _targetTreeId Target tree to checkout (unused for now, checks HEAD vs staging)
|
|
325
|
-
* @returns Array of conflicting paths
|
|
326
|
-
*/
|
|
327
|
-
private async detectCheckoutConflicts(_targetTreeId?: ObjectId): Promise<string[]> {
|
|
328
|
-
const conflicts: string[] = [];
|
|
329
|
-
|
|
330
|
-
// Get current HEAD tree for comparison
|
|
331
|
-
const headId = await this.resolveCommitId("HEAD");
|
|
332
|
-
if (!headId) {
|
|
333
|
-
// No HEAD commit yet (empty repository) - no conflicts possible
|
|
334
|
-
return conflicts;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
const headTreeId = await this.commits.getTree(headId);
|
|
338
|
-
if (!headTreeId) {
|
|
339
|
-
return conflicts;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// Collect HEAD tree entries
|
|
343
|
-
const headEntries = new Map<string, { objectId: ObjectId; mode: number }>();
|
|
344
|
-
await this.collectTreeEntries(headTreeId, "", headEntries);
|
|
345
|
-
|
|
346
|
-
// Compare staging with HEAD
|
|
347
|
-
for await (const entry of this.staging.entries()) {
|
|
348
|
-
// Skip conflict stages (1-3) - only check stage 0
|
|
349
|
-
if (entry.stage !== 0) continue;
|
|
350
|
-
|
|
351
|
-
const headEntry = headEntries.get(entry.path);
|
|
352
|
-
|
|
353
|
-
// Check if staged content differs from HEAD
|
|
354
|
-
if (!headEntry || headEntry.objectId !== entry.objectId) {
|
|
355
|
-
// Staged change differs from HEAD - this is an uncommitted change
|
|
356
|
-
conflicts.push(entry.path);
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
return conflicts;
|
|
346
|
+
/** Whether an untracked (not ignored) file or link is at `path`. */
|
|
347
|
+
private async untrackedFileAt(worktree: Worktree, path: string): Promise<boolean> {
|
|
348
|
+
const entry = await worktree.getEntry(path);
|
|
349
|
+
return !!entry && !entry.isDirectory && !(await worktree.isIgnored(path));
|
|
361
350
|
}
|
|
362
351
|
|
|
363
352
|
/**
|
|
@@ -370,6 +359,9 @@ export class CheckoutCommand extends GitCommand<CheckoutResult> {
|
|
|
370
359
|
|
|
371
360
|
// Create branch if requested
|
|
372
361
|
if (this.createBranch) {
|
|
362
|
+
if (!isValidBranchName(this.name)) {
|
|
363
|
+
throw new InvalidRefNameError(this.name);
|
|
364
|
+
}
|
|
373
365
|
const startPointId = this.startPoint
|
|
374
366
|
? await this.resolveCommitId(this.startPoint)
|
|
375
367
|
: await this.resolveCommitId("HEAD");
|
|
@@ -390,18 +382,147 @@ export class CheckoutCommand extends GitCommand<CheckoutResult> {
|
|
|
390
382
|
// Get target tree for conflict detection and checkout
|
|
391
383
|
const targetTreeId = targetId ? await this.commits.getTree(targetId) : undefined;
|
|
392
384
|
|
|
393
|
-
//
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
385
|
+
// git's two-way merge (unpack-trees `twoway_merge`, checkout without -f): a path HEAD
|
|
386
|
+
// and the target agree on keeps its index entry and worktree file as they are (local
|
|
387
|
+
// edits survive the switch); a path they differ on is switched only when the index and
|
|
388
|
+
// the worktree still hold HEAD's version, else the checkout is refused, as are
|
|
389
|
+
// untracked files in the way and an index with unmerged entries. With `setForce`, as
|
|
390
|
+
// `git checkout -f`, the target's tree replaces index and worktree whatever they hold.
|
|
391
|
+
const worktree = this.worktreeAccess;
|
|
392
|
+
const headId = await this.resolveCommitId("HEAD");
|
|
393
|
+
const headTreeId = headId ? await this.commits.getTree(headId) : undefined;
|
|
394
|
+
const head = new Map<string, Side>();
|
|
395
|
+
const target = new Map<string, Side>();
|
|
396
|
+
if (headTreeId) await this.collectTreeEntries(headTreeId, "", head);
|
|
397
|
+
if (targetTreeId) await this.collectTreeEntries(targetTreeId, "", target);
|
|
398
|
+
const index = new Map<string, Side>();
|
|
399
|
+
const unmerged = new Set<string>();
|
|
400
|
+
for await (const entry of this.staging.entries()) {
|
|
401
|
+
if (entry.stage === 0) index.set(entry.path, entry);
|
|
402
|
+
else unmerged.add(entry.path);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (!this.force && !this.orphan && unmerged.size > 0) {
|
|
406
|
+
return {
|
|
407
|
+
status: CheckoutStatus.CONFLICTS,
|
|
408
|
+
updated: [],
|
|
409
|
+
removed: [],
|
|
410
|
+
conflicts: [...unmerged].sort(),
|
|
411
|
+
ref: null,
|
|
412
|
+
message: "error: you need to resolve your current index first",
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// the paths the switch changes, and those it must refuse to touch
|
|
417
|
+
const switched: string[] = [];
|
|
418
|
+
const local: string[] = [];
|
|
419
|
+
const untracked: string[] = [];
|
|
420
|
+
const untrackedRemoved: string[] = [];
|
|
421
|
+
const lostDirs: string[] = [];
|
|
422
|
+
const trust = worktree ? worktreeModeTrust(this.workingCopy, worktree) : undefined;
|
|
423
|
+
const ignoreCase = coreBool(this.workingCopy, "ignorecase", false);
|
|
424
|
+
const paths = [...new Set([...head.keys(), ...target.keys()])].sort();
|
|
425
|
+
// (an orphan branch keeps index and worktree as they are, as git's --orphan)
|
|
426
|
+
for (const path of this.orphan ? [] : paths) {
|
|
427
|
+
const h = head.get(path);
|
|
428
|
+
const t = target.get(path);
|
|
429
|
+
if (!this.force && sameSide(h, t)) continue;
|
|
430
|
+
const i = index.get(path);
|
|
431
|
+
if (this.force) {
|
|
432
|
+
if (!sameSide(i, t) || t) switched.push(path);
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
if (sameSide(i, t)) {
|
|
436
|
+
// kept; but a staged deletion the target also makes leaves the file untracked in
|
|
437
|
+
// the worktree, and the switch would remove it (git refuses)
|
|
438
|
+
if (h && !i && !t && worktree && (await this.untrackedFileAt(worktree, path))) {
|
|
439
|
+
untrackedRemoved.push(path);
|
|
440
|
+
}
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
if (!sameSide(i, h)) {
|
|
444
|
+
// staged changes (or a staged deletion) the switch would lose
|
|
445
|
+
if (i !== undefined || t !== undefined) local.push(path);
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (i !== undefined && worktree && (await worktreeDiffers(worktree, path, i, trust))) {
|
|
449
|
+
local.push(path);
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
switched.push(path);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// what stands where the switch writes (git's verify_absent, check_leading_path and
|
|
456
|
+
// verify_clean_subdirectory): untracked files or links on the path or a leading
|
|
457
|
+
// component, untracked files in a directory a file replaces
|
|
458
|
+
if (worktree && !this.force) {
|
|
459
|
+
const found = await findInTheWay(worktree, {
|
|
460
|
+
writes: switched.filter((p) => target.has(p)),
|
|
461
|
+
removes: new Set(switched.filter((p) => !target.has(p))),
|
|
462
|
+
index,
|
|
463
|
+
ignoreCase,
|
|
464
|
+
});
|
|
465
|
+
untracked.push(...found.untracked);
|
|
466
|
+
lostDirs.push(...found.lostDirs);
|
|
467
|
+
}
|
|
468
|
+
if (local.length + untracked.length + untrackedRemoved.length + lostDirs.length > 0) {
|
|
469
|
+
const message = overwriteMessage("checkout", {
|
|
470
|
+
local,
|
|
471
|
+
lostDirs,
|
|
472
|
+
untracked,
|
|
473
|
+
untrackedRemoved,
|
|
474
|
+
});
|
|
475
|
+
return {
|
|
476
|
+
status: CheckoutStatus.CONFLICTS,
|
|
477
|
+
updated: [],
|
|
478
|
+
removed: [],
|
|
479
|
+
conflicts: [...local, ...lostDirs, ...untracked, ...untrackedRemoved],
|
|
480
|
+
ref: null,
|
|
481
|
+
message,
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// The worktree first, as git: the target tree is planned (paths verified, entries
|
|
486
|
+
// checked not to collide) before anything is written; the switched paths the target
|
|
487
|
+
// drops are removed, then its switched files, links and submodule directories written.
|
|
488
|
+
const updated: string[] = [];
|
|
489
|
+
const removed: string[] = [];
|
|
490
|
+
const switching = new Set(switched);
|
|
491
|
+
if (targetTreeId && worktree) {
|
|
492
|
+
const plan = await planTreeRestore(this.trees, this.blobs, targetTreeId, CHECKOUT, {
|
|
493
|
+
symlinks: true,
|
|
494
|
+
ignoreCase: coreBool(this.workingCopy, "ignorecase", false),
|
|
495
|
+
windows: worktree.capabilities?.windowsPaths === true,
|
|
496
|
+
reservedNames: worktree.capabilities?.reservedNames,
|
|
497
|
+
});
|
|
498
|
+
for (const path of switched) {
|
|
499
|
+
if (target.has(path)) continue;
|
|
500
|
+
await worktree.remove(path);
|
|
501
|
+
await pruneEmptyParents(worktree, path);
|
|
502
|
+
removed.push(path);
|
|
503
|
+
}
|
|
504
|
+
// a forced checkout also drops tracked files no tree holds any more
|
|
505
|
+
if (this.force) {
|
|
506
|
+
for (const path of index.keys()) {
|
|
507
|
+
if (!target.has(path) && !switching.has(path)) {
|
|
508
|
+
await worktree.remove(path);
|
|
509
|
+
removed.push(path);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
404
512
|
}
|
|
513
|
+
const only = {
|
|
514
|
+
...plan,
|
|
515
|
+
entries: plan.entries.filter((e) => switching.has(e.path)),
|
|
516
|
+
gitlinks: plan.gitlinks.filter((e) => switching.has(e.path)),
|
|
517
|
+
};
|
|
518
|
+
// everything in the way was checked above (or the checkout is forced): a file or link
|
|
519
|
+
// where a directory goes, a clean directory where a file goes, are replaced
|
|
520
|
+
await writeTreeRestorePlan(worktree, this.blobs, only, CHECKOUT, {
|
|
521
|
+
symlinks: worktreeModeTrust(this.workingCopy, worktree).symlinks,
|
|
522
|
+
gitlinks: true,
|
|
523
|
+
replaceInTheWay: true,
|
|
524
|
+
});
|
|
525
|
+
updated.push(...only.entries.map((e) => e.path));
|
|
405
526
|
}
|
|
406
527
|
|
|
407
528
|
// Determine if we should use symbolic ref (branch) or detached HEAD (commit/tag)
|
|
@@ -421,21 +542,29 @@ export class CheckoutCommand extends GitCommand<CheckoutResult> {
|
|
|
421
542
|
await this.refsStore.set("HEAD", targetId);
|
|
422
543
|
}
|
|
423
544
|
|
|
424
|
-
//
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
if (targetTreeId) {
|
|
429
|
-
// Use this.trees which is CommandTrees (has loadTree method for TreeStore compatibility)
|
|
545
|
+
// The index: forced, the target's tree; else the switched paths take the target's
|
|
546
|
+
// entries (with the stat of the files just written), every other path keeps its own
|
|
547
|
+
if (targetTreeId && this.force) {
|
|
430
548
|
await this.staging.readTree(this.trees as any, targetTreeId);
|
|
431
|
-
|
|
432
|
-
// Persist staging to .git/index so native git sees the correct state
|
|
433
549
|
await this.staging.write();
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
550
|
+
} else if (switched.length > 0) {
|
|
551
|
+
const editor = this.staging.createEditor();
|
|
552
|
+
for (const path of switched) {
|
|
553
|
+
const t = target.get(path);
|
|
554
|
+
if (!t) {
|
|
555
|
+
editor.remove(path);
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
const stats = worktree ? await worktree.getEntry(path) : undefined;
|
|
559
|
+
editor.upsert({
|
|
560
|
+
path,
|
|
561
|
+
objectId: t.objectId,
|
|
562
|
+
mode: t.mode,
|
|
563
|
+
...entryStatOf(stats ?? {}),
|
|
564
|
+
});
|
|
438
565
|
}
|
|
566
|
+
await editor.finish();
|
|
567
|
+
await this.staging.write();
|
|
439
568
|
}
|
|
440
569
|
|
|
441
570
|
// Get the ref we checked out
|
|
@@ -450,57 +579,6 @@ export class CheckoutCommand extends GitCommand<CheckoutResult> {
|
|
|
450
579
|
};
|
|
451
580
|
}
|
|
452
581
|
|
|
453
|
-
/**
|
|
454
|
-
* Check if the store supports file writes (not a bare repository).
|
|
455
|
-
*
|
|
456
|
-
* A repository is considered "bare" if it doesn't have a worktree interface.
|
|
457
|
-
*/
|
|
458
|
-
private hasFileWriteSupport(): boolean {
|
|
459
|
-
return !!this.worktreeAccess;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
/**
|
|
463
|
-
* Write all files from a tree to the working directory.
|
|
464
|
-
*
|
|
465
|
-
* @param treeId The tree ID to checkout
|
|
466
|
-
* @param basePath The base path prefix for entries in this tree
|
|
467
|
-
*/
|
|
468
|
-
private async writeTreeToWorkdir(treeId: ObjectId, basePath: string): Promise<void> {
|
|
469
|
-
const worktree = this.worktreeAccess;
|
|
470
|
-
if (!worktree) {
|
|
471
|
-
throw new Error("Worktree not available for file writes");
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
for await (const entry of this.trees.loadTree(treeId)) {
|
|
475
|
-
const relativePath = basePath ? `${basePath}/${entry.name}` : entry.name;
|
|
476
|
-
const isDirectory = entry.mode === FileMode.TREE;
|
|
477
|
-
|
|
478
|
-
if (isDirectory) {
|
|
479
|
-
// Recursively process subdirectories
|
|
480
|
-
await this.writeTreeToWorkdir(entry.id, relativePath);
|
|
481
|
-
} else {
|
|
482
|
-
// Ensure parent directory exists
|
|
483
|
-
const lastSlash = relativePath.lastIndexOf("/");
|
|
484
|
-
if (lastSlash > 0) {
|
|
485
|
-
const parentDir = relativePath.substring(0, lastSlash);
|
|
486
|
-
await worktree.mkdir(parentDir);
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
// Load blob content
|
|
490
|
-
const chunks: Uint8Array[] = [];
|
|
491
|
-
const blobContent = await this.blobs.load(entry.id);
|
|
492
|
-
if (blobContent) {
|
|
493
|
-
for await (const chunk of blobContent) {
|
|
494
|
-
chunks.push(chunk);
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
// Write file using worktree interface
|
|
499
|
-
await worktree.writeContent(relativePath, chunks);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
|
|
504
582
|
/**
|
|
505
583
|
* Checkout a path from the staging index.
|
|
506
584
|
*/
|