@innovatorved/code-lab 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/ARCHITECTURE.md +2 -2
- package/CHANGELOG.md +21 -0
- package/README.md +4 -28
- package/dist/code-lab-sql.esm.js +1 -1
- package/dist/code-lab-sql.umd.js +2 -2
- package/dist/code-lab.css +1553 -98
- package/dist/code-lab.d.ts +1 -1
- package/dist/code-lab.esm.js +11883 -1619
- package/dist/code-lab.js +11882 -1618
- package/dist/code-lab.umd.js +11882 -1618
- package/dist/diagnostics.d.ts +3 -0
- package/dist/extensions/cpp.esm.js +27 -29
- package/dist/extensions/cpp.js +27 -29
- package/dist/extensions/python.esm.js +27 -28
- package/dist/extensions/python.js +27 -28
- package/dist/index.d.ts +1 -1
- package/dist/packages/settings/src/configuration-service.d.ts +7 -0
- package/dist/packages/settings/src/index.d.ts +1 -1
- package/dist/packages/ui/src/index.d.ts +2 -2
- package/dist/packages/ui/src/plugin-registry.d.ts +1 -0
- package/dist/packages/ui/src/settings-dialog.d.ts +31 -3
- package/dist/packages/ui/src/themes.d.ts +5 -0
- package/dist/preview-sw.js +1 -1
- package/dist/runtime.esm.js +1 -1
- package/dist/runtime.js +1 -1
- package/dist/schemas.d.ts +1 -1
- package/dist/scm.d.ts +92 -11
- package/dist/shell.esm.js +937 -113
- package/dist/shell.js +937 -113
- package/dist/storage.d.ts +47 -0
- package/dist/terminal.esm.js +1 -1
- package/dist/terminal.js +1 -1
- package/dist/workbench/index.d.ts +1 -1
- package/dist/workbench/styles.d.ts +1 -1
- package/package.json +2 -2
- package/packages/commands/package.json +1 -1
- package/packages/core/package.json +1 -1
- package/packages/editor/package.json +1 -1
- package/packages/fs/package.json +1 -1
- package/packages/preview/package.json +1 -1
- package/packages/runtime/package.json +1 -1
- package/packages/settings/package.json +1 -1
- package/packages/settings/src/builtin-schemas.ts +17 -6
- package/packages/settings/src/configuration-service.ts +72 -6
- package/packages/settings/src/index.ts +1 -0
- package/packages/sql/package.json +1 -1
- package/packages/terminal/package.json +1 -1
- package/packages/ui/package.json +1 -1
- package/packages/ui/src/index.ts +4 -0
- package/packages/ui/src/plugin-registry.ts +17 -2
- package/packages/ui/src/settings-dialog.ts +374 -92
- package/packages/ui/src/themes.ts +105 -12
- package/src/code-lab.css +1553 -98
- package/src/diagnostics.ts +139 -7
- package/src/extensions/cpp.ts +33 -33
- package/src/extensions/python.ts +33 -32
- package/src/index.ts +1 -1
- package/src/schemas.ts +1 -1
- package/src/scm.ts +781 -58
- package/src/shell.ts +936 -112
- package/src/storage.ts +336 -3
- package/src/workbench/icons.ts +8 -1
- package/src/workbench/styles.ts +25 -19
- package/src/workbench.ts +12010 -3814
package/src/scm.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @innovatorved/code-lab — Interactive Git Source Control (SCM) Engine
|
|
3
3
|
* ============================================================================
|
|
4
|
-
* Pure TypeScript in-browser Git status, staging, unstaging, and
|
|
5
|
-
* backed by workspace VFS state. Powers both the interactive
|
|
6
|
-
* and shell/CLI Git operations.
|
|
4
|
+
* Pure TypeScript in-browser Git status, staging, unstaging, discard, and
|
|
5
|
+
* commit engine backed by workspace VFS state. Powers both the interactive
|
|
6
|
+
* Source Control UI and shell/CLI Git operations with a unified GitState schema.
|
|
7
7
|
*
|
|
8
8
|
* Strict leaf module: fully typed, zero explicit any, zero empty catches.
|
|
9
9
|
*/
|
|
@@ -18,21 +18,46 @@ export interface GitFileStatus {
|
|
|
18
18
|
newContent?: string;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
export interface GitVfsLike {
|
|
22
|
+
existsSync?(path: string): boolean;
|
|
23
|
+
statSync?(path: string): { isDirectory(): boolean };
|
|
24
|
+
readdirSync?(path: string): string[];
|
|
25
|
+
readFileSync?(path: string, encoding?: string): string | Uint8Array;
|
|
26
|
+
writeFileSync?(path: string, data: string): void;
|
|
27
|
+
mkdirSync?(path: string, options?: { recursive?: boolean }): unknown;
|
|
28
|
+
unlinkSync?(path: string): void;
|
|
29
|
+
rmSync?(path: string, options?: { recursive?: boolean; force?: boolean }): void;
|
|
30
|
+
files?: Map<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface GitShellLike {
|
|
34
|
+
cwd?: string;
|
|
35
|
+
vfs?: GitVfsLike | null;
|
|
36
|
+
gitState?: GitState | null;
|
|
37
|
+
_emitVfsMutation?(type: string, absPath: string): void;
|
|
38
|
+
}
|
|
39
|
+
|
|
21
40
|
export interface GitCommitRecord {
|
|
22
41
|
hash: string;
|
|
23
42
|
message: string;
|
|
24
43
|
timestamp: number;
|
|
25
44
|
author?: string;
|
|
26
|
-
|
|
45
|
+
date?: string;
|
|
46
|
+
files?: Record<string, string>;
|
|
27
47
|
}
|
|
28
48
|
|
|
29
49
|
export interface GitState {
|
|
50
|
+
initialized?: boolean;
|
|
51
|
+
repoRoot?: string;
|
|
30
52
|
branch: string;
|
|
31
|
-
branches?: string[]
|
|
53
|
+
branches?: string[] | Set<string>;
|
|
54
|
+
remoteUrl?: string | null;
|
|
55
|
+
remotes?: Record<string, string>;
|
|
32
56
|
headCommit: string;
|
|
33
57
|
headFiles: Record<string, string>;
|
|
34
58
|
headSnapshot?: Record<string, string>;
|
|
35
59
|
staged: Map<string, string | null> | Set<string>;
|
|
60
|
+
stagedFiles?: Map<string, string | null>;
|
|
36
61
|
commits: GitCommitRecord[];
|
|
37
62
|
}
|
|
38
63
|
|
|
@@ -57,6 +82,18 @@ export interface GitStatusResult {
|
|
|
57
82
|
isClean: boolean;
|
|
58
83
|
}
|
|
59
84
|
|
|
85
|
+
export interface CreateGitStateOptions {
|
|
86
|
+
initialized?: boolean;
|
|
87
|
+
repoRoot?: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface InitRepositoryOptions {
|
|
91
|
+
branch?: string;
|
|
92
|
+
repoRoot?: string;
|
|
93
|
+
snapshotExisting?: boolean;
|
|
94
|
+
initialFiles?: Record<string, string>;
|
|
95
|
+
}
|
|
96
|
+
|
|
60
97
|
/**
|
|
61
98
|
* Computes a deterministic 7-character short hash for a commit payload.
|
|
62
99
|
*/
|
|
@@ -74,30 +111,515 @@ export function computeShortCommitHash(input: string): string {
|
|
|
74
111
|
return combined.toString(16).padStart(7, '0').slice(0, 7);
|
|
75
112
|
}
|
|
76
113
|
|
|
114
|
+
export function isIgnoredScmPath(relPath: string): boolean {
|
|
115
|
+
if (!relPath) return true;
|
|
116
|
+
if (
|
|
117
|
+
relPath === '.git' ||
|
|
118
|
+
relPath.startsWith('.git/') ||
|
|
119
|
+
relPath.includes('/.git/') ||
|
|
120
|
+
relPath.endsWith('/.git')
|
|
121
|
+
) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
if (
|
|
125
|
+
relPath === 'node_modules' ||
|
|
126
|
+
relPath.startsWith('node_modules/') ||
|
|
127
|
+
relPath.includes('/node_modules/') ||
|
|
128
|
+
relPath.endsWith('/node_modules')
|
|
129
|
+
) {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function normalizeScmRelPath(rawPath: string, repoRoot = '/workspace'): string {
|
|
136
|
+
if (typeof rawPath !== 'string') return '';
|
|
137
|
+
let p = rawPath.trim().replace(/\\/g, '/');
|
|
138
|
+
if (!p) return '';
|
|
139
|
+
|
|
140
|
+
const cleanRoot = (repoRoot || '/workspace').replace(/\\/g, '/').replace(/\/+$/, '') || '/';
|
|
141
|
+
if (p === cleanRoot) return '';
|
|
142
|
+
if (cleanRoot !== '/' && p.startsWith(`${cleanRoot}/`)) {
|
|
143
|
+
p = p.slice(cleanRoot.length + 1);
|
|
144
|
+
} else if (p.startsWith('/workspace/')) {
|
|
145
|
+
p = p.slice('/workspace/'.length);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
while (p.startsWith('./')) {
|
|
149
|
+
p = p.slice(2);
|
|
150
|
+
}
|
|
151
|
+
if (p.startsWith('/')) {
|
|
152
|
+
p = p.replace(/^\/+/, '');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const out: string[] = [];
|
|
156
|
+
for (const seg of p.split('/')) {
|
|
157
|
+
if (!seg || seg === '.') continue;
|
|
158
|
+
if (seg === '..') {
|
|
159
|
+
if (out.length === 0) return '';
|
|
160
|
+
out.pop();
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
out.push(seg);
|
|
164
|
+
}
|
|
165
|
+
return out.join('/');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function cleanFileMap(
|
|
169
|
+
rawFiles: Record<string, string> | undefined,
|
|
170
|
+
repoRoot = '/workspace'
|
|
171
|
+
): Record<string, string> {
|
|
172
|
+
const out: Record<string, string> = {};
|
|
173
|
+
if (!rawFiles || typeof rawFiles !== 'object') return out;
|
|
174
|
+
for (const [k, v] of Object.entries(rawFiles)) {
|
|
175
|
+
const rel = normalizeScmRelPath(k, repoRoot);
|
|
176
|
+
if (!rel || isIgnoredScmPath(rel)) continue;
|
|
177
|
+
out[rel] = String(v ?? '');
|
|
178
|
+
}
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function isUnbornGitHead(gitState: GitState | null | undefined): boolean {
|
|
183
|
+
return (
|
|
184
|
+
(!Array.isArray(gitState?.commits) || gitState.commits.length === 0) &&
|
|
185
|
+
!gitState?.headCommit
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function isEffectiveStagedChange(
|
|
190
|
+
gitState: GitState | null | undefined,
|
|
191
|
+
relPath: string,
|
|
192
|
+
stagedVal: string | null | undefined,
|
|
193
|
+
resolvedHeadFiles?: Record<string, string>
|
|
194
|
+
): boolean {
|
|
195
|
+
if (!relPath || isIgnoredScmPath(relPath)) return false;
|
|
196
|
+
const repoRoot = gitState?.repoRoot || '/workspace';
|
|
197
|
+
const headFiles =
|
|
198
|
+
resolvedHeadFiles ?? cleanFileMap(gitState?.headFiles || gitState?.headSnapshot || {}, repoRoot);
|
|
199
|
+
const inHead = Object.prototype.hasOwnProperty.call(headFiles, relPath);
|
|
200
|
+
const normalizedVal = stagedVal === undefined || stagedVal === null ? null : String(stagedVal);
|
|
201
|
+
|
|
202
|
+
if (normalizedVal === null) {
|
|
203
|
+
return inHead;
|
|
204
|
+
}
|
|
205
|
+
if (!inHead || isUnbornGitHead(gitState)) {
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
return normalizedVal !== headFiles[relPath];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function applyStagedEntry(
|
|
212
|
+
gitState: GitState | null | undefined,
|
|
213
|
+
map: Map<string, string | null>,
|
|
214
|
+
rawPath: string,
|
|
215
|
+
rawVal: string | null | undefined,
|
|
216
|
+
resolvedHeadFiles?: Record<string, string>
|
|
217
|
+
): void {
|
|
218
|
+
const repoRoot = gitState?.repoRoot || '/workspace';
|
|
219
|
+
const rel = normalizeScmRelPath(rawPath, repoRoot);
|
|
220
|
+
if (!rel || isIgnoredScmPath(rel)) return;
|
|
221
|
+
const normalizedVal = rawVal === undefined || rawVal === null ? null : String(rawVal);
|
|
222
|
+
if (isEffectiveStagedChange(gitState, rel, normalizedVal, resolvedHeadFiles)) {
|
|
223
|
+
map.set(rel, normalizedVal);
|
|
224
|
+
} else {
|
|
225
|
+
map.delete(rel);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function pruneStagedMapInPlace(
|
|
230
|
+
gitState: GitState | null | undefined,
|
|
231
|
+
map: Map<string, string | null>,
|
|
232
|
+
resolvedHeadFiles?: Record<string, string>
|
|
233
|
+
): void {
|
|
234
|
+
const repoRoot = gitState?.repoRoot || '/workspace';
|
|
235
|
+
const headFiles =
|
|
236
|
+
resolvedHeadFiles ?? cleanFileMap(gitState?.headFiles || gitState?.headSnapshot || {}, repoRoot);
|
|
237
|
+
for (const [rawK, rawV] of Array.from(map.entries())) {
|
|
238
|
+
const rel = normalizeScmRelPath(rawK, repoRoot);
|
|
239
|
+
if (rel !== rawK) {
|
|
240
|
+
map.delete(rawK);
|
|
241
|
+
}
|
|
242
|
+
if (!rel || isIgnoredScmPath(rel)) {
|
|
243
|
+
map.delete(rawK);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
const normalizedVal = rawV === undefined || rawV === null ? null : String(rawV);
|
|
247
|
+
if (isEffectiveStagedChange(gitState, rel, normalizedVal, headFiles)) {
|
|
248
|
+
map.set(rel, normalizedVal);
|
|
249
|
+
} else {
|
|
250
|
+
map.delete(rel);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function attachSetCompatShim(
|
|
256
|
+
map: Map<string, string | null>,
|
|
257
|
+
gitState?: GitState,
|
|
258
|
+
resolveContent?: (relPath: string) => string | null
|
|
259
|
+
): Map<string, string | null> {
|
|
260
|
+
const mapObj = map as unknown as Record<string, unknown>;
|
|
261
|
+
if (typeof mapObj.add !== 'function') {
|
|
262
|
+
Object.defineProperty(map, 'add', {
|
|
263
|
+
value: (rawPath: string) => {
|
|
264
|
+
const repoRoot = gitState?.repoRoot || '/workspace';
|
|
265
|
+
const rel = normalizeScmRelPath(rawPath, repoRoot);
|
|
266
|
+
if (rel && !isIgnoredScmPath(rel)) {
|
|
267
|
+
const val = resolveContent ? resolveContent(rel) : null;
|
|
268
|
+
applyStagedEntry(gitState, map, rel, val);
|
|
269
|
+
}
|
|
270
|
+
return map;
|
|
271
|
+
},
|
|
272
|
+
enumerable: false,
|
|
273
|
+
configurable: true,
|
|
274
|
+
writable: true
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
return map;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Synchronizes `gitState.headFiles` and `gitState.headSnapshot` so both
|
|
282
|
+
* properties reference the exact same clean `Record<string, string>` object.
|
|
283
|
+
*/
|
|
284
|
+
export function syncGitHead(
|
|
285
|
+
gitState: GitState,
|
|
286
|
+
nextHead?: Record<string, string>
|
|
287
|
+
): Record<string, string> {
|
|
288
|
+
const repoRoot = gitState.repoRoot || '/workspace';
|
|
289
|
+
const source =
|
|
290
|
+
nextHead !== undefined ? nextHead : (gitState.headFiles || gitState.headSnapshot || {});
|
|
291
|
+
const synced = cleanFileMap(source, repoRoot);
|
|
292
|
+
gitState.headFiles = synced;
|
|
293
|
+
gitState.headSnapshot = synced;
|
|
294
|
+
return synced;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function ensureStagedMap(
|
|
298
|
+
gitState: GitState,
|
|
299
|
+
vfsFiles?: Record<string, string>
|
|
300
|
+
): Map<string, string | null> {
|
|
301
|
+
const repoRoot = gitState.repoRoot || '/workspace';
|
|
302
|
+
const head = cleanFileMap(gitState.headFiles || gitState.headSnapshot || {}, repoRoot);
|
|
303
|
+
const resolveContent = (rel: string): string | null => {
|
|
304
|
+
if (vfsFiles) {
|
|
305
|
+
return Object.prototype.hasOwnProperty.call(vfsFiles, rel) ? vfsFiles[rel] : null;
|
|
306
|
+
}
|
|
307
|
+
return Object.prototype.hasOwnProperty.call(head, rel) ? head[rel] : null;
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
if (gitState.staged instanceof Map) {
|
|
311
|
+
const shimmed = attachSetCompatShim(gitState.staged, gitState, resolveContent);
|
|
312
|
+
pruneStagedMapInPlace(gitState, shimmed, head);
|
|
313
|
+
gitState.stagedFiles = shimmed;
|
|
314
|
+
return shimmed;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const migrated = attachSetCompatShim(new Map<string, string | null>(), gitState, resolveContent);
|
|
318
|
+
|
|
319
|
+
if (gitState.staged instanceof Set) {
|
|
320
|
+
for (const rawPath of gitState.staged.values()) {
|
|
321
|
+
const rel = normalizeScmRelPath(rawPath, repoRoot);
|
|
322
|
+
if (!rel || isIgnoredScmPath(rel)) continue;
|
|
323
|
+
applyStagedEntry(gitState, migrated, rel, resolveContent(rel), head);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
gitState.staged = migrated;
|
|
328
|
+
gitState.stagedFiles = migrated;
|
|
329
|
+
return migrated;
|
|
330
|
+
}
|
|
331
|
+
|
|
77
332
|
/**
|
|
78
|
-
* Initializes
|
|
333
|
+
* Initializes an in-memory Git repository state from initial VFS files,
|
|
334
|
+
* keeping `headFiles` / `headSnapshot` and `staged` / `stagedFiles` synchronized.
|
|
79
335
|
*/
|
|
80
336
|
export function createGitState(
|
|
81
337
|
initialFiles: Record<string, string> = {},
|
|
82
|
-
branch = 'main'
|
|
338
|
+
branch = 'main',
|
|
339
|
+
options?: CreateGitStateOptions
|
|
83
340
|
): GitState {
|
|
84
|
-
const
|
|
85
|
-
const
|
|
86
|
-
|
|
341
|
+
const repoRoot = options?.repoRoot || '/workspace';
|
|
342
|
+
const isInit = options?.initialized !== false;
|
|
343
|
+
let _initialized = isInit;
|
|
344
|
+
let _repoRoot = repoRoot;
|
|
345
|
+
let _headFiles: Record<string, string> = isInit ? cleanFileMap(initialFiles, repoRoot) : {};
|
|
346
|
+
let _headCommit = isInit ? computeShortCommitHash(JSON.stringify(_headFiles) + ':init') : '';
|
|
347
|
+
const now = Date.now();
|
|
348
|
+
let _commits: GitCommitRecord[] = isInit
|
|
349
|
+
? [
|
|
350
|
+
{
|
|
351
|
+
hash: _headCommit,
|
|
352
|
+
message: 'Initial commit',
|
|
353
|
+
timestamp: now,
|
|
354
|
+
author: 'CodeLab Developer <dev@codelab.internal>',
|
|
355
|
+
date: new Date(now).toUTCString(),
|
|
356
|
+
files: { ..._headFiles }
|
|
357
|
+
}
|
|
358
|
+
]
|
|
359
|
+
: [];
|
|
360
|
+
|
|
361
|
+
const stateObj = {
|
|
87
362
|
branch,
|
|
88
363
|
branches: [branch],
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
364
|
+
remoteUrl: null as string | null,
|
|
365
|
+
remotes: {} as Record<string, string>
|
|
366
|
+
} as unknown as GitState;
|
|
367
|
+
|
|
368
|
+
const resolveFromHead = (rel: string): string | null =>
|
|
369
|
+
Object.prototype.hasOwnProperty.call(_headFiles, rel) ? _headFiles[rel] : null;
|
|
370
|
+
|
|
371
|
+
let _stagedMap = attachSetCompatShim(new Map<string, string | null>(), stateObj, resolveFromHead);
|
|
372
|
+
|
|
373
|
+
Object.defineProperties(stateObj, {
|
|
374
|
+
initialized: {
|
|
375
|
+
enumerable: true,
|
|
376
|
+
configurable: true,
|
|
377
|
+
get(): boolean {
|
|
378
|
+
return _initialized;
|
|
379
|
+
},
|
|
380
|
+
set(v: boolean) {
|
|
381
|
+
_initialized = Boolean(v);
|
|
382
|
+
}
|
|
383
|
+
},
|
|
384
|
+
repoRoot: {
|
|
385
|
+
enumerable: true,
|
|
386
|
+
configurable: true,
|
|
387
|
+
get(): string {
|
|
388
|
+
return _repoRoot;
|
|
389
|
+
},
|
|
390
|
+
set(v: string) {
|
|
391
|
+
_repoRoot = String(v || '/workspace');
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
headCommit: {
|
|
395
|
+
enumerable: true,
|
|
396
|
+
configurable: true,
|
|
397
|
+
get(): string {
|
|
398
|
+
return _headCommit;
|
|
399
|
+
},
|
|
400
|
+
set(v: string) {
|
|
401
|
+
_headCommit = String(v || '');
|
|
402
|
+
}
|
|
403
|
+
},
|
|
404
|
+
headFiles: {
|
|
405
|
+
enumerable: true,
|
|
406
|
+
configurable: true,
|
|
407
|
+
get(): Record<string, string> {
|
|
408
|
+
return _headFiles;
|
|
409
|
+
},
|
|
410
|
+
set(v: Record<string, string>) {
|
|
411
|
+
_headFiles = v && typeof v === 'object' ? v : {};
|
|
412
|
+
}
|
|
413
|
+
},
|
|
414
|
+
headSnapshot: {
|
|
415
|
+
enumerable: true,
|
|
416
|
+
configurable: true,
|
|
417
|
+
get(): Record<string, string> {
|
|
418
|
+
return _headFiles;
|
|
419
|
+
},
|
|
420
|
+
set(v: Record<string, string>) {
|
|
421
|
+
_headFiles = v && typeof v === 'object' ? v : {};
|
|
422
|
+
}
|
|
423
|
+
},
|
|
424
|
+
staged: {
|
|
425
|
+
enumerable: true,
|
|
426
|
+
configurable: true,
|
|
427
|
+
get(): Map<string, string | null> {
|
|
428
|
+
return _stagedMap;
|
|
429
|
+
},
|
|
430
|
+
set(v: Map<string, string | null> | Set<string>) {
|
|
431
|
+
if (v instanceof Map) {
|
|
432
|
+
_stagedMap = attachSetCompatShim(v, stateObj, resolveFromHead);
|
|
433
|
+
pruneStagedMapInPlace(stateObj, _stagedMap);
|
|
434
|
+
} else {
|
|
435
|
+
const next = attachSetCompatShim(
|
|
436
|
+
new Map<string, string | null>(),
|
|
437
|
+
stateObj,
|
|
438
|
+
resolveFromHead
|
|
439
|
+
);
|
|
440
|
+
if (v instanceof Set) {
|
|
441
|
+
for (const raw of v.values()) {
|
|
442
|
+
const rel = normalizeScmRelPath(raw, _repoRoot);
|
|
443
|
+
if (!rel || isIgnoredScmPath(rel)) continue;
|
|
444
|
+
applyStagedEntry(stateObj, next, rel, resolveFromHead(rel));
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
_stagedMap = next;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
},
|
|
451
|
+
stagedFiles: {
|
|
452
|
+
enumerable: true,
|
|
453
|
+
configurable: true,
|
|
454
|
+
get(): Map<string, string | null> {
|
|
455
|
+
return _stagedMap;
|
|
456
|
+
},
|
|
457
|
+
set(v: Map<string, string | null>) {
|
|
458
|
+
if (v instanceof Map) {
|
|
459
|
+
_stagedMap = attachSetCompatShim(v, stateObj, resolveFromHead);
|
|
460
|
+
pruneStagedMapInPlace(stateObj, _stagedMap);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
},
|
|
464
|
+
commits: {
|
|
465
|
+
enumerable: true,
|
|
466
|
+
configurable: true,
|
|
467
|
+
get(): GitCommitRecord[] {
|
|
468
|
+
return _commits;
|
|
469
|
+
},
|
|
470
|
+
set(v: GitCommitRecord[]) {
|
|
471
|
+
_commits = Array.isArray(v) ? v : [];
|
|
472
|
+
if (_commits.length > 0 && _commits[0]?.hash) {
|
|
473
|
+
_headCommit = _commits[0].hash;
|
|
474
|
+
} else if (_commits.length === 0) {
|
|
475
|
+
_headCommit = '';
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
return stateObj;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function readVfsTree(vfs: GitVfsLike, rootDir: string): Record<string, string> {
|
|
485
|
+
const out: Record<string, string> = {};
|
|
486
|
+
if (
|
|
487
|
+
!vfs ||
|
|
488
|
+
typeof vfs.existsSync !== 'function' ||
|
|
489
|
+
typeof vfs.readdirSync !== 'function' ||
|
|
490
|
+
typeof vfs.statSync !== 'function' ||
|
|
491
|
+
typeof vfs.readFileSync !== 'function'
|
|
492
|
+
) {
|
|
493
|
+
return out;
|
|
494
|
+
}
|
|
495
|
+
const cleanRoot = rootDir.replace(/\/+$/, '') || '/';
|
|
496
|
+
if (!vfs.existsSync(cleanRoot)) return out;
|
|
497
|
+
|
|
498
|
+
const walk = (dir: string): void => {
|
|
499
|
+
let entries: string[] = [];
|
|
500
|
+
try {
|
|
501
|
+
entries = vfs.readdirSync ? vfs.readdirSync(dir) : [];
|
|
502
|
+
} catch (err) {
|
|
503
|
+
const _ignored = err;
|
|
504
|
+
void _ignored;
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
for (const name of entries) {
|
|
508
|
+
if (name === '.git' || name === 'node_modules') continue;
|
|
509
|
+
const full = dir === '/' ? `/${name}` : `${dir}/${name}`;
|
|
510
|
+
try {
|
|
511
|
+
const st = vfs.statSync ? vfs.statSync(full) : null;
|
|
512
|
+
if (st && st.isDirectory()) {
|
|
513
|
+
walk(full);
|
|
514
|
+
} else {
|
|
515
|
+
const rel = normalizeScmRelPath(full, cleanRoot);
|
|
516
|
+
if (!rel || isIgnoredScmPath(rel)) continue;
|
|
517
|
+
const raw = vfs.readFileSync ? vfs.readFileSync(full, 'utf8') : '';
|
|
518
|
+
out[rel] = typeof raw === 'string' ? raw : new TextDecoder().decode(raw);
|
|
519
|
+
}
|
|
520
|
+
} catch (err) {
|
|
521
|
+
const _ignored = err;
|
|
522
|
+
void _ignored;
|
|
98
523
|
}
|
|
99
|
-
|
|
524
|
+
}
|
|
100
525
|
};
|
|
526
|
+
|
|
527
|
+
walk(cleanRoot);
|
|
528
|
+
return out;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function isGitShellLike(target: GitShellLike | GitState): target is GitShellLike {
|
|
532
|
+
return (
|
|
533
|
+
Boolean(target && typeof target === 'object') &&
|
|
534
|
+
('vfs' in target || 'gitState' in target || ('cwd' in target && !('headFiles' in target)))
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Initializes a Git repository on a `PosixShell` (writing `.git/HEAD` & `.git/config`)
|
|
540
|
+
* or directly on a `GitState` object. Defaults `snapshotExisting` to `false` so
|
|
541
|
+
* existing workspace files appear as untracked until staged and committed.
|
|
542
|
+
*/
|
|
543
|
+
export function initRepository(
|
|
544
|
+
target: GitShellLike | GitState,
|
|
545
|
+
options?: InitRepositoryOptions
|
|
546
|
+
): GitState {
|
|
547
|
+
if (isGitShellLike(target)) {
|
|
548
|
+
const repoRoot = (
|
|
549
|
+
options?.repoRoot ||
|
|
550
|
+
target.gitState?.repoRoot ||
|
|
551
|
+
target.cwd ||
|
|
552
|
+
'/workspace'
|
|
553
|
+
).replace(/\/+$/, '') || '/';
|
|
554
|
+
const branch = options?.branch || target.gitState?.branch || 'main';
|
|
555
|
+
const vfs = target.vfs ?? null;
|
|
556
|
+
|
|
557
|
+
if (vfs) {
|
|
558
|
+
const gitDir = repoRoot === '/' ? '/.git' : `${repoRoot}/.git`;
|
|
559
|
+
const headPath = `${gitDir}/HEAD`;
|
|
560
|
+
const configPath = `${gitDir}/config`;
|
|
561
|
+
if (typeof vfs.mkdirSync === 'function' && (!vfs.existsSync || !vfs.existsSync(gitDir))) {
|
|
562
|
+
vfs.mkdirSync(gitDir, { recursive: true });
|
|
563
|
+
}
|
|
564
|
+
vfs.writeFileSync?.(headPath, `ref: refs/heads/${branch}\n`);
|
|
565
|
+
vfs.writeFileSync?.(
|
|
566
|
+
configPath,
|
|
567
|
+
`[core]\n\trepositoryformatversion = 0\n\tfilemode = true\n\tbare = false\n`
|
|
568
|
+
);
|
|
569
|
+
target._emitVfsMutation?.('create', headPath);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
const snap = options?.snapshotExisting
|
|
573
|
+
? (options.initialFiles
|
|
574
|
+
? cleanFileMap(options.initialFiles, repoRoot)
|
|
575
|
+
: (vfs ? readVfsTree(vfs, repoRoot) : {}))
|
|
576
|
+
: {};
|
|
577
|
+
|
|
578
|
+
let gs = target.gitState;
|
|
579
|
+
if (!gs) {
|
|
580
|
+
gs = createGitState(snap, branch, { initialized: true, repoRoot });
|
|
581
|
+
if (!options?.snapshotExisting) {
|
|
582
|
+
gs.commits = [];
|
|
583
|
+
gs.headCommit = '';
|
|
584
|
+
}
|
|
585
|
+
target.gitState = gs;
|
|
586
|
+
return gs;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
gs.initialized = true;
|
|
590
|
+
gs.repoRoot = repoRoot;
|
|
591
|
+
gs.branch = branch;
|
|
592
|
+
if (gs.branches instanceof Set) {
|
|
593
|
+
gs.branches.add(branch);
|
|
594
|
+
} else if (Array.isArray(gs.branches)) {
|
|
595
|
+
if (!gs.branches.includes(branch)) gs.branches.push(branch);
|
|
596
|
+
} else {
|
|
597
|
+
gs.branches = [branch];
|
|
598
|
+
}
|
|
599
|
+
ensureStagedMap(gs).clear();
|
|
600
|
+
syncGitHead(gs, snap);
|
|
601
|
+
if (!options?.snapshotExisting && gs.commits.length === 0) {
|
|
602
|
+
gs.headCommit = '';
|
|
603
|
+
}
|
|
604
|
+
return gs;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
const gs = target;
|
|
608
|
+
const repoRoot = options?.repoRoot || gs.repoRoot || '/workspace';
|
|
609
|
+
const branch = options?.branch || gs.branch || 'main';
|
|
610
|
+
gs.initialized = true;
|
|
611
|
+
gs.repoRoot = repoRoot;
|
|
612
|
+
gs.branch = branch;
|
|
613
|
+
if (gs.branches instanceof Set) {
|
|
614
|
+
gs.branches.add(branch);
|
|
615
|
+
} else if (Array.isArray(gs.branches)) {
|
|
616
|
+
if (!gs.branches.includes(branch)) gs.branches.push(branch);
|
|
617
|
+
} else {
|
|
618
|
+
gs.branches = [branch];
|
|
619
|
+
}
|
|
620
|
+
ensureStagedMap(gs).clear();
|
|
621
|
+
syncGitHead(gs, options?.snapshotExisting ? (options.initialFiles || {}) : {});
|
|
622
|
+
return gs;
|
|
101
623
|
}
|
|
102
624
|
|
|
103
625
|
/**
|
|
@@ -110,7 +632,8 @@ export function computeGitStatus(
|
|
|
110
632
|
arg2: string | Record<string, string> = '/workspace',
|
|
111
633
|
gitState?: GitState
|
|
112
634
|
): GitStatusResult {
|
|
113
|
-
let
|
|
635
|
+
let rawVfsFiles: Record<string, string>;
|
|
636
|
+
let repoRoot = '/workspace';
|
|
114
637
|
let state: GitState;
|
|
115
638
|
|
|
116
639
|
if (
|
|
@@ -119,22 +642,35 @@ export function computeGitStatus(
|
|
|
119
642
|
('headFiles' in arg1 || 'headSnapshot' in arg1 || 'staged' in arg1 || 'commits' in arg1)
|
|
120
643
|
) {
|
|
121
644
|
state = arg1 as GitState;
|
|
122
|
-
|
|
645
|
+
rawVfsFiles = (arg2 && typeof arg2 === 'object' ? arg2 : {}) as Record<string, string>;
|
|
646
|
+
repoRoot = state.repoRoot || '/workspace';
|
|
123
647
|
} else {
|
|
124
|
-
|
|
648
|
+
rawVfsFiles = (arg1 && typeof arg1 === 'object' ? arg1 : {}) as Record<string, string>;
|
|
649
|
+
repoRoot = typeof arg2 === 'string' && arg2 ? arg2 : (gitState?.repoRoot || '/workspace');
|
|
125
650
|
state = gitState || createGitState({});
|
|
126
651
|
}
|
|
127
652
|
|
|
128
|
-
const
|
|
653
|
+
const vfsFiles = cleanFileMap(rawVfsFiles, repoRoot);
|
|
654
|
+
const headFiles = cleanFileMap(state.headFiles || state.headSnapshot || {}, repoRoot);
|
|
655
|
+
const unbornHead = isUnbornGitHead(state);
|
|
656
|
+
|
|
129
657
|
const currentStaged = new Map<string, string | null>();
|
|
130
658
|
if (state.staged instanceof Map) {
|
|
131
|
-
for (const [
|
|
132
|
-
|
|
659
|
+
for (const [rawK, v] of state.staged.entries()) {
|
|
660
|
+
const k = normalizeScmRelPath(rawK, repoRoot);
|
|
661
|
+
if (!k || isIgnoredScmPath(k)) continue;
|
|
662
|
+
const normalizedV = v === undefined || v === null ? null : String(v);
|
|
663
|
+
if (!isEffectiveStagedChange(state, k, normalizedV, headFiles)) continue;
|
|
664
|
+
currentStaged.set(k, normalizedV);
|
|
133
665
|
}
|
|
134
666
|
} else if (state.staged instanceof Set) {
|
|
135
|
-
for (const
|
|
667
|
+
for (const rawK of state.staged.values()) {
|
|
668
|
+
const k = normalizeScmRelPath(rawK, repoRoot);
|
|
669
|
+
if (!k || isIgnoredScmPath(k)) continue;
|
|
136
670
|
const existsInVfs = Object.prototype.hasOwnProperty.call(vfsFiles, k);
|
|
137
|
-
|
|
671
|
+
const normalizedV = existsInVfs ? vfsFiles[k] : null;
|
|
672
|
+
if (!isEffectiveStagedChange(state, k, normalizedV, headFiles)) continue;
|
|
673
|
+
currentStaged.set(k, normalizedV);
|
|
138
674
|
}
|
|
139
675
|
}
|
|
140
676
|
|
|
@@ -145,6 +681,7 @@ export function computeGitStatus(
|
|
|
145
681
|
|
|
146
682
|
for (const [filePath, stagedContent] of currentStaged.entries()) {
|
|
147
683
|
const inHead = Object.prototype.hasOwnProperty.call(headFiles, filePath);
|
|
684
|
+
const effectiveInHead = inHead && !unbornHead;
|
|
148
685
|
if (stagedContent === null) {
|
|
149
686
|
if (inHead) {
|
|
150
687
|
staged.push({
|
|
@@ -157,18 +694,35 @@ export function computeGitStatus(
|
|
|
157
694
|
} else {
|
|
158
695
|
staged.push({
|
|
159
696
|
path: filePath,
|
|
160
|
-
status:
|
|
697
|
+
status: effectiveInHead ? 'modified' : 'added',
|
|
161
698
|
staged: true,
|
|
162
|
-
oldContent:
|
|
699
|
+
oldContent: effectiveInHead ? headFiles[filePath] : undefined,
|
|
163
700
|
newContent: stagedContent
|
|
164
701
|
});
|
|
702
|
+
// Detect worktree deletion of a staged file (MD / AD)
|
|
703
|
+
if (!Object.prototype.hasOwnProperty.call(vfsFiles, filePath)) {
|
|
704
|
+
deleted.push({
|
|
705
|
+
path: filePath,
|
|
706
|
+
status: 'deleted',
|
|
707
|
+
staged: false,
|
|
708
|
+
oldContent: stagedContent
|
|
709
|
+
});
|
|
710
|
+
}
|
|
165
711
|
}
|
|
166
712
|
}
|
|
167
713
|
|
|
168
|
-
for (const [filePath, content] of Object.entries(vfsFiles
|
|
714
|
+
for (const [filePath, content] of Object.entries(vfsFiles)) {
|
|
169
715
|
if (currentStaged.has(filePath)) {
|
|
170
716
|
const stagedVal = currentStaged.get(filePath);
|
|
171
|
-
if (stagedVal
|
|
717
|
+
if (stagedVal === null) {
|
|
718
|
+
// Staged for deletion, then recreated in working tree
|
|
719
|
+
untracked.push({
|
|
720
|
+
path: filePath,
|
|
721
|
+
status: 'untracked',
|
|
722
|
+
staged: false,
|
|
723
|
+
newContent: content
|
|
724
|
+
});
|
|
725
|
+
} else if (stagedVal !== content) {
|
|
172
726
|
modified.push({
|
|
173
727
|
path: filePath,
|
|
174
728
|
status: 'modified',
|
|
@@ -211,27 +765,37 @@ export function computeGitStatus(
|
|
|
211
765
|
}
|
|
212
766
|
}
|
|
213
767
|
|
|
768
|
+
const modifiedPaths = new Set(modified.map((m) => m.path));
|
|
769
|
+
const deletedPaths = new Set(deleted.map((d) => d.path));
|
|
770
|
+
const stagedIndexByPath = new Map<string, string>();
|
|
771
|
+
for (const s of staged) {
|
|
772
|
+
stagedIndexByPath.set(
|
|
773
|
+
s.path,
|
|
774
|
+
s.status === 'added' ? 'A' : s.status === 'deleted' ? 'D' : 'M'
|
|
775
|
+
);
|
|
776
|
+
}
|
|
777
|
+
|
|
214
778
|
const unstaged: GitFileStatus[] = [...modified, ...deleted];
|
|
215
779
|
const entries: GitStatusEntry[] = [
|
|
216
780
|
...staged.map((s) => ({
|
|
217
781
|
path: s.path,
|
|
218
782
|
status: s.status,
|
|
219
783
|
staged: true,
|
|
220
|
-
index:
|
|
221
|
-
worktree: ' '
|
|
784
|
+
index: stagedIndexByPath.get(s.path) || 'M',
|
|
785
|
+
worktree: modifiedPaths.has(s.path) ? 'M' : deletedPaths.has(s.path) ? 'D' : ' '
|
|
222
786
|
})),
|
|
223
787
|
...modified.map((m) => ({
|
|
224
788
|
path: m.path,
|
|
225
789
|
status: m.status,
|
|
226
790
|
staged: false,
|
|
227
|
-
index: ' ',
|
|
791
|
+
index: stagedIndexByPath.get(m.path) || ' ',
|
|
228
792
|
worktree: 'M'
|
|
229
793
|
})),
|
|
230
794
|
...deleted.map((d) => ({
|
|
231
795
|
path: d.path,
|
|
232
796
|
status: d.status,
|
|
233
797
|
staged: false,
|
|
234
|
-
index: ' ',
|
|
798
|
+
index: stagedIndexByPath.get(d.path) || ' ',
|
|
235
799
|
worktree: 'D'
|
|
236
800
|
})),
|
|
237
801
|
...untracked.map((u) => ({
|
|
@@ -242,6 +806,7 @@ export function computeGitStatus(
|
|
|
242
806
|
worktree: '?'
|
|
243
807
|
}))
|
|
244
808
|
];
|
|
809
|
+
|
|
245
810
|
const isClean =
|
|
246
811
|
untracked.length === 0 &&
|
|
247
812
|
modified.length === 0 &&
|
|
@@ -271,19 +836,52 @@ export function stageFile(
|
|
|
271
836
|
currentContent: string | null
|
|
272
837
|
): void {
|
|
273
838
|
if (!gitState) return;
|
|
274
|
-
|
|
275
|
-
|
|
839
|
+
const repoRoot = gitState.repoRoot || '/workspace';
|
|
840
|
+
const rel = normalizeScmRelPath(relativePath, repoRoot);
|
|
841
|
+
if (!rel || isIgnoredScmPath(rel)) return;
|
|
842
|
+
|
|
843
|
+
const headFiles = cleanFileMap(gitState.headFiles || gitState.headSnapshot || {}, repoRoot);
|
|
844
|
+
const stagedMap = ensureStagedMap(gitState);
|
|
845
|
+
const normalizedContent =
|
|
846
|
+
currentContent === undefined || currentContent === null ? null : String(currentContent);
|
|
847
|
+
applyStagedEntry(gitState, stagedMap, rel, normalizedContent, headFiles);
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* Stages all modified, untracked (non-ignored), and deleted tracked files.
|
|
852
|
+
*/
|
|
853
|
+
export function stageAll(
|
|
854
|
+
gitState: GitState,
|
|
855
|
+
vfsFiles: Record<string, string>,
|
|
856
|
+
repoRoot = '/workspace'
|
|
857
|
+
): void {
|
|
858
|
+
if (!gitState) return;
|
|
859
|
+
const resolvedRoot = gitState.repoRoot || repoRoot || '/workspace';
|
|
860
|
+
const cleanVfs = cleanFileMap(vfsFiles, resolvedRoot);
|
|
861
|
+
const headFiles = cleanFileMap(gitState.headFiles || gitState.headSnapshot || {}, resolvedRoot);
|
|
862
|
+
const stagedMap = ensureStagedMap(gitState, cleanVfs);
|
|
863
|
+
|
|
864
|
+
for (const [rel, content] of Object.entries(cleanVfs)) {
|
|
865
|
+
applyStagedEntry(gitState, stagedMap, rel, content, headFiles);
|
|
276
866
|
}
|
|
277
|
-
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
867
|
+
|
|
868
|
+
for (const rel of Object.keys(headFiles)) {
|
|
869
|
+
if (!Object.prototype.hasOwnProperty.call(cleanVfs, rel)) {
|
|
870
|
+
applyStagedEntry(gitState, stagedMap, rel, null, headFiles);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
for (const stagedKey of Array.from(stagedMap.keys())) {
|
|
875
|
+
const normKey = normalizeScmRelPath(stagedKey, resolvedRoot);
|
|
876
|
+
if (
|
|
877
|
+
!normKey ||
|
|
878
|
+
isIgnoredScmPath(normKey) ||
|
|
879
|
+
(!Object.prototype.hasOwnProperty.call(cleanVfs, normKey) &&
|
|
880
|
+
!Object.prototype.hasOwnProperty.call(headFiles, normKey))
|
|
881
|
+
) {
|
|
882
|
+
stagedMap.delete(stagedKey);
|
|
883
|
+
}
|
|
285
884
|
}
|
|
286
|
-
gitState.staged.set(relativePath, normalizedContent);
|
|
287
885
|
}
|
|
288
886
|
|
|
289
887
|
/**
|
|
@@ -291,29 +889,58 @@ export function stageFile(
|
|
|
291
889
|
*/
|
|
292
890
|
export function unstageFile(gitState: GitState, relativePath: string): void {
|
|
293
891
|
if (!gitState) return;
|
|
892
|
+
const repoRoot = gitState.repoRoot || '/workspace';
|
|
893
|
+
const rel = normalizeScmRelPath(relativePath, repoRoot);
|
|
294
894
|
if (gitState.staged instanceof Map) {
|
|
895
|
+
gitState.staged.delete(rel);
|
|
295
896
|
gitState.staged.delete(relativePath);
|
|
296
897
|
} else if (gitState.staged instanceof Set) {
|
|
898
|
+
gitState.staged.delete(rel);
|
|
297
899
|
gitState.staged.delete(relativePath);
|
|
298
900
|
}
|
|
299
901
|
}
|
|
300
902
|
|
|
301
903
|
/**
|
|
302
|
-
*
|
|
303
|
-
* and clearing the staging area. Throws if message is empty or staging area is empty.
|
|
904
|
+
* Clears all staged entries from `gitState`.
|
|
304
905
|
*/
|
|
305
|
-
export function
|
|
906
|
+
export function unstageAll(gitState: GitState): void {
|
|
907
|
+
if (!gitState || !gitState.staged) return;
|
|
908
|
+
gitState.staged.clear();
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
/**
|
|
912
|
+
* Commits all staged changes to `gitState`, advancing `headCommit` and
|
|
913
|
+
* synchronizing `headFiles` & `headSnapshot`, prepending the new commit record,
|
|
914
|
+
* and clearing the staging area. Throws if message or staging area is empty.
|
|
915
|
+
*/
|
|
916
|
+
export function commitChanges(
|
|
917
|
+
gitState: GitState,
|
|
918
|
+
message: string,
|
|
919
|
+
author = 'CodeLab Developer <dev@codelab.internal>'
|
|
920
|
+
): string {
|
|
306
921
|
const trimmed = typeof message === 'string' ? message.trim() : '';
|
|
307
922
|
if (!trimmed) {
|
|
308
923
|
throw new Error('Commit message cannot be empty');
|
|
309
924
|
}
|
|
310
|
-
if (!gitState
|
|
925
|
+
if (!gitState) {
|
|
311
926
|
throw new Error('Nothing to commit (staging area is empty)');
|
|
312
927
|
}
|
|
313
928
|
|
|
314
|
-
const
|
|
929
|
+
const repoRoot = gitState.repoRoot || '/workspace';
|
|
930
|
+
const newHeadFiles: Record<string, string> = cleanFileMap(
|
|
931
|
+
gitState.headFiles || gitState.headSnapshot || {},
|
|
932
|
+
repoRoot
|
|
933
|
+
);
|
|
934
|
+
const stagedMap = ensureStagedMap(gitState);
|
|
935
|
+
pruneStagedMapInPlace(gitState, stagedMap, newHeadFiles);
|
|
936
|
+
|
|
937
|
+
if (stagedMap.size === 0) {
|
|
938
|
+
throw new Error('Nothing to commit (staging area is empty)');
|
|
939
|
+
}
|
|
315
940
|
const diffSummaryParts: string[] = [];
|
|
316
|
-
for (const [
|
|
941
|
+
for (const [rawPath, content] of stagedMap.entries()) {
|
|
942
|
+
const filePath = normalizeScmRelPath(rawPath, repoRoot);
|
|
943
|
+
if (!filePath || isIgnoredScmPath(filePath)) continue;
|
|
317
944
|
diffSummaryParts.push(`${filePath}:${content === null ? '<DEL>' : content}`);
|
|
318
945
|
if (content === null) {
|
|
319
946
|
delete newHeadFiles[filePath];
|
|
@@ -327,29 +954,125 @@ export function commitChanges(gitState: GitState, message: string): string {
|
|
|
327
954
|
const hash = computeShortCommitHash(
|
|
328
955
|
`${gitState.headCommit}:${trimmed}:${timestamp}:${commitSeq}:${diffSummaryParts.join('|')}`
|
|
329
956
|
);
|
|
957
|
+
const syncedHead = syncGitHead(gitState, newHeadFiles);
|
|
330
958
|
const commit: GitCommitRecord = {
|
|
331
959
|
hash,
|
|
332
960
|
message: trimmed,
|
|
333
961
|
timestamp,
|
|
334
|
-
|
|
962
|
+
author,
|
|
963
|
+
date: new Date(timestamp).toUTCString(),
|
|
964
|
+
files: { ...syncedHead }
|
|
335
965
|
};
|
|
336
966
|
|
|
337
967
|
if (!Array.isArray(gitState.commits)) {
|
|
338
968
|
gitState.commits = [];
|
|
339
969
|
}
|
|
340
|
-
gitState.commits.
|
|
970
|
+
gitState.commits.unshift(commit);
|
|
971
|
+
gitState.initialized = true;
|
|
341
972
|
gitState.headCommit = hash;
|
|
342
|
-
|
|
343
|
-
gitState.staged.clear();
|
|
973
|
+
stagedMap.clear();
|
|
344
974
|
|
|
345
975
|
return hash;
|
|
346
976
|
}
|
|
347
977
|
|
|
978
|
+
/**
|
|
979
|
+
* Discards working-tree (and staged) changes for `filePath`:
|
|
980
|
+
* - If tracked in `HEAD`, restores `headFiles[relPath]` to `shell.vfs` (creating
|
|
981
|
+
* parent directories first) and optional `workspaceFiles`.
|
|
982
|
+
* - If untracked, deletes the file from `shell.vfs` and optional `workspaceFiles`.
|
|
983
|
+
*/
|
|
984
|
+
export function discardChanges(
|
|
985
|
+
shell: GitShellLike,
|
|
986
|
+
filePath: string,
|
|
987
|
+
workspaceFiles?: Record<string, string>
|
|
988
|
+
): boolean {
|
|
989
|
+
if (!shell || typeof filePath !== 'string' || !filePath.trim()) {
|
|
990
|
+
return false;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
const gs = shell.gitState ?? null;
|
|
994
|
+
const repoRoot = ((gs && gs.repoRoot) || shell.cwd || '/workspace').replace(/\/+$/, '') || '/';
|
|
995
|
+
const relPath = normalizeScmRelPath(filePath, repoRoot);
|
|
996
|
+
if (!relPath || isIgnoredScmPath(relPath)) {
|
|
997
|
+
return false;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
const absPath = repoRoot === '/' ? `/${relPath}` : `${repoRoot}/${relPath}`;
|
|
1001
|
+
const headFiles = gs ? cleanFileMap(gs.headFiles || gs.headSnapshot || {}, repoRoot) : {};
|
|
1002
|
+
const inHead = Object.prototype.hasOwnProperty.call(headFiles, relPath);
|
|
1003
|
+
const vfs = shell.vfs ?? null;
|
|
1004
|
+
|
|
1005
|
+
if (inHead) {
|
|
1006
|
+
const headContent = headFiles[relPath];
|
|
1007
|
+
if (vfs) {
|
|
1008
|
+
const lastSlash = absPath.lastIndexOf('/');
|
|
1009
|
+
const parentDir = lastSlash > 0 ? absPath.slice(0, lastSlash) : '/';
|
|
1010
|
+
if (
|
|
1011
|
+
parentDir &&
|
|
1012
|
+
typeof vfs.mkdirSync === 'function' &&
|
|
1013
|
+
(typeof vfs.existsSync !== 'function' || !vfs.existsSync(parentDir))
|
|
1014
|
+
) {
|
|
1015
|
+
vfs.mkdirSync(parentDir, { recursive: true });
|
|
1016
|
+
}
|
|
1017
|
+
vfs.writeFileSync?.(absPath, headContent);
|
|
1018
|
+
}
|
|
1019
|
+
if (workspaceFiles && typeof workspaceFiles === 'object') {
|
|
1020
|
+
workspaceFiles[relPath] = headContent;
|
|
1021
|
+
}
|
|
1022
|
+
if (gs) {
|
|
1023
|
+
unstageFile(gs, relPath);
|
|
1024
|
+
}
|
|
1025
|
+
shell._emitVfsMutation?.('change', absPath);
|
|
1026
|
+
return true;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
const existsInVfs = Boolean(vfs && typeof vfs.existsSync === 'function' && vfs.existsSync(absPath));
|
|
1030
|
+
const existsInMapVfs = Boolean(vfs && vfs.files instanceof Map && vfs.files.has(absPath));
|
|
1031
|
+
const existsInWs = Boolean(
|
|
1032
|
+
workspaceFiles && Object.prototype.hasOwnProperty.call(workspaceFiles, relPath)
|
|
1033
|
+
);
|
|
1034
|
+
const existsInStaged = Boolean(gs && ensureStagedMap(gs).has(relPath));
|
|
1035
|
+
|
|
1036
|
+
if (!existsInVfs && !existsInMapVfs && !existsInWs && !existsInStaged) {
|
|
1037
|
+
return false;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
if (vfs) {
|
|
1041
|
+
if (typeof vfs.rmSync === 'function') {
|
|
1042
|
+
vfs.rmSync(absPath, { force: true });
|
|
1043
|
+
} else if (typeof vfs.unlinkSync === 'function') {
|
|
1044
|
+
if (typeof vfs.existsSync !== 'function' || vfs.existsSync(absPath)) {
|
|
1045
|
+
vfs.unlinkSync(absPath);
|
|
1046
|
+
}
|
|
1047
|
+
} else if (vfs.files instanceof Map) {
|
|
1048
|
+
vfs.files.delete(absPath);
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
if (workspaceFiles && typeof workspaceFiles === 'object') {
|
|
1053
|
+
delete workspaceFiles[relPath];
|
|
1054
|
+
}
|
|
1055
|
+
if (gs) {
|
|
1056
|
+
unstageFile(gs, relPath);
|
|
1057
|
+
}
|
|
1058
|
+
shell._emitVfsMutation?.('delete', absPath);
|
|
1059
|
+
return true;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
348
1062
|
export default {
|
|
1063
|
+
computeShortCommitHash,
|
|
1064
|
+
isIgnoredScmPath,
|
|
1065
|
+
normalizeScmRelPath,
|
|
1066
|
+
isUnbornGitHead,
|
|
1067
|
+
isEffectiveStagedChange,
|
|
1068
|
+
syncGitHead,
|
|
349
1069
|
createGitState,
|
|
1070
|
+
initRepository,
|
|
350
1071
|
computeGitStatus,
|
|
351
1072
|
stageFile,
|
|
1073
|
+
stageAll,
|
|
352
1074
|
unstageFile,
|
|
1075
|
+
unstageAll,
|
|
353
1076
|
commitChanges,
|
|
354
|
-
|
|
1077
|
+
discardChanges
|
|
355
1078
|
};
|