Skip to content

Commit 9442019

Browse files
Filter out stale worktree paths from branch listing
Worktrees in /tmp or other ephemeral locations get cleaned by the OS but remain in git's worktree list as "prunable". These stale entries caused branches to show a worktree label pointing at a non-existent directory, leading to broken state when selected. Now validates each worktree path with existsSync before including it in the branch→worktreePath map. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent cf66f53 commit 9442019

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

‎apps/server/src/git.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { spawn } from "node:child_process";
2+
import fs from "node:fs";
23
import os from "node:os";
34
import path from "node:path";
45

@@ -131,12 +132,14 @@ export async function listGitBranches(input: GitListBranchesInput): Promise<GitL
131132
defaultRef.code === 0 ? defaultRef.stdout.trim().replace(/^refs\/remotes\/origin\//, "") : null;
132133

133134
// Build branch-name → worktree-path map from porcelain output.
135+
// Only include worktrees whose directories still exist on disk (skip prunable/stale ones).
134136
const worktreeMap = new Map<string, string>();
135137
if (worktreeList.code === 0) {
136138
let currentPath: string | null = null;
137139
for (const line of worktreeList.stdout.split("\n")) {
138140
if (line.startsWith("worktree ")) {
139141
currentPath = line.slice("worktree ".length);
142+
if (!fs.existsSync(currentPath)) currentPath = null;
140143
} else if (line.startsWith("branch refs/heads/") && currentPath) {
141144
worktreeMap.set(line.slice("branch refs/heads/".length), currentPath);
142145
} else if (line === "") {

0 commit comments

Comments
 (0)