Understand any codebase instantly. System intelligence for codebases, built for humans and AI.
TypeScript aiai-memoryclaude-codeclicode-analysiscode-mappingcode-memorycodebase-understandingcodexdeveloper-toolsllmopenclawpersistent-memoryprogram-analysissoftware-architecturesystem-intelligencesystem-mapping
Detect monorepo vs multi-repo by git provenance (ix map, no flags) (#240)
Joseph Mikhailee205947819d
diff --git a/ix-cli/src/cli/__tests__/system.test.ts b/ix-cli/src/cli/__tests__/system.test.tsindex 6faf3425..ed31b4c5 100644--- a/ix-cli/src/cli/__tests__/system.test.ts+++ b/ix-cli/src/cli/__tests__/system.test.ts@@ -76,3 +76,65 @@ describe("Maven name + declared-dependency graph", () => {expect(d.repoDeps["lib"]).toBeUndefined();});});++describe("git-aware system detection (no flags, monorepo vs separate repos)", () => {+ let base: string;+ const mk = (rel: string) => fs.mkdirSync(nodePath.join(base, rel), { recursive: true });+ const wf = (rel: string, c = "{}") => { const p = nodePath.join(base, rel); fs.mkdirSync(nodePath.dirname(p), { recursive: true }); fs.writeFileSync(p, c); };++ beforeAll(() => {+ base = fs.mkdtempSync(nodePath.join(os.tmpdir(), "ix-gitdet-"));+ // A monorepo: one .git at the root, package dirs with manifests but no own .git.+ mk("mono/.git");+ wf("mono/packages/pkg-a/package.json", '{"name":"pkg-a"}');+ wf("mono/packages/pkg-b/package.json", '{"name":"pkg-b"}');+ // A repo with submodule-style git children directly under it.+ mk("withsubs/.git");+ wf("withsubs/package.json", '{"name":"withsubs"}');+ mk("withsubs/sub-a/.git"); wf("withsubs/sub-a/package.json", '{"name":"sub-a"}');+ mk("withsubs/sub-b/.git"); wf("withsubs/sub-b/package.json", '{"name":"sub-b"}');+ // A plain folder (no .git) collecting two independently-"cloned" git repos.+ mk("collection/repo-a/.git"); wf("collection/repo-a/package.json", '{"name":"repo-a"}');+ mk("collection/repo-b/.git"); wf("collection/repo-b/package.json", '{"name":"repo-b"}');+ // A monorepo with NO .git (downloaded tarball) but a workspaces declaration.+ wf("ws_npm/package.json", '{"name":"root","workspaces":["packages/*"]}');+ wf("ws_npm/packages/a/package.json", '{"name":"a"}');+ wf("ws_npm/packages/b/package.json", '{"name":"b"}');+ // A no-.git monorepo declared via pnpm-workspace.yaml.+ wf("ws_pnpm/pnpm-workspace.yaml", 'packages: ["*"]');+ wf("ws_pnpm/pkg-a/package.json", '{"name":"a"}');+ wf("ws_pnpm/pkg-b/package.json", '{"name":"b"}');+ // A no-.git, no-workspace folder of separate projects (genuinely ambiguous).+ wf("plain/proj-a/package.json", '{"name":"proj-a"}');+ wf("plain/proj-b/package.json", '{"name":"proj-b"}');+ });+ afterAll(() => { try { fs.rmSync(base, { recursive: true, force: true }); } catch { /* ignore */ } });++ it("treats a monorepo (one .git, package dirs) as a SINGLE repo", () => {+ expect(detectSystem(nodePath.join(base, "mono"))).toBeUndefined();+ expect(detectSystem(nodePath.join(base, "mono", "packages"))).toBeUndefined();+ });++ it("treats a repo WITH submodule git children as a single repo (not a system of submodules)", () => {+ expect(detectSystem(nodePath.join(base, "withsubs"))).toBeUndefined();+ });++ it("treats a plain folder of independently-cloned repos as a multi-repo SYSTEM", () => {+ const d = detectSystem(nodePath.join(base, "collection"));+ expect(d).toBeTruthy();+ expect(d!.members.sort()).toEqual(["repo-a", "repo-b"]);+ });++ it("treats a no-.git monorepo (workspaces / pnpm-workspace) as a SINGLE repo", () => {+ // A monorepo extracted without its .git must NOT be split into separate repos.+ expect(detectSystem(nodePath.join(base, "ws_npm"))).toBeUndefined();+ expect(detectSystem(nodePath.join(base, "ws_npm", "packages"))).toBeUndefined();+ expect(detectSystem(nodePath.join(base, "ws_pnpm"))).toBeUndefined();+ });++ it("still treats a no-.git, no-workspace folder of projects as a SYSTEM", () => {+ const d = detectSystem(nodePath.join(base, "plain"));+ expect(d).toBeTruthy();+ expect(d!.members.sort()).toEqual(["proj-a", "proj-b"]);+ });+});diff --git a/ix-cli/src/cli/system.ts b/ix-cli/src/cli/system.tsindex 0682f5d1..f4a45c06 100644--- a/ix-cli/src/cli/system.ts+++ b/ix-cli/src/cli/system.ts@@ -38,6 +38,56 @@ function isRepoRoot(dir: string): boolean {}}+/** A directory is its own git repository iff it contains a `.git` entry (a dir+ * for a normal clone, or a file for a submodule/worktree). This is the+ * ground-truth signal that a child is a DISTINCT repository — a monorepo's+ * package dirs never have their own .git (they share the root's). */+function hasOwnGit(dir: string): boolean {+ try { return fs.existsSync(nodePath.join(dir, ".git")); } catch { return false; }+}++/**+ * True if `dir` declares itself the root of a single multi-package project (a+ * monorepo / workspace), independent of git. Covers the case where a monorepo is+ * mapped without its .git (a downloaded tarball/zip): the workspace config is an+ * explicit "these subdirs are MY packages, not separate repos" declaration.+ */+function hasWorkspaceMarker(dir: string): boolean {+ const has = (f: string) => { try { return fs.existsSync(nodePath.join(dir, f)); } catch { return false; } };+ const reads = (f: string): string | null => { try { return fs.readFileSync(nodePath.join(dir, f), "utf8"); } catch { return null; } };+ // Dedicated workspace/monorepo config files.+ if (has("pnpm-workspace.yaml") || has("lerna.json") || has("nx.json") ||+ has("turbo.json") || has("rush.json") || has("go.work") ||+ has("WORKSPACE") || has("WORKSPACE.bazel") || has("MODULE.bazel")) return true;+ // package.json with a `workspaces` field (npm / yarn / bun workspaces).+ const pkg = reads("package.json");+ if (pkg) { try { if ("workspaces" in (JSON.parse(pkg) as Record<string, unknown>)) return true; } catch { /* ignore */ } }+ // Cargo workspace.+ const cargo = reads("Cargo.toml");+ if (cargo && /^\s*\[workspace\]/m.test(cargo)) return true;+ // Maven multi-module / Gradle multi-project.+ const pom = reads("pom.xml");+ if (pom && /<modules>/.test(pom)) return true;+ const settings = reads("settings.gradle") ?? reads("settings.gradle.kts");+ if (settings && /\binclude\b/.test(settings)) return true;+ return false;+}++/** True if `dir` or any ancestor is a single repository: a git repo root (.git)+ * OR a declared monorepo/workspace root. Used to tell a monorepo's package dir+ * (part of ONE repo) from a plain folder that collects independently-cloned+ * repos. */+function isInsideSingleRepo(dir: string): boolean {+ let cur = nodePath.resolve(dir);+ // Walk up to the filesystem root, checking each level.+ while (true) {+ if (hasOwnGit(cur) || hasWorkspaceMarker(cur)) return true;+ const parent = nodePath.dirname(cur);+ if (parent === cur) return false;+ cur = parent;+ }+}+/*** Published package name(s) for a member repo, read (pragmatically) from its* build manifest. These are matched against other members' imports to derive the@@ -264,8 +314,29 @@ function buildRepoDeps(rootPath: string, members: string[], registry: Record<strreturn graph;}-/** Returns the detected system, or undefined when `rootPath` is a single repo. */+/**+ * Returns the detected multi-repo system, or undefined when `rootPath` is a+ * single repository (including a monorepo). The discriminator is GIT PROVENANCE,+ * not directory layout, so `ix map <path>` needs no flags:+ *+ * - >= 2 immediate children are each their OWN git repo -> a real multi-repo+ * system (separately-cloned repos, possibly sharing a lib). Co-ingest them.+ * - `rootPath` is inside one git repo (a monorepo / single repo) with no+ * nested git-repo children -> ONE repository. Its package dirs are NOT+ * separate repos, so they are never split apart (this is what stops a+ * monorepo like babel from being shredded into 147 "systems").+ * - `rootPath` is not in any git repo -> fall back to manifest-based members+ * (a non-git folder that collects several projects).+ */export function detectSystem(rootPath: string): DetectedSystem | undefined {+ // If the mapped path is within a single repository — a git repo (its own .git+ // or an ancestor's) OR a declared monorepo/workspace root (workspaces,+ // pnpm/lerna/nx/turbo, Cargo [workspace], go.work, Maven modules, ...) — it IS+ // one repo. A monorepo's package dirs, or a repo with submodules, are never+ // split into separate "repos" (the workspace check covers a monorepo mapped+ // without its .git). Only a plain folder that collects independently-cloned+ // repos becomes a multi-repo system.+ if (isInsideSingleRepo(rootPath)) return undefined;let children: fs.Dirent[];try {children = fs.readdirSync(rootPath, { withFileTypes: true });