* Get all files visible to git (tracked + untracked but not ignored). * Respects .gitignore at all levels (root, subdirectories) and descends into * embedded (nested, non-submodule) git repos. Returns null on failure * (non-git project) so callers can fall back to a filesystem walk.
(rootDir: string, onManifestEvidence?: (evidence: ManifestEvidence) => void)
| 1556 | * (non-git project) so callers can fall back to a filesystem walk. |
| 1557 | */ |
| 1558 | function getGitVisibleFiles(rootDir: string, onManifestEvidence?: (evidence: ManifestEvidence) => void): Set<string> | null { |
| 1559 | try { |
| 1560 | // Check if the project directory is gitignored by a parent repo. |
| 1561 | // When rootDir lives inside a parent git repo that ignores it, |
| 1562 | // `git ls-files` returns nothing — fall back to filesystem walk. |
| 1563 | const gitRoot = execFileSync( |
| 1564 | 'git', |
| 1565 | ['rev-parse', '--show-toplevel'], |
| 1566 | { cwd: rootDir, encoding: 'utf-8', timeout: 5000, stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true } |
| 1567 | ).trim(); |
| 1568 | |
| 1569 | if (path.resolve(gitRoot) !== path.resolve(rootDir) && isIgnoredByEnclosingRepo(rootDir)) { |
| 1570 | // Directory is gitignored by parent repo — fall back to filesystem walk |
| 1571 | logDebug('project root is gitignored by a parent repo — falling back to filesystem walk', { |
| 1572 | rootDir, |
| 1573 | gitRoot, |
| 1574 | }); |
| 1575 | return null; |
| 1576 | } |
| 1577 | |
| 1578 | const files = new Set<string>(); |
| 1579 | const embeddedRoots = new Set<string>(); |
| 1580 | const gitlinks: string[] = []; |
| 1581 | collectGitFiles(rootDir, '', files, embeddedRoots, loadIncludeIgnoredMatcher(rootDir), gitlinks); |
| 1582 | // Apply built-in default ignores uniformly — to tracked files too, since |
| 1583 | // committing a dependency/build dir doesn't make it project code. A |
| 1584 | // `.gitignore` negation (e.g. `!vendor/`) is the explicit opt-in. (issue #407) |
| 1585 | // Files inside an EMBEDDED repo are matched against that repo's own rules, |
| 1586 | // not the parent's: the parent's .gitignore hides the child repo from git, |
| 1587 | // not from the index. (#514) |
| 1588 | const ignoredEntries: string[] | undefined = onManifestEvidence ? [] : undefined; |
| 1589 | const ig = buildScopeIgnore(rootDir, embeddedRoots, ignoredEntries); |
| 1590 | // What git's listings say about the package manifests the workspace walk |
| 1591 | // reads, taken from the lists this scan made anyway: every file git |
| 1592 | // lists, before any scope rule, and what it ignores. A full sync compares |
| 1593 | // it with what the last walk found (see trustedManifestList). |
| 1594 | if (onManifestEvidence) { |
| 1595 | onManifestEvidence(manifestEvidence(rootDir, files, ignoredEntries!, |
| 1596 | embeddedRoots.size === 0 && gitlinks.length === 0 && !fs.existsSync(path.join(gitRoot, '.gitmodules')))); |
| 1597 | } |
| 1598 | const visible = new Set([...files].filter((f) => !ig.ignores(f))); |
| 1599 | // Force-include first-party source the project whitelisted in |
| 1600 | // `codegraph.json` `include`. These are gitignored, so `git ls-files` never |
| 1601 | // listed them above — discover them directly off disk and add them. (The |
| 1602 | // common SVN+Git dual-VCS case: source committed to SVN, gitignored out of |
| 1603 | // Git, but still wanted in the graph.) |
| 1604 | for (const f of collectIncludedFilesForRoot(rootDir)) visible.add(f); |
| 1605 | return visible; |
| 1606 | } catch (error) { |
| 1607 | // Any failure here (git missing, a `git rev-parse`/`ls-files` timeout or |
| 1608 | // buffer overrun under load, an unreadable repo, unsupported flag combo on |
| 1609 | // older git, etc.) silently sent every caller to `scanDirectoryWalk` with |
| 1610 | // zero signal that the fast git-delegated path was skipped — making reports |
| 1611 | // like #1567 (nested-`.gitignore`-excluded `node_modules` walked into) |
| 1612 | // hard to triage, since both ignore implementations look correct in |
| 1613 | // isolation but there was no way to tell which one ran. Log it under the |
| 1614 | // existing CODEGRAPH_DEBUG gate so a future report can confirm or rule out |
| 1615 | // the fallback in one step. |
no test coverage detected