@statewalker/vcs-commands 0.4.0 → 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/dist/commands/add-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 +62 -7
- 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 +96 -14
- 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/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/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-BpeXZTC3.js → commands-CsLNyvZ1.js} +2940 -631
- package/dist/core-commands/add.command.impl.d.ts.map +1 -1
- package/dist/core-commands/index.d.ts +0 -3
- package/dist/core-commands/index.d.ts.map +1 -1
- 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 +18 -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/fetch-result.d.ts +9 -1
- package/dist/results/fetch-result.d.ts.map +1 -1
- package/dist/results/index.js +2 -2
- package/dist/results/status-result.d.ts +91 -20
- package/dist/results/status-result.d.ts.map +1 -1
- package/dist/{status-result-BapZoQaR.js → status-result-BpyUK1Sg.js} +113 -5
- package/dist/transport-command.d.ts +12 -0
- package/dist/transport-command.d.ts.map +1 -1
- package/package.json +11 -10
- package/src/commands/add-command.ts +9 -7
- package/src/commands/checkout-command.ts +200 -118
- package/src/commands/clean-command.ts +187 -88
- package/src/commands/clone-command.ts +244 -40
- package/src/commands/core-config.ts +41 -0
- package/src/commands/fetch-command.ts +476 -57
- package/src/commands/index.ts +1 -0
- package/src/commands/init-command.ts +32 -7
- 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/tree-restore.ts +142 -13
- package/src/commands/worktree-overwrite-checks.ts +215 -0
- package/src/core-commands/add.command.impl.ts +2 -4
- package/src/core-commands/index.ts +0 -8
- 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 +84 -8
- package/src/remote-config/remote-refs.ts +103 -0
- package/src/results/clone-result.ts +6 -0
- package/src/results/fetch-result.ts +9 -1
- 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 -110
- package/dist/core-commands/checkout.command.impl.d.ts.map +0 -1
- package/src/core-commands/checkout.command.impl.ts +0 -564
- package/src/core-commands/checkout.command.ts +0 -125
|
@@ -1,564 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CheckoutCommand - Materializes tree contents to filesystem.
|
|
3
|
-
*
|
|
4
|
-
* Implements the Checkout interface to:
|
|
5
|
-
* - Checkout branches, tags, or commits
|
|
6
|
-
* - Checkout specific paths from index or commits
|
|
7
|
-
* - Detect and report conflicts
|
|
8
|
-
* - Update HEAD and index after checkout
|
|
9
|
-
*
|
|
10
|
-
* Reference: jgit/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import {
|
|
14
|
-
assertValidWorktreePath,
|
|
15
|
-
type Blobs,
|
|
16
|
-
type Commits,
|
|
17
|
-
FileMode,
|
|
18
|
-
type FilesApi,
|
|
19
|
-
isValidBranchName,
|
|
20
|
-
joinPath,
|
|
21
|
-
type ObjectId,
|
|
22
|
-
type Refs,
|
|
23
|
-
type TreeEntry,
|
|
24
|
-
type Trees,
|
|
25
|
-
} from "@statewalker/vcs-core";
|
|
26
|
-
import type { Staging } from "@statewalker/vcs-working-tree";
|
|
27
|
-
import { InvalidRefNameError } from "../errors/index.js";
|
|
28
|
-
import type { Checkout, CheckoutOptions, CheckoutResult } from "./checkout.command.js";
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Flattened tree entry with full path.
|
|
32
|
-
*/
|
|
33
|
-
interface FlatTreeEntry extends TreeEntry {
|
|
34
|
-
/** Full path from repository root */
|
|
35
|
-
path: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Options for creating a CheckoutCommand.
|
|
40
|
-
*/
|
|
41
|
-
export interface CheckoutCommandOptions {
|
|
42
|
-
/** FilesApi for filesystem operations */
|
|
43
|
-
files: FilesApi;
|
|
44
|
-
|
|
45
|
-
/** Working tree root path */
|
|
46
|
-
workTreeRoot: string;
|
|
47
|
-
|
|
48
|
-
/** Blob storage for loading file content */
|
|
49
|
-
blobs: Blobs;
|
|
50
|
-
|
|
51
|
-
/** Tree storage for loading tree entries */
|
|
52
|
-
trees: Trees;
|
|
53
|
-
|
|
54
|
-
/** Commit storage for resolving commits */
|
|
55
|
-
commits: Commits;
|
|
56
|
-
|
|
57
|
-
/** Reference storage for resolving refs and updating HEAD */
|
|
58
|
-
refs: Refs;
|
|
59
|
-
|
|
60
|
-
/** Staging area (index) for updating after checkout */
|
|
61
|
-
staging: Staging;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* CheckoutCommand implementation.
|
|
66
|
-
*/
|
|
67
|
-
export class CheckoutCommand implements Checkout {
|
|
68
|
-
private readonly files: FilesApi;
|
|
69
|
-
private readonly workTreeRoot: string;
|
|
70
|
-
private readonly blobs: Blobs;
|
|
71
|
-
private readonly trees: Trees;
|
|
72
|
-
private readonly commits: Commits;
|
|
73
|
-
private readonly refs: Refs;
|
|
74
|
-
private readonly staging: Staging;
|
|
75
|
-
|
|
76
|
-
constructor(options: CheckoutCommandOptions) {
|
|
77
|
-
this.files = options.files;
|
|
78
|
-
this.workTreeRoot = options.workTreeRoot;
|
|
79
|
-
this.blobs = options.blobs;
|
|
80
|
-
this.trees = options.trees;
|
|
81
|
-
this.commits = options.commits;
|
|
82
|
-
this.refs = options.refs;
|
|
83
|
-
this.staging = options.staging;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Checkout a branch, tag, or commit.
|
|
88
|
-
*/
|
|
89
|
-
async checkout(target: string, options: CheckoutOptions = {}): Promise<CheckoutResult> {
|
|
90
|
-
if (options.createBranch !== undefined && !isValidBranchName(options.createBranch)) {
|
|
91
|
-
throw new InvalidRefNameError(options.createBranch);
|
|
92
|
-
}
|
|
93
|
-
// Resolve target to commit
|
|
94
|
-
const commitId = await this.resolveTarget(target);
|
|
95
|
-
if (!commitId) {
|
|
96
|
-
throw new Error(`Cannot resolve '${target}' to a commit`);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Get target tree
|
|
100
|
-
const treeId = await this.commits.getTree(commitId);
|
|
101
|
-
if (!treeId) {
|
|
102
|
-
throw new Error(`Cannot get tree for commit ${commitId}`);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Check for conflicts unless force
|
|
106
|
-
if (!options.force) {
|
|
107
|
-
const conflicts = await this.detectConflicts(treeId);
|
|
108
|
-
if (conflicts.length > 0) {
|
|
109
|
-
return {
|
|
110
|
-
updated: [],
|
|
111
|
-
added: [],
|
|
112
|
-
removed: [],
|
|
113
|
-
conflicts,
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Perform checkout
|
|
119
|
-
const result = await this.checkoutTree(treeId, options);
|
|
120
|
-
|
|
121
|
-
// Update HEAD
|
|
122
|
-
if (await this.isLocalBranch(target)) {
|
|
123
|
-
await this.refs.setSymbolic("HEAD", `refs/heads/${target}`);
|
|
124
|
-
result.newBranch = target;
|
|
125
|
-
} else {
|
|
126
|
-
await this.refs.set("HEAD", commitId);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Create new branch if requested
|
|
130
|
-
if (options.createBranch) {
|
|
131
|
-
await this.refs.set(`refs/heads/${options.createBranch}`, commitId);
|
|
132
|
-
await this.refs.setSymbolic("HEAD", `refs/heads/${options.createBranch}`);
|
|
133
|
-
result.newBranch = options.createBranch;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
result.newHead = commitId;
|
|
137
|
-
return result;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Checkout specific paths from index or commit.
|
|
142
|
-
*/
|
|
143
|
-
async checkoutPaths(paths: string[], options: CheckoutOptions = {}): Promise<CheckoutResult> {
|
|
144
|
-
const source = options.source ?? "index";
|
|
145
|
-
|
|
146
|
-
const result: CheckoutResult = {
|
|
147
|
-
updated: [],
|
|
148
|
-
added: [],
|
|
149
|
-
removed: [],
|
|
150
|
-
conflicts: [],
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
for (const path of paths) {
|
|
154
|
-
try {
|
|
155
|
-
if (source === "index") {
|
|
156
|
-
await this.checkoutFromIndex(path);
|
|
157
|
-
} else {
|
|
158
|
-
const treeId =
|
|
159
|
-
source === "head" ? await this.getHeadTreeId() : await this.commits.getTree(source);
|
|
160
|
-
if (treeId) {
|
|
161
|
-
await this.checkoutFromTree(path, treeId);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
result.updated.push(path);
|
|
165
|
-
} catch {
|
|
166
|
-
result.conflicts.push(path);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return result;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Checkout a path from the index.
|
|
175
|
-
*/
|
|
176
|
-
private async checkoutFromIndex(path: string): Promise<void> {
|
|
177
|
-
const entry = await this.staging.getEntry(path);
|
|
178
|
-
if (!entry) {
|
|
179
|
-
throw new Error(`Path not in index: ${path}`);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
await this.writeFileContent(path, entry.objectId, entry.mode);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Checkout a path from a tree.
|
|
187
|
-
*/
|
|
188
|
-
private async checkoutFromTree(path: string, treeId: ObjectId): Promise<void> {
|
|
189
|
-
// Find the entry in the tree
|
|
190
|
-
const parts = path.split("/");
|
|
191
|
-
let currentTreeId = treeId;
|
|
192
|
-
let entry: TreeEntry | undefined;
|
|
193
|
-
|
|
194
|
-
for (let i = 0; i < parts.length; i++) {
|
|
195
|
-
const name = parts[i];
|
|
196
|
-
entry = await this.trees.getEntry(currentTreeId, name);
|
|
197
|
-
|
|
198
|
-
if (!entry) {
|
|
199
|
-
throw new Error(`Path not found in tree: ${path}`);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (i < parts.length - 1) {
|
|
203
|
-
// Navigate into subtree
|
|
204
|
-
if (entry.mode !== FileMode.TREE) {
|
|
205
|
-
throw new Error(`Not a directory: ${parts.slice(0, i + 1).join("/")}`);
|
|
206
|
-
}
|
|
207
|
-
currentTreeId = entry.id;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (!entry) {
|
|
212
|
-
throw new Error(`Path not found in tree: ${path}`);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
await this.writeFileContent(path, entry.id, entry.mode);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Checkout an entire tree to the working directory.
|
|
220
|
-
*/
|
|
221
|
-
private async checkoutTree(treeId: ObjectId, options: CheckoutOptions): Promise<CheckoutResult> {
|
|
222
|
-
const result: CheckoutResult = {
|
|
223
|
-
updated: [],
|
|
224
|
-
added: [],
|
|
225
|
-
removed: [],
|
|
226
|
-
conflicts: [],
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
// Build current index map
|
|
230
|
-
const currentIndex = new Map<string, { objectId: ObjectId; mode: number }>();
|
|
231
|
-
for await (const entry of this.staging.entries()) {
|
|
232
|
-
if (entry.stage === 0) {
|
|
233
|
-
// Only stage 0 entries
|
|
234
|
-
currentIndex.set(entry.path, { objectId: entry.objectId, mode: entry.mode });
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Build target tree map
|
|
239
|
-
const targetTree = await this.flattenTree(treeId, "");
|
|
240
|
-
|
|
241
|
-
// Calculate operations
|
|
242
|
-
const toRemove = new Set<string>();
|
|
243
|
-
const toUpdate = new Map<string, FlatTreeEntry>();
|
|
244
|
-
|
|
245
|
-
// Find files to remove (in current but not in target)
|
|
246
|
-
for (const path of currentIndex.keys()) {
|
|
247
|
-
if (!targetTree.has(path)) {
|
|
248
|
-
toRemove.add(path);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// Find files to add/update
|
|
253
|
-
for (const [path, entry] of targetTree) {
|
|
254
|
-
const current = currentIndex.get(path);
|
|
255
|
-
if (!current || current.objectId !== entry.id) {
|
|
256
|
-
toUpdate.set(path, entry);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// Execute removals
|
|
261
|
-
for (const path of toRemove) {
|
|
262
|
-
try {
|
|
263
|
-
await this.removeFile(path);
|
|
264
|
-
result.removed.push(path);
|
|
265
|
-
} catch {
|
|
266
|
-
result.conflicts.push(path);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// Execute updates
|
|
271
|
-
let processed = 0;
|
|
272
|
-
const total = toUpdate.size;
|
|
273
|
-
|
|
274
|
-
for (const [path, entry] of toUpdate) {
|
|
275
|
-
try {
|
|
276
|
-
await this.writeFileContent(path, entry.id, entry.mode);
|
|
277
|
-
|
|
278
|
-
if (currentIndex.has(path)) {
|
|
279
|
-
result.updated.push(path);
|
|
280
|
-
} else {
|
|
281
|
-
result.added.push(path);
|
|
282
|
-
}
|
|
283
|
-
} catch {
|
|
284
|
-
result.conflicts.push(path);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
processed++;
|
|
288
|
-
options.onProgress?.(processed, total, path);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// Update index from target tree
|
|
292
|
-
await this.rebuildIndex(targetTree);
|
|
293
|
-
|
|
294
|
-
return result;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* Write file content to working tree.
|
|
299
|
-
*/
|
|
300
|
-
private async writeFileContent(path: string, objectId: ObjectId, _mode: number): Promise<void> {
|
|
301
|
-
// Tree and index paths may come from a crafted repository: never write into `.git`.
|
|
302
|
-
assertValidWorktreePath(path);
|
|
303
|
-
const absolutePath = joinPath(this.workTreeRoot, path);
|
|
304
|
-
|
|
305
|
-
// Ensure parent directory exists
|
|
306
|
-
const lastSlash = path.lastIndexOf("/");
|
|
307
|
-
if (lastSlash > 0) {
|
|
308
|
-
const parentDir = joinPath(this.workTreeRoot, path.substring(0, lastSlash));
|
|
309
|
-
await this.files.mkdir(parentDir);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
// Stream the blob straight into the file
|
|
313
|
-
const content = await this.blobs.load(objectId);
|
|
314
|
-
if (!content) {
|
|
315
|
-
throw new Error(`Blob ${objectId} not found`);
|
|
316
|
-
}
|
|
317
|
-
await this.files.write(absolutePath, content);
|
|
318
|
-
|
|
319
|
-
// Note: FilesApi doesn't support chmod, so executable bit is not set
|
|
320
|
-
// This could be extended with FilesApiAdapter if needed
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Remove file from working tree.
|
|
325
|
-
*/
|
|
326
|
-
private async removeFile(path: string): Promise<void> {
|
|
327
|
-
assertValidWorktreePath(path);
|
|
328
|
-
const absolutePath = joinPath(this.workTreeRoot, path);
|
|
329
|
-
await this.files.remove(absolutePath);
|
|
330
|
-
|
|
331
|
-
// Try to remove empty parent directories
|
|
332
|
-
await this.cleanEmptyParents(path);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* Remove empty parent directories.
|
|
337
|
-
*/
|
|
338
|
-
private async cleanEmptyParents(path: string): Promise<void> {
|
|
339
|
-
const parts = path.split("/");
|
|
340
|
-
parts.pop(); // Remove filename
|
|
341
|
-
|
|
342
|
-
while (parts.length > 0) {
|
|
343
|
-
const dirPath = joinPath(this.workTreeRoot, parts.join("/"));
|
|
344
|
-
|
|
345
|
-
try {
|
|
346
|
-
let isEmpty = true;
|
|
347
|
-
for await (const _ of this.files.list(dirPath)) {
|
|
348
|
-
isEmpty = false;
|
|
349
|
-
break;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
if (isEmpty) {
|
|
353
|
-
await this.files.remove(dirPath);
|
|
354
|
-
parts.pop();
|
|
355
|
-
} else {
|
|
356
|
-
break;
|
|
357
|
-
}
|
|
358
|
-
} catch {
|
|
359
|
-
break;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* Flatten a tree into a map of path -> entry.
|
|
366
|
-
*/
|
|
367
|
-
private async flattenTree(treeId: ObjectId, prefix: string): Promise<Map<string, FlatTreeEntry>> {
|
|
368
|
-
const map = new Map<string, FlatTreeEntry>();
|
|
369
|
-
const entries = await this.trees.load(treeId);
|
|
370
|
-
if (!entries) {
|
|
371
|
-
return map;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
for await (const entry of entries) {
|
|
375
|
-
const path = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
376
|
-
|
|
377
|
-
if (entry.mode === FileMode.TREE) {
|
|
378
|
-
// Recurse into subtree
|
|
379
|
-
const subtree = await this.flattenTree(entry.id, path);
|
|
380
|
-
for (const [subPath, subEntry] of subtree) {
|
|
381
|
-
map.set(subPath, subEntry);
|
|
382
|
-
}
|
|
383
|
-
} else {
|
|
384
|
-
map.set(path, { ...entry, path });
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
return map;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* Resolve target to commit ID.
|
|
393
|
-
*/
|
|
394
|
-
private async resolveTarget(target: string): Promise<ObjectId | undefined> {
|
|
395
|
-
// Try as branch first
|
|
396
|
-
let ref = await this.refs.resolve(`refs/heads/${target}`);
|
|
397
|
-
if (ref?.objectId) {
|
|
398
|
-
return ref.objectId;
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
// Try as tag
|
|
402
|
-
ref = await this.refs.resolve(`refs/tags/${target}`);
|
|
403
|
-
if (ref?.objectId) {
|
|
404
|
-
return ref.objectId;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// Try as direct ref
|
|
408
|
-
ref = await this.refs.resolve(target);
|
|
409
|
-
if (ref?.objectId) {
|
|
410
|
-
return ref.objectId;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
// Try as commit ID
|
|
414
|
-
if (await this.commits.has(target)) {
|
|
415
|
-
return target;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
return undefined;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
/**
|
|
422
|
-
* Check if name is a local branch.
|
|
423
|
-
*/
|
|
424
|
-
private async isLocalBranch(name: string): Promise<boolean> {
|
|
425
|
-
return this.refs.has(`refs/heads/${name}`);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* Get HEAD's tree ID.
|
|
430
|
-
*/
|
|
431
|
-
private async getHeadTreeId(): Promise<ObjectId | undefined> {
|
|
432
|
-
const headRef = await this.refs.resolve("HEAD");
|
|
433
|
-
if (headRef?.objectId) {
|
|
434
|
-
return this.commits.getTree(headRef.objectId);
|
|
435
|
-
}
|
|
436
|
-
return undefined;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* Detect conflicts - files that would be overwritten.
|
|
441
|
-
*/
|
|
442
|
-
private async detectConflicts(targetTreeId: ObjectId): Promise<string[]> {
|
|
443
|
-
const conflicts: string[] = [];
|
|
444
|
-
|
|
445
|
-
// Get current HEAD tree
|
|
446
|
-
const headTreeId = await this.getHeadTreeId();
|
|
447
|
-
const headTree = headTreeId
|
|
448
|
-
? await this.flattenTree(headTreeId, "")
|
|
449
|
-
: new Map<string, FlatTreeEntry>();
|
|
450
|
-
|
|
451
|
-
// Get current index
|
|
452
|
-
const indexEntries = new Map<string, { objectId: ObjectId; size: number; mtime: number }>();
|
|
453
|
-
for await (const entry of this.staging.entries()) {
|
|
454
|
-
if (entry.stage === 0) {
|
|
455
|
-
indexEntries.set(entry.path, {
|
|
456
|
-
objectId: entry.objectId,
|
|
457
|
-
size: entry.size,
|
|
458
|
-
mtime: entry.mtime,
|
|
459
|
-
});
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
// Get target tree
|
|
464
|
-
const targetTree = await this.flattenTree(targetTreeId, "");
|
|
465
|
-
|
|
466
|
-
// Check each path that differs between HEAD and target
|
|
467
|
-
for (const [path, targetEntry] of targetTree) {
|
|
468
|
-
const headEntry = headTree.get(path);
|
|
469
|
-
const indexEntry = indexEntries.get(path);
|
|
470
|
-
|
|
471
|
-
// If path is same in HEAD and target, no conflict possible
|
|
472
|
-
if (headEntry && headEntry.id === targetEntry.id) {
|
|
473
|
-
continue;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
// If index differs from HEAD, user has staged changes
|
|
477
|
-
if (indexEntry && headEntry && indexEntry.objectId !== headEntry.id) {
|
|
478
|
-
conflicts.push(path);
|
|
479
|
-
continue;
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
// Check working tree for modifications
|
|
483
|
-
const fullPath = joinPath(this.workTreeRoot, path);
|
|
484
|
-
try {
|
|
485
|
-
const stats = await this.files.stats(fullPath);
|
|
486
|
-
if (stats && indexEntry) {
|
|
487
|
-
// If size or mtime differs, might be modified
|
|
488
|
-
if (stats.size !== indexEntry.size || (stats.lastModified ?? 0) > indexEntry.mtime) {
|
|
489
|
-
conflicts.push(path);
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
} catch {
|
|
493
|
-
// File doesn't exist, no conflict
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
// Check for files that would be removed but have local changes
|
|
498
|
-
for (const [path] of headTree) {
|
|
499
|
-
if (!targetTree.has(path)) {
|
|
500
|
-
const indexEntry = indexEntries.get(path);
|
|
501
|
-
const fullPath = joinPath(this.workTreeRoot, path);
|
|
502
|
-
|
|
503
|
-
try {
|
|
504
|
-
const stats = await this.files.stats(fullPath);
|
|
505
|
-
if (stats && indexEntry) {
|
|
506
|
-
if (stats.size !== indexEntry.size || (stats.lastModified ?? 0) > indexEntry.mtime) {
|
|
507
|
-
conflicts.push(path);
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
} catch {
|
|
511
|
-
// File already deleted, no conflict
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
return [...new Set(conflicts)]; // Remove duplicates
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
/**
|
|
520
|
-
* Rebuild index from tree.
|
|
521
|
-
*/
|
|
522
|
-
private async rebuildIndex(targetTree: Map<string, FlatTreeEntry>): Promise<void> {
|
|
523
|
-
const builder = this.staging.createBuilder();
|
|
524
|
-
|
|
525
|
-
for (const [path, entry] of targetTree) {
|
|
526
|
-
// Get file stats from working tree
|
|
527
|
-
const fullPath = joinPath(this.workTreeRoot, path);
|
|
528
|
-
let size = 0;
|
|
529
|
-
let mtime = Date.now();
|
|
530
|
-
|
|
531
|
-
try {
|
|
532
|
-
const stats = await this.files.stats(fullPath);
|
|
533
|
-
if (stats) {
|
|
534
|
-
size = stats.size ?? 0;
|
|
535
|
-
mtime = stats.lastModified ?? Date.now();
|
|
536
|
-
}
|
|
537
|
-
} catch {
|
|
538
|
-
// Use defaults
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
builder.add({
|
|
542
|
-
path,
|
|
543
|
-
mode: entry.mode,
|
|
544
|
-
objectId: entry.id,
|
|
545
|
-
stage: 0,
|
|
546
|
-
size,
|
|
547
|
-
mtime,
|
|
548
|
-
});
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
builder.finish();
|
|
552
|
-
await this.staging.write();
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
/**
|
|
557
|
-
* Create a CheckoutCommand.
|
|
558
|
-
*
|
|
559
|
-
* @param options Command options
|
|
560
|
-
* @returns New CheckoutCommand instance
|
|
561
|
-
*/
|
|
562
|
-
export function createCheckoutCommand(options: CheckoutCommandOptions): Checkout {
|
|
563
|
-
return new CheckoutCommand(options);
|
|
564
|
-
}
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checkout interface for materializing tree contents to filesystem.
|
|
3
|
-
*
|
|
4
|
-
* Supports:
|
|
5
|
-
* - Branch/tag/commit checkout
|
|
6
|
-
* - Path-specific checkout
|
|
7
|
-
* - Conflict detection
|
|
8
|
-
* - Progress reporting
|
|
9
|
-
*
|
|
10
|
-
* Reference: jgit/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import type { ObjectId } from "@statewalker/vcs-core";
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Options for checkout operations.
|
|
17
|
-
*/
|
|
18
|
-
export interface CheckoutOptions {
|
|
19
|
-
/** Force checkout even if working tree has changes */
|
|
20
|
-
force?: boolean;
|
|
21
|
-
|
|
22
|
-
/** Create branch at target commit */
|
|
23
|
-
createBranch?: string;
|
|
24
|
-
|
|
25
|
-
/** Checkout specific paths only */
|
|
26
|
-
paths?: string[];
|
|
27
|
-
|
|
28
|
-
/** Source for path checkout (default: "index") */
|
|
29
|
-
source?: "index" | "head" | ObjectId;
|
|
30
|
-
|
|
31
|
-
/** Progress callback */
|
|
32
|
-
onProgress?: (current: number, total: number, path: string) => void;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Result of a checkout operation.
|
|
37
|
-
*/
|
|
38
|
-
export interface CheckoutResult {
|
|
39
|
-
/** Files updated in working tree */
|
|
40
|
-
updated: string[];
|
|
41
|
-
|
|
42
|
-
/** Files added to working tree */
|
|
43
|
-
added: string[];
|
|
44
|
-
|
|
45
|
-
/** Files removed from working tree */
|
|
46
|
-
removed: string[];
|
|
47
|
-
|
|
48
|
-
/** Files that couldn't be checked out (conflicts) */
|
|
49
|
-
conflicts: string[];
|
|
50
|
-
|
|
51
|
-
/** New HEAD after checkout */
|
|
52
|
-
newHead?: ObjectId;
|
|
53
|
-
|
|
54
|
-
/** New branch name if created */
|
|
55
|
-
newBranch?: string;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Checkout command interface.
|
|
60
|
-
*
|
|
61
|
-
* Materializes tree contents from commits, branches, or tags to the working tree.
|
|
62
|
-
* Handles conflict detection and index updates.
|
|
63
|
-
*/
|
|
64
|
-
export interface Checkout {
|
|
65
|
-
/**
|
|
66
|
-
* Checkout a branch, tag, or commit.
|
|
67
|
-
*
|
|
68
|
-
* If target is a branch name, HEAD becomes a symbolic ref to that branch.
|
|
69
|
-
* If target is a tag or commit, HEAD becomes detached.
|
|
70
|
-
*
|
|
71
|
-
* @param target Branch name, tag name, or commit ID
|
|
72
|
-
* @param options Checkout options
|
|
73
|
-
* @returns Checkout result with lists of affected files
|
|
74
|
-
* @throws Error if target cannot be resolved or conflicts exist (without force)
|
|
75
|
-
*/
|
|
76
|
-
checkout(target: string, options?: CheckoutOptions): Promise<CheckoutResult>;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Checkout specific paths from index or commit.
|
|
80
|
-
*
|
|
81
|
-
* Unlike full checkout, this:
|
|
82
|
-
* - Does not move HEAD
|
|
83
|
-
* - Only updates specified paths
|
|
84
|
-
* - Restores files from index (default) or a specific commit
|
|
85
|
-
*
|
|
86
|
-
* @param paths Paths to checkout
|
|
87
|
-
* @param options Checkout options (source specifies where to get files)
|
|
88
|
-
* @returns Checkout result
|
|
89
|
-
*/
|
|
90
|
-
checkoutPaths(paths: string[], options?: CheckoutOptions): Promise<CheckoutResult>;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Conflict information for a path.
|
|
95
|
-
*/
|
|
96
|
-
export interface CheckoutConflict {
|
|
97
|
-
/** Path with conflict */
|
|
98
|
-
path: string;
|
|
99
|
-
|
|
100
|
-
/** Reason for conflict */
|
|
101
|
-
reason: CheckoutConflictReason;
|
|
102
|
-
|
|
103
|
-
/** Local changes that would be overwritten (if applicable) */
|
|
104
|
-
localChanges?: "modified" | "deleted" | "untracked";
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Reasons why checkout failed for a path.
|
|
109
|
-
*/
|
|
110
|
-
export const CheckoutConflictReason = {
|
|
111
|
-
/** Local modifications would be overwritten */
|
|
112
|
-
LOCAL_MODIFIED: "LOCAL_MODIFIED",
|
|
113
|
-
|
|
114
|
-
/** Untracked file would be overwritten */
|
|
115
|
-
UNTRACKED_OVERWRITTEN: "UNTRACKED_OVERWRITTEN",
|
|
116
|
-
|
|
117
|
-
/** File deleted locally but modified in target */
|
|
118
|
-
DELETED_LOCALLY: "DELETED_LOCALLY",
|
|
119
|
-
|
|
120
|
-
/** Cannot delete directory - not empty */
|
|
121
|
-
DIRECTORY_NOT_EMPTY: "DIRECTORY_NOT_EMPTY",
|
|
122
|
-
} as const;
|
|
123
|
-
|
|
124
|
-
export type CheckoutConflictReason =
|
|
125
|
-
(typeof CheckoutConflictReason)[keyof typeof CheckoutConflictReason];
|