fix: Handle gitignored project directories in git file detection
When a project directory is gitignored by a parent git repository, `git ls-files` returns no results even though files exist. Added detection for this scenario using `git rev-parse` and `git check-ignore` to fall back to filesystem walking when the project directory is ignored by an ancestor repo.
This commit is contained in:
@@ -111,6 +111,30 @@ export function shouldIncludeFile(
|
|||||||
*/
|
*/
|
||||||
function getGitVisibleFiles(rootDir: string): Set<string> | null {
|
function getGitVisibleFiles(rootDir: string): Set<string> | null {
|
||||||
try {
|
try {
|
||||||
|
// Check if the project directory is gitignored by a parent repo.
|
||||||
|
// When rootDir lives inside a parent git repo that ignores it,
|
||||||
|
// `git ls-files` returns nothing — fall back to filesystem walk.
|
||||||
|
const gitRoot = execFileSync(
|
||||||
|
'git',
|
||||||
|
['rev-parse', '--show-toplevel'],
|
||||||
|
{ cwd: rootDir, encoding: 'utf-8', timeout: 5000, stdio: ['pipe', 'pipe', 'pipe'] }
|
||||||
|
).trim();
|
||||||
|
|
||||||
|
if (path.resolve(gitRoot) !== path.resolve(rootDir)) {
|
||||||
|
try {
|
||||||
|
// git check-ignore exits 0 if the path IS ignored, 1 if not
|
||||||
|
execFileSync(
|
||||||
|
'git',
|
||||||
|
['check-ignore', '-q', path.resolve(rootDir)],
|
||||||
|
{ cwd: rootDir, encoding: 'utf-8', timeout: 5000, stdio: ['pipe', 'pipe', 'pipe'] }
|
||||||
|
);
|
||||||
|
// Directory is gitignored by parent repo — fall back to filesystem walk
|
||||||
|
return null;
|
||||||
|
} catch {
|
||||||
|
// Not ignored — safe to use git ls-files
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// -c = cached (tracked), -o = others (untracked), --exclude-standard = respect .gitignore
|
// -c = cached (tracked), -o = others (untracked), --exclude-standard = respect .gitignore
|
||||||
const output = execFileSync(
|
const output = execFileSync(
|
||||||
'git',
|
'git',
|
||||||
|
|||||||
Reference in New Issue
Block a user