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.

feat(project-settings): pure worktree baseRef/basePath resolver

enesd5bfec9ac99b
diff --git a/src/shared/worktree.test.ts b/src/shared/worktree.test.ts
index b5f11ba6e..5c5d719ff 100644
--- a/src/shared/worktree.test.ts
+++ b/src/shared/worktree.test.ts
@@ -15,8 +15,12 @@ import {
worktreeFromCreate,
worktreeFromEntry,
worktreeRemoveMessage,
+ effectiveWorktreeBaseRef,
+ effectiveWorktreeTemplate,
DEFAULT_BASE_REF,
- type WorktreeEntry
+ DEFAULT_WORKTREE_PATH_TEMPLATE,
+ type WorktreeEntry,
+ type ProjectWorktreeDefaults
} from './worktree'
import { filterWorktrees } from './worktree'
@@ -444,3 +448,67 @@ describe('filterWorktrees', () => {
expect(filterWorktrees(entries, 'not-here')).toEqual([])
})
})
+
+// Pure worktree-creation resolver: what baseRef and path TEMPLATE a new worktree should default to,
+// given a project's own settings. Neither function does IO or validates a path — computeWorktreePath
+// (the caller) already refuses a shallow-rooted result, so a hostile basePath is left to fall back
+// exactly as it does today; see the `computeWorktreePath` case below.
+describe('effectiveWorktreeBaseRef', () => {
+ const entries = [entry('/repo', 'trunk'), entry('/wt/x', 'feature/x')]
+
+ it('a project baseRef wins over the repo-derived default', () => {
+ const project: ProjectWorktreeDefaults = { baseRef: 'release' }
+ expect(effectiveWorktreeBaseRef(project, entries)).toBe('release')
+ })
+
+ it('falls back to resolveBaseRef(entries) when the project has no baseRef', () => {
+ expect(effectiveWorktreeBaseRef(undefined, entries)).toBe('trunk')
+ expect(effectiveWorktreeBaseRef({}, entries)).toBe('trunk')
+ })
+
+ it('treats a whitespace-only project baseRef as unset', () => {
+ expect(effectiveWorktreeBaseRef({ baseRef: ' ' }, entries)).toBe('trunk')
+ })
+
+ it('falls all the way to DEFAULT_BASE_REF when neither the project nor the repo says', () => {
+ expect(effectiveWorktreeBaseRef(undefined, [])).toBe(DEFAULT_BASE_REF)
+ })
+})
+
+describe('effectiveWorktreeTemplate', () => {
+ it('a set project basePath becomes `<basePath>/${branch}`', () => {
+ expect(effectiveWorktreeTemplate({ basePath: '/srv/worktrees' }, undefined)).toBe(
+ '/srv/worktrees/${branch}'
+ )
+ })
+
+ it('joins with exactly one slash even when basePath has a trailing slash', () => {
+ expect(effectiveWorktreeTemplate({ basePath: '/srv/worktrees/' }, undefined)).toBe(
+ '/srv/worktrees/${branch}'
+ )
+ })
+
+ it('a whitespace-only basePath is ignored, falling through to the global template', () => {
+ expect(effectiveWorktreeTemplate({ basePath: ' ' }, './worktrees')).toBe('./worktrees')
+ })
+
+ it('falls back to the trimmed global template when there is no project basePath', () => {
+ expect(effectiveWorktreeTemplate(undefined, ' ./worktrees ')).toBe('./worktrees')
+ expect(effectiveWorktreeTemplate({}, './worktrees')).toBe('./worktrees')
+ })
+
+ it('falls back to DEFAULT_WORKTREE_PATH_TEMPLATE when neither project basePath nor global template is set', () => {
+ expect(effectiveWorktreeTemplate(undefined, undefined)).toBe(DEFAULT_WORKTREE_PATH_TEMPLATE)
+ expect(effectiveWorktreeTemplate({}, ' ')).toBe(DEFAULT_WORKTREE_PATH_TEMPLATE)
+ })
+
+ // The function itself does no path resolution or validation — it only builds a template string.
+ // A hostile basePath like '/' must still end up refused, but that refusal happens DOWNSTREAM in
+ // computeWorktreePath (via resolvePathFromRepo's shallow-root guard), not here. Prove the chain
+ // actually holds by feeding the synthesized template through computeWorktreePath.
+ it('a rooted basePath produces a template that computeWorktreePath later refuses', () => {
+ const template = effectiveWorktreeTemplate({ basePath: '/' }, undefined)
+ expect(template).toBe('/${branch}')
+ expect(computeWorktreePath('/src/repo', 'topic/a', template)).toBe('')
+ })
+})
diff --git a/src/shared/worktree.ts b/src/shared/worktree.ts
index 2ae3e5f0c..522dfe499 100644
--- a/src/shared/worktree.ts
+++ b/src/shared/worktree.ts
@@ -188,6 +188,43 @@ export function resolveBaseRef(entries: WorktreeEntry[]): string {
return entries[0]?.branch?.trim() || DEFAULT_BASE_REF
}
+/** The worktree-related fields a project can override — see `effectiveWorktreeBaseRef` /
+ * `effectiveWorktreeTemplate`. Both optional: an unset or whitespace-only value defers below it. */
+export interface ProjectWorktreeDefaults {
+ basePath?: string
+ baseRef?: string
+}
+
+/**
+ * The baseRef a NEW worktree should default to: a project override wins, else the repo's own
+ * default branch (`resolveBaseRef`, which falls back to `DEFAULT_BASE_REF`). Pure — no IO.
+ */
+export function effectiveWorktreeBaseRef(
+ project: ProjectWorktreeDefaults | undefined,
+ entries: WorktreeEntry[]
+): string {
+ return project?.baseRef?.trim() || resolveBaseRef(entries)
+}
+
+/**
+ * The path TEMPLATE a new worktree's location should expand from — fed straight into
+ * `computeWorktreePath`. A project `basePath` becomes `<basePath>/${branch}` (exactly one slash
+ * between the two); otherwise the given global template, or `DEFAULT_WORKTREE_PATH_TEMPLATE`.
+ *
+ * Deliberately does NOT resolve or validate `basePath` — `computeWorktreePath`'s
+ * `resolvePathFromRepo` already refuses a template that resolves to (or under) the filesystem root,
+ * so a hostile `basePath` such as `'/'` synthesizes `/${branch}`, which that downstream guard
+ * refuses (returns `''`), and creation falls back exactly as it does today. Pure — no IO.
+ */
+export function effectiveWorktreeTemplate(
+ project: ProjectWorktreeDefaults | undefined,
+ globalTemplate: string | undefined
+): string {
+ const basePath = project?.basePath?.trim()
+ if (basePath) return `${basePath.replace(/\/+$/, '')}/\${branch}`
+ return globalTemplate?.trim() || DEFAULT_WORKTREE_PATH_TEMPLATE
+}
+
/**
* 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.