shell-dsl 0.0.32 → 0.0.34
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 +130 -0
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/src/index.cjs +3 -1
- package/dist/cjs/src/index.cjs.map +3 -3
- package/dist/cjs/src/vcs/diff.cjs +107 -0
- package/dist/cjs/src/vcs/diff.cjs.map +10 -0
- package/dist/cjs/src/vcs/index.cjs +47 -0
- package/dist/cjs/src/vcs/index.cjs.map +10 -0
- package/dist/cjs/src/vcs/match.cjs +106 -0
- package/dist/cjs/src/vcs/match.cjs.map +10 -0
- package/dist/cjs/src/vcs/rules.cjs +180 -0
- package/dist/cjs/src/vcs/rules.cjs.map +10 -0
- package/dist/cjs/src/vcs/snapshot.cjs +228 -0
- package/dist/cjs/src/vcs/snapshot.cjs.map +10 -0
- package/dist/cjs/src/vcs/storage.cjs +120 -0
- package/dist/cjs/src/vcs/storage.cjs.map +10 -0
- package/dist/cjs/src/vcs/types.cjs +30 -0
- package/dist/cjs/src/vcs/types.cjs.map +9 -0
- package/dist/cjs/src/vcs/vcs.cjs +322 -0
- package/dist/cjs/src/vcs/vcs.cjs.map +10 -0
- package/dist/cjs/src/vcs/walk.cjs +89 -0
- package/dist/cjs/src/vcs/walk.cjs.map +10 -0
- package/dist/mjs/package.json +1 -1
- package/dist/mjs/src/index.mjs +3 -1
- package/dist/mjs/src/index.mjs.map +3 -3
- package/dist/mjs/src/vcs/diff.mjs +67 -0
- package/dist/mjs/src/vcs/diff.mjs.map +10 -0
- package/dist/mjs/src/vcs/index.mjs +7 -0
- package/dist/mjs/src/vcs/index.mjs.map +10 -0
- package/dist/mjs/src/vcs/match.mjs +66 -0
- package/dist/mjs/src/vcs/match.mjs.map +10 -0
- package/dist/mjs/src/vcs/rules.mjs +140 -0
- package/dist/mjs/src/vcs/rules.mjs.map +10 -0
- package/dist/mjs/src/vcs/snapshot.mjs +188 -0
- package/dist/mjs/src/vcs/snapshot.mjs.map +10 -0
- package/dist/mjs/src/vcs/storage.mjs +79 -0
- package/dist/mjs/src/vcs/storage.mjs.map +10 -0
- package/dist/mjs/src/vcs/types.mjs +2 -0
- package/dist/mjs/src/vcs/types.mjs.map +9 -0
- package/dist/mjs/src/vcs/vcs.mjs +282 -0
- package/dist/mjs/src/vcs/vcs.mjs.map +10 -0
- package/dist/mjs/src/vcs/walk.mjs +49 -0
- package/dist/mjs/src/vcs/walk.mjs.map +10 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/vcs/diff.d.ts +11 -0
- package/dist/types/src/vcs/index.d.ts +2 -0
- package/dist/types/src/vcs/match.d.ts +5 -0
- package/dist/types/src/vcs/rules.d.ts +23 -0
- package/dist/types/src/vcs/snapshot.d.ts +26 -0
- package/dist/types/src/vcs/storage.d.ts +22 -0
- package/dist/types/src/vcs/types.d.ts +99 -0
- package/dist/types/src/vcs/vcs.d.ts +35 -0
- package/dist/types/src/vcs/walk.d.ts +19 -0
- package/package.json +1 -1
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
// src/vcs/vcs.ts
|
|
2
|
+
import { VCSStorage } from "./storage.mjs";
|
|
3
|
+
import { diffManifests, diffWorkingTree } from "./diff.mjs";
|
|
4
|
+
import { matchVCSPath, VCSRules } from "./rules.mjs";
|
|
5
|
+
import { buildTreeManifest, restoreTree } from "./snapshot.mjs";
|
|
6
|
+
|
|
7
|
+
class VersionControlSystem {
|
|
8
|
+
workFs;
|
|
9
|
+
workPath;
|
|
10
|
+
storage;
|
|
11
|
+
vcsInternalPath;
|
|
12
|
+
rules;
|
|
13
|
+
constructor(config) {
|
|
14
|
+
this.workFs = config.fs;
|
|
15
|
+
this.workPath = config.fs.resolve(config.path);
|
|
16
|
+
const metaFs = config.vcsPath?.fs ?? config.fs;
|
|
17
|
+
const metaPath = config.vcsPath?.path ?? metaFs.resolve(config.path, ".vcs");
|
|
18
|
+
this.storage = new VCSStorage(metaFs, metaPath);
|
|
19
|
+
this.vcsInternalPath = resolveInternalPath(config.fs, metaFs, this.workPath, metaPath);
|
|
20
|
+
this.rules = new VCSRules({
|
|
21
|
+
internalPath: this.vcsInternalPath,
|
|
22
|
+
ignore: config.ignore,
|
|
23
|
+
attributes: config.attributes
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
async init() {
|
|
27
|
+
if (await this.storage.isInitialized())
|
|
28
|
+
return;
|
|
29
|
+
await this.storage.initialize();
|
|
30
|
+
}
|
|
31
|
+
async ensureInit() {
|
|
32
|
+
if (!await this.storage.isInitialized()) {
|
|
33
|
+
await this.init();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async resolveHead() {
|
|
37
|
+
const head = await this.storage.readHead();
|
|
38
|
+
if (head.revision !== undefined) {
|
|
39
|
+
return { branch: null, revision: head.revision };
|
|
40
|
+
}
|
|
41
|
+
if (head.ref) {
|
|
42
|
+
const branchName = head.ref.replace("refs/heads/", "");
|
|
43
|
+
const branchRef = await this.storage.readBranch(branchName);
|
|
44
|
+
return { branch: branchName, revision: branchRef?.revision ?? null };
|
|
45
|
+
}
|
|
46
|
+
return { branch: null, revision: null };
|
|
47
|
+
}
|
|
48
|
+
async headManifest() {
|
|
49
|
+
const { revision } = await this.resolveHead();
|
|
50
|
+
if (revision === null)
|
|
51
|
+
return {};
|
|
52
|
+
const rev = await this.storage.readRevision(revision);
|
|
53
|
+
return rev.tree;
|
|
54
|
+
}
|
|
55
|
+
async commit(message, opts) {
|
|
56
|
+
await this.ensureInit();
|
|
57
|
+
const { branch, revision: parentId } = await this.resolveHead();
|
|
58
|
+
const parentManifest = parentId !== null ? (await this.storage.readRevision(parentId)).tree : {};
|
|
59
|
+
let newTree;
|
|
60
|
+
let changes;
|
|
61
|
+
if (opts?.paths && opts.paths.length > 0) {
|
|
62
|
+
const fullManifest = await buildTreeManifest(this.workFs, this.workPath, {
|
|
63
|
+
rules: this.rules,
|
|
64
|
+
trackedPaths: Object.keys(parentManifest)
|
|
65
|
+
});
|
|
66
|
+
const matchedPaths = filterPathsByGlobs(Object.keys(fullManifest), opts.paths);
|
|
67
|
+
newTree = { ...parentManifest };
|
|
68
|
+
const parentMatchedPaths = filterPathsByGlobs(Object.keys(parentManifest), opts.paths);
|
|
69
|
+
for (const p of parentMatchedPaths) {
|
|
70
|
+
if (!fullManifest[p]) {
|
|
71
|
+
delete newTree[p];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
for (const p of matchedPaths) {
|
|
75
|
+
newTree[p] = fullManifest[p];
|
|
76
|
+
}
|
|
77
|
+
const relevantBefore = {};
|
|
78
|
+
const relevantAfter = {};
|
|
79
|
+
const allRelevant = new Set([...matchedPaths, ...parentMatchedPaths]);
|
|
80
|
+
for (const p of allRelevant) {
|
|
81
|
+
if (parentManifest[p])
|
|
82
|
+
relevantBefore[p] = parentManifest[p];
|
|
83
|
+
if (newTree[p])
|
|
84
|
+
relevantAfter[p] = newTree[p];
|
|
85
|
+
}
|
|
86
|
+
changes = diffManifests(relevantBefore, relevantAfter, this.rules);
|
|
87
|
+
} else {
|
|
88
|
+
newTree = await buildTreeManifest(this.workFs, this.workPath, {
|
|
89
|
+
rules: this.rules,
|
|
90
|
+
trackedPaths: Object.keys(parentManifest)
|
|
91
|
+
});
|
|
92
|
+
changes = diffManifests(parentManifest, newTree, this.rules);
|
|
93
|
+
}
|
|
94
|
+
if (changes.length === 0) {
|
|
95
|
+
throw new Error("nothing to commit");
|
|
96
|
+
}
|
|
97
|
+
const id = await this.storage.nextRevisionId();
|
|
98
|
+
const rev = {
|
|
99
|
+
id,
|
|
100
|
+
parent: parentId,
|
|
101
|
+
branch: branch ?? "detached",
|
|
102
|
+
message,
|
|
103
|
+
timestamp: new Date().toISOString(),
|
|
104
|
+
changes,
|
|
105
|
+
tree: newTree
|
|
106
|
+
};
|
|
107
|
+
await this.storage.writeRevision(rev);
|
|
108
|
+
if (branch) {
|
|
109
|
+
await this.storage.writeBranch(branch, { revision: id });
|
|
110
|
+
} else {
|
|
111
|
+
await this.storage.writeHead({ revision: id });
|
|
112
|
+
}
|
|
113
|
+
return rev;
|
|
114
|
+
}
|
|
115
|
+
async checkout(target, opts) {
|
|
116
|
+
await this.ensureInit();
|
|
117
|
+
const isPartial = opts?.paths && opts.paths.length > 0;
|
|
118
|
+
let targetRevision;
|
|
119
|
+
let targetBranch = null;
|
|
120
|
+
if (typeof target === "string") {
|
|
121
|
+
const branchRef = await this.storage.readBranch(target);
|
|
122
|
+
if (branchRef) {
|
|
123
|
+
targetBranch = target;
|
|
124
|
+
targetRevision = branchRef.revision;
|
|
125
|
+
} else {
|
|
126
|
+
throw new Error(`unknown branch or revision: "${target}"`);
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
targetRevision = target;
|
|
130
|
+
}
|
|
131
|
+
let rev;
|
|
132
|
+
try {
|
|
133
|
+
rev = await this.storage.readRevision(targetRevision);
|
|
134
|
+
} catch {
|
|
135
|
+
throw new Error(`revision ${targetRevision} not found`);
|
|
136
|
+
}
|
|
137
|
+
const currentManifest = await this.headManifest();
|
|
138
|
+
if (isPartial) {
|
|
139
|
+
await restoreTree(this.workFs, this.workPath, rev.tree, {
|
|
140
|
+
fullRestore: false,
|
|
141
|
+
paths: opts.paths,
|
|
142
|
+
rules: this.rules,
|
|
143
|
+
trackedPaths: Object.keys(currentManifest)
|
|
144
|
+
});
|
|
145
|
+
} else {
|
|
146
|
+
if (!opts?.force) {
|
|
147
|
+
const changes = await this.status();
|
|
148
|
+
if (changes.length > 0) {
|
|
149
|
+
throw new Error("working tree has uncommitted changes (use force to discard)");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
await restoreTree(this.workFs, this.workPath, rev.tree, {
|
|
153
|
+
fullRestore: true,
|
|
154
|
+
rules: this.rules,
|
|
155
|
+
trackedPaths: Object.keys(currentManifest)
|
|
156
|
+
});
|
|
157
|
+
if (targetBranch) {
|
|
158
|
+
await this.storage.writeHead({ ref: `refs/heads/${targetBranch}` });
|
|
159
|
+
} else {
|
|
160
|
+
await this.storage.writeHead({ revision: targetRevision });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async branch(name) {
|
|
165
|
+
await this.ensureInit();
|
|
166
|
+
const existing = await this.storage.readBranch(name);
|
|
167
|
+
if (existing) {
|
|
168
|
+
throw new Error(`branch "${name}" already exists`);
|
|
169
|
+
}
|
|
170
|
+
const { revision } = await this.resolveHead();
|
|
171
|
+
if (revision === null) {
|
|
172
|
+
throw new Error("cannot create branch: no commits yet");
|
|
173
|
+
}
|
|
174
|
+
await this.storage.writeBranch(name, { revision });
|
|
175
|
+
}
|
|
176
|
+
async branches() {
|
|
177
|
+
await this.ensureInit();
|
|
178
|
+
const names = await this.storage.listBranches();
|
|
179
|
+
const head = await this.resolveHead();
|
|
180
|
+
const result = [];
|
|
181
|
+
for (const name of names) {
|
|
182
|
+
const ref = await this.storage.readBranch(name);
|
|
183
|
+
if (ref) {
|
|
184
|
+
result.push({
|
|
185
|
+
name,
|
|
186
|
+
revision: ref.revision,
|
|
187
|
+
current: head.branch === name
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return result.sort((a, b) => a.name.localeCompare(b.name));
|
|
192
|
+
}
|
|
193
|
+
async log(opts) {
|
|
194
|
+
await this.ensureInit();
|
|
195
|
+
let startRevision;
|
|
196
|
+
if (opts?.branch) {
|
|
197
|
+
const branchRef = await this.storage.readBranch(opts.branch);
|
|
198
|
+
if (!branchRef)
|
|
199
|
+
throw new Error(`branch "${opts.branch}" not found`);
|
|
200
|
+
startRevision = branchRef.revision;
|
|
201
|
+
} else {
|
|
202
|
+
const { revision } = await this.resolveHead();
|
|
203
|
+
startRevision = revision;
|
|
204
|
+
}
|
|
205
|
+
if (startRevision === null)
|
|
206
|
+
return [];
|
|
207
|
+
const entries = [];
|
|
208
|
+
let currentId = startRevision;
|
|
209
|
+
while (currentId !== null) {
|
|
210
|
+
if (opts?.limit && entries.length >= opts.limit)
|
|
211
|
+
break;
|
|
212
|
+
let rev;
|
|
213
|
+
try {
|
|
214
|
+
rev = await this.storage.readRevision(currentId);
|
|
215
|
+
} catch {
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
const changedPaths = rev.changes.map((c) => c.path);
|
|
219
|
+
if (opts?.path) {
|
|
220
|
+
const matchesPath = changedPaths.some((p) => matchVCSPath(opts.path, p));
|
|
221
|
+
if (matchesPath) {
|
|
222
|
+
entries.push({
|
|
223
|
+
id: rev.id,
|
|
224
|
+
parent: rev.parent,
|
|
225
|
+
branch: rev.branch,
|
|
226
|
+
message: rev.message,
|
|
227
|
+
timestamp: rev.timestamp,
|
|
228
|
+
paths: changedPaths
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
entries.push({
|
|
233
|
+
id: rev.id,
|
|
234
|
+
parent: rev.parent,
|
|
235
|
+
branch: rev.branch,
|
|
236
|
+
message: rev.message,
|
|
237
|
+
timestamp: rev.timestamp,
|
|
238
|
+
paths: changedPaths
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
currentId = rev.parent;
|
|
242
|
+
}
|
|
243
|
+
return entries;
|
|
244
|
+
}
|
|
245
|
+
async status() {
|
|
246
|
+
await this.ensureInit();
|
|
247
|
+
const manifest = await this.headManifest();
|
|
248
|
+
return diffWorkingTree(this.workFs, this.workPath, manifest, this.rules);
|
|
249
|
+
}
|
|
250
|
+
async diff(revA, revB) {
|
|
251
|
+
await this.ensureInit();
|
|
252
|
+
const a = await this.storage.readRevision(revA);
|
|
253
|
+
const b = await this.storage.readRevision(revB);
|
|
254
|
+
return diffManifests(a.tree, b.tree, this.rules);
|
|
255
|
+
}
|
|
256
|
+
async head() {
|
|
257
|
+
await this.ensureInit();
|
|
258
|
+
return this.resolveHead();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function filterPathsByGlobs(paths, patterns) {
|
|
262
|
+
return paths.filter((filePath) => patterns.some((pattern) => matchVCSPath(pattern, filePath)));
|
|
263
|
+
}
|
|
264
|
+
function resolveInternalPath(workFs, metaFs, workPath, metaPath) {
|
|
265
|
+
if (workFs !== metaFs)
|
|
266
|
+
return "";
|
|
267
|
+
const normalizedWork = normalizeFsPath(workPath);
|
|
268
|
+
const normalizedMeta = normalizeFsPath(metaFs.resolve(metaPath));
|
|
269
|
+
if (normalizedMeta === normalizedWork)
|
|
270
|
+
return "";
|
|
271
|
+
if (!normalizedMeta.startsWith(`${normalizedWork}/`))
|
|
272
|
+
return "";
|
|
273
|
+
return normalizedMeta.slice(normalizedWork.length + 1);
|
|
274
|
+
}
|
|
275
|
+
function normalizeFsPath(path) {
|
|
276
|
+
return path.replace(/\\/g, "/").replace(/\/+$/, "");
|
|
277
|
+
}
|
|
278
|
+
export {
|
|
279
|
+
VersionControlSystem
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
//# debugId=8A01883E52B7978864756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/vcs/vcs.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { VirtualFS } from \"../types.mjs\";\nimport type {\n VCSConfig,\n Revision,\n DiffEntry,\n TreeManifest,\n CommitOptions,\n CheckoutOptions,\n LogOptions,\n LogEntry,\n BranchInfo,\n} from \"./types.mjs\";\nimport { VCSStorage } from \"./storage.mjs\";\nimport { diffManifests, diffWorkingTree } from \"./diff.mjs\";\nimport { matchVCSPath, VCSRules } from \"./rules.mjs\";\nimport { buildTreeManifest, restoreTree } from \"./snapshot.mjs\";\n\nexport class VersionControlSystem {\n private readonly workFs: VirtualFS;\n private readonly workPath: string;\n private readonly storage: VCSStorage;\n private readonly vcsInternalPath: string;\n private readonly rules: VCSRules;\n\n constructor(config: VCSConfig) {\n this.workFs = config.fs;\n this.workPath = config.fs.resolve(config.path);\n\n const metaFs = config.vcsPath?.fs ?? config.fs;\n const metaPath = config.vcsPath?.path ?? metaFs.resolve(config.path, \".vcs\");\n this.storage = new VCSStorage(metaFs, metaPath);\n\n this.vcsInternalPath = resolveInternalPath(config.fs, metaFs, this.workPath, metaPath);\n\n this.rules = new VCSRules({\n internalPath: this.vcsInternalPath,\n ignore: config.ignore,\n attributes: config.attributes,\n });\n }\n\n /** Initialize the .vcs directory. Called automatically on first operation if needed. */\n async init(): Promise<void> {\n if (await this.storage.isInitialized()) return;\n await this.storage.initialize();\n }\n\n private async ensureInit(): Promise<void> {\n if (!(await this.storage.isInitialized())) {\n await this.init();\n }\n }\n\n /** Get the current HEAD revision number, or null if no commits yet. */\n private async resolveHead(): Promise<{ branch: string | null; revision: number | null }> {\n const head = await this.storage.readHead();\n if (head.revision !== undefined) {\n return { branch: null, revision: head.revision };\n }\n if (head.ref) {\n const branchName = head.ref.replace(\"refs/heads/\", \"\");\n const branchRef = await this.storage.readBranch(branchName);\n return { branch: branchName, revision: branchRef?.revision ?? null };\n }\n return { branch: null, revision: null };\n }\n\n /** Get current HEAD manifest, or empty if no commits. */\n private async headManifest(): Promise<TreeManifest> {\n const { revision } = await this.resolveHead();\n if (revision === null) return {};\n const rev = await this.storage.readRevision(revision);\n return rev.tree;\n }\n\n /** Commit all pending changes, or selective changes if paths are provided. */\n async commit(message: string, opts?: CommitOptions): Promise<Revision> {\n await this.ensureInit();\n\n const { branch, revision: parentId } = await this.resolveHead();\n const parentManifest = parentId !== null\n ? (await this.storage.readRevision(parentId)).tree\n : {};\n\n let newTree: TreeManifest;\n let changes: DiffEntry[];\n\n if (opts?.paths && opts.paths.length > 0) {\n // Selective commit: only include matching files\n const fullManifest = await buildTreeManifest(this.workFs, this.workPath, {\n rules: this.rules,\n trackedPaths: Object.keys(parentManifest),\n });\n const matchedPaths = filterPathsByGlobs(Object.keys(fullManifest), opts.paths);\n\n // Start with parent manifest, overlay matched files from working tree\n newTree = { ...parentManifest };\n\n // Also check for deletions: files in parent that match patterns but are gone from working tree\n const parentMatchedPaths = filterPathsByGlobs(Object.keys(parentManifest), opts.paths);\n for (const p of parentMatchedPaths) {\n if (!fullManifest[p]) {\n delete newTree[p]; // file was deleted\n }\n }\n\n for (const p of matchedPaths) {\n newTree[p] = fullManifest[p]!;\n }\n\n // Compute changes only for matched paths\n const relevantBefore: TreeManifest = {};\n const relevantAfter: TreeManifest = {};\n const allRelevant = new Set([...matchedPaths, ...parentMatchedPaths]);\n for (const p of allRelevant) {\n if (parentManifest[p]) relevantBefore[p] = parentManifest[p]!;\n if (newTree[p]) relevantAfter[p] = newTree[p]!;\n }\n changes = diffManifests(relevantBefore, relevantAfter, this.rules);\n } else {\n // Full commit\n newTree = await buildTreeManifest(this.workFs, this.workPath, {\n rules: this.rules,\n trackedPaths: Object.keys(parentManifest),\n });\n changes = diffManifests(parentManifest, newTree, this.rules);\n }\n\n if (changes.length === 0) {\n throw new Error(\"nothing to commit\");\n }\n\n const id = await this.storage.nextRevisionId();\n const rev: Revision = {\n id,\n parent: parentId,\n branch: branch ?? \"detached\",\n message,\n timestamp: new Date().toISOString(),\n changes,\n tree: newTree,\n };\n\n await this.storage.writeRevision(rev);\n\n // Update branch ref or HEAD\n if (branch) {\n await this.storage.writeBranch(branch, { revision: id });\n } else {\n await this.storage.writeHead({ revision: id });\n }\n\n return rev;\n }\n\n /** Checkout a revision number or branch name. */\n async checkout(target: string | number, opts?: CheckoutOptions): Promise<void> {\n await this.ensureInit();\n\n const isPartial = opts?.paths && opts.paths.length > 0;\n\n let targetRevision: number;\n let targetBranch: string | null = null;\n\n if (typeof target === \"string\") {\n // Check if it's a branch name\n const branchRef = await this.storage.readBranch(target);\n if (branchRef) {\n targetBranch = target;\n targetRevision = branchRef.revision;\n } else {\n throw new Error(`unknown branch or revision: \"${target}\"`);\n }\n } else {\n targetRevision = target;\n }\n\n // Verify revision exists\n let rev: Revision;\n try {\n rev = await this.storage.readRevision(targetRevision);\n } catch {\n throw new Error(`revision ${targetRevision} not found`);\n }\n\n const currentManifest = await this.headManifest();\n\n if (isPartial) {\n // Partial checkout: restore specific files, don't update HEAD\n await restoreTree(this.workFs, this.workPath, rev.tree, {\n fullRestore: false,\n paths: opts!.paths!,\n rules: this.rules,\n trackedPaths: Object.keys(currentManifest),\n });\n } else {\n // Full checkout\n if (!opts?.force) {\n const changes = await this.status();\n if (changes.length > 0) {\n throw new Error(\"working tree has uncommitted changes (use force to discard)\");\n }\n }\n\n await restoreTree(this.workFs, this.workPath, rev.tree, {\n fullRestore: true,\n rules: this.rules,\n trackedPaths: Object.keys(currentManifest),\n });\n\n // Update HEAD\n if (targetBranch) {\n await this.storage.writeHead({ ref: `refs/heads/${targetBranch}` });\n } else {\n await this.storage.writeHead({ revision: targetRevision });\n }\n }\n }\n\n /** Create a new branch at HEAD. */\n async branch(name: string): Promise<void> {\n await this.ensureInit();\n\n const existing = await this.storage.readBranch(name);\n if (existing) {\n throw new Error(`branch \"${name}\" already exists`);\n }\n\n const { revision } = await this.resolveHead();\n if (revision === null) {\n throw new Error(\"cannot create branch: no commits yet\");\n }\n\n await this.storage.writeBranch(name, { revision });\n }\n\n /** List all branches. */\n async branches(): Promise<BranchInfo[]> {\n await this.ensureInit();\n\n const names = await this.storage.listBranches();\n const head = await this.resolveHead();\n const result: BranchInfo[] = [];\n\n for (const name of names) {\n const ref = await this.storage.readBranch(name);\n if (ref) {\n result.push({\n name,\n revision: ref.revision,\n current: head.branch === name,\n });\n }\n }\n\n return result.sort((a, b) => a.name.localeCompare(b.name));\n }\n\n /** Get revision history. */\n async log(opts?: LogOptions): Promise<LogEntry[]> {\n await this.ensureInit();\n\n let startRevision: number | null;\n\n if (opts?.branch) {\n const branchRef = await this.storage.readBranch(opts.branch);\n if (!branchRef) throw new Error(`branch \"${opts.branch}\" not found`);\n startRevision = branchRef.revision;\n } else {\n const { revision } = await this.resolveHead();\n startRevision = revision;\n }\n\n if (startRevision === null) return [];\n\n const entries: LogEntry[] = [];\n let currentId: number | null = startRevision;\n\n while (currentId !== null) {\n if (opts?.limit && entries.length >= opts.limit) break;\n\n let rev: Revision;\n try {\n rev = await this.storage.readRevision(currentId);\n } catch {\n break;\n }\n\n const changedPaths = rev.changes.map((c) => c.path);\n\n if (opts?.path) {\n // Filter: only include if this revision touches the specified path\n const matchesPath = changedPaths.some((p) => matchVCSPath(opts.path!, p));\n if (matchesPath) {\n entries.push({\n id: rev.id,\n parent: rev.parent,\n branch: rev.branch,\n message: rev.message,\n timestamp: rev.timestamp,\n paths: changedPaths,\n });\n }\n } else {\n entries.push({\n id: rev.id,\n parent: rev.parent,\n branch: rev.branch,\n message: rev.message,\n timestamp: rev.timestamp,\n paths: changedPaths,\n });\n }\n\n currentId = rev.parent;\n }\n\n return entries;\n }\n\n /** Get uncommitted changes as DiffEntry[]. */\n async status(): Promise<DiffEntry[]> {\n await this.ensureInit();\n const manifest = await this.headManifest();\n return diffWorkingTree(this.workFs, this.workPath, manifest, this.rules);\n }\n\n /** Diff between two revisions. */\n async diff(revA: number, revB: number): Promise<DiffEntry[]> {\n await this.ensureInit();\n const a = await this.storage.readRevision(revA);\n const b = await this.storage.readRevision(revB);\n return diffManifests(a.tree, b.tree, this.rules);\n }\n\n /** Get current HEAD info. */\n async head(): Promise<{ branch: string | null; revision: number | null }> {\n await this.ensureInit();\n return this.resolveHead();\n }\n}\n\n/**\n * Filter a list of paths to only those matching any of the given glob patterns.\n * Patterns may start with `/` which is stripped before matching.\n */\nfunction filterPathsByGlobs(paths: string[], patterns: string[]): string[] {\n return paths.filter((filePath) =>\n patterns.some((pattern) => matchVCSPath(pattern, filePath)),\n );\n}\n\nfunction resolveInternalPath(\n workFs: VirtualFS,\n metaFs: VirtualFS,\n workPath: string,\n metaPath: string,\n): string {\n if (workFs !== metaFs) return \"\";\n\n const normalizedWork = normalizeFsPath(workPath);\n const normalizedMeta = normalizeFsPath(metaFs.resolve(metaPath));\n\n if (normalizedMeta === normalizedWork) return \"\";\n if (!normalizedMeta.startsWith(`${normalizedWork}/`)) return \"\";\n\n return normalizedMeta.slice(normalizedWork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CAAC,MAAc,MAAoC;AAAA,IAC3D,MAAM,KAAK,WAAW;AAAA,IACtB,MAAM,IAAI,MAAM,KAAK,QAAQ,aAAa,IAAI;AAAA,IAC9C,MAAM,IAAI,MAAM,KAAK,QAAQ,aAAa,IAAI;AAAA,IAC9C,OAAO,cAAc,EAAE,MAAM,EAAE,MAAM,KAAK,KAAK;AAAA;AAAA,OAI3C,KAAI,GAAgE;AAAA,IACxE,MAAM,KAAK,WAAW;AAAA,IACtB,OAAO,KAAK,YAAY;AAAA;AAE5B;AAMA,SAAS,kBAAkB,CAAC,OAAiB,UAA8B;AAAA,EACzE,OAAO,MAAM,OAAO,CAAC,aACnB,SAAS,KAAK,CAAC,YAAY,aAAa,SAAS,QAAQ,CAAC,CAC5D;AAAA;AAGF,SAAS,mBAAmB,CAC1B,QACA,QACA,UACA,UACQ;AAAA,EACR,IAAI,WAAW;AAAA,IAAQ,OAAO;AAAA,EAE9B,MAAM,iBAAiB,gBAAgB,QAAQ;AAAA,EAC/C,MAAM,iBAAiB,gBAAgB,OAAO,QAAQ,QAAQ,CAAC;AAAA,EAE/D,IAAI,mBAAmB;AAAA,IAAgB,OAAO;AAAA,EAC9C,IAAI,CAAC,eAAe,WAAW,GAAG,iBAAiB;AAAA,IAAG,OAAO;AAAA,EAE7D,OAAO,eAAe,MAAM,eAAe,SAAS,CAAC;AAAA;AAGvD,SAAS,eAAe,CAAC,MAAsB;AAAA,EAC7C,OAAO,KAAK,QAAQ,OAAO,GAAG,EAAE,QAAQ,QAAQ,EAAE;AAAA;",
|
|
8
|
+
"debugId": "8A01883E52B7978864756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// src/vcs/walk.ts
|
|
2
|
+
async function walkTree(fs, root, options = {}) {
|
|
3
|
+
const results = await walkTreeEntries(fs, root, options);
|
|
4
|
+
return results.filter((entry) => entry.kind === "file").map((entry) => entry.path);
|
|
5
|
+
}
|
|
6
|
+
async function walkTreeEntries(fs, root, options = {}) {
|
|
7
|
+
const results = [];
|
|
8
|
+
await walkDir(fs, root, "", options, results);
|
|
9
|
+
return results.sort((a, b) => a.path.localeCompare(b.path));
|
|
10
|
+
}
|
|
11
|
+
async function walkDir(fs, dir, relativeDir, options, results) {
|
|
12
|
+
let entries;
|
|
13
|
+
try {
|
|
14
|
+
entries = await fs.readdir(dir);
|
|
15
|
+
} catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
for (const entry of entries) {
|
|
19
|
+
const fullPath = fs.resolve(dir, entry);
|
|
20
|
+
let stat;
|
|
21
|
+
try {
|
|
22
|
+
stat = await fs.stat(fullPath);
|
|
23
|
+
} catch {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const relative = relativeDir ? `${relativeDir}/${entry}` : entry;
|
|
27
|
+
if (stat.isDirectory()) {
|
|
28
|
+
if (options.enterDirectory && !await options.enterDirectory(relative)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const empty = await walkDir(fs, fullPath, relative, options, results);
|
|
32
|
+
if (options.includeDirectory && await options.includeDirectory(relative, { empty })) {
|
|
33
|
+
results.push({ path: relative, kind: "directory" });
|
|
34
|
+
}
|
|
35
|
+
} else if (stat.isFile()) {
|
|
36
|
+
if (options.includeFile && !await options.includeFile(relative)) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
results.push({ path: relative, kind: "file" });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return entries.length === 0;
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
walkTreeEntries,
|
|
46
|
+
walkTree
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
//# debugId=9529ABAA0ECE187C64756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/vcs/walk.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { VirtualFS } from \"../types.mjs\";\n\ninterface WalkTreeOptions {\n enterDirectory?: (relPath: string) => boolean | Promise<boolean>;\n includeFile?: (relPath: string) => boolean | Promise<boolean>;\n includeDirectory?: (\n relPath: string,\n info: { empty: boolean },\n ) => boolean | Promise<boolean>;\n}\n\nexport interface WalkTreeEntry {\n path: string;\n kind: \"file\" | \"directory\";\n}\n\n/**\n * Recursively walk a directory tree and return all file paths\n * relative to the given root.\n */\nexport async function walkTree(\n fs: VirtualFS,\n root: string,\n options: WalkTreeOptions = {},\n): Promise<string[]> {\n const results = await walkTreeEntries(fs, root, options);\n return results\n .filter((entry) => entry.kind === \"file\")\n .map((entry) => entry.path);\n}\n\nexport async function walkTreeEntries(\n fs: VirtualFS,\n root: string,\n options: WalkTreeOptions = {},\n): Promise<WalkTreeEntry[]> {\n const results: WalkTreeEntry[] = [];\n await walkDir(fs, root, \"\", options, results);\n return results.sort((a, b) => a.path.localeCompare(b.path));\n}\n\nasync function walkDir(\n fs: VirtualFS,\n dir: string,\n relativeDir: string,\n options: WalkTreeOptions,\n results: WalkTreeEntry[],\n): Promise<boolean> {\n let entries: string[];\n try {\n entries = await fs.readdir(dir);\n } catch {\n return false;\n }\n\n for (const entry of entries) {\n const fullPath = fs.resolve(dir, entry);\n let stat;\n try {\n stat = await fs.stat(fullPath);\n } catch {\n continue;\n }\n\n const relative = relativeDir ? `${relativeDir}/${entry}` : entry;\n\n if (stat.isDirectory()) {\n if (options.enterDirectory && !(await options.enterDirectory(relative))) {\n continue;\n }\n const empty = await walkDir(fs, fullPath, relative, options, results);\n if (options.includeDirectory && (await options.includeDirectory(relative, { empty }))) {\n results.push({ path: relative, kind: \"directory\" });\n }\n } else if (stat.isFile()) {\n if (options.includeFile && !(await options.includeFile(relative))) {\n continue;\n }\n results.push({ path: relative, kind: \"file\" });\n }\n }\n\n return entries.length === 0;\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";AAoBA,eAAsB,QAAQ,CAC5B,IAC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AAAA,MACxB,IAAI,QAAQ,eAAe,CAAE,MAAM,QAAQ,YAAY,QAAQ,GAAI;AAAA,QACjE;AAAA,MACF;AAAA,MACA,QAAQ,KAAK,EAAE,MAAM,UAAU,MAAM,OAAO,CAAC;AAAA,IAC/C;AAAA,EACF;AAAA,EAEA,OAAO,QAAQ,WAAW;AAAA;",
|
|
8
|
+
"debugId": "9529ABAA0ECE187C64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -15,3 +15,5 @@ export { createStdin, StdinImpl } from "./io/index.ts";
|
|
|
15
15
|
export { createStdout, createStderr, createPipe, OutputCollectorImpl, PipeBuffer } from "./io/index.ts";
|
|
16
16
|
export { escape, escapeForInterpolation, globVirtualFS } from "./utils/index.ts";
|
|
17
17
|
export type { GlobVirtualFS, GlobOptions } from "./utils/index.ts";
|
|
18
|
+
export { VersionControlSystem } from "./vcs/index.ts";
|
|
19
|
+
export type { VCSConfig, VCSAttributeRule, VCSResolvedAttributes, VCSDiffMode, Revision, DiffEntry, TreeManifest, TreeEntry, FileEntry, DirectoryEntry, CommitOptions, CheckoutOptions, LogOptions, LogEntry, BranchInfo, } from "./vcs/index.ts";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { VirtualFS } from "../types.ts";
|
|
2
|
+
import type { TreeManifest, DiffEntry } from "./types.ts";
|
|
3
|
+
import { VCSRules } from "./rules.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Compute diff entries between two tree manifests.
|
|
6
|
+
*/
|
|
7
|
+
export declare function diffManifests(before: TreeManifest, after: TreeManifest, rules?: VCSRules): DiffEntry[];
|
|
8
|
+
/**
|
|
9
|
+
* Compute diff between a tree manifest and the current working tree.
|
|
10
|
+
*/
|
|
11
|
+
export declare function diffWorkingTree(fs: VirtualFS, rootPath: string, manifest: TreeManifest, rules?: VCSRules): Promise<DiffEntry[]>;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { VersionControlSystem } from "./vcs.ts";
|
|
2
|
+
export type { VCSConfig, VCSAttributeRule, VCSResolvedAttributes, VCSDiffMode, Revision, DiffEntry, TreeManifest, TreeEntry, FileEntry, DirectoryEntry, CommitOptions, CheckoutOptions, LogOptions, LogEntry, BranchInfo, } from "./types.ts";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { VCSAttributeRule, VCSResolvedAttributes } from "./types.ts";
|
|
2
|
+
interface VCSRulesConfig {
|
|
3
|
+
internalPath?: string;
|
|
4
|
+
internalDirName?: string;
|
|
5
|
+
ignore?: string[];
|
|
6
|
+
attributes?: VCSAttributeRule[];
|
|
7
|
+
}
|
|
8
|
+
export declare class VCSRules {
|
|
9
|
+
private readonly internalPath;
|
|
10
|
+
private readonly ignorePatterns;
|
|
11
|
+
private readonly attributeRules;
|
|
12
|
+
constructor(config?: VCSRulesConfig);
|
|
13
|
+
isInternalPath(relPath: string): boolean;
|
|
14
|
+
isIgnored(relPath: string): boolean;
|
|
15
|
+
shouldEnterDirectory(relPath: string, trackedPaths: Iterable<string>): boolean;
|
|
16
|
+
shouldIncludeWorkingFile(relPath: string, trackedPaths: ReadonlySet<string>): boolean;
|
|
17
|
+
shouldIncludeEmptyDirectory(relPath: string, trackedPaths: ReadonlySet<string>): boolean;
|
|
18
|
+
shouldIncludeRestoreScanFile(relPath: string): boolean;
|
|
19
|
+
shouldPreserveUntrackedIgnored(relPath: string, trackedPaths: ReadonlySet<string>): boolean;
|
|
20
|
+
resolveAttributes(relPath: string): VCSResolvedAttributes;
|
|
21
|
+
}
|
|
22
|
+
export declare function matchVCSPath(pattern: string, relPath: string): boolean;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { VirtualFS } from "../types.ts";
|
|
2
|
+
import type { TreeManifest } from "./types.ts";
|
|
3
|
+
import { VCSRules } from "./rules.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Build a TreeManifest from the current working tree.
|
|
6
|
+
*/
|
|
7
|
+
export declare function buildTreeManifest(fs: VirtualFS, rootPath: string, options?: {
|
|
8
|
+
rules?: VCSRules;
|
|
9
|
+
trackedPaths?: Iterable<string>;
|
|
10
|
+
}): Promise<TreeManifest>;
|
|
11
|
+
/**
|
|
12
|
+
* Build a TreeManifest for only the specified relative paths.
|
|
13
|
+
*/
|
|
14
|
+
export declare function buildPartialManifest(fs: VirtualFS, rootPath: string, paths: string[]): Promise<TreeManifest>;
|
|
15
|
+
/**
|
|
16
|
+
* Restore a working tree from a TreeManifest.
|
|
17
|
+
*
|
|
18
|
+
* If `fullRestore` is true, deletes working tree files not in the manifest.
|
|
19
|
+
* If `paths` is provided, only restores matching files.
|
|
20
|
+
*/
|
|
21
|
+
export declare function restoreTree(fs: VirtualFS, rootPath: string, manifest: TreeManifest, options?: {
|
|
22
|
+
fullRestore?: boolean;
|
|
23
|
+
paths?: string[];
|
|
24
|
+
rules?: VCSRules;
|
|
25
|
+
trackedPaths?: Iterable<string>;
|
|
26
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { VirtualFS } from "../types.ts";
|
|
2
|
+
import type { HeadRef, BranchRef, VCSConfigFile, Revision } from "./types.ts";
|
|
3
|
+
export declare class VCSStorage {
|
|
4
|
+
private readonly fs;
|
|
5
|
+
private readonly basePath;
|
|
6
|
+
constructor(fs: VirtualFS, basePath: string);
|
|
7
|
+
private path;
|
|
8
|
+
isInitialized(): Promise<boolean>;
|
|
9
|
+
initialize(defaultBranch?: string): Promise<void>;
|
|
10
|
+
readHead(): Promise<HeadRef>;
|
|
11
|
+
writeHead(head: HeadRef): Promise<void>;
|
|
12
|
+
readBranch(name: string): Promise<BranchRef | null>;
|
|
13
|
+
writeBranch(name: string, ref: BranchRef): Promise<void>;
|
|
14
|
+
deleteBranch(name: string): Promise<void>;
|
|
15
|
+
listBranches(): Promise<string[]>;
|
|
16
|
+
readRevision(id: number): Promise<Revision>;
|
|
17
|
+
writeRevision(rev: Revision): Promise<void>;
|
|
18
|
+
nextRevisionId(): Promise<number>;
|
|
19
|
+
readConfig(): Promise<VCSConfigFile>;
|
|
20
|
+
private readJSON;
|
|
21
|
+
private writeJSON;
|
|
22
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { VirtualFS } from "../types.ts";
|
|
2
|
+
export interface VCSConfig {
|
|
3
|
+
/** The working tree filesystem */
|
|
4
|
+
fs: VirtualFS;
|
|
5
|
+
/** Root path of the working tree */
|
|
6
|
+
path: string;
|
|
7
|
+
/** Glob-style patterns for untracked paths that VCS should ignore */
|
|
8
|
+
ignore?: string[];
|
|
9
|
+
/** Path attribute rules applied in declaration order, with later matches winning */
|
|
10
|
+
attributes?: VCSAttributeRule[];
|
|
11
|
+
/** Optional separate storage for VCS metadata */
|
|
12
|
+
vcsPath?: {
|
|
13
|
+
/** Filesystem for metadata storage (defaults to config.fs) */
|
|
14
|
+
fs?: VirtualFS;
|
|
15
|
+
/** Path for metadata storage (defaults to `${config.path}/.vcs`) */
|
|
16
|
+
path?: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export interface HeadRef {
|
|
20
|
+
ref?: string;
|
|
21
|
+
revision?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface BranchRef {
|
|
24
|
+
revision: number;
|
|
25
|
+
}
|
|
26
|
+
export interface RevisionCounter {
|
|
27
|
+
next: number;
|
|
28
|
+
}
|
|
29
|
+
export interface VCSConfigFile {
|
|
30
|
+
defaultBranch: string;
|
|
31
|
+
}
|
|
32
|
+
export interface FileEntry {
|
|
33
|
+
kind?: "file";
|
|
34
|
+
content: string;
|
|
35
|
+
size: number;
|
|
36
|
+
}
|
|
37
|
+
export interface DirectoryEntry {
|
|
38
|
+
kind: "directory";
|
|
39
|
+
size: 0;
|
|
40
|
+
content?: undefined;
|
|
41
|
+
}
|
|
42
|
+
export type TreeEntry = FileEntry | DirectoryEntry;
|
|
43
|
+
export interface TreeManifest {
|
|
44
|
+
[path: string]: TreeEntry;
|
|
45
|
+
}
|
|
46
|
+
export type VCSDiffMode = "text" | "binary" | "none";
|
|
47
|
+
export interface VCSAttributeRule {
|
|
48
|
+
pattern: string;
|
|
49
|
+
binary?: boolean;
|
|
50
|
+
diff?: VCSDiffMode;
|
|
51
|
+
}
|
|
52
|
+
export interface VCSResolvedAttributes {
|
|
53
|
+
binary: boolean;
|
|
54
|
+
diff: VCSDiffMode;
|
|
55
|
+
}
|
|
56
|
+
export interface DiffEntry {
|
|
57
|
+
type: "add" | "modify" | "delete";
|
|
58
|
+
path: string;
|
|
59
|
+
binary: boolean;
|
|
60
|
+
diff: VCSDiffMode;
|
|
61
|
+
entryKind?: "file" | "directory";
|
|
62
|
+
previousEntryKind?: "file" | "directory";
|
|
63
|
+
content?: string;
|
|
64
|
+
previousContent?: string;
|
|
65
|
+
}
|
|
66
|
+
export interface Revision {
|
|
67
|
+
id: number;
|
|
68
|
+
parent: number | null;
|
|
69
|
+
branch: string;
|
|
70
|
+
message: string;
|
|
71
|
+
timestamp: string;
|
|
72
|
+
changes: DiffEntry[];
|
|
73
|
+
tree: TreeManifest;
|
|
74
|
+
}
|
|
75
|
+
export interface CommitOptions {
|
|
76
|
+
paths?: string[];
|
|
77
|
+
}
|
|
78
|
+
export interface CheckoutOptions {
|
|
79
|
+
force?: boolean;
|
|
80
|
+
paths?: string[];
|
|
81
|
+
}
|
|
82
|
+
export interface LogOptions {
|
|
83
|
+
path?: string;
|
|
84
|
+
branch?: string;
|
|
85
|
+
limit?: number;
|
|
86
|
+
}
|
|
87
|
+
export interface LogEntry {
|
|
88
|
+
id: number;
|
|
89
|
+
parent: number | null;
|
|
90
|
+
branch: string;
|
|
91
|
+
message: string;
|
|
92
|
+
timestamp: string;
|
|
93
|
+
paths: string[];
|
|
94
|
+
}
|
|
95
|
+
export interface BranchInfo {
|
|
96
|
+
name: string;
|
|
97
|
+
revision: number;
|
|
98
|
+
current: boolean;
|
|
99
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { VCSConfig, Revision, DiffEntry, CommitOptions, CheckoutOptions, LogOptions, LogEntry, BranchInfo } from "./types.ts";
|
|
2
|
+
export declare class VersionControlSystem {
|
|
3
|
+
private readonly workFs;
|
|
4
|
+
private readonly workPath;
|
|
5
|
+
private readonly storage;
|
|
6
|
+
private readonly vcsInternalPath;
|
|
7
|
+
private readonly rules;
|
|
8
|
+
constructor(config: VCSConfig);
|
|
9
|
+
/** Initialize the .vcs directory. Called automatically on first operation if needed. */
|
|
10
|
+
init(): Promise<void>;
|
|
11
|
+
private ensureInit;
|
|
12
|
+
/** Get the current HEAD revision number, or null if no commits yet. */
|
|
13
|
+
private resolveHead;
|
|
14
|
+
/** Get current HEAD manifest, or empty if no commits. */
|
|
15
|
+
private headManifest;
|
|
16
|
+
/** Commit all pending changes, or selective changes if paths are provided. */
|
|
17
|
+
commit(message: string, opts?: CommitOptions): Promise<Revision>;
|
|
18
|
+
/** Checkout a revision number or branch name. */
|
|
19
|
+
checkout(target: string | number, opts?: CheckoutOptions): Promise<void>;
|
|
20
|
+
/** Create a new branch at HEAD. */
|
|
21
|
+
branch(name: string): Promise<void>;
|
|
22
|
+
/** List all branches. */
|
|
23
|
+
branches(): Promise<BranchInfo[]>;
|
|
24
|
+
/** Get revision history. */
|
|
25
|
+
log(opts?: LogOptions): Promise<LogEntry[]>;
|
|
26
|
+
/** Get uncommitted changes as DiffEntry[]. */
|
|
27
|
+
status(): Promise<DiffEntry[]>;
|
|
28
|
+
/** Diff between two revisions. */
|
|
29
|
+
diff(revA: number, revB: number): Promise<DiffEntry[]>;
|
|
30
|
+
/** Get current HEAD info. */
|
|
31
|
+
head(): Promise<{
|
|
32
|
+
branch: string | null;
|
|
33
|
+
revision: number | null;
|
|
34
|
+
}>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { VirtualFS } from "../types.ts";
|
|
2
|
+
interface WalkTreeOptions {
|
|
3
|
+
enterDirectory?: (relPath: string) => boolean | Promise<boolean>;
|
|
4
|
+
includeFile?: (relPath: string) => boolean | Promise<boolean>;
|
|
5
|
+
includeDirectory?: (relPath: string, info: {
|
|
6
|
+
empty: boolean;
|
|
7
|
+
}) => boolean | Promise<boolean>;
|
|
8
|
+
}
|
|
9
|
+
export interface WalkTreeEntry {
|
|
10
|
+
path: string;
|
|
11
|
+
kind: "file" | "directory";
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Recursively walk a directory tree and return all file paths
|
|
15
|
+
* relative to the given root.
|
|
16
|
+
*/
|
|
17
|
+
export declare function walkTree(fs: VirtualFS, root: string, options?: WalkTreeOptions): Promise<string[]>;
|
|
18
|
+
export declare function walkTreeEntries(fs: VirtualFS, root: string, options?: WalkTreeOptions): Promise<WalkTreeEntry[]>;
|
|
19
|
+
export {};
|
package/package.json
CHANGED