MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / getWorktreePaths

Function getWorktreePaths

source code/utils/getWorktreePaths.ts:20–72  ·  view source on GitHub ↗
(cwd: string)

Source from the content-addressed store, hash-verified

18 * @returns Array of absolute worktree paths
19 */
20export async function getWorktreePaths(cwd: string): Promise<string[]> {
21 const startTime = Date.now()
22
23 const { stdout, code } = await execFileNoThrowWithCwd(
24 gitExe(),
25 ['worktree', 'list', '--porcelain'],
26 {
27 cwd,
28 preserveOutputOnError: false,
29 },
30 )
31
32 const durationMs = Date.now() - startTime
33
34 if (code !== 0) {
35 logEvent('tengu_worktree_detection', {
36 duration_ms: durationMs,
37 worktree_count: 0,
38 success: false,
39 })
40 return []
41 }
42
43 // Parse porcelain output - lines starting with "worktree " contain paths
44 // Example:
45 // worktree /Users/foo/repo
46 // HEAD abc123
47 // branch refs/heads/main
48 //
49 // worktree /Users/foo/repo-wt1
50 // HEAD def456
51 // branch refs/heads/feature
52 const worktreePaths = stdout
53 .split('\n')
54 .filter(line => line.startsWith('worktree '))
55 .map(line => line.slice('worktree '.length).normalize('NFC'))
56
57 logEvent('tengu_worktree_detection', {
58 duration_ms: durationMs,
59 worktree_count: worktreePaths.length,
60 success: true,
61 })
62
63 // Sort worktrees: current worktree first, then alphabetically
64 const currentWorktree = worktreePaths.find(
65 path => cwd === path || cwd.startsWith(path + sep),
66 )
67 const otherWorktrees = worktreePaths
68 .filter(path => path !== currentWorktree)
69 .sort((a, b) => a.localeCompare(b))
70
71 return currentWorktree ? [currentWorktree, ...otherWorktrees] : otherWorktrees
72}

Callers 5

runFunction · 0.85
LogSelectorFunction · 0.85
initFunction · 0.85
callFunction · 0.85

Calls 2

execFileNoThrowWithCwdFunction · 0.85
logEventFunction · 0.85

Tested by

no test coverage detected