@@ -11,6 +11,7 @@ import {
1111 addIgnoreFileContents ,
1212 isIgnoredByIgnoreChain ,
1313 PROJECT_IGNORE_FILES ,
14+ testChain ,
1415} from './util/project-ignore'
1516
1617import type { CodebuffFileSystem } from './types/filesystem'
@@ -249,6 +250,71 @@ export async function parseGitignore(params: {
249250 return parseGitignoreWithMode ( { ...params , throwOnReadError : false } )
250251}
251252
253+ /**
254+ * `getProjectFileTree`'s ignore decision for a flat list of files that another
255+ * lister found (the SDK's ripgrep listing), built from the same rules in the
256+ * same order: the defaults, then each directory's ignore files from the root
257+ * down. `ignoreFileDirs` names every directory (POSIX, relative, `''` for the
258+ * root) holding one of PROJECT_IGNORE_FILES; any other directory adds no rules.
259+ * Returns whether a file is ignored.
260+ */
261+ export async function createProjectIgnoreFilter ( params : {
262+ projectRoot : string
263+ fs : CodebuffFileSystem
264+ ignoreFileDirs : Iterable < string >
265+ } ) : Promise < ( relativeFilePath : string ) => boolean > {
266+ const { projectRoot, fs } = params
267+ const rulesByDir = new Map < string , ignore . Ignore > ( )
268+ await Promise . all (
269+ [ ...new Set ( params . ignoreFileDirs ) ] . map ( async ( dir ) => {
270+ const fullDirPath = path . join ( projectRoot , dir )
271+ rulesByDir . set ( dir , await parseGitignore ( { fullDirPath, fs } ) )
272+ } ) ,
273+ )
274+
275+ const chainByDir = new Map < string , DirIgnore [ ] > ( )
276+ const chainFor = ( dir : string ) : DirIgnore [ ] => {
277+ const cached = chainByDir . get ( dir )
278+ if ( cached ) return cached
279+ const inherited =
280+ dir === ''
281+ ? [ { base : '' , ig : createDefaultIgnore ( ) } ]
282+ : chainFor ( parentDir ( dir ) )
283+ const own = rulesByDir . get ( dir )
284+ const chain = own ? [ ...inherited , { base : dir , ig : own } ] : inherited
285+ chainByDir . set ( dir , chain )
286+ return chain
287+ }
288+
289+ // What the walk decides for a directory, decided once instead of once per
290+ // file under it. The walk prunes it when its parent's chain ignores either
291+ // its bare name, tested before it descends, or its `dir/` form, tested for
292+ // each file inside; a `!` that matches only one form does not save it.
293+ const dirIgnored = new Map < string , boolean > ( )
294+ const isDirIgnored = ( dir : string ) : boolean => {
295+ if ( dir === '' ) return false
296+ const cached = dirIgnored . get ( dir )
297+ if ( cached !== undefined ) return cached
298+ const parent = parentDir ( dir )
299+ const chain = chainFor ( parent )
300+ const ignored =
301+ isDirIgnored ( parent ) ||
302+ testChain ( chain , dir ) ||
303+ testChain ( chain , `${ dir } /` )
304+ dirIgnored . set ( dir , ignored )
305+ return ignored
306+ }
307+
308+ return ( relativeFilePath ) => {
309+ const dir = parentDir ( relativeFilePath )
310+ return isDirIgnored ( dir ) || testChain ( chainFor ( dir ) , relativeFilePath )
311+ }
312+ }
313+
314+ function parentDir ( posixPath : string ) : string {
315+ return posixPath . slice ( 0 , Math . max ( posixPath . lastIndexOf ( '/' ) , 0 ) )
316+ }
317+
252318export function getAllFilePaths (
253319 nodes : FileTreeNode [ ] ,
254320 basePath : string = '' ,
0 commit comments