perf: avoid Git probes for ordinary checkout roots

This commit is contained in:
Gergo Magyar 2026-11-07 06:51:54 +00:00
parent 07dfbc979e
commit 50ffdd7be7
2 changed files with 55 additions and 1 deletions

View file

@ -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

View file

@ -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', () => {