Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(worktree): pure, tested mapping to GroupWorktree (createdByApp, baseRef, safe default path)

enes4a6cbb239f16
diff --git a/src/shared/worktree.test.ts b/src/shared/worktree.test.ts
index 8ff7e7942..c6f96fb19 100644
--- a/src/shared/worktree.test.ts
+++ b/src/shared/worktree.test.ts
@@ -5,9 +5,21 @@ import {
parseWorktreePorcelain,
isDangerousWorktreeRemovalPath,
decideMergeStrategy,
- isValidGitRef
+ isValidGitRef,
+ resolveBaseRef,
+ worktreeFromCreate,
+ worktreeFromEntry,
+ DEFAULT_BASE_REF,
+ type WorktreeEntry
} from './worktree'
+const entry = (path: string, branch: string | null): WorktreeEntry => ({
+ path,
+ branch,
+ head: 'abc',
+ isBare: false
+})
+
describe('isValidGitRef', () => {
it('accepts a normal branch name', () => {
expect(isValidGitRef('feature/x')).toBe(true)
@@ -32,6 +44,78 @@ describe('computeWorktreePath', () => {
it('builds <userData>/worktrees/<repo>/<branch> with a flattened branch', () => {
expect(computeWorktreePath('/u', 'myrepo', 'feature/x')).toBe('/u/worktrees/myrepo/feature-x')
})
+ it('suggests NOTHING when the base dir is unknown (never a filesystem-root path)', () => {
+ // An empty base used to produce `/worktrees/<repo>/<branch>` — i.e. the filesystem root,
+ // which the Server Edition (running as root) would happily create.
+ expect(computeWorktreePath('', 'myrepo', 'x')).toBe('')
+ expect(computeWorktreePath(' ', 'myrepo', 'x')).toBe('')
+ })
+ it('drops a trailing slash on the base dir', () => {
+ expect(computeWorktreePath('/u/', 'r', 'b')).toBe('/u/worktrees/r/b')
+ })
+})
+
+describe('resolveBaseRef', () => {
+ it("uses the main checkout's branch — git prints it first", () => {
+ expect(resolveBaseRef([entry('/repo', 'trunk'), entry('/wt/x', 'feature/x')])).toBe('trunk')
+ expect(resolveBaseRef([entry('/repo', 'master')])).toBe('master')
+ })
+ it('falls back to main when the default branch is unknown (detached / empty list)', () => {
+ expect(resolveBaseRef([])).toBe(DEFAULT_BASE_REF)
+ expect(resolveBaseRef([entry('/repo', null)])).toBe(DEFAULT_BASE_REF)
+ expect(resolveBaseRef([entry('/repo', ' ')])).toBe(DEFAULT_BASE_REF)
+ })
+})
+
+describe('worktreeFromCreate', () => {
+ it('marks a worktree the app just created as createdByApp (Remove may delete the dir)', () => {
+ expect(
+ worktreeFromCreate({
+ repoPath: '/repo',
+ mode: 'new',
+ branch: 'feature/x',
+ baseRef: 'master',
+ path: '/u/worktrees/repo/feature-x'
+ })
+ ).toEqual({
+ repoPath: '/repo',
+ branch: 'feature/x',
+ baseRef: 'master',
+ path: '/u/worktrees/repo/feature-x',
+ createdByApp: true
+ })
+ })
+ it('trims the collected fields', () => {
+ const wt = worktreeFromCreate({
+ repoPath: ' /repo ',
+ mode: 'existing',
+ branch: ' feature/x ',
+ baseRef: ' main ',
+ path: ' /wt/x '
+ })
+ expect(wt).toMatchObject({ repoPath: '/repo', branch: 'feature/x', baseRef: 'main', path: '/wt/x' })
+ })
+})
+
+describe('worktreeFromEntry', () => {
+ it('marks an ADOPTED worktree as NOT createdByApp (Remove must not delete the user\'s dir)', () => {
+ const wt = worktreeFromEntry(entry('/wt/x', 'feature/x'), '/repo', 'trunk')
+ expect(wt).toEqual({
+ repoPath: '/repo',
+ branch: 'feature/x',
+ baseRef: 'trunk',
+ path: '/wt/x',
+ createdByApp: false
+ })
+ })
+ it('defaults the base ref to main when the repo default is unknown', () => {
+ expect(worktreeFromEntry(entry('/wt/x', 'feature/x'), '/repo', '')?.baseRef).toBe(DEFAULT_BASE_REF)
+ })
+ it('refuses a detached worktree or a missing repo root', () => {
+ expect(worktreeFromEntry(entry('/wt/x', null), '/repo', 'main')).toBeNull()
+ expect(worktreeFromEntry(entry('/wt/x', 'b'), '', 'main')).toBeNull()
+ expect(worktreeFromEntry(entry('', 'b'), '/repo', 'main')).toBeNull()
+ })
})
describe('parseWorktreePorcelain', () => {
diff --git a/src/shared/worktree.ts b/src/shared/worktree.ts
index 1d856d5cc..4b25b877c 100644
--- a/src/shared/worktree.ts
+++ b/src/shared/worktree.ts
@@ -39,10 +39,75 @@ export function sanitizeWorktreeBranch(input: string): string {
.replace(/(^-|-$)/g, '')
}
-/** Default on-disk location: <userData>/worktrees/<repo>/<branch-flattened>. */
+/**
+ * Default on-disk location: <userData>/worktrees/<repo>/<branch-flattened>.
+ * An unknown base dir yields NO suggestion (empty string) rather than a root-relative
+ * `/worktrees/...` — the Server Edition often runs as root and `git worktree add` would
+ * cheerfully create that at the filesystem root. Callers must treat '' as "no default".
+ */
export function computeWorktreePath(userDataDir: string, repoName: string, branch: string): string {
+ const base = userDataDir.trim().replace(/\/+$/, '')
+ if (!base) return ''
const flat = branch.replace(/\//g, '-')
- return `${userDataDir}/worktrees/${repoName}/${flat}`
+ return `${base}/worktrees/${repoName}/${flat}`
+}
+
+/** Values the worktree dialog collects. Mapped to a `GroupWorktree` by `worktreeFromCreate`. */
+export interface WorktreeCreateValue {
+ repoPath: string
+ mode: 'new' | 'existing'
+ branch: string
+ baseRef: string
+ path: string
+}
+
+/** Last-resort merge target when the repo's default branch cannot be read. */
+export const DEFAULT_BASE_REF = 'main'
+
+/**
+ * The repo's default branch = the branch of its MAIN checkout, which git prints FIRST in
+ * `git worktree list` (the caller must keep git's order). Hardcoding 'main' would send a
+ * master/trunk repo's merge at a ref that does not exist.
+ */
+export function resolveBaseRef(entries: WorktreeEntry[]): string {
+ return entries[0]?.branch?.trim() || DEFAULT_BASE_REF
+}
+
+/**
+ * Binding for a worktree THIS APP just created — `createdByApp: true` grants Remove the right
+ * to delete the directory. Only call this after `git worktree add` succeeded.
+ */
+export function worktreeFromCreate(v: WorktreeCreateValue): GroupWorktree {
+ return {
+ repoPath: v.repoPath.trim(),
+ branch: v.branch.trim(),
+ baseRef: v.baseRef.trim() || DEFAULT_BASE_REF,
+ path: v.path.trim(),
+ createdByApp: true
+ }
+}
+
+/**
+ * Binding for a worktree that ALREADY EXISTED on disk (adopted from `git worktree list`) —
+ * `createdByApp: false`, so Remove must never delete a directory the user made themselves.
+ * Returns null when the binding cannot be trusted (detached HEAD, unknown repo root/path).
+ */
+export function worktreeFromEntry(
+ entry: WorktreeEntry,
+ repoPath: string,
+ baseRef: string
+): GroupWorktree | null {
+ const path = entry.path.trim()
+ const branch = entry.branch?.trim()
+ const repo = repoPath.trim()
+ if (!repo || !branch || !path) return null
+ return {
+ repoPath: repo,
+ branch,
+ baseRef: baseRef.trim() || DEFAULT_BASE_REF,
+ path,
+ createdByApp: false
+ }
}
/** Parse `git worktree list --porcelain` into structured entries. */