( startPath: string, indexRoot: string, )
| 86 | * and monorepo-subdir layouts from producing false warnings. |
| 87 | */ |
| 88 | export function detectWorktreeIndexMismatch( |
| 89 | startPath: string, |
| 90 | indexRoot: string, |
| 91 | ): WorktreeIndexMismatch | null { |
| 92 | const worktreeRoot = gitWorktreeRoot(startPath); |
| 93 | if (!worktreeRoot) return null; |
| 94 | |
| 95 | const resolvedIndexRoot = realpath(indexRoot); |
| 96 | if (worktreeRoot === resolvedIndexRoot) return null; |
| 97 | |
| 98 | // Only flag it when the index root is itself a real working-tree root. This |
| 99 | // distinguishes "borrowed another worktree's index" from "index sits in a |
| 100 | // plain ancestor directory", and avoids warning outside git entirely. |
| 101 | if (gitWorktreeRoot(resolvedIndexRoot) !== resolvedIndexRoot) return null; |
| 102 | |
| 103 | // Don't flag a nested repo (submodule / embedded clone) that `indexRoot`'s |
| 104 | // index ALREADY covers: indexing a super-repo descends into its submodules |
| 105 | // and gitlinked clones, so a query run from inside one resolves up to the |
| 106 | // parent index — whose graph *does* contain that nested repo's files. The |
| 107 | // warning's premise ("results are a different branch; symbols changed only |
| 108 | // here are missing") is false there, and its "run codegraph init -i" advice |
| 109 | // would needlessly fragment the unified workspace index. A genuine borrowed |
| 110 | // worktree and the index root are the SAME repository (they share a git |
| 111 | // common dir); a submodule/embedded clone is a DIFFERENT repository and does |
| 112 | // not — so suppress only when the two clearly differ. (#1031, #1033) |
| 113 | const worktreeCommon = gitCommonDir(worktreeRoot); |
| 114 | const indexCommon = gitCommonDir(resolvedIndexRoot); |
| 115 | if (worktreeCommon && indexCommon && worktreeCommon !== indexCommon) return null; |
| 116 | |
| 117 | return { worktreeRoot, indexRoot: resolvedIndexRoot }; |
| 118 | } |
| 119 | |
| 120 | /** One-line-per-fact warning describing a detected mismatch. */ |
| 121 | export function worktreeMismatchWarning(m: WorktreeIndexMismatch): string { |
no test coverage detected