detectRepo() — mcp Function Reference
Architecture documentation for the detectRepo() function in graph-cache.ts from the mcp codebase.
Entity Profile
Dependency Diagram
graph TD b0309b0d_ce6c_f9db_7d04_57a3e85e30fc["detectRepo()"] b9fca090_95d0_4cf7_0bb0_7a7efcc55ccb["resolveOrFetchGraph()"] b9fca090_95d0_4cf7_0bb0_7a7efcc55ccb -->|calls| b0309b0d_ce6c_f9db_7d04_57a3e85e30fc 966fdd0a_509a_01b0_865e_32cfde84f964["precacheForDirectory()"] 966fdd0a_509a_01b0_865e_32cfde84f964 -->|calls| b0309b0d_ce6c_f9db_7d04_57a3e85e30fc c652ed9f_2901_0635_2a5b_fc305e9cd6c1["has()"] b0309b0d_ce6c_f9db_7d04_57a3e85e30fc -->|calls| c652ed9f_2901_0635_2a5b_fc305e9cd6c1 55bde18a_7860_173e_f211_5874970475e3["get()"] b0309b0d_ce6c_f9db_7d04_57a3e85e30fc -->|calls| 55bde18a_7860_173e_f211_5874970475e3 style b0309b0d_ce6c_f9db_7d04_57a3e85e30fc 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
Source
Frequently Asked Questions
What does detectRepo() do?
detectRepo() is a function in the mcp codebase.
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