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(windows): compare worktree paths by identity, not spelling

review24af2c7a82f9
diff --git a/src/shared/worktree-ops.test.ts b/src/shared/worktree-ops.test.ts
index f8d74f7a5..932859938 100644
--- a/src/shared/worktree-ops.test.ts
+++ b/src/shared/worktree-ops.test.ts
@@ -272,6 +272,13 @@ describe('worktreeRemove', () => {
expect(calls.some((c) => c.join(' ') === 'worktree prune')).toBe(true)
expect(calls.some((c) => c.join(' ') === 'branch -d feature/x')).toBe(true)
})
+ it('finds a Windows binding spelled with backslashes in git\'s forward-slash listing', async () => {
+ const list = 'worktree C:/r\nbranch refs/heads/main\n\nworktree C:/r/wt\nbranch refs/heads/feat\n'
+ const { git, calls } = fakeGit({ 'worktree list --porcelain': ok(list) })
+ const r = await worktreeRemove(git, 'C:/r', 'C:\\r\\wt', 'C:\\Users\\me', false)
+ expect(r.ok).toBe(true)
+ expect(calls.some((c) => c.join(' ') === 'worktree remove --force -- C:\\r\\wt')).toBe(true)
+ })
it('keeps the branch when the caller does not own it', async () => {
const list = 'worktree /repo\nbranch refs/heads/main\n\nworktree /wt/x\nbranch refs/heads/feature/x\n'
const { git, calls } = fakeGit({ 'worktree list --porcelain': ok(list) })
diff --git a/src/shared/worktree-ops.ts b/src/shared/worktree-ops.ts
index 265ef7f16..126715d77 100644
--- a/src/shared/worktree-ops.ts
+++ b/src/shared/worktree-ops.ts
@@ -3,6 +3,7 @@ import {
isDangerousWorktreeRemovalPath,
decideMergeStrategy,
isValidGitRef,
+ worktreePathKey,
type WorktreeListResult
} from './worktree'
@@ -234,7 +235,7 @@ export async function worktreeRemove(
// terminal's tmux session and rewrite their persisted cwds while the directory is still there.
// Report the failure; a removal the user can retry beats a removal that destroys the wrong thing.
if (!listed.ok) return UNREADABLE(repoPath)
- const entry = listed.entries.find((e) => e.path.replace(/\/+$/, '') === wtPath.replace(/\/+$/, ''))
+ const entry = listed.entries.find((e) => worktreePathKey(e.path) === worktreePathKey(wtPath))
if (!entry) {
// git answered, and it does not know this path. Only the DIRECTORY's absence proves the
// worktree is gone: a path git does not list but that still EXISTS is a binding pointing
diff --git a/src/shared/worktree-reconcile.test.ts b/src/shared/worktree-reconcile.test.ts
index f496e5264..65788bd76 100644
--- a/src/shared/worktree-reconcile.test.ts
+++ b/src/shared/worktree-reconcile.test.ts
@@ -32,6 +32,21 @@ describe('reconcileWorktrees', () => {
expect(r.stale).toEqual([])
})
+ it('matches a Windows binding to git\'s path across separators and drive-letter case', () => {
+ const entries = [wt('C:/r', 'main'), wt('C:/r/wt', 'feat')]
+ for (const p of ['C:\\r\\wt', 'c:/r/wt', 'C:\\R\\WT\\']) {
+ const r = reconcileWorktrees([bound('g1', p, 'feat')], entries)
+ expect(r.stale).toEqual([])
+ expect(r.orphans).toEqual([])
+ }
+ })
+
+ it('keeps POSIX paths case-sensitive', () => {
+ const r = reconcileWorktrees([bound('g1', '/A/wt', 'feat')], [wt('/repo', 'main'), wt('/a/wt', 'feat')])
+ expect(r.stale).toEqual(['g1'])
+ expect(r.orphans.map((o) => o.path)).toEqual(['/a/wt'])
+ })
+
it('reports a worktree on disk that no group is bound to as an orphan', () => {
const r = reconcileWorktrees([], [wt('/repo', 'main'), wt('/wt/loose', 'loose')])
expect(r.orphans.map((o) => o.path)).toEqual(['/wt/loose'])
diff --git a/src/shared/worktree-reconcile.ts b/src/shared/worktree-reconcile.ts
index 8815b8c73..ef38fd7bc 100644
--- a/src/shared/worktree-reconcile.ts
+++ b/src/shared/worktree-reconcile.ts
@@ -1,4 +1,4 @@
-import type { GroupWorktree, WorktreeEntry } from './worktree'
+import { worktreePathKey, type GroupWorktree, type WorktreeEntry } from './worktree'
/** A group node that carries a worktree binding. */
export interface BoundGroup {
@@ -20,8 +20,9 @@ export interface Reconciliation {
orphans: WorktreeEntry[]
}
-/** Compare worktree paths as git prints them vs. as we persisted them: trailing slashes differ. */
-export const normWorktreePath = (p: string): string => p.trim().replace(/\/+$/, '')
+/** Compare worktree paths as git prints them vs. as we persisted them: trailing slashes differ, and
+ * on Windows so do separators and case (`worktreePathKey`). A comparison KEY — never a path to use. */
+export const normWorktreePath = (p: string): string => worktreePathKey(p)
const norm = normWorktreePath
diff --git a/src/shared/worktree.test.ts b/src/shared/worktree.test.ts
index 05fec8db7..707f7e37c 100644
--- a/src/shared/worktree.test.ts
+++ b/src/shared/worktree.test.ts
@@ -2,7 +2,9 @@ import { describe, it, expect } from 'vitest'
import {
descendantIds,
displacedByWorktree,
+ isAncestorPath,
isInsideDir,
+ worktreePathKey,
sanitizeWorktreeBranch,
computeWorktreePath,
resolveWorktreePath,
@@ -179,6 +181,11 @@ describe('worktreeFromCreate', () => {
createdByApp: true
})
})
+ it("stores a Windows path '/'-separated, case kept, the shape git prints", () => {
+ expect(
+ worktreeFromCreate({ repoPath: 'C:\\r', mode: 'new', branch: 'b', baseRef: 'main', path: 'C:\\r.worktrees\\B' }).path
+ ).toBe('C:/r.worktrees/B')
+ })
it('trims the collected fields', () => {
const wt = worktreeFromCreate({
repoPath: ' /repo ',
@@ -265,6 +272,42 @@ describe('isDangerousWorktreeRemovalPath', () => {
})
})
+describe('worktreePathKey (Windows-shaped paths compare by identity, POSIX by exact case)', () => {
+ it('folds separators, trailing slashes, drive-letter and path case for Windows paths', () => {
+ expect(worktreePathKey('C:\\r\\wt\\')).toBe('c:/r/wt')
+ expect(worktreePathKey('c:/R/Wt')).toBe(worktreePathKey('C:\\r\\wt'))
+ expect(worktreePathKey('\\\\server\\share\\Wt')).toBe('//server/share/wt')
+ })
+ it('keeps a POSIX path byte-identical to the old trailing-slash strip', () => {
+ expect(worktreePathKey('/A/wt/')).toBe('/A/wt')
+ expect(worktreePathKey('/A/wt')).not.toBe(worktreePathKey('/a/wt'))
+ expect(worktreePathKey(' /x ')).toBe('/x')
+ })
+ it('isAncestorPath matches across separators and case on Windows paths only', () => {
+ expect(isAncestorPath('C:/Users', 'C:\\Users\\x')).toBe(true)
+ expect(isAncestorPath('c:\\users\\', 'C:/Users/x/y')).toBe(true)
+ expect(isAncestorPath('C:/Users', 'C:/Users2')).toBe(false)
+ expect(isAncestorPath('/Users', '/users/x')).toBe(false)
+ expect(isAncestorPath('/', '/x')).toBe(true)
+ })
+})
+
+describe('isDangerousWorktreeRemovalPath on Windows paths', () => {
+ it('refuses home spelled with the other separator or case', () => {
+ expect(isDangerousWorktreeRemovalPath('C:/Users/x', 'C:/r', 'C:\\Users\\x')).toBe(true)
+ expect(isDangerousWorktreeRemovalPath('c:/users', 'C:/r', 'C:\\Users\\x')).toBe(true)
+ expect(isDangerousWorktreeRemovalPath('C:\\R', 'c:/r', 'C:\\Users\\x')).toBe(true)
+ })
+ it('refuses a bare drive root in every spelling', () => {
+ for (const root of ['C:/', 'C:\\', 'c:', 'D:\\']) {
+ expect(isDangerousWorktreeRemovalPath(root, 'E:/r', 'E:/Users/x')).toBe(true)
+ }
+ })
+ it('allows a sibling worktree', () => {
+ expect(isDangerousWorktreeRemovalPath('C:/r.worktrees/feat', 'C:/r', 'C:\\Users\\x')).toBe(false)
+ })
+})
+
describe('decideMergeStrategy', () => {
it('fetch-updates when base is not checked out anywhere', () => {
expect(decideMergeStrategy({ baseCheckedOutPath: null, baseDirty: false }))
diff --git a/src/shared/worktree.ts b/src/shared/worktree.ts
index 55d8d2b2d..b8054fe3b 100644
--- a/src/shared/worktree.ts
+++ b/src/shared/worktree.ts
@@ -333,7 +333,9 @@ export function worktreeFromCreate(v: WorktreeCreateValue): GroupWorktree {
repoPath: v.repoPath.trim(),
branch: v.branch.trim(),
baseRef: v.baseRef.trim() || DEFAULT_BASE_REF,
- path: v.path.trim(),
+ // '/'-separated (case kept), the shape `git worktree list` prints on every platform, so the
+ // binding and git agree without a key fold. A POSIX path has no backslash: unchanged.
+ path: v.path.trim().replace(/\\/g, '/'),
createdByApp: true
}
}
@@ -396,10 +398,30 @@ export function parseWorktreePorcelain(out: string): WorktreeEntry[] {
return entries
}
-/** Is `child` the directory `parent` itself, or somewhere inside it? (Trailing slashes ignored.) */
+/**
+ * The identity key of a worktree/repo path, for COMPARING paths — never for using one. Git prints
+ * `C:/r/wt` on Windows while a typed or pasted binding says `C:\r\wt` (or `c:/…`), and NTFS is
+ * case-insensitive by default, so a Windows-shaped path (any backslash, a drive letter, or UNC
+ * `//`) is folded to forward slashes and lower case. A POSIX path keeps its exact case and is only
+ * stripped of trailing slashes — byte-identical to the comparison this replaced. Keyed on the
+ * path's SHAPE, not on `process.platform`: this module runs in the renderer, whose platform is not
+ * the core's.
+ */
+export function worktreePathKey(p: string): string {
+ return pathKey(p.trim())
+}
+
+/** `worktreePathKey` without the trim — `isAncestorPath` and the removal guard never trimmed. */
+function pathKey(t: string): string {
+ if (!/\\|^[A-Za-z]:|^\/\//.test(t)) return t.replace(/\/+$/, '')
+ return t.replace(/\\/g, '/').replace(/\/+$/, '').toLowerCase()
+}
+
+/** Is `child` the directory `parent` itself, or somewhere inside it? (Trailing slashes ignored;
+ * Windows-shaped paths compared by `worktreePathKey`.) */
export const isAncestorPath = (parent: string, child: string): boolean => {
- const p = parent.replace(/\/+$/, '')
- const c = child.replace(/\/+$/, '')
+ const p = pathKey(parent)
+ const c = pathKey(child)
return c === p || c.startsWith(p + '/')
}
@@ -512,9 +534,11 @@ export function displacedByWorktree(
/** Refuse removals that would nuke the repo, home, or filesystem root. */
export function isDangerousWorktreeRemovalPath(worktreePath: string, repoPath: string, homeDir: string): boolean {
- const wt = (worktreePath || '').replace(/\/+$/, '')
+ const wt = pathKey(worktreePath || '')
if (!wt) return true
- if (wt === '/' || wt === repoPath.replace(/\/+$/, '') || wt === homeDir.replace(/\/+$/, '')) return true
+ // A bare drive root (`C:`, `C:\`, `c:/`) is Windows' `/`.
+ if (wt === '/' || /^[a-z]:$/.test(wt)) return true
+ if (wt === pathKey(repoPath) || wt === pathKey(homeDir)) return true
// worktree is an ancestor of the repo or of home → dangerous.
if (isAncestorPath(wt, repoPath) || isAncestorPath(wt, homeDir)) return true
return false