MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / detectWorktreeIndexMismatch

Function detectWorktreeIndexMismatch

src/sync/worktree.ts:93–123  ·  view source on GitHub ↗
(
  startPath: string,
  indexRoot: string,
)

Source from the content-addressed store, hash-verified

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

Callers 3

worktreeMismatchForMethod · 0.90
mainFunction · 0.90

Calls 3

gitWorktreeRootFunction · 0.85
realpathFunction · 0.85
gitCommonDirFunction · 0.85

Tested by

no test coverage detected