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

Function detectWorktreeIndexMismatch

src/sync/worktree.ts:93–124  ·  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 (sameDirectory(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 const indexWorktreeRoot = gitWorktreeRoot(resolvedIndexRoot);
107 if (!indexWorktreeRoot || !sameDirectory(indexWorktreeRoot, resolvedIndexRoot)) return null;
108
109 // Don't flag a nested repo (submodule / embedded clone) that `indexRoot`'s
110 // index ALREADY covers: indexing a super-repo descends into its submodules
111 // and gitlinked clones, so a query run from inside one resolves up to the
112 // parent index — whose graph *does* contain that nested repo's files. The
113 // warning's premise ("results are a different branch; symbols changed only
114 // here are missing") is false there, and its "codegraph init" advice would
115 // needlessly fragment the unified workspace index. A genuine borrowed
116 // worktree and the index root are the SAME repository (they share a git
117 // common dir); a submodule/embedded clone is a DIFFERENT repository and does
118 // not — so suppress only when the two clearly differ. (#1031, #1033)
119 const worktreeCommon = gitCommonDir(worktreeRoot);
120 const indexCommon = gitCommonDir(resolvedIndexRoot);
121 if (worktreeCommon && indexCommon && !sameDirectory(worktreeCommon, indexCommon)) return null;
122
123 return { worktreeRoot, indexRoot: resolvedIndexRoot };
124}
125
126/** A git repository of its own nested below an index root. */
127export interface NestedRepository {

Callers 3

worktreeMismatchForMethod · 0.90
mainFunction · 0.90

Calls 4

gitWorktreeRootFunction · 0.85
sameDirectoryFunction · 0.85
gitCommonDirFunction · 0.85
realpathFunction · 0.70

Tested by

no test coverage detected