diff --git a/src/core/git/index.ts b/src/core/git/index.ts index 38f398a..ad58baa 100644 --- a/src/core/git/index.ts +++ b/src/core/git/index.ts @@ -52,17 +52,26 @@ export function getRepositoryName(directory: string): string | null { function extractRepoNameFromConfig(content: string): string | null { const lines = content.split('\n'); + let inRemoteSection = false; for (const line of lines) { const trimmed = line.trim(); - // Look for URL lines - const urlMatch = trimmed.match(/url\s*=\s*(.+)/); - if (urlMatch) { - const url = urlMatch[1].trim(); - const repoName = extractRepoNameFromURL(url); - if (repoName) { - return repoName; + // Track section headers — only extract URLs from [remote "..."] sections, + // not [submodule "..."] sections whose URLs point to unrelated repos. + if (trimmed.startsWith('[')) { + inRemoteSection = /^\[remote\s+"/.test(trimmed); + continue; + } + + if (inRemoteSection) { + const urlMatch = trimmed.match(/url\s*=\s*(.+)/); + if (urlMatch) { + const url = urlMatch[1].trim(); + const repoName = extractRepoNameFromURL(url); + if (repoName) { + return repoName; + } } } } diff --git a/src/core/parser/index.ts b/src/core/parser/index.ts index 1d89667..5f706f5 100644 --- a/src/core/parser/index.ts +++ b/src/core/parser/index.ts @@ -33,7 +33,10 @@ function getCachedRepositoryName(directory: string): string { return repoName; } -// Find parent repository by walking up the directory tree +// Find parent repository by walking up the directory tree. +// Only checks for actual .git/config with remotes — never uses the +// repositoryCache to avoid cross-project name pollution (e.g. a cached +// parent "/home/user" → "user" would incorrectly rename unrelated child projects). function findParentRepository(directory: string): string | null { let currentDir = directory; @@ -44,19 +47,11 @@ function findParentRepository(directory: string): string | null { break; } - if (repositoryCache.has(parentDir)) { - const repoName = repositoryCache.get(parentDir)!; - if (repoName) { - repositoryCache.set(directory, repoName); - return repoName; - } - } else { - const repoName = getRepositoryName(parentDir); - if (repoName) { - repositoryCache.set(parentDir, repoName); - repositoryCache.set(directory, repoName); - return repoName; - } + const repoName = getRepositoryName(parentDir); + if (repoName) { + repositoryCache.set(parentDir, repoName); + repositoryCache.set(directory, repoName); + return repoName; } currentDir = parentDir;