diff --git a/gitnexus/src/storage/git.ts b/gitnexus/src/storage/git.ts index 72f59f618..c3c9317bd 100644 --- a/gitnexus/src/storage/git.ts +++ b/gitnexus/src/storage/git.ts @@ -1,5 +1,5 @@ import { execFileSync, execSync, spawnSync } from 'child_process'; -import { statSync, existsSync } from 'fs'; +import { statSync, existsSync, lstatSync, realpathSync } from 'fs'; import path from 'path'; import os from 'os'; import { logger } from '../core/logger.js'; @@ -585,6 +585,21 @@ export const getCoreExcludesFilePath = (fromPath: string): string => { */ export const resolveRepoIdentityRoot = (fromPath: string): string => { const resolved = path.resolve(fromPath); + // An ordinary checkout is its own identity root. Let Git interpret worktrees, + // symlinks, common-directory indirection and environment overrides as before. + try { + const gitDir = path.join(resolved, '.git'); + if ( + process.env.GIT_DIR === undefined && + process.env.GIT_COMMON_DIR === undefined && + lstatSync(gitDir).isDirectory() && + !existsSync(path.join(gitDir, 'commondir')) && + realpathSync.native(resolved) === resolved + ) + return resolved; + } catch { + // Missing or unreadable paths retain the existing Git/fallback behavior. + } const canonical = getCanonicalRepoRoot(resolved); if (!canonical) return resolved; // non-git → use as-is if (canonical === resolved) return canonical; // canonical checkout diff --git a/gitnexus/test/unit/git-utils.test.ts b/gitnexus/test/unit/git-utils.test.ts index b1e00d727..4ea785b4d 100644 --- a/gitnexus/test/unit/git-utils.test.ts +++ b/gitnexus/test/unit/git-utils.test.ts @@ -416,6 +416,45 @@ describe('getCanonicalRepoRoot', () => { }); }); +describe('resolveRepoIdentityRoot', () => { + it.each(['ordinary', 'GIT_DIR', 'GIT_COMMON_DIR', 'commondir', 'symlink'] as const)( + 'preserves Git identity for %s checkouts', + async (layout) => { + const { getCanonicalRepoRoot, resolveRepoIdentityRoot } = + await import('../../src/storage/git.js'); + const root = fs.realpathSync.native(makeIsolatedTempDir('gitnexus-identity-')); + let repo = path.join(root, 'repo'); + const other = path.join(root, 'other'); + vi.stubEnv('GIT_DIR', undefined); + vi.stubEnv('GIT_COMMON_DIR', undefined); + try { + for (const directory of [repo, other]) { + fs.mkdirSync(directory); + execFileSync(gitExecutable, ['init', '-q'], { cwd: directory, stdio: 'pipe' }); + } + if (layout === 'GIT_DIR' || layout === 'GIT_COMMON_DIR') + vi.stubEnv(layout, path.join(other, '.git')); + else if (layout === 'commondir') + fs.writeFileSync(path.join(repo, '.git', 'commondir'), path.join(other, '.git') + '\n'); + else if (layout === 'symlink') { + const link = path.join(root, 'linked-name'); + fs.symlinkSync(repo, link, 'junction'); + repo = link; + } + const canonical = getCanonicalRepoRoot(repo); + expect(canonical).not.toBeNull(); + expect(resolveRepoIdentityRoot(repo)).toBe(canonical); + const subdir = path.join(repo, 'subdir'); + fs.mkdirSync(subdir); + expect(resolveRepoIdentityRoot(subdir)).toBe(path.resolve(subdir)); + } finally { + vi.unstubAllEnvs(); + fs.rmSync(root, { recursive: true, force: true }); + } + }, + ); +}); + // ─── selfCommitContextFiles (#2639) ──────────────────────────────────────── describe('selfCommitContextFiles', () => {