diff --git a/plugin/core/git.js b/plugin/core/git.js index 7918bed..8301e8e 100644 --- a/plugin/core/git.js +++ b/plugin/core/git.js @@ -230,9 +230,9 @@ export async function isWorktree(dir) { return false } - // Verify it contains a gitdir reference + // Verify it contains a gitdir reference pointing to a worktree administrative directory const content = await readFile(gitPath, 'utf-8') - return content.startsWith('gitdir:') + return content.startsWith('gitdir:') && /[/\\]worktrees[/\\][^/\\]+\s*$/.test(content) } catch { return false } diff --git a/test/unit/git-worktree.test.js b/test/unit/git-worktree.test.js index 3501a5c..be1c6e5 100644 --- a/test/unit/git-worktree.test.js +++ b/test/unit/git-worktree.test.js @@ -63,6 +63,25 @@ describe('isWorktree', () => { const result = await isWorktree('/nonexistent/path') assert.strictEqual(result, false) }) + + test('returns false for a submodule directory', async () => { + const subRepo = join(testDir, 'sub-repo') + const submodulePath = join(mainRepo, 'sub') + + mkdirSync(subRepo, { recursive: true }) + execSync('git init -b main', { cwd: subRepo }) + writeFileSync(join(subRepo, 'sub.txt'), 'sub') + execSync('git add .', { cwd: subRepo }) + execSync('git commit -m "sub commit"', { cwd: subRepo }) + + // Add submodule to main repo + execSync('git config --global protocol.file.allow always', { cwd: mainRepo }) + execSync(`git submodule add ${subRepo} sub`, { cwd: mainRepo }) + execSync('git commit -m "add submodule"', { cwd: mainRepo }) + + const result = await isWorktree(submodulePath) + assert.strictEqual(result, false) + }) }) describe('getWorktreeMainRepo', () => {