multi-repo-workspace.test.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /**
  2. * Multi-repo workspaces (#514) — and the `.gitignore`-respect default (#970, #976).
  3. *
  4. * A directory holding several independent git repositories can be indexed as a
  5. * whole, but ONLY when the project opts the gitignored directories in. The
  6. * default is the universal one: `.gitignore` excludes. Walking into a gitignored
  7. * directory to index embedded repos there is OPT-IN via `codegraph.json`
  8. * `includeIgnored` (#622, #699) — without it a gitignored `node_modules`-style
  9. * reference/data dir full of nested clones is left untouched, instead of blowing
  10. * the graph up or stalling the scan (#970, #976).
  11. *
  12. * Two enumeration paths are exercised under opt-in:
  13. * - git path: the workspace root is itself a git repo (a "super-repo") whose
  14. * `.gitignore` hides the child repos. They are discovered via the ignored-
  15. * directories listing and enumerated by their own `git ls-files`. (#193
  16. * covered the *untracked* embedded case, which stays on by default.)
  17. * - sync path: `git status` in the parent says nothing about embedded repos;
  18. * change detection recurses into the opted-in ones.
  19. *
  20. * The non-git-parent case (plain folder of repos) works via the filesystem walk
  21. * regardless — locked in here so it stays that way.
  22. */
  23. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  24. import * as fs from 'fs';
  25. import * as path from 'path';
  26. import * as os from 'os';
  27. import { execFileSync } from 'child_process';
  28. import CodeGraph from '../src/index';
  29. import { scanDirectory, buildScopeIgnore, discoverEmbeddedRepoRoots, findUnindexedIgnoredRepos } from '../src/extraction';
  30. import { clearProjectConfigCache } from '../src/project-config';
  31. function git(cwd: string, ...args: string[]): void {
  32. execFileSync('git', args, { cwd, stdio: ['ignore', 'ignore', 'ignore'] });
  33. }
  34. /** git init + commit everything currently in `dir` as one repo. */
  35. function makeRepo(dir: string): void {
  36. git(dir, 'init', '-q');
  37. git(dir, 'add', '-A');
  38. git(dir, '-c', 'user.email=t@t', '-c', 'user.name=t', 'commit', '-qm', 'init', '--allow-empty');
  39. }
  40. function write(file: string, content: string): void {
  41. fs.mkdirSync(path.dirname(file), { recursive: true });
  42. fs.writeFileSync(file, content);
  43. }
  44. describe('multi-repo workspaces (#514) + .gitignore-respect default (#970, #976)', () => {
  45. let ws: string;
  46. beforeEach(() => {
  47. ws = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-multirepo-'));
  48. clearProjectConfigCache();
  49. });
  50. afterEach(() => {
  51. clearProjectConfigCache();
  52. fs.rmSync(ws, { recursive: true, force: true });
  53. });
  54. /** Drop a `codegraph.json` at the workspace root. */
  55. const writeConfig = (obj: unknown) =>
  56. fs.writeFileSync(path.join(ws, 'codegraph.json'),
  57. typeof obj === 'string' ? obj : JSON.stringify(obj));
  58. describe('default: .gitignore is respected (#970, #976)', () => {
  59. it('does NOT index embedded repos inside a gitignored dir without opt-in', () => {
  60. // The exact #976 layout: nested clones under a directory the user
  61. // explicitly gitignored. They must stay out of the index — no graph blowup.
  62. write(path.join(ws, '.repos/lib-a/src/a.ts'), 'export function fromLibA() { return 1; }\n');
  63. write(path.join(ws, '.repos/lib-b/src/b.ts'), 'export function fromLibB() { return 2; }\n');
  64. makeRepo(path.join(ws, '.repos/lib-a'));
  65. makeRepo(path.join(ws, '.repos/lib-b'));
  66. write(path.join(ws, '.gitignore'), '/.repos/\n');
  67. write(path.join(ws, 'app.ts'), 'export function app() { return 0; }\n');
  68. makeRepo(ws);
  69. const files = scanDirectory(ws);
  70. expect(files).toContain('app.ts'); // the project's own code still indexes
  71. expect(files.some((f) => f.startsWith('.repos/'))).toBe(false);
  72. });
  73. it('does NOT discover gitignored embedded roots without opt-in', () => {
  74. write(path.join(ws, 'resource/ref/src/x.ts'), 'export const x = 1;\n');
  75. makeRepo(path.join(ws, 'resource/ref'));
  76. write(path.join(ws, '.gitignore'), '/resource/\n');
  77. makeRepo(ws);
  78. // The #970 perf fix: a gitignored dir of reference repos is never walked.
  79. expect(discoverEmbeddedRepoRoots(ws)).toEqual([]);
  80. });
  81. it('ScopeIgnore: a gitignored dir is fully pruned without opt-in', () => {
  82. write(path.join(ws, 'resource/ref/src/x.ts'), 'export const x = 1;\n');
  83. makeRepo(path.join(ws, 'resource/ref'));
  84. write(path.join(ws, '.gitignore'), '/resource/\n');
  85. makeRepo(ws);
  86. const scope = buildScopeIgnore(ws);
  87. // Both the dir and its contents are ignored — the watcher won't descend.
  88. expect(scope.ignores('resource/')).toBe(true);
  89. expect(scope.ignores('resource/ref/src/x.ts')).toBe(true);
  90. });
  91. });
  92. describe('opt-in: codegraph.json includeIgnored re-includes a gitignored dir (#622, #699)', () => {
  93. it('indexes embedded repos hidden by the super-repo .gitignore', () => {
  94. write(path.join(ws, 'packages/proj-a/src/auth.ts'), 'export function login() { return 1; }\n');
  95. write(path.join(ws, 'packages/proj-b/src/billing.ts'), 'export function charge() { return 2; }\n');
  96. makeRepo(path.join(ws, 'packages/proj-a'));
  97. makeRepo(path.join(ws, 'packages/proj-b'));
  98. write(path.join(ws, '.gitignore'), '/packages/\n');
  99. write(path.join(ws, 'tools.ts'), 'export function tool() { return 0; }\n');
  100. writeConfig({ includeIgnored: ['packages/'] });
  101. makeRepo(ws);
  102. const files = scanDirectory(ws);
  103. expect(files).toContain('packages/proj-a/src/auth.ts');
  104. expect(files).toContain('packages/proj-b/src/billing.ts');
  105. expect(files).toContain('tools.ts'); // the parent's own tracked code still indexes
  106. });
  107. it('child-pattern spelling revives repos whose PARENT dir carries the gitignore rule (#1295)', () => {
  108. // `.gitignore: /repos/` lists `repos/` as ONE ignored entry, while the
  109. // CLI hint suggests `includeIgnored: ["repos/a/", "repos/b/"]` — the
  110. // child spelling. That never matched the parent path, so the documented
  111. // opt-in silently indexed nothing.
  112. write(path.join(ws, 'repos/a/a.ts'), 'export const a = 1;\n');
  113. write(path.join(ws, 'repos/b/b.ts'), 'export const b = 2;\n');
  114. makeRepo(path.join(ws, 'repos/a'));
  115. makeRepo(path.join(ws, 'repos/b'));
  116. write(path.join(ws, '.gitignore'), '/repos/\n');
  117. writeConfig({ includeIgnored: ['repos/a/', 'repos/b/'] });
  118. makeRepo(ws);
  119. const files = scanDirectory(ws);
  120. expect(files).toContain('repos/a/a.ts');
  121. expect(files).toContain('repos/b/b.ts');
  122. // Discovery (the watcher path) agrees with the scanner.
  123. expect(discoverEmbeddedRepoRoots(ws).sort()).toEqual(['repos/a/', 'repos/b/']);
  124. // And the CLI hint has nothing left to nag about.
  125. expect(findUnindexedIgnoredRepos(ws)).toEqual([]);
  126. });
  127. it('child-pattern spelling opts in ONLY the named repo; siblings stay out and stay hinted (#1295)', () => {
  128. write(path.join(ws, 'repos/a/a.ts'), 'export const a = 1;\n');
  129. write(path.join(ws, 'repos/b/b.ts'), 'export const b = 2;\n');
  130. makeRepo(path.join(ws, 'repos/a'));
  131. makeRepo(path.join(ws, 'repos/b'));
  132. write(path.join(ws, '.gitignore'), '/repos/\n');
  133. writeConfig({ includeIgnored: ['repos/a/'] });
  134. makeRepo(ws);
  135. const files = scanDirectory(ws);
  136. expect(files).toContain('repos/a/a.ts');
  137. expect(files.some((f) => f.startsWith('repos/b/'))).toBe(false);
  138. expect(discoverEmbeddedRepoRoots(ws)).toEqual(['repos/a/']);
  139. // The unopted sibling is still worth hinting about.
  140. expect(findUnindexedIgnoredRepos(ws)).toEqual(['repos/b/']);
  141. });
  142. it('only re-includes the opted-in dir, not every gitignored dir', () => {
  143. // `packages/` is opted in; `scratch/` (also holding a repo) is NOT.
  144. write(path.join(ws, 'packages/proj-a/src/auth.ts'), 'export function login() {}\n');
  145. makeRepo(path.join(ws, 'packages/proj-a'));
  146. write(path.join(ws, 'scratch/throwaway/src/junk.ts'), 'export function junk() {}\n');
  147. makeRepo(path.join(ws, 'scratch/throwaway'));
  148. write(path.join(ws, '.gitignore'), '/packages/\n/scratch/\n');
  149. writeConfig({ includeIgnored: ['packages/'] });
  150. makeRepo(ws);
  151. const files = scanDirectory(ws);
  152. expect(files).toContain('packages/proj-a/src/auth.ts');
  153. expect(files.some((f) => f.startsWith('scratch/'))).toBe(false);
  154. });
  155. it('discovers the opted-in ignored root alongside untracked roots', () => {
  156. write(path.join(ws, 'packages/proj-a/src/auth.ts'), 'export function login() {}\n');
  157. makeRepo(path.join(ws, 'packages/proj-a'));
  158. write(path.join(ws, 'vendor-src/lib/util.ts'), 'export function util() {}\n');
  159. makeRepo(path.join(ws, 'vendor-src/lib'));
  160. write(path.join(ws, '.gitignore'), '/packages/\n'); // vendor-src stays untracked
  161. writeConfig({ includeIgnored: ['packages/'] });
  162. makeRepo(ws);
  163. git(ws, 'rm', '-r', '--cached', '-q', 'vendor-src');
  164. git(ws, '-c', 'user.email=t@t', '-c', 'user.name=t', 'commit', '-qm', 'untrack');
  165. const roots = discoverEmbeddedRepoRoots(ws);
  166. expect(roots).toContain('packages/proj-a/'); // opted-in ignored kind
  167. expect(roots).toContain('vendor-src/lib/'); // untracked kind (always on)
  168. });
  169. it('ScopeIgnore: opted-in embedded files use the child rules; the watcher can descend', () => {
  170. write(path.join(ws, 'packages/proj-a/src/auth.ts'), 'export function login() {}\n');
  171. write(path.join(ws, 'packages/proj-a/.gitignore'), 'build/\n');
  172. makeRepo(path.join(ws, 'packages/proj-a'));
  173. write(path.join(ws, '.gitignore'), '/packages/\n');
  174. writeConfig({ includeIgnored: ['packages/'] });
  175. makeRepo(ws);
  176. const scope = buildScopeIgnore(ws);
  177. // Inside the opted-in embedded repo: the CHILD's rules decide.
  178. expect(scope.ignores('packages/proj-a/src/auth.ts')).toBe(false);
  179. expect(scope.ignores('packages/proj-a/build/out.ts')).toBe(true);
  180. // Under the ignored dir but NOT in any embedded repo: parent rules apply.
  181. expect(scope.ignores('packages/stray.ts')).toBe(true);
  182. // Directory form: ancestors of an embedded root are never pruned —
  183. // the Linux per-directory watcher must descend through `packages/`.
  184. expect(scope.ignores('packages/')).toBe(false);
  185. // Ordinary paths: unchanged semantics.
  186. expect(scope.ignores('node_modules/dep/index.ts')).toBe(true);
  187. expect(scope.ignores('src/app.ts')).toBe(false);
  188. });
  189. it('sync picks up a change inside an opted-in gitignored embedded repo', async () => {
  190. write(path.join(ws, 'packages/proj-a/src/auth.ts'), 'export function login() { return 1; }\n');
  191. makeRepo(path.join(ws, 'packages/proj-a'));
  192. write(path.join(ws, '.gitignore'), '/packages/\n');
  193. writeConfig({ includeIgnored: ['packages/'] });
  194. makeRepo(ws);
  195. const cg = CodeGraph.initSync(ws, { config: { include: ['**/*.ts'], exclude: [] } });
  196. try {
  197. await cg.indexAll();
  198. expect(cg.searchNodes('login', { limit: 5 }).length).toBeGreaterThan(0);
  199. // Change inside the embedded repo — invisible to the parent's `git status`.
  200. write(path.join(ws, 'packages/proj-a/src/auth.ts'),
  201. 'export function login() { return 1; }\nexport function logout() { return 0; }\n');
  202. await cg.sync();
  203. expect(cg.searchNodes('logout', { limit: 5 }).length).toBeGreaterThan(0);
  204. } finally {
  205. cg.destroy();
  206. }
  207. });
  208. });
  209. describe('discovery/classifier machinery (exercised under opt-in)', () => {
  210. it('keeps respecting the parent .gitignore for the parent own (non-repo) dirs', () => {
  211. write(path.join(ws, 'scratch/junk.ts'), 'export function junk() { return 9; }\n');
  212. write(path.join(ws, 'src/app.ts'), 'export function app() { return 1; }\n');
  213. write(path.join(ws, '.gitignore'), '/scratch/\n');
  214. makeRepo(ws);
  215. const files = scanDirectory(ws);
  216. expect(files).toContain('src/app.ts');
  217. // scratch/ is gitignored and contains NO embedded repo — stays excluded.
  218. expect(files.some((f) => f.startsWith('scratch/'))).toBe(false);
  219. });
  220. it('never descends into git repos inside node_modules (npm git-dependencies)', () => {
  221. // Embedded repo first (clean), node_modules dropped in afterwards —
  222. // matching reality, where node_modules is never committed.
  223. write(path.join(ws, 'packages/proj-a/src/auth.ts'), 'export function login() {}\n');
  224. makeRepo(path.join(ws, 'packages/proj-a'));
  225. write(path.join(ws, 'packages/proj-a/node_modules/inner/src/evil2.ts'), 'export function evil2() {}\n');
  226. makeRepo(path.join(ws, 'packages/proj-a/node_modules/inner')); // npm git-dep: has commits
  227. // Workspace-level git-dep too.
  228. write(path.join(ws, 'node_modules/git-dep/src/evil.ts'), 'export function evil() {}\n');
  229. makeRepo(path.join(ws, 'node_modules/git-dep'));
  230. write(path.join(ws, '.gitignore'), '/packages/\nnode_modules\n');
  231. writeConfig({ includeIgnored: ['packages/'] });
  232. makeRepo(ws);
  233. const files = scanDirectory(ws);
  234. expect(files).toContain('packages/proj-a/src/auth.ts');
  235. // node_modules is a built-in default exclude — never re-included, even though
  236. // `packages/` is opted in and node_modules is gitignored.
  237. expect(files.some((f) => f.includes('node_modules'))).toBe(false);
  238. });
  239. it('still indexes UNTRACKED embedded repos by default (#193 regression)', () => {
  240. write(path.join(ws, 'vendor-src/lib/src/util.ts'), 'export function util() {}\n');
  241. makeRepo(path.join(ws, 'vendor-src/lib'));
  242. write(path.join(ws, 'main.ts'), 'export function main() {}\n');
  243. makeRepo(ws); // vendor-src/ is untracked (not ignored) — committed ws has only main.ts + nothing else
  244. // NOTE: makeRepo committed vendor-src too via add -A… recreate untracked state:
  245. git(ws, 'rm', '-r', '--cached', '-q', 'vendor-src');
  246. git(ws, '-c', 'user.email=t@t', '-c', 'user.name=t', 'commit', '-qm', 'untrack');
  247. // No codegraph.json: the untracked path is unaffected by the opt-in gate.
  248. const files = scanDirectory(ws);
  249. expect(files).toContain('vendor-src/lib/src/util.ts');
  250. expect(files).toContain('main.ts');
  251. });
  252. it('skips nested git worktrees instead of indexing them as duplicate embedded repos (#848)', () => {
  253. // Claude Code (and others) create worktrees under a gitignored path like
  254. // `.claude/worktrees/<name>/`. A worktree's `.git` is a FILE pointing into
  255. // the host repo's own `.git/worktrees/`, so it is the SAME repo already
  256. // indexed — sweeping it in as an embedded repo multiplies the whole graph.
  257. // A genuine embedded clone (a `.git` *directory*) must still be indexed.
  258. // Both dirs are opted in so the classifier (not the gitignore gate) is what
  259. // decides: the worktree is skipped, the genuine clone is kept.
  260. write(path.join(ws, 'src/app.ts'), 'export function app() { return 1; }\n');
  261. write(path.join(ws, '.gitignore'), '.claude/\nvendored/\n');
  262. writeConfig({ includeIgnored: ['.claude/', 'vendored/'] });
  263. makeRepo(ws);
  264. // A real linked worktree under the gitignored .claude/worktrees/.
  265. git(ws, 'worktree', 'add', '-q', '.claude/worktrees/feature', '-b', 'feature');
  266. // A genuine embedded clone, also gitignored — must STAY indexed under opt-in.
  267. write(path.join(ws, 'vendored/lib.ts'), 'export function vendoredFn() { return 9; }\n');
  268. makeRepo(path.join(ws, 'vendored'));
  269. const files = scanDirectory(ws);
  270. expect(files).toContain('src/app.ts');
  271. // The worktree is a duplicate working view — never indexed (#848).
  272. expect(files.some((f) => f.includes('.claude/worktrees'))).toBe(false);
  273. // The genuine embedded clone is still indexed under opt-in (#514/#622).
  274. expect(files).toContain('vendored/lib.ts');
  275. });
  276. it('skips a submodule worktree instead of indexing it as a duplicate (#945)', () => {
  277. // A worktree OF A SUBMODULE points its `.git` into
  278. // `.git/modules/<module>/worktrees/<name>` — not the top-level repo's
  279. // `.git/worktrees/`. The detector used to miss that extra `modules/<name>`
  280. // segment, so the worktree fell through to "embedded" and every symbol it
  281. // shared with the real submodule checkout got indexed twice. The submodule's
  282. // own checkout (`.git/modules/<module>`, no `worktrees/`) is distinct code
  283. // and must stay indexed. The worktree dir is opted in so the classifier is
  284. // what skips it (not the gitignore gate).
  285. const upstream = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-945-up-'));
  286. try {
  287. // The repo that becomes the submodule's origin.
  288. write(path.join(upstream, 'lib.ts'), 'export function libFn() { return 1; }\n');
  289. makeRepo(upstream);
  290. write(path.join(ws, 'src/app.ts'), 'export function app() { return 1; }\n');
  291. write(path.join(ws, '.gitignore'), '.worktrees/\n');
  292. writeConfig({ includeIgnored: ['.worktrees/'] });
  293. git(ws, 'init', '-q');
  294. // protocol.file.allow=always: modern git refuses a local-path submodule otherwise.
  295. git(ws, '-c', 'protocol.file.allow=always', 'submodule', 'add', '-q', upstream, 'common');
  296. git(ws, '-c', 'user.email=t@t', '-c', 'user.name=t', 'commit', '-qm', 'add submodule');
  297. // A worktree of the submodule, under the gitignored .worktrees/ — its `.git`
  298. // points into `.git/modules/common/worktrees/<name>`.
  299. git(path.join(ws, 'common'), 'worktree', 'add', '-q', '../.worktrees/common-feature', '-b', 'feature');
  300. const files = scanDirectory(ws);
  301. expect(files).toContain('src/app.ts');
  302. // The real submodule checkout is distinct code — still indexed (#514).
  303. expect(files).toContain('common/lib.ts');
  304. // The submodule worktree is a duplicate working view — never indexed (#945).
  305. expect(files.some((f) => f.includes('.worktrees'))).toBe(false);
  306. } finally {
  307. fs.rmSync(upstream, { recursive: true, force: true });
  308. }
  309. });
  310. it('non-git workspace: walks children and respects each child own .gitignore', () => {
  311. write(path.join(ws, 'proj-a/src/auth.ts'), 'export function login() {}\n');
  312. write(path.join(ws, 'proj-a/build/out.ts'), 'export function generated() {}\n');
  313. write(path.join(ws, 'proj-a/.gitignore'), 'build/\n');
  314. write(path.join(ws, 'proj-b/src/billing.ts'), 'export function charge() {}\n');
  315. makeRepo(path.join(ws, 'proj-a'));
  316. makeRepo(path.join(ws, 'proj-b'));
  317. // ws itself is NOT a git repo.
  318. const files = scanDirectory(ws);
  319. expect(files).toContain('proj-a/src/auth.ts');
  320. expect(files).toContain('proj-b/src/billing.ts');
  321. expect(files.some((f) => f.includes('build/'))).toBe(false);
  322. });
  323. it('does not search beyond the embedded-repo depth cap (opted-in dir)', () => {
  324. // Repo buried 5 levels under the ignored dir — past EMBEDDED_REPO_SEARCH_DEPTH (4).
  325. const deep = path.join(ws, 'pkgs/a/b/c/d/e');
  326. write(path.join(deep, 'src/deep.ts'), 'export function deep() {}\n');
  327. makeRepo(deep);
  328. write(path.join(ws, 'main.ts'), 'export function main() {}\n');
  329. write(path.join(ws, '.gitignore'), '/pkgs/\n');
  330. writeConfig({ includeIgnored: ['pkgs/'] });
  331. makeRepo(ws);
  332. const files = scanDirectory(ws);
  333. expect(files).toContain('main.ts');
  334. expect(files.some((f) => f.includes('deep.ts'))).toBe(false);
  335. });
  336. it('buildScopeIgnore: indexed root is itself a gitignored subdir of an enclosing repo (#936)', () => {
  337. // `child/` is NOT its own repo, so `git` resolves the ENCLOSING repo from
  338. // inside it — and `git ls-files --directory`, whose cwd is then a wholly
  339. // ignored directory, emits the literal `./` ("this entire directory").
  340. // That sentinel used to reach the `ignore` matcher and throw
  341. // ("path should be a `path.relative()`d string, but got "./""), aborting
  342. // buildScopeIgnore → the MCP daemon's watcher never started and auto-sync
  343. // silently stalled until a manual `codegraph sync`.
  344. write(path.join(ws, 'child/src/a.ts'), 'export const x = 1;\n');
  345. write(path.join(ws, '.gitignore'), '/child/\n');
  346. makeRepo(ws);
  347. const child = path.join(ws, 'child');
  348. // The crux: building scope for the ignored subdir must not throw.
  349. const scope = buildScopeIgnore(child);
  350. // The subdir's own source is watchable/indexable, not ignored.
  351. expect(scope.ignores('src/a.ts')).toBe(false);
  352. // And the `./` self entry must not be mistaken for a nested embedded repo.
  353. expect(discoverEmbeddedRepoRoots(child)).toEqual([]);
  354. });
  355. });
  356. describe('findUnindexedIgnoredRepos: the skipped-child-repos hint (#1156)', () => {
  357. // The reported layout: a super-repo whose `.gitignore` excludes its child
  358. // repos, so `init` at the parent correctly indexes ~nothing. This detector
  359. // is the inverse of `discoverEmbeddedRepoRoots` — it names exactly the repos
  360. // the default scan skipped so the CLI can offer to opt them in.
  361. it('names the gitignored child repos a default index skipped', () => {
  362. write(path.join(ws, 'mtc-activity/src/a.ts'), 'export const a = 1;\n');
  363. write(path.join(ws, 'mtc-admin/src/b.ts'), 'export const b = 2;\n');
  364. makeRepo(path.join(ws, 'mtc-activity'));
  365. makeRepo(path.join(ws, 'mtc-admin'));
  366. write(path.join(ws, '.gitignore'), 'mtc-*/\n');
  367. write(path.join(ws, 'AGENTS.md'), '# docs\n');
  368. makeRepo(ws);
  369. // Nothing of the child repos indexes by default — the symptom being fixed.
  370. expect(scanDirectory(ws).some((f) => f.startsWith('mtc-'))).toBe(false);
  371. // ...but the detector names them (trailing-slashed, valid includeIgnored patterns).
  372. expect(findUnindexedIgnoredRepos(ws).sort()).toEqual(['mtc-activity/', 'mtc-admin/']);
  373. });
  374. it('excludes repos already opted in via includeIgnored (only the rest remain)', () => {
  375. write(path.join(ws, 'mtc-activity/src/a.ts'), 'export const a = 1;\n');
  376. write(path.join(ws, 'mtc-admin/src/b.ts'), 'export const b = 2;\n');
  377. makeRepo(path.join(ws, 'mtc-activity'));
  378. makeRepo(path.join(ws, 'mtc-admin'));
  379. write(path.join(ws, '.gitignore'), 'mtc-*/\n');
  380. writeConfig({ includeIgnored: ['mtc-activity/'] });
  381. makeRepo(ws);
  382. expect(findUnindexedIgnoredRepos(ws)).toEqual(['mtc-admin/']);
  383. });
  384. it('returns [] when every gitignored repo is already opted in (nothing to nag)', () => {
  385. write(path.join(ws, 'pkgs/a/src/a.ts'), 'export const a = 1;\n');
  386. makeRepo(path.join(ws, 'pkgs/a'));
  387. write(path.join(ws, '.gitignore'), '/pkgs/\n');
  388. writeConfig({ includeIgnored: ['pkgs/'] });
  389. makeRepo(ws);
  390. expect(findUnindexedIgnoredRepos(ws)).toEqual([]);
  391. });
  392. it('does NOT report nested repos that are NOT gitignored (they already index)', () => {
  393. // Scenario A: an untracked, non-ignored nested repo is indexed via the
  394. // untracked-embedded path, so there is nothing to hint about.
  395. write(path.join(ws, 'sub/src/a.ts'), 'export const a = 1;\n');
  396. makeRepo(path.join(ws, 'sub'));
  397. write(path.join(ws, 'app.ts'), 'export const app = 0;\n');
  398. makeRepo(ws); // sub/ stays untracked, not ignored
  399. expect(findUnindexedIgnoredRepos(ws)).toEqual([]);
  400. });
  401. it('skips a gitignored node_modules even when it holds a git repo', () => {
  402. write(path.join(ws, 'node_modules/dep/index.js'), 'module.exports = 1;\n');
  403. makeRepo(path.join(ws, 'node_modules/dep'));
  404. write(path.join(ws, '.gitignore'), 'node_modules/\n');
  405. makeRepo(ws);
  406. expect(findUnindexedIgnoredRepos(ws)).toEqual([]);
  407. });
  408. it('finds repos nested inside a gitignored data dir, not just top-level ones', () => {
  409. write(path.join(ws, 'refs/lib-a/x.ts'), 'export const x = 1;\n');
  410. makeRepo(path.join(ws, 'refs/lib-a'));
  411. write(path.join(ws, '.gitignore'), '/refs/\n');
  412. makeRepo(ws);
  413. expect(findUnindexedIgnoredRepos(ws)).toEqual(['refs/lib-a/']);
  414. });
  415. it('returns [] for a non-git directory', () => {
  416. write(path.join(ws, 'a.ts'), 'export const a = 1;\n'); // no git init at all
  417. expect(findUnindexedIgnoredRepos(ws)).toEqual([]);
  418. });
  419. });
  420. });