From c0bc3affb2a71233435ae5bf3b92e66df818638f Mon Sep 17 00:00:00 2001 From: Haileyesus Date: Thu, 12 Feb 2026 23:25:06 +0300 Subject: [PATCH] refactor(validateGitRepository): improve directory validation for git work tree --- server/routes/git.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/server/routes/git.js b/server/routes/git.js index 0df4e44..5f6d803 100755 --- a/server/routes/git.js +++ b/server/routes/git.js @@ -60,19 +60,16 @@ async function validateGitRepository(projectPath) { } try { - // Use --show-toplevel to get the root of the git repository - const { stdout: gitRoot } = await execAsync('git rev-parse --show-toplevel', { cwd: projectPath }); - const normalizedGitRoot = path.resolve(gitRoot.trim()); - const normalizedProjectPath = path.resolve(projectPath); - - // Ensure the git root matches our project path (prevent using parent git repos) - if (normalizedGitRoot !== normalizedProjectPath) { - 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 (error) { - if (error.message.includes('Project directory is not a git repository')) { - throw error; + // Allow any directory that is inside a work tree (repo root or nested folder). + const { stdout: insideWorkTreeOutput } = await execAsync('git rev-parse --is-inside-work-tree', { cwd: projectPath }); + const isInsideWorkTree = insideWorkTreeOutput.trim() === 'true'; + if (!isInsideWorkTree) { + throw new Error('Not inside a git work tree'); } + + // Ensure git can resolve the repository root for this directory. + await execAsync('git rev-parse --show-toplevel', { cwd: projectPath }); + } catch { 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.'); } } @@ -1125,4 +1122,4 @@ router.post('/delete-untracked', async (req, res) => { } }); -export default router; \ No newline at end of file +export default router;