dsh-git-desk 0.23.0 → 0.25.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.
@@ -234,6 +234,7 @@ export async function writeCommitMessage(ctx, request) {
234
234
  messages: [{ role: 'user', content: [{ type: 'text', text: promptFor(request.recent, request.patch) }] }],
235
235
  temperature: 0.2,
236
236
  maxTokens: 200,
237
+ ...(request.sessionId === undefined ? {} : { sessionId: request.sessionId }),
237
238
  signal: request.signal,
238
239
  });
239
240
  for await (const chunk of chunks) {
@@ -65,6 +65,7 @@ export function createRuntime(subprocess) {
65
65
  const signal = options.signal === undefined ? timeout : AbortSignal.any([timeout, options.signal]);
66
66
  const env = {
67
67
  ...process.env,
68
+ ...(options.env ?? {}),
68
69
  LC_ALL: 'C',
69
70
  LANG: 'C',
70
71
  GIT_TERMINAL_PROMPT: '0',
@@ -292,22 +292,49 @@ async function readStatus(runtime, root) {
292
292
  binary,
293
293
  });
294
294
  }
295
- const [mergeHead, rebaseHead, revertHead, cherryHead] = await Promise.all([
295
+ const [mergeHead, revertHead, cherryHead, rebaseDirs] = await Promise.all([
296
296
  runtime.run(root, ['rev-parse', '-q', '--verify', 'MERGE_HEAD']),
297
- runtime.run(root, ['rev-parse', '-q', '--verify', 'REBASE_HEAD']),
298
297
  runtime.run(root, ['rev-parse', '-q', '--verify', 'REVERT_HEAD']),
299
298
  runtime.run(root, ['rev-parse', '-q', '--verify', 'CHERRY_PICK_HEAD']),
299
+ // Git keeps REBASE_HEAD after a successful rebase, so it cannot tell a
300
+ // running rebase from a finished one. The state directories can. They live
301
+ // in the per-worktree Git directory.
302
+ runtime.run(root, [
303
+ 'rev-parse',
304
+ '--path-format=absolute',
305
+ '--git-path',
306
+ 'rebase-merge',
307
+ '--git-path',
308
+ 'rebase-apply',
309
+ ]),
300
310
  ]);
311
+ const rebaseDirPaths = rebaseDirs.code === 0
312
+ ? rebaseDirs.stdout
313
+ .split('\n')
314
+ .map((line) => line.trim())
315
+ .filter((line) => line !== '')
316
+ : [];
317
+ const rebaseStats = await Promise.all(rebaseDirPaths.map((target) => runtime.lstatPath(target)));
318
+ const rebaseStarted = rebaseStats.some((stat) => stat !== null && stat.isDirectory());
319
+ let operationCommit = null;
320
+ if (rebaseStarted) {
321
+ // REBASE_HEAD survives a finished rebase, so read it only while the state
322
+ // directories say a rebase is actually running.
323
+ const stopped = await runtime.run(root, ['rev-parse', '--verify', '--quiet', 'REBASE_HEAD']);
324
+ if (stopped.code === 0 && stopped.stdout.trim() !== '')
325
+ operationCommit = stopped.stdout.trim();
326
+ }
301
327
  return {
302
328
  repo: true,
303
329
  root,
304
330
  branch: parsed.branch,
305
331
  entries,
306
332
  merging: mergeHead.code === 0,
307
- rebasing: rebaseHead.code === 0,
333
+ rebasing: rebaseStarted,
308
334
  reverting: revertHead.code === 0,
309
335
  cherryPicking: cherryHead.code === 0,
310
336
  conflicts: entries.filter((entry) => entry.conflicted).map((entry) => entry.path),
337
+ operationCommit,
311
338
  };
312
339
  }
313
340
  /**
@@ -1233,6 +1260,235 @@ export async function fetch(runtime, root) {
1233
1260
  status: await readStatus(runtime, root),
1234
1261
  };
1235
1262
  }
1263
+ /**
1264
+ * Read a branch argument from a request.
1265
+ * @param value the caller value.
1266
+ * @returns the trimmed name.
1267
+ */
1268
+ function branchArgument(value) {
1269
+ const name = typeof value === 'string' ? value.trim() : '';
1270
+ if (name === '' || name.startsWith('-') || name.includes('\0') || name.includes('\n')) {
1271
+ throw new GitError('git/branch-invalid', 'Choose a branch.');
1272
+ }
1273
+ return name;
1274
+ }
1275
+ /**
1276
+ * Resolve one branch name to its tip commit.
1277
+ * @param runtime the runtime helpers.
1278
+ * @param root the repository root.
1279
+ * @param value the caller branch.
1280
+ * @returns the name and hash.
1281
+ */
1282
+ async function branchTip(runtime, root, value) {
1283
+ const name = branchArgument(value);
1284
+ const resolved = await runtime.run(root, ['rev-parse', '--verify', '--quiet', `${name}^{commit}`]);
1285
+ const hash = resolved.stdout.trim();
1286
+ if (resolved.code !== 0 || hash === '') {
1287
+ throw new GitError('git/branch-invalid', `The branch ${name} no longer exists.`);
1288
+ }
1289
+ return { name, hash };
1290
+ }
1291
+ /**
1292
+ * Name the merge or rebase an operation endpoint can act on.
1293
+ * @param snapshot the current status.
1294
+ * @returns the kind, or null.
1295
+ */
1296
+ function mergeOrRebase(snapshot) {
1297
+ if (snapshot.merging)
1298
+ return 'merge';
1299
+ if (snapshot.rebasing)
1300
+ return 'rebase';
1301
+ return null;
1302
+ }
1303
+ /**
1304
+ * Build the failure for a merge or rebase that did not stop on a conflict.
1305
+ * @param kind the operation.
1306
+ * @param result the command result.
1307
+ * @returns the error.
1308
+ */
1309
+ function operationFailure(kind, result) {
1310
+ const message = result.stderr.trim() || result.stdout.trim() || (kind === 'merge' ? 'Merge failed.' : 'Rebase failed.');
1311
+ return new GitError(kind === 'merge' ? 'git/merge-failed' : 'git/rebase-failed', message, {
1312
+ stdout: result.stdout,
1313
+ stderr: result.stderr,
1314
+ });
1315
+ }
1316
+ /**
1317
+ * Merge one branch into the current branch.
1318
+ * @param runtime the runtime helpers.
1319
+ * @param root the repository root.
1320
+ * @param options the merge request.
1321
+ * @returns the output, fresh status, and conflicted paths.
1322
+ */
1323
+ export async function merge(runtime, root, options) {
1324
+ const snapshot = await readStatus(runtime, root);
1325
+ refuseOperation(snapshot);
1326
+ if (snapshot.branch.detached || snapshot.branch.name === null) {
1327
+ throw new GitError('git/detached', 'Check out a branch before merging.');
1328
+ }
1329
+ const source = await branchTip(runtime, root, options.branch);
1330
+ if (source.name === snapshot.branch.name) {
1331
+ throw new GitError('git/branch-invalid', 'Choose a branch other than the current one.');
1332
+ }
1333
+ const args = ['merge', '--no-edit', options.noFastForward ? '--no-ff' : '--ff', source.name];
1334
+ const result = await runtime.run(root, args);
1335
+ const status = await readStatus(runtime, root);
1336
+ const output = `${result.stdout}${result.stderr}`.trim();
1337
+ if (result.code !== 0) {
1338
+ if (status.merging)
1339
+ return { output, status, conflicted: status.conflicts };
1340
+ throw operationFailure('merge', result);
1341
+ }
1342
+ return { output, status, ...(status.conflicts.length > 0 ? { conflicted: status.conflicts } : {}) };
1343
+ }
1344
+ /**
1345
+ * Rebase the current branch onto another branch.
1346
+ * @param runtime the runtime helpers.
1347
+ * @param root the repository root.
1348
+ * @param options the rebase request.
1349
+ * @returns the output, fresh status, and conflicted paths.
1350
+ */
1351
+ export async function rebase(runtime, root, options) {
1352
+ const snapshot = await readStatus(runtime, root);
1353
+ refuseOperation(snapshot);
1354
+ if (snapshot.branch.detached || snapshot.branch.name === null) {
1355
+ throw new GitError('git/detached', 'Check out a branch before rebasing.');
1356
+ }
1357
+ const source = await branchTip(runtime, root, options.branch);
1358
+ if (source.name === snapshot.branch.name) {
1359
+ throw new GitError('git/branch-invalid', 'Choose a branch other than the current one.');
1360
+ }
1361
+ if (snapshot.entries.length > 0) {
1362
+ throw new GitError('git/dirty-tree', 'Commit or stash your changes before rebasing.');
1363
+ }
1364
+ const result = await runtime.run(root, ['rebase', source.name], {
1365
+ env: { GIT_EDITOR: 'true', GIT_SEQUENCE_EDITOR: 'true' },
1366
+ });
1367
+ const status = await readStatus(runtime, root);
1368
+ const output = `${result.stdout}${result.stderr}`.trim();
1369
+ if (result.code !== 0) {
1370
+ if (status.rebasing)
1371
+ return { output, status, conflicted: status.conflicts };
1372
+ throw operationFailure('rebase', result);
1373
+ }
1374
+ return { output, status, ...(status.conflicts.length > 0 ? { conflicted: status.conflicts } : {}) };
1375
+ }
1376
+ /**
1377
+ * Continue a stopped merge or rebase.
1378
+ * @param runtime the runtime helpers.
1379
+ * @param root the repository root.
1380
+ * @returns the output, fresh status, and conflicted paths.
1381
+ */
1382
+ export async function operationContinue(runtime, root) {
1383
+ const snapshot = await readStatus(runtime, root);
1384
+ const kind = mergeOrRebase(snapshot);
1385
+ if (kind === null) {
1386
+ throw new GitError('git/no-operation', 'No merge or rebase is in progress.');
1387
+ }
1388
+ if (snapshot.conflicts.length > 0) {
1389
+ throw new GitError('git/conflict', 'Resolve and stage the conflicts before continuing.');
1390
+ }
1391
+ const args = kind === 'merge' ? ['merge', '--continue'] : ['rebase', '--continue'];
1392
+ const result = await runtime.run(root, args, { env: { GIT_EDITOR: 'true', GIT_SEQUENCE_EDITOR: 'true' } });
1393
+ const status = await readStatus(runtime, root);
1394
+ const output = `${result.stdout}${result.stderr}`.trim();
1395
+ if (result.code !== 0) {
1396
+ if ((status.merging || status.rebasing) && status.conflicts.length > 0) {
1397
+ return { output, status, conflicted: status.conflicts };
1398
+ }
1399
+ throw operationFailure(kind, result);
1400
+ }
1401
+ return { output, status, ...(status.conflicts.length > 0 ? { conflicted: status.conflicts } : {}) };
1402
+ }
1403
+ /**
1404
+ * Abort a stopped merge or rebase.
1405
+ * @param runtime the runtime helpers.
1406
+ * @param root the repository root.
1407
+ * @returns the output and fresh status.
1408
+ */
1409
+ export async function operationAbort(runtime, root) {
1410
+ const snapshot = await readStatus(runtime, root);
1411
+ const kind = mergeOrRebase(snapshot);
1412
+ if (kind === null) {
1413
+ throw new GitError('git/no-operation', 'No merge or rebase is in progress.');
1414
+ }
1415
+ const result = await runtime.run(root, kind === 'merge' ? ['merge', '--abort'] : ['rebase', '--abort']);
1416
+ if (result.code !== 0) {
1417
+ throw new GitError(kind === 'merge' ? 'git/merge-abort-failed' : 'git/rebase-abort-failed', result.stderr.trim() || result.stdout.trim() || `Git could not abort the ${kind}.`, { stdout: result.stdout, stderr: result.stderr });
1418
+ }
1419
+ return {
1420
+ output: `${result.stdout}${result.stderr}`.trim(),
1421
+ status: await readStatus(runtime, root),
1422
+ };
1423
+ }
1424
+ /**
1425
+ * Skip the commit a rebase stopped on.
1426
+ * @param runtime the runtime helpers.
1427
+ * @param root the repository root.
1428
+ * @returns the output, fresh status, the skipped commit, and conflicted paths.
1429
+ */
1430
+ export async function rebaseSkip(runtime, root) {
1431
+ const snapshot = await readStatus(runtime, root);
1432
+ if (!snapshot.rebasing) {
1433
+ throw new GitError('git/no-operation', 'No rebase is in progress.');
1434
+ }
1435
+ const stopped = await runtime.run(root, ['rev-parse', '--verify', '--quiet', 'REBASE_HEAD']);
1436
+ const skipped = stopped.code === 0 ? stopped.stdout.trim() : '';
1437
+ const result = await runtime.run(root, ['rebase', '--skip'], {
1438
+ env: { GIT_EDITOR: 'true', GIT_SEQUENCE_EDITOR: 'true' },
1439
+ });
1440
+ const status = await readStatus(runtime, root);
1441
+ const output = `${result.stdout}${result.stderr}`.trim();
1442
+ const extra = skipped === '' ? {} : { skipped };
1443
+ if (result.code !== 0) {
1444
+ if (status.rebasing && status.conflicts.length > 0) {
1445
+ return { output, status, conflicted: status.conflicts, ...extra };
1446
+ }
1447
+ throw operationFailure('rebase', result);
1448
+ }
1449
+ return { output, status, ...extra };
1450
+ }
1451
+ /**
1452
+ * Read what a rebase dialog needs before it starts.
1453
+ * @param runtime the runtime helpers.
1454
+ * @param root the repository root.
1455
+ * @param options the rebase request.
1456
+ * @returns the replay and pushed commit counts.
1457
+ */
1458
+ export async function rebasePreview(runtime, root, options) {
1459
+ const snapshot = await readStatus(runtime, root);
1460
+ refuseOperation(snapshot);
1461
+ if (snapshot.branch.detached || snapshot.branch.name === null) {
1462
+ throw new GitError('git/detached', 'Check out a branch before rebasing.');
1463
+ }
1464
+ const source = await branchTip(runtime, root, options.branch);
1465
+ if (source.name === snapshot.branch.name) {
1466
+ throw new GitError('git/branch-invalid', 'Choose a branch other than the current one.');
1467
+ }
1468
+ const totalResult = await runtime.run(root, ['rev-list', '--count', `${source.name}..HEAD`]);
1469
+ if (totalResult.code !== 0) {
1470
+ throw failure(totalResult, 'Git could not count the commits to replay.');
1471
+ }
1472
+ const total = Number.parseInt(totalResult.stdout.trim(), 10);
1473
+ const commits = Number.isFinite(total) ? total : 0;
1474
+ const upstream = snapshot.branch.upstream;
1475
+ let pushed = 0;
1476
+ if (upstream !== null && upstream !== '') {
1477
+ const unpushedResult = await runtime.run(root, [
1478
+ 'rev-list',
1479
+ '--count',
1480
+ `${source.name}..HEAD`,
1481
+ '--not',
1482
+ upstream,
1483
+ ]);
1484
+ if (unpushedResult.code === 0) {
1485
+ const unpushed = Number.parseInt(unpushedResult.stdout.trim(), 10);
1486
+ if (Number.isFinite(unpushed))
1487
+ pushed = Math.max(0, commits - unpushed);
1488
+ }
1489
+ }
1490
+ return { commits, pushed, upstream };
1491
+ }
1236
1492
  /**
1237
1493
  * Read the staged patch for the message writer.
1238
1494
  * @param runtime the runtime helpers.
@@ -1597,15 +1853,35 @@ export async function stashList(runtime, root) {
1597
1853
  */
1598
1854
  export async function stashPush(runtime, root, options) {
1599
1855
  const message = typeof options.message === 'string' && options.message.trim() !== '' ? options.message.trim() : 'WIP';
1600
- const paths = Array.isArray(options.paths) ? options.paths.map((value) => repoPath(root, value).path) : [];
1856
+ if (!Array.isArray(options.paths) || options.paths.length === 0) {
1857
+ throw new GitError('git/invalid-path', 'Select at least one file to stash.');
1858
+ }
1859
+ const paths = [...new Set(options.paths.map((value) => repoPath(root, value).path))];
1860
+ const snapshot = await readStatus(runtime, root);
1861
+ if (snapshot.merging || snapshot.rebasing || snapshot.reverting || snapshot.cherryPicking) {
1862
+ throw new GitError('git/operation', 'Finish the current operation before stashing changes.');
1863
+ }
1864
+ const entries = paths.map((path) => snapshot.entries.find((entry) => entry.path === path));
1865
+ if (entries.some((entry) => entry === undefined)) {
1866
+ throw new GitError('git/stale', 'A selected file changed after the list was read. Refresh and try again.');
1867
+ }
1868
+ if (entries.some((entry) => entry?.conflicted)) {
1869
+ throw new GitError('git/conflict', 'Resolve conflicts before stashing changes.');
1870
+ }
1871
+ if (entries.some((entry) => entry?.kind === 'renamed') ||
1872
+ snapshot.entries.some((entry) => entry.kind === 'renamed' && entry.from != null && paths.includes(entry.from))) {
1873
+ throw new GitError('git/unsupported', 'Stashing renamed files or their old paths is not supported. Commit or unstage the rename first.');
1874
+ }
1601
1875
  const args = ['stash', 'push', '-m', message];
1602
- if (options.includeUntracked === true)
1876
+ if (entries.some((entry) => entry?.untracked))
1603
1877
  args.push('-u');
1604
- if (paths.length > 0)
1605
- args.push('--', ...paths);
1878
+ args.push('--', ...paths.map((value) => `:(literal)${value}`));
1606
1879
  const result = await runtime.run(root, args);
1607
1880
  if (result.code !== 0)
1608
1881
  throw failure(result, 'Git could not stash the selected files.');
1882
+ if (result.stdout.includes('No local changes to save')) {
1883
+ throw new GitError('git/stale', 'The selected files no longer have changes. Refresh and try again.');
1884
+ }
1609
1885
  return {
1610
1886
  output: `${result.stdout}${result.stderr}`.trim(),
1611
1887
  status: await readStatus(runtime, root),
@@ -1,6 +1,6 @@
1
1
  import { preferences, writeCommitMessage } from './ai.js';
2
2
  import { GitError } from './exec.js';
3
- import { applyBlocks, branches, checkout, cherryPickCommit, commit, commitDetail, createBranch, createBranchAndCommit, discardPaths, discoverRepositories, diff, fetch, findRepository, initRepository, log, pull, push, recentSubjects, resetToCommit, resolveConflict, resolveRepository, revertCommit, sessionCheckout, stagePaths, stagedPatch, stashApply, stashDrop, stashList, stashPush, status, undoCommit, unstagePaths, } from './git.js';
3
+ import { applyBlocks, branches, checkout, cherryPickCommit, commit, commitDetail, createBranch, createBranchAndCommit, discardPaths, discoverRepositories, diff, fetch, findRepository, initRepository, log, merge, operationAbort, operationContinue, pull, push, rebase, rebasePreview, rebaseSkip, recentSubjects, resetToCommit, resolveConflict, resolveRepository, revertCommit, sessionCheckout, stagePaths, stagedPatch, stashApply, stashDrop, stashList, stashPush, status, undoCommit, unstagePaths, } from './git.js';
4
4
  /**
5
5
  * Read one field from a client payload.
6
6
  * @param payload the payload.
@@ -251,6 +251,21 @@ async function dispatch(ctx, runtime, endpoint, payload) {
251
251
  branch: stringField(payload, 'branch'),
252
252
  remote: valueOf(payload, 'remote') === true,
253
253
  });
254
+ case 'merge':
255
+ return merge(runtime, root, {
256
+ branch: stringField(payload, 'branch'),
257
+ noFastForward: valueOf(payload, 'noFastForward') === true,
258
+ });
259
+ case 'rebase':
260
+ return rebase(runtime, root, { branch: stringField(payload, 'branch') });
261
+ case 'rebasePreview':
262
+ return rebasePreview(runtime, root, { branch: stringField(payload, 'branch') });
263
+ case 'operationContinue':
264
+ return operationContinue(runtime, root);
265
+ case 'operationAbort':
266
+ return operationAbort(runtime, root);
267
+ case 'rebaseSkip':
268
+ return rebaseSkip(runtime, root);
254
269
  case 'stashList':
255
270
  return stashList(runtime, root);
256
271
  case 'stashPush':
@@ -283,6 +298,7 @@ async function dispatch(ctx, runtime, endpoint, payload) {
283
298
  signal: AbortSignal.timeout(60_000),
284
299
  model: valueOf(payload, 'model'),
285
300
  instruction: valueOf(payload, 'instruction'),
301
+ sessionId: optionalString(payload, 'sessionId'),
286
302
  });
287
303
  }
288
304
  default:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-git-desk",
3
- "version": "0.23.0",
3
+ "version": "0.25.0",
4
4
  "type": "module",
5
5
  "description": "Git desk for DeepSeek Harness: a right-sidebar Git workbench with full-file side-by-side diffs, block staging, commits, history, branches, stash, and sync.",
6
6
  "keywords": [
@@ -34,6 +34,7 @@
34
34
  "files": [
35
35
  "dist",
36
36
  "client.js",
37
+ "client.highlight.js",
37
38
  "cordis.patch.yml",
38
39
  "locale/*.json",
39
40
  "icon.png",
@@ -65,6 +66,23 @@
65
66
  "test": "node --test tests/*.test.mjs"
66
67
  },
67
68
  "devDependencies": {
69
+ "@codemirror/lang-cpp": "^6.0.3",
70
+ "@codemirror/lang-css": "^6.3.1",
71
+ "@codemirror/lang-go": "^6.0.1",
72
+ "@codemirror/lang-html": "^6.4.12",
73
+ "@codemirror/lang-java": "^6.0.2",
74
+ "@codemirror/lang-javascript": "^6.2.5",
75
+ "@codemirror/lang-json": "^6.0.2",
76
+ "@codemirror/lang-markdown": "^6.5.2",
77
+ "@codemirror/lang-php": "^6.0.2",
78
+ "@codemirror/lang-python": "^6.2.1",
79
+ "@codemirror/lang-rust": "^6.0.2",
80
+ "@codemirror/lang-sql": "^6.10.0",
81
+ "@codemirror/lang-xml": "^6.1.0",
82
+ "@codemirror/lang-yaml": "^6.1.3",
83
+ "@codemirror/language": "^6.13.1",
84
+ "@codemirror/state": "^6.7.6",
85
+ "@lezer/highlight": "^1.2.5",
68
86
  "@types/node": "^22.20.5",
69
87
  "@types/react": "^18.3.31",
70
88
  "@types/react-dom": "^18.3.7",