refactor(validateGitRepository): improve directory validation for git work tree

This commit is contained in:
Haileyesus
2026-02-12 23:25:06 +03:00
parent 9fece98612
commit c0bc3affb2

View File

@@ -60,19 +60,16 @@ async function validateGitRepository(projectPath) {
} }
try { try {
// Use --show-toplevel to get the root of the git repository // Allow any directory that is inside a work tree (repo root or nested folder).
const { stdout: gitRoot } = await execAsync('git rev-parse --show-toplevel', { cwd: projectPath }); const { stdout: insideWorkTreeOutput } = await execAsync('git rev-parse --is-inside-work-tree', { cwd: projectPath });
const normalizedGitRoot = path.resolve(gitRoot.trim()); const isInsideWorkTree = insideWorkTreeOutput.trim() === 'true';
const normalizedProjectPath = path.resolve(projectPath); if (!isInsideWorkTree) {
throw new Error('Not inside a git work tree');
}
// Ensure the git root matches our project path (prevent using parent git repos) // Ensure git can resolve the repository root for this directory.
if (normalizedGitRoot !== normalizedProjectPath) { await execAsync('git rev-parse --show-toplevel', { cwd: projectPath });
throw new Error(`Project directory is not a git repository. This directory is inside a git repository at ${normalizedGitRoot}, but git operations should be run from the repository root.`); } catch {
}
} catch (error) {
if (error.message.includes('Project directory is not a git repository')) {
throw error;
}
throw new Error('Not a git repository. This directory does not contain a .git folder. Initialize a git repository with "git init" to use source control features.'); throw new Error('Not a git repository. This directory does not contain a .git folder. Initialize a git repository with "git init" to use source control features.');
} }
} }