@myrialabs/clopen 0.3.20 → 0.3.22
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/backend/engine/adapters/claude/environment.ts +0 -17
- package/backend/engine/adapters/claude/error-handler.ts +0 -4
- package/backend/engine/adapters/claude/models.ts +43 -0
- package/backend/engine/install-recipes.ts +69 -117
- package/backend/engine/version-check.ts +145 -0
- package/backend/files/file-operations.test.ts +75 -0
- package/backend/files/file-operations.ts +10 -56
- package/backend/files/image-processing.test.ts +52 -0
- package/backend/files/image-processing.ts +11 -1
- package/backend/files/path-browsing.ts +141 -139
- package/backend/git/git-parser.ts +11 -0
- package/backend/git/git-service.ts +65 -9
- package/backend/http/files-upload.test.ts +174 -0
- package/backend/http/files-upload.ts +27 -7
- package/backend/http/upload-temp-cleanup.ts +53 -0
- package/backend/index.ts +31 -12
- package/backend/mcp/remote-server.test.ts +150 -0
- package/backend/mcp/remote-server.ts +68 -0
- package/backend/terminal/pty-session-manager.ts +18 -11
- package/backend/ws/chat/stream.ts +17 -6
- package/backend/ws/files/index.ts +5 -0
- package/backend/ws/files/reveal.ts +61 -0
- package/backend/ws/files/search.test.ts +115 -0
- package/backend/ws/files/search.ts +77 -26
- package/backend/ws/files/write.ts +3 -1
- package/backend/ws/git/branch-name.ts +170 -0
- package/backend/ws/git/branch.ts +15 -2
- package/backend/ws/git/commit-message.ts +8 -2
- package/backend/ws/git/index.ts +3 -1
- package/backend/ws/system-tools/status.ts +14 -0
- package/backend/ws/user/crud.ts +8 -4
- package/dist/assets/main-Ba19ADkk.js +377 -0
- package/dist/assets/main-CwCZzMVm.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +3 -1
- package/shared/constants/git.ts +5 -0
- package/shared/types/git.ts +13 -0
- package/shared/types/stores/settings.ts +17 -0
- package/dist/assets/main-72Z_L-nD.js +0 -362
- package/dist/assets/main-ClADBkFf.css +0 -1
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { describe, expect, mock, test } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
// Stub sharp before importing the module under test so no real image I/O
|
|
4
|
+
// happens during unit tests. mockPages is set per-test to control metadata.
|
|
5
|
+
let mockPages: number | undefined;
|
|
6
|
+
|
|
7
|
+
await mock.module('sharp', () => ({
|
|
8
|
+
default: (_input: unknown, _opts?: unknown) => ({
|
|
9
|
+
metadata: () =>
|
|
10
|
+
Promise.resolve({
|
|
11
|
+
width: 100,
|
|
12
|
+
height: 100,
|
|
13
|
+
format: 'gif',
|
|
14
|
+
pages: mockPages,
|
|
15
|
+
}),
|
|
16
|
+
}),
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
// Stub file-size-limit so the database is never touched.
|
|
20
|
+
await mock.module('./file-size-limit', () => ({
|
|
21
|
+
validateFileSize: () => {},
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
const { processImageEdit } = await import('./image-processing');
|
|
25
|
+
|
|
26
|
+
const RECIPE = { output: { format: 'gif' as const } };
|
|
27
|
+
|
|
28
|
+
describe('processImageEdit — animation frame limit', () => {
|
|
29
|
+
test('rejects when frame count exceeds the 100-frame limit', async () => {
|
|
30
|
+
mockPages = 101;
|
|
31
|
+
await expect(processImageEdit('/dev/null', RECIPE)).rejects.toThrow(
|
|
32
|
+
'Cannot process animated image: 101 frames exceeds the limit of 100'
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('error message includes the actual and maximum frame counts', async () => {
|
|
37
|
+
mockPages = 500;
|
|
38
|
+
await expect(processImageEdit('/dev/null', RECIPE)).rejects.toThrow(
|
|
39
|
+
'Cannot process animated image: 500 frames exceeds the limit of 100'
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('allows animated images at exactly 100 frames', async () => {
|
|
44
|
+
mockPages = 100;
|
|
45
|
+
await expect(processImageEdit('/dev/null', RECIPE)).resolves.toBeDefined();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('allows static images with no pages field', async () => {
|
|
49
|
+
mockPages = undefined;
|
|
50
|
+
await expect(processImageEdit('/dev/null', RECIPE)).resolves.toBeDefined();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -27,6 +27,9 @@ export interface ProcessedImage {
|
|
|
27
27
|
size: number;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/** Hard cap on animation frames — prevents resource exhaustion via GIF bomb. */
|
|
31
|
+
const MAX_ANIMATION_FRAMES = 100;
|
|
32
|
+
|
|
30
33
|
/** Clamp a value into [min, max], rounding to an integer. */
|
|
31
34
|
function clampInt(value: number, min: number, max: number): number {
|
|
32
35
|
return Math.max(min, Math.min(max, Math.round(value)));
|
|
@@ -193,6 +196,13 @@ export async function processImageEdit(
|
|
|
193
196
|
throw new Error('Source file is not a readable image');
|
|
194
197
|
}
|
|
195
198
|
|
|
199
|
+
const frameCount = srcMeta.pages ?? 1;
|
|
200
|
+
if (frameCount > MAX_ANIMATION_FRAMES) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
`Cannot process animated image: ${frameCount} frames exceeds the limit of ${MAX_ANIMATION_FRAMES}`
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
196
206
|
// Nothing to do: not compressing, no edits, and the format is unchanged →
|
|
197
207
|
// return the original bytes verbatim so an untouched file stays identical.
|
|
198
208
|
const compress = recipe.output.compress ?? false;
|
|
@@ -208,7 +218,7 @@ export async function processImageEdit(
|
|
|
208
218
|
};
|
|
209
219
|
}
|
|
210
220
|
|
|
211
|
-
const animated =
|
|
221
|
+
const animated = frameCount > 1;
|
|
212
222
|
const rotate = recipe.rotate ?? 0;
|
|
213
223
|
|
|
214
224
|
let pipeline: Sharp;
|
|
@@ -1,19 +1,34 @@
|
|
|
1
1
|
import { join, extname } from 'path';
|
|
2
|
+
import { readdir as fsReaddir } from 'node:fs/promises';
|
|
2
3
|
import { existsSync } from 'fs';
|
|
3
4
|
|
|
4
5
|
import { debug } from '$shared/utils/logger';
|
|
5
6
|
|
|
6
|
-
//
|
|
7
|
+
// Cross-platform readdir using fs.promises
|
|
7
8
|
async function readdir(path: string): Promise<string[]> {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
const entries = await fsReaddir(path, { withFileTypes: true });
|
|
10
|
+
return entries.map(e => e.name);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function statIfExists(path: string): Promise<Awaited<ReturnType<ReturnType<typeof Bun.file>['stat']>> | null> {
|
|
14
|
+
try {
|
|
15
|
+
return await Bun.file(path).stat();
|
|
16
|
+
} catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Returns true when every visible entry is a .app bundle or the "Applications" alias —
|
|
22
|
+
// the signature of a DMG/installer mount, not a general-purpose volume.
|
|
23
|
+
async function isLikelyMacAppVolume(volumePath: string): Promise<boolean> {
|
|
24
|
+
try {
|
|
25
|
+
const entries = await readdir(volumePath);
|
|
26
|
+
const visible = entries.filter(e => !e.startsWith('.'));
|
|
27
|
+
if (visible.length === 0) return false;
|
|
28
|
+
return visible.every(e => e.endsWith('.app') || e === 'Applications');
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
13
31
|
}
|
|
14
|
-
const result = await new Response(proc.stdout).text();
|
|
15
|
-
// Split and clean up, removing \r characters for Windows compatibility
|
|
16
|
-
return result.trim().split(/\r?\n/).map(line => line.trim()).filter(Boolean);
|
|
17
32
|
}
|
|
18
33
|
|
|
19
34
|
// Return types
|
|
@@ -29,6 +44,7 @@ export interface PathBrowseData {
|
|
|
29
44
|
type: 'file' | 'directory';
|
|
30
45
|
path: string;
|
|
31
46
|
modified: string;
|
|
47
|
+
kind?: 'home' | 'root' | 'volume' | 'drive';
|
|
32
48
|
size?: number;
|
|
33
49
|
extension?: string;
|
|
34
50
|
}>;
|
|
@@ -58,6 +74,16 @@ export async function handlePathBrowsing(path: string): Promise<PathBrowseData>
|
|
|
58
74
|
targetPath = path;
|
|
59
75
|
}
|
|
60
76
|
|
|
77
|
+
// Windows: a bare drive letter ("C:") is drive-relative — it points at the
|
|
78
|
+
// drive's *current* directory, not its root. Path normalization (resolve /
|
|
79
|
+
// realpath) and manual entry can both strip the trailing separator, which
|
|
80
|
+
// then makes join() produce drive-relative child paths like "C:Users" whose
|
|
81
|
+
// stat() fails, leaving the listing empty. Append the separator so we always
|
|
82
|
+
// browse the drive root.
|
|
83
|
+
if (/^[A-Za-z]:$/.test(targetPath)) {
|
|
84
|
+
targetPath += '\\';
|
|
85
|
+
}
|
|
86
|
+
|
|
61
87
|
// Check if path exists (use stat instead of exists for directories)
|
|
62
88
|
const file = Bun.file(targetPath);
|
|
63
89
|
const stats = await file.stat();
|
|
@@ -161,165 +187,138 @@ export async function handlePathBrowsing(path: string): Promise<PathBrowseData>
|
|
|
161
187
|
|
|
162
188
|
// Helper function to handle Unix mount points listing (Linux, macOS, etc.)
|
|
163
189
|
export async function handleUnixMountPoints(): Promise<PathBrowseData> {
|
|
164
|
-
const
|
|
190
|
+
const items: Array<{
|
|
165
191
|
name: string;
|
|
166
192
|
type: 'file' | 'directory';
|
|
167
193
|
path: string;
|
|
168
194
|
modified: string;
|
|
169
|
-
|
|
170
|
-
extension?: string;
|
|
195
|
+
kind: 'home' | 'root' | 'volume';
|
|
171
196
|
}> = [];
|
|
172
197
|
|
|
173
|
-
//
|
|
174
|
-
const rootFile = Bun.file('/');
|
|
175
|
-
if (await rootFile.exists()) {
|
|
176
|
-
const stats = await rootFile.stat();
|
|
177
|
-
commonMountPoints.push({
|
|
178
|
-
name: 'Root (/) Filesystem',
|
|
179
|
-
type: 'directory',
|
|
180
|
-
path: '/',
|
|
181
|
-
modified: stats.mtime.toISOString()
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// Add user home directory
|
|
198
|
+
// Home directory — all Unix platforms
|
|
186
199
|
const homeDir = process.env.HOME;
|
|
187
200
|
if (homeDir) {
|
|
188
|
-
const
|
|
189
|
-
if (
|
|
190
|
-
|
|
191
|
-
commonMountPoints.push({
|
|
192
|
-
name: 'Home Directory',
|
|
193
|
-
type: 'directory',
|
|
194
|
-
path: homeDir,
|
|
195
|
-
modified: stats.mtime.toISOString()
|
|
196
|
-
});
|
|
201
|
+
const stats = await statIfExists(homeDir);
|
|
202
|
+
if (stats?.isDirectory()) {
|
|
203
|
+
items.push({ name: 'Home Directory', type: 'directory', path: homeDir, modified: stats.mtime.toISOString(), kind: 'home' });
|
|
197
204
|
}
|
|
198
205
|
}
|
|
199
206
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
'/
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const stats = await mountFile.stat();
|
|
216
|
-
if (stats.isDirectory()) {
|
|
217
|
-
let displayName = mountPath;
|
|
218
|
-
|
|
219
|
-
// Special names for common directories
|
|
220
|
-
switch (mountPath) {
|
|
221
|
-
case '/mnt':
|
|
222
|
-
displayName = 'Mount Points (/mnt)';
|
|
223
|
-
break;
|
|
224
|
-
case '/media':
|
|
225
|
-
displayName = 'Media (/media)';
|
|
226
|
-
break;
|
|
227
|
-
case '/Volumes':
|
|
228
|
-
displayName = 'Volumes (/Volumes)';
|
|
229
|
-
break;
|
|
230
|
-
case '/usr':
|
|
231
|
-
displayName = 'System (/usr)';
|
|
232
|
-
break;
|
|
233
|
-
case '/var':
|
|
234
|
-
displayName = 'Variable (/var)';
|
|
235
|
-
break;
|
|
236
|
-
case '/opt':
|
|
237
|
-
displayName = 'Optional (/opt)';
|
|
238
|
-
break;
|
|
239
|
-
case '/tmp':
|
|
240
|
-
displayName = 'Temporary (/tmp)';
|
|
241
|
-
break;
|
|
207
|
+
if (process.platform === 'darwin') {
|
|
208
|
+
// macOS: enumerate /Volumes/* — skip installer/DMG mounts
|
|
209
|
+
const volumesStats = await statIfExists('/Volumes');
|
|
210
|
+
if (volumesStats?.isDirectory()) {
|
|
211
|
+
try {
|
|
212
|
+
const volumeNames = await readdir('/Volumes');
|
|
213
|
+
for (const name of volumeNames) {
|
|
214
|
+
const vPath = `/Volumes/${name}`;
|
|
215
|
+
try {
|
|
216
|
+
const vStats = await statIfExists(vPath);
|
|
217
|
+
if (!vStats?.isDirectory()) continue;
|
|
218
|
+
if (await isLikelyMacAppVolume(vPath)) continue;
|
|
219
|
+
items.push({ name, type: 'directory', path: vPath, modified: vStats.mtime.toISOString(), kind: 'volume' });
|
|
220
|
+
} catch {
|
|
221
|
+
// Skip inaccessible volumes
|
|
242
222
|
}
|
|
243
|
-
|
|
244
|
-
commonMountPoints.push({
|
|
245
|
-
name: displayName,
|
|
246
|
-
type: 'directory',
|
|
247
|
-
path: mountPath,
|
|
248
|
-
modified: stats.mtime.toISOString()
|
|
249
|
-
});
|
|
250
223
|
}
|
|
224
|
+
} catch {
|
|
225
|
+
// Skip if /Volumes is unreadable
|
|
251
226
|
}
|
|
252
|
-
} catch {
|
|
253
|
-
// Skip inaccessible mount points
|
|
254
227
|
}
|
|
255
|
-
}
|
|
228
|
+
} else if (process.platform === 'linux') {
|
|
229
|
+
// Linux: root + /mnt/*, /media/*, /run/media/{user}/* (systemd automount)
|
|
230
|
+
const rootStats = await statIfExists('/');
|
|
231
|
+
if (rootStats?.isDirectory()) {
|
|
232
|
+
items.push({ name: 'Root (/) Filesystem', type: 'directory', path: '/', modified: rootStats.mtime.toISOString(), kind: 'root' });
|
|
233
|
+
}
|
|
256
234
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
if (stats.isDirectory()) {
|
|
269
|
-
commonMountPoints.push({
|
|
270
|
-
name: `${volume} Volume`,
|
|
271
|
-
type: 'directory',
|
|
272
|
-
path: volumePath,
|
|
273
|
-
modified: stats.mtime.toISOString()
|
|
274
|
-
});
|
|
235
|
+
for (const base of ['/mnt', '/media']) {
|
|
236
|
+
const baseStats = await statIfExists(base);
|
|
237
|
+
if (!baseStats?.isDirectory()) continue;
|
|
238
|
+
try {
|
|
239
|
+
const entries = await readdir(base);
|
|
240
|
+
for (const entry of entries) {
|
|
241
|
+
const entryPath = `${base}/${entry}`;
|
|
242
|
+
try {
|
|
243
|
+
const entryStats = await statIfExists(entryPath);
|
|
244
|
+
if (entryStats?.isDirectory()) {
|
|
245
|
+
items.push({ name: entry, type: 'directory', path: entryPath, modified: entryStats.mtime.toISOString(), kind: 'volume' });
|
|
275
246
|
}
|
|
247
|
+
} catch {
|
|
248
|
+
// Skip inaccessible mounts
|
|
276
249
|
}
|
|
277
|
-
} catch {
|
|
278
|
-
// Skip inaccessible volumes
|
|
279
250
|
}
|
|
251
|
+
} catch {
|
|
252
|
+
// Skip if base is unreadable
|
|
280
253
|
}
|
|
281
|
-
} catch {
|
|
282
|
-
// Skip if can't read /Volumes
|
|
283
254
|
}
|
|
284
|
-
}
|
|
285
255
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
path: mountPath,
|
|
304
|
-
modified: stats.mtime.toISOString()
|
|
305
|
-
});
|
|
256
|
+
// systemd automount: /run/media/{user}/{volume}
|
|
257
|
+
const runMediaStats = await statIfExists('/run/media');
|
|
258
|
+
if (runMediaStats?.isDirectory()) {
|
|
259
|
+
try {
|
|
260
|
+
const users = await readdir('/run/media');
|
|
261
|
+
for (const user of users) {
|
|
262
|
+
const userPath = `/run/media/${user}`;
|
|
263
|
+
try {
|
|
264
|
+
const userStats = await statIfExists(userPath);
|
|
265
|
+
if (!userStats?.isDirectory()) continue;
|
|
266
|
+
const volumes = await readdir(userPath);
|
|
267
|
+
for (const vol of volumes) {
|
|
268
|
+
const volPath = `${userPath}/${vol}`;
|
|
269
|
+
try {
|
|
270
|
+
const volStats = await statIfExists(volPath);
|
|
271
|
+
if (volStats?.isDirectory()) {
|
|
272
|
+
items.push({ name: vol, type: 'directory', path: volPath, modified: volStats.mtime.toISOString(), kind: 'volume' });
|
|
306
273
|
}
|
|
274
|
+
} catch {
|
|
275
|
+
// Skip inaccessible
|
|
307
276
|
}
|
|
308
|
-
} catch {
|
|
309
|
-
// Skip inaccessible mounts
|
|
310
277
|
}
|
|
278
|
+
} catch {
|
|
279
|
+
// Skip inaccessible user dir
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
} catch {
|
|
283
|
+
// Skip if /run/media is unreadable
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
} else {
|
|
287
|
+
// Other Unix (FreeBSD, OpenBSD, etc.): root + /mnt and /media subdirectories
|
|
288
|
+
const rootStats = await statIfExists('/');
|
|
289
|
+
if (rootStats?.isDirectory()) {
|
|
290
|
+
items.push({ name: 'Root (/) Filesystem', type: 'directory', path: '/', modified: rootStats.mtime.toISOString(), kind: 'root' });
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
for (const base of ['/mnt', '/media']) {
|
|
294
|
+
const baseStats = await statIfExists(base);
|
|
295
|
+
if (!baseStats?.isDirectory()) continue;
|
|
296
|
+
try {
|
|
297
|
+
const entries = await readdir(base);
|
|
298
|
+
for (const entry of entries) {
|
|
299
|
+
const entryPath = `${base}/${entry}`;
|
|
300
|
+
try {
|
|
301
|
+
const entryStats = await statIfExists(entryPath);
|
|
302
|
+
if (entryStats?.isDirectory()) {
|
|
303
|
+
items.push({ name: entry, type: 'directory', path: entryPath, modified: entryStats.mtime.toISOString(), kind: 'volume' });
|
|
304
|
+
}
|
|
305
|
+
} catch {
|
|
306
|
+
// Skip inaccessible
|
|
311
307
|
}
|
|
312
|
-
} catch {
|
|
313
|
-
// Skip if can't read mount directory
|
|
314
308
|
}
|
|
309
|
+
} catch {
|
|
310
|
+
// Skip if base is unreadable
|
|
315
311
|
}
|
|
316
312
|
}
|
|
317
313
|
}
|
|
318
314
|
|
|
319
|
-
//
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
315
|
+
// Deduplicate by path
|
|
316
|
+
const seen = new Set<string>();
|
|
317
|
+
const uniqueMounts = items.filter(item => {
|
|
318
|
+
if (seen.has(item.path)) return false;
|
|
319
|
+
seen.add(item.path);
|
|
320
|
+
return true;
|
|
321
|
+
});
|
|
323
322
|
|
|
324
323
|
return {
|
|
325
324
|
name: 'System',
|
|
@@ -341,6 +340,7 @@ export async function handleWindowsDrives(): Promise<PathBrowseData> {
|
|
|
341
340
|
type: 'file' | 'directory';
|
|
342
341
|
path: string;
|
|
343
342
|
modified: string;
|
|
343
|
+
kind: 'drive';
|
|
344
344
|
size?: number;
|
|
345
345
|
extension?: string;
|
|
346
346
|
}> = [];
|
|
@@ -364,7 +364,8 @@ export async function handleWindowsDrives(): Promise<PathBrowseData> {
|
|
|
364
364
|
name: `${drive} Drive`,
|
|
365
365
|
type: 'directory',
|
|
366
366
|
path: drivePath,
|
|
367
|
-
modified: new Date().toISOString()
|
|
367
|
+
modified: new Date().toISOString(),
|
|
368
|
+
kind: 'drive'
|
|
368
369
|
});
|
|
369
370
|
}
|
|
370
371
|
|
|
@@ -378,7 +379,8 @@ export async function handleWindowsDrives(): Promise<PathBrowseData> {
|
|
|
378
379
|
name: `${drive} Drive`,
|
|
379
380
|
type: 'directory',
|
|
380
381
|
path: drivePath,
|
|
381
|
-
modified: new Date().toISOString()
|
|
382
|
+
modified: new Date().toISOString(),
|
|
383
|
+
kind: 'drive'
|
|
382
384
|
});
|
|
383
385
|
}
|
|
384
386
|
} catch {
|
|
@@ -114,6 +114,17 @@ export function parseBranches(localOutput: string, remoteOutput: string): GitBra
|
|
|
114
114
|
// Format: "* main abc1234 commit message" or " dev abc1234 commit message"
|
|
115
115
|
const isCurrent = line.startsWith('*');
|
|
116
116
|
const trimmed = line.replace(/^\*?\s+/, '');
|
|
117
|
+
|
|
118
|
+
// When HEAD is detached, git emits a parenthesized pseudo-ref instead of a
|
|
119
|
+
// branch name, e.g. "* (no branch, rebasing main)" or "* (HEAD detached at
|
|
120
|
+
// abc1234)". These are not real branches — skip them so they never leak into
|
|
121
|
+
// the branch list or get treated as the current branch (which produced the
|
|
122
|
+
// bogus "(no" branch name). Match git's exact pseudo-ref prefixes so legit
|
|
123
|
+
// branch names containing parentheses (e.g. "feature(x)") are preserved. The
|
|
124
|
+
// real current branch / detached state is resolved authoritatively in
|
|
125
|
+
// git-service. Output is forced to en_US locale, so these strings are stable.
|
|
126
|
+
if (isCurrent && /^\((?:HEAD detached|no branch)\b/.test(trimmed)) continue;
|
|
127
|
+
|
|
117
128
|
const parts = trimmed.split(/\s+/);
|
|
118
129
|
const name = parts[0];
|
|
119
130
|
const lastCommit = parts[1] || '';
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
import type {
|
|
20
20
|
GitStatus,
|
|
21
21
|
GitBranchInfo,
|
|
22
|
+
GitOperation,
|
|
22
23
|
GitFileDiff,
|
|
23
24
|
GitLogResult,
|
|
24
25
|
GitRemote,
|
|
@@ -233,9 +234,10 @@ export class GitService {
|
|
|
233
234
|
// ============================================
|
|
234
235
|
|
|
235
236
|
async getBranches(cwd: string): Promise<GitBranchInfo> {
|
|
236
|
-
const [localResult, remoteResult] = await Promise.all([
|
|
237
|
+
const [localResult, remoteResult, headRef] = await Promise.all([
|
|
237
238
|
execGit(['branch', '-v', '--no-color'], cwd),
|
|
238
|
-
execGit(['branch', '-r', '-v', '--no-color'], cwd)
|
|
239
|
+
execGit(['branch', '-r', '-v', '--no-color'], cwd),
|
|
240
|
+
execGit(['rev-parse', '--abbrev-ref', 'HEAD'], cwd)
|
|
239
241
|
]);
|
|
240
242
|
|
|
241
243
|
// Handle empty repo (no commits yet) — branch command returns empty
|
|
@@ -247,6 +249,25 @@ export class GitService {
|
|
|
247
249
|
}
|
|
248
250
|
|
|
249
251
|
const branchInfo = parseBranches(localResult.stdout, remoteResult.stdout);
|
|
252
|
+
branchInfo.operation = await this.detectGitOperation(cwd);
|
|
253
|
+
|
|
254
|
+
// Resolve the current branch authoritatively instead of trusting the parsed
|
|
255
|
+
// `git branch -v` text: while HEAD is detached (rebase, bisect, or a detached
|
|
256
|
+
// checkout) git prints a parenthesized pseudo-ref, which previously leaked
|
|
257
|
+
// through as a bogus "(no" branch name.
|
|
258
|
+
const headName = headRef.exitCode === 0 ? headRef.stdout.trim() : '';
|
|
259
|
+
if (headName === '' || headName === 'HEAD') {
|
|
260
|
+
// Detached HEAD — show a clean short hash, never a real branch.
|
|
261
|
+
branchInfo.detached = true;
|
|
262
|
+
const shortHash = await execGit(['rev-parse', '--short', 'HEAD'], cwd);
|
|
263
|
+
branchInfo.current = shortHash.exitCode === 0 ? shortHash.stdout.trim() : '';
|
|
264
|
+
for (const b of branchInfo.local) b.isCurrent = false;
|
|
265
|
+
return branchInfo;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
branchInfo.detached = false;
|
|
269
|
+
branchInfo.current = headName;
|
|
270
|
+
for (const b of branchInfo.local) b.isCurrent = b.name === headName;
|
|
250
271
|
|
|
251
272
|
// Get ahead/behind for current branch
|
|
252
273
|
if (branchInfo.current) {
|
|
@@ -296,6 +317,14 @@ export class GitService {
|
|
|
296
317
|
}
|
|
297
318
|
}
|
|
298
319
|
|
|
320
|
+
async checkoutCommit(cwd: string, commitHash: string): Promise<void> {
|
|
321
|
+
assertSafeGitRevish(commitHash, 'commit hash');
|
|
322
|
+
const result = await execGit(['checkout', commitHash], cwd);
|
|
323
|
+
if (result.exitCode !== 0) {
|
|
324
|
+
throw new Error(`git checkout failed: ${result.stderr}`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
299
328
|
async deleteBranch(cwd: string, name: string, force = false): Promise<void> {
|
|
300
329
|
assertSafeGitRevish(name, 'branch name');
|
|
301
330
|
const flag = force ? '-D' : '-d';
|
|
@@ -314,9 +343,12 @@ export class GitService {
|
|
|
314
343
|
}
|
|
315
344
|
}
|
|
316
345
|
|
|
317
|
-
async mergeBranch(cwd: string, branchName: string): Promise<{ success: boolean; message: string }> {
|
|
346
|
+
async mergeBranch(cwd: string, branchName: string, noFastForward = false): Promise<{ success: boolean; message: string }> {
|
|
318
347
|
assertSafeGitRevish(branchName, 'merge branch');
|
|
319
|
-
const
|
|
348
|
+
const args = ['merge'];
|
|
349
|
+
if (noFastForward) args.push('--no-ff');
|
|
350
|
+
args.push(branchName);
|
|
351
|
+
const result = await execGit(args, cwd);
|
|
320
352
|
return {
|
|
321
353
|
success: result.exitCode === 0,
|
|
322
354
|
message: result.exitCode === 0 ? result.stdout : result.stderr
|
|
@@ -643,14 +675,38 @@ export class GitService {
|
|
|
643
675
|
* For stash conflicts (no sentinel) we fall back to `git reset --merge`,
|
|
644
676
|
* which unwinds the unmerged paths while keeping unrelated local edits.
|
|
645
677
|
*/
|
|
646
|
-
|
|
678
|
+
/** Resolve the absolute git dir for a working tree (handles worktrees/.git files). */
|
|
679
|
+
private async resolveGitDir(cwd: string): Promise<string> {
|
|
680
|
+
const { join } = await import('node:path');
|
|
681
|
+
const result = await execGit(['rev-parse', '--git-dir'], cwd);
|
|
682
|
+
const raw = result.exitCode === 0 ? result.stdout.trim() : '.git';
|
|
683
|
+
return raw.startsWith('/') ? raw : join(cwd, raw);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* Detect an in-progress operation (rebase, merge, cherry-pick, revert, bisect)
|
|
688
|
+
* by probing the sentinel files git writes under the git dir. Returns null when
|
|
689
|
+
* the working tree is in a normal state.
|
|
690
|
+
*/
|
|
691
|
+
private async detectGitOperation(cwd: string): Promise<GitOperation | null> {
|
|
647
692
|
const { existsSync } = await import('node:fs');
|
|
648
693
|
const { join } = await import('node:path');
|
|
694
|
+
const gitDir = await this.resolveGitDir(cwd);
|
|
695
|
+
const has = (name: string) => existsSync(join(gitDir, name));
|
|
649
696
|
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
697
|
+
if (has('rebase-merge') || has('rebase-apply')) return 'rebase';
|
|
698
|
+
if (has('MERGE_HEAD')) return 'merge';
|
|
699
|
+
if (has('CHERRY_PICK_HEAD')) return 'cherry-pick';
|
|
700
|
+
if (has('REVERT_HEAD')) return 'revert';
|
|
701
|
+
if (has('BISECT_LOG')) return 'bisect';
|
|
702
|
+
return null;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
async abortMerge(cwd: string): Promise<void> {
|
|
706
|
+
const { existsSync } = await import('node:fs');
|
|
707
|
+
const { join } = await import('node:path');
|
|
653
708
|
|
|
709
|
+
const gitDir = await this.resolveGitDir(cwd);
|
|
654
710
|
const has = (name: string) => existsSync(join(gitDir, name));
|
|
655
711
|
|
|
656
712
|
let args: string[] | null = null;
|
|
@@ -742,7 +798,7 @@ export class GitService {
|
|
|
742
798
|
if (!npmPath) {
|
|
743
799
|
throw new Error('npm binary not found on PATH');
|
|
744
800
|
}
|
|
745
|
-
const proc = Bun.spawn([npmPath, 'version', bump
|
|
801
|
+
const proc = Bun.spawn([npmPath, 'version', bump], {
|
|
746
802
|
cwd,
|
|
747
803
|
stdout: 'pipe',
|
|
748
804
|
stderr: 'pipe',
|