/ utils / getWorktreePathsPortable.ts
getWorktreePathsPortable.ts
 1  import { execFile as execFileCb } from 'child_process'
 2  import { promisify } from 'util'
 3  
 4  const execFileAsync = promisify(execFileCb)
 5  
 6  /**
 7   * Portable worktree detection using only child_process — no analytics,
 8   * no bootstrap deps, no execa. Used by listSessionsImpl.ts (SDK) and
 9   * anywhere that needs worktree paths without pulling in the CLI
10   * dependency chain (execa → cross-spawn → which).
11   */
12  export async function getWorktreePathsPortable(cwd: string): Promise<string[]> {
13    try {
14      const { stdout } = await execFileAsync(
15        'git',
16        ['worktree', 'list', '--porcelain'],
17        { cwd, timeout: 5000 },
18      )
19      if (!stdout) return []
20      return stdout
21        .split('\n')
22        .filter(line => line.startsWith('worktree '))
23        .map(line => line.slice('worktree '.length).normalize('NFC'))
24    } catch {
25      return []
26    }
27  }