From 5e892711127505cb8bdf5a9bf5efe663d9b01eaf Mon Sep 17 00:00:00 2001 From: Vuong Vu Date: Wed, 4 Nov 2026 01:02:50 +0100 Subject: [PATCH 1/2] feat: add editor command settings and open editor functionality --- server/index.ts | 26 +++++++++++++++++++++++++- src/RepoView.tsx | 12 +++++++++++- src/SettingsDialog.tsx | 17 +++++++++++++++++ src/settings.test.ts | 6 ++++++ src/settings.ts | 3 +++ src/styles.css | 7 +++++-- 6 files changed, 67 insertions(+), 4 deletions(-) diff --git a/server/index.ts b/server/index.ts index 8c8b461..55eccf6 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1,5 +1,5 @@ import { createApp, serveStatic, type Handler } from './http.ts' -import { execFile } from 'node:child_process' +import { execFile, spawn } from 'node:child_process' import { existsSync, readdirSync, readFileSync } from 'node:fs' import { readFile, realpath, writeFile } from 'node:fs/promises' import { homedir } from 'node:os' @@ -297,6 +297,30 @@ app.get('/api/worktrees', repoGuard, async (req, res) => { } }) +// Opens the repo in the user's editor. The command comes from the client's +// settings — no new capability, /api/term already runs anything, and the Host pin +// keeps foreign pages out. No shell: the command splits on whitespace and the repo +// path is its own argv entry, so neither can inject. Answers once the process has +// either spawned or failed, so a missing `code` reaches the user as an error. +// ponytail: whitespace split — an editor path containing spaces needs a PATH entry +// instead. Windows .cmd shims (code.cmd) can't spawn without a shell; unverified there. +app.post('/api/open-editor', repoGuard, (req, res) => { + const argv = typeof req.body.cmd === 'string' ? req.body.cmd.trim().split(/\s+/).filter(Boolean) : [] + if (!argv.length) { + res.status(400).json({ error: 'no editor command set' }) + return + } + const child = spawn(argv[0], [...argv.slice(1), String(req.query.repo)], { detached: true, stdio: 'ignore' }) + child.once('spawn', () => { + child.unref() + res.json({ ok: true }) + }) + child.once('error', e => { + const notFound = (e as NodeJS.ErrnoException).code === 'ENOENT' + res.status(notFound ? 404 : 500).json({ error: notFound ? `"${argv[0]}" not found — set your editor in Settings` : e.message }) + }) +}) + // Removes a linked worktree. The path is allow-listed against git's own list, minus // [0] (the main worktree, which git can't remove). Without force, git refuses a // worktree with uncommitted or untracked files — the client only sends force after diff --git a/src/RepoView.tsx b/src/RepoView.tsx index 3986a7d..e762abd 100644 --- a/src/RepoView.tsx +++ b/src/RepoView.tsx @@ -13,7 +13,7 @@ import type { Worktree } from '../server/worktrees.ts' import WorktreeStrip from './WorktreeStrip' import { label, matchLocal, stepMatch } from './search' import { toastErr } from './Toast' -import { useSetting } from './settingsStore' +import { getSettings, useSetting } from './settingsStore' import { tilde } from './paths' // diff2html + highlight.js (~1 MB, 82% of the old main bundle) stay out until a @@ -442,6 +442,16 @@ export default function RepoView({ repo, home, linked, onOpen, onRemove, hasTerm )} +