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): a merge never chases a conflict in a base checkout that is gone

enesc12d99108b89
diff --git a/src/core/git-service.ts b/src/core/git-service.ts
index 85bfcf395..db856a9e5 100644
--- a/src/core/git-service.ts
+++ b/src/core/git-service.ts
@@ -240,7 +240,9 @@ export class GitService {
return worktreeOps.worktreeAdd(git, repoPath, wtPath, branch, baseRef, isNew)
}
worktreeMerge(repoPath: string, branch: string, baseRef: string, push = false) {
- return worktreeOps.worktreeMerge(git, repoPath, branch, baseRef, push)
+ // `pathExists` is git's `prunable` flag's fallback for git < 2.36 (see worktree-ops) — and the
+ // only thing that stops a merge into a base checkout whose directory is gone.
+ return worktreeOps.worktreeMerge(git, repoPath, branch, baseRef, push, async (p) => fs.existsSync(p))
}
worktreeRemove(repoPath: string, wtPath: string, deleteBranch: boolean, pruneOnly = false) {
// `pathExists` is git's `prunable` flag's fallback for git < 2.36 (see worktree-ops).
diff --git a/src/shared/worktree-ops.test.ts b/src/shared/worktree-ops.test.ts
index a13300d96..3ac8cf3f0 100644
--- a/src/shared/worktree-ops.test.ts
+++ b/src/shared/worktree-ops.test.ts
@@ -124,6 +124,32 @@ describe('worktreeMerge', () => {
expect(r.message).not.toContain('worktree terminal')
})
+ // Without the `pathExists` seam, a base checkout deleted behind git's back is listed as healthy,
+ // `decideMergeStrategy` picks merge-in-place, and the merge into a directory that is not there
+ // fails — leaving the user chasing "a conflict" in a directory that does not exist.
+ it('refuses honestly when the base checkout´s directory is gone (no phantom conflict)', async () => {
+ const list = 'worktree /base\nHEAD aaa\nbranch refs/heads/main\n'
+ const { git, calls } = fakeGit({ 'worktree list --porcelain': ok(list) })
+ const r = await worktreeMerge(git, '/repo', 'feature/x', 'main', false, async (p) => p !== '/base')
+ expect(r.ok).toBe(false)
+ expect(r.message).toContain('/base')
+ expect(r.message).toMatch(/missing/i)
+ expect(r.message).not.toMatch(/conflict/i)
+ // Nothing was attempted in the dead directory, and the ref was not advanced behind git's back.
+ expect(calls.some((c) => c[0] === 'merge' || c[0] === 'fetch')).toBe(false)
+ })
+
+ it('still merges in place when the base checkout is really there (pathExists says so)', async () => {
+ const list = 'worktree /base\nHEAD aaa\nbranch refs/heads/main\n'
+ const { git, calls } = fakeGit({
+ 'worktree list --porcelain': ok(list),
+ 'status --porcelain': ok('')
+ })
+ const r = await worktreeMerge(git, '/repo', 'feature/x', 'main', false, async () => true)
+ expect(r.ok).toBe(true)
+ expect(calls.some((c) => c.join(' ') === 'merge --no-ff --no-edit feature/x')).toBe(true)
+ })
+
// The push is what the user is asked to consent to (Task 6): merging must NEVER publish to a
// shared remote unless the caller explicitly said so.
it('never pushes unless the caller asks for it', async () => {
diff --git a/src/shared/worktree-ops.ts b/src/shared/worktree-ops.ts
index a8c62dce7..c5e895e3a 100644
--- a/src/shared/worktree-ops.ts
+++ b/src/shared/worktree-ops.ts
@@ -92,11 +92,28 @@ export async function worktreeAdd(
* default is not to push, and the result message states exactly what happened.
*/
export async function worktreeMerge(
- git: GitExecutor, repoPath: string, branch: string, baseRef: string, push = false
+ git: GitExecutor, repoPath: string, branch: string, baseRef: string, push = false,
+ /** See `PathExists` / `worktreeList`. Without it, a base checkout whose directory was deleted
+ * behind git's back is listed as healthy and `decideMergeStrategy` merges INTO A DIRECTORY THAT
+ * IS NOT THERE — the merge fails and the user is told to resolve a conflict that does not exist
+ * in a directory that does not exist. */
+ pathExists: PathExists = alwaysExists
): Promise<WorktreeOpResult> {
if (!isValidGitRef(branch) || !isValidGitRef(baseRef)) return { ok: false, message: 'Invalid ref.' }
- const list = await worktreeList(git, repoPath)
+ const list = await worktreeList(git, repoPath, pathExists)
const baseEntry = list.find((e) => e.branch === baseRef) ?? null
+ // The base branch is checked out in a worktree whose directory is GONE. Do not merge in place
+ // (nothing to merge into) and do not quietly fall through to `fetch-update` either: git still has
+ // the registration and refuses to update a ref that is checked out elsewhere, so that would fail
+ // with an obscure message. Say what is actually wrong.
+ if (baseEntry?.prunable) {
+ return {
+ ok: false,
+ message:
+ `The checkout of ${baseRef} is missing (${baseEntry.path}). ` +
+ `Restore that directory or run \`git worktree prune\`, then try again.`
+ }
+ }
let baseDirty = false
if (baseEntry) {
const st = await git(baseEntry.path, ['status', '--porcelain'])