* Filters out locations whose file paths are gitignored. * Uses `git check-ignore` with batched path arguments for efficiency.
( locations: T[], cwd: string, )
| 554 | * Uses `git check-ignore` with batched path arguments for efficiency. |
| 555 | */ |
| 556 | async function filterGitIgnoredLocations<T extends Location>( |
| 557 | locations: T[], |
| 558 | cwd: string, |
| 559 | ): Promise<T[]> { |
| 560 | if (locations.length === 0) { |
| 561 | return locations |
| 562 | } |
| 563 | |
| 564 | // Collect unique file paths from URIs |
| 565 | const uriToPath = new Map<string, string>() |
| 566 | for (const loc of locations) { |
| 567 | if (loc.uri && !uriToPath.has(loc.uri)) { |
| 568 | uriToPath.set(loc.uri, uriToFilePath(loc.uri)) |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | const uniquePaths = uniq(uriToPath.values()) |
| 573 | if (uniquePaths.length === 0) { |
| 574 | return locations |
| 575 | } |
| 576 | |
| 577 | // Batch check paths with git check-ignore |
| 578 | // Exit code 0 = at least one path is ignored, 1 = none ignored, 128 = not a git repo |
| 579 | const ignoredPaths = new Set<string>() |
| 580 | const BATCH_SIZE = 50 |
| 581 | for (let i = 0; i < uniquePaths.length; i += BATCH_SIZE) { |
| 582 | const batch = uniquePaths.slice(i, i + BATCH_SIZE) |
| 583 | const result = await execFileNoThrowWithCwd( |
| 584 | 'git', |
| 585 | ['check-ignore', ...batch], |
| 586 | { |
| 587 | cwd, |
| 588 | preserveOutputOnError: false, |
| 589 | timeout: 5_000, |
| 590 | }, |
| 591 | ) |
| 592 | |
| 593 | if (result.code === 0 && result.stdout) { |
| 594 | for (const line of result.stdout.split('\n')) { |
| 595 | const trimmed = line.trim() |
| 596 | if (trimmed) { |
| 597 | ignoredPaths.add(trimmed) |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (ignoredPaths.size === 0) { |
| 604 | return locations |
| 605 | } |
| 606 | |
| 607 | return locations.filter(loc => { |
| 608 | const filePath = uriToPath.get(loc.uri) |
| 609 | return !filePath || !ignoredPaths.has(filePath) |
| 610 | }) |
| 611 | } |
| 612 | |
| 613 | /** |
no test coverage detected