Home / Function/ detectRepo() — mcp Function Reference

detectRepo() — mcp Function Reference

Architecture documentation for the detectRepo() function in graph-cache.ts from the mcp codebase.

Entity Profile

Dependency Diagram

graph TD
  40378b99_8607_8e31_116f_694f33f24f45["detectRepo()"]
  108c9ff4_bdb8_518a_9256_9ff4cd9d39a7["graph-cache.ts"]
  40378b99_8607_8e31_116f_694f33f24f45 -->|defined in| 108c9ff4_bdb8_518a_9256_9ff4cd9d39a7
  0c2dbb97_347e_7226_4d31_fbdcb85ac22b["resolveOrFetchGraph()"]
  0c2dbb97_347e_7226_4d31_fbdcb85ac22b -->|calls| 40378b99_8607_8e31_116f_694f33f24f45
  5f051fd3_b5fd_05fe_3e0b_f20364a0b064["precacheForDirectory()"]
  5f051fd3_b5fd_05fe_3e0b_f20364a0b064 -->|calls| 40378b99_8607_8e31_116f_694f33f24f45
  5b58088c_d9af_5bc6_19ab_1fff095079b9["has()"]
  40378b99_8607_8e31_116f_694f33f24f45 -->|calls| 5b58088c_d9af_5bc6_19ab_1fff095079b9
  2ef71e67_fa6d_b33f_d005_85a400698718["get()"]
  40378b99_8607_8e31_116f_694f33f24f45 -->|calls| 2ef71e67_fa6d_b33f_d005_85a400698718
  style 40378b99_8607_8e31_116f_694f33f24f45 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/cache/graph-cache.ts lines 464–538

function detectRepo(
  directory: string,
  repoMap: Map<string, IndexedGraph>
): IndexedGraph | null {
  if (repoMap.size === 0) return null;

  // Strategy 0 (highest priority): Match by exact commit hash
  try {
    const { execSync } = require('child_process');
    const commitHash = execSync('git rev-parse --short HEAD', {
      cwd: directory, encoding: 'utf-8', timeout: 2000,
    }).trim();
    if (commitHash && repoMap.has(`commit:${commitHash}`)) {
      return repoMap.get(`commit:${commitHash}`)!;
    }
  } catch {
    // Not a git repo
  }

  // Strategy 1: Try directory basename
  const dirName = basename(directory).toLowerCase();
  if (repoMap.has(dirName)) {
    return repoMap.get(dirName)!;
  }

  // Strategy 2: Try git remote (sync, best-effort)
  try {
    const { execSync } = require('child_process');
    const remote = execSync('git remote get-url origin', {
      cwd: directory,
      encoding: 'utf-8',
      timeout: 2000,
    }).trim();

    // Extract repo name from URL: "https://github.com/django/django.git" -> "django"
    const match = remote.match(/\/([^\/]+?)(?:\.git)?$/);
    if (match) {
      const repoName = match[1].toLowerCase();
      if (repoMap.has(repoName)) {
        return repoMap.get(repoName)!;
      }
    }

    // Try org/repo format: "django/django" -> try "django"
    const orgMatch = remote.match(/\/([^\/]+)\/([^\/]+?)(?:\.git)?$/);
    if (orgMatch) {
      const orgName = orgMatch[1].toLowerCase();
      const repoName = orgMatch[2].toLowerCase();
      // Try "org__repo" format (swe-bench style)
      const sweKey = `${orgName}__${repoName}`;
      if (repoMap.has(sweKey)) {
        return repoMap.get(sweKey)!;
      }
      if (repoMap.has(orgName)) {
        return repoMap.get(orgName)!;
      }
      if (repoMap.has(repoName)) {
        return repoMap.get(repoName)!;
      }
    }
  } catch {
    // Not a git repo or git not available -- that's fine
  }

  // Strategy 3: If there's only one cached graph, use it (common in SWE-bench)
  if (repoMap.size <= 3) {
    // Small map likely has only one real repo with name variants
    const uniqueGraphs = new Set([...repoMap.values()]);
    if (uniqueGraphs.size === 1) {
      return [...uniqueGraphs][0];
    }
  }

  return null;
}

Domain

Subdomains

Calls

Frequently Asked Questions

What does detectRepo() do?
detectRepo() is a function in the mcp codebase, defined in src/cache/graph-cache.ts.
Where is detectRepo() defined?
detectRepo() is defined in src/cache/graph-cache.ts at line 464.
What does detectRepo() call?
detectRepo() calls 2 function(s): get, has.
What calls detectRepo()?
detectRepo() is called by 2 function(s): precacheForDirectory, resolveOrFetchGraph.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free