@@ -27,6 +27,8 @@ import type {
2727} from "@t3tools/contracts" ;
2828import { isProjectFaviconPath } from "@t3tools/shared/projectFavicon" ;
2929
30+ import * as VcsProcess from "../vcs/VcsProcess.ts" ;
31+
3032// fff-node's native library stays external; single-executables require disk packages instead of importing them.
3133const requireForFff = NodeModule . createRequire ( import . meta. url ) ;
3234const { FileFinder } = requireForFff ( "@ff-labs/fff-node" ) as typeof import ( "@ff-labs/fff-node" ) ;
@@ -289,6 +291,61 @@ function withDirectoryAncestors(entries: ReadonlyArray<ProjectEntry>): ProjectEn
289291 return [ ...entryByPath . values ( ) ] ;
290292}
291293
294+ const GITIGNORE_SOURCE = / ( ^ | [ \\ / ] ) \. g i t i g n o r e $ / ;
295+ // A collapsed `dir/` that fits in one fff query token: no whitespace, glob, or negation syntax.
296+ const QUERY_SAFE_DIRECTORY = / ^ [ ^ \s * ? [ \] { } ! \\ ] + \/ $ / ;
297+
298+ /**
299+ * fff applies `.gitignore` files but not `.git/info/exclude` or `core.excludesFile`, where tools
300+ * commonly hide nested worktrees. Directories git excludes through those sources become
301+ * root-anchored `!dir/**` constraints appended to every query.
302+ */
303+ const loadExclusionConstraints = Effect . fn ( "WorkspaceSearchIndex.loadExclusionConstraints" ) (
304+ function * ( vcsProcess : VcsProcess . VcsProcess [ "Service" ] , cwd : string ) {
305+ const runGit = ( args : ReadonlyArray < string > , stdin ?: string ) =>
306+ vcsProcess . run ( {
307+ operation : "WorkspaceSearchIndex.loadExclusionConstraints" ,
308+ command : "git" ,
309+ args : [ "-c" , "core.fsmonitor=false" , ...args ] ,
310+ cwd,
311+ ...( stdin !== undefined ? { stdin } : { } ) ,
312+ allowNonZeroExit : true ,
313+ timeoutMs : 10_000 ,
314+ maxOutputBytes : 4 * 1024 * 1024 ,
315+ } ) ;
316+ // `--directory` collapses a wholly ignored folder into one `name/` entry.
317+ const listed = yield * runGit ( [
318+ "ls-files" ,
319+ "--others" ,
320+ "--ignored" ,
321+ "--exclude-standard" ,
322+ "--directory" ,
323+ "--no-empty-directory" ,
324+ "-z" ,
325+ ] ) ;
326+ if ( listed . exitCode !== 0 ) return "" ;
327+ const directories = listed . stdout
328+ . split ( "\0" )
329+ . filter ( ( entry ) => QUERY_SAFE_DIRECTORY . test ( entry ) ) ;
330+ if ( directories . length === 0 ) return "" ;
331+ const classified = yield * runGit (
332+ [ "check-ignore" , "--verbose" , "-z" , "--stdin" ] ,
333+ `${ directories . join ( "\0" ) } \0` ,
334+ ) ;
335+ if ( classified . exitCode !== 0 ) return "" ;
336+ // Records are `source NUL line NUL pattern NUL path NUL`.
337+ const fields = classified . stdout . split ( "\0" ) ;
338+ const constraints : string [ ] = [ ] ;
339+ for ( let index = 0 ; index + 3 < fields . length ; index += 4 ) {
340+ if ( ! GITIGNORE_SOURCE . test ( fields [ index ] ! ) ) {
341+ constraints . push ( `!${ fields [ index + 3 ] } **` ) ;
342+ }
343+ }
344+ return constraints . join ( " " ) ;
345+ } ,
346+ Effect . orElseSucceed ( ( ) => "" ) ,
347+ ) ;
348+
292349const createFinder = Effect . fn ( "WorkspaceSearchIndex.createFinder" ) ( function * (
293350 cwd : string ,
294351 variant : WorkspaceSearchIndexVariant ,
@@ -375,6 +432,13 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
375432
376433 const hasIncompleteInitialScan = ( ) => initialScanTimedOut && finder . isScanning ( ) ;
377434
435+ const vcsProcess = yield * VcsProcess . VcsProcess ;
436+ let exclusionConstraints = yield * loadExclusionConstraints ( vcsProcess , cwd ) ;
437+ const withExclusions = ( query : string ) =>
438+ exclusionConstraints && query
439+ ? `${ query } ${ exclusionConstraints } `
440+ : query || exclusionConstraints ;
441+
378442 const runSearch = Effect . fn ( "WorkspaceSearchIndex.runSearch" ) ( function * < A > (
379443 query : string ,
380444 pageSize : number ,
@@ -431,15 +495,27 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
431495 cause,
432496 } ) ,
433497 ) ;
498+ exclusionConstraints = yield * loadExclusionConstraints ( vcsProcess , cwd ) ;
434499 } ) ;
435500
436501 const list : WorkspaceSearchIndex [ "Service" ] [ "list" ] = Effect . fn ( "WorkspaceSearchIndex.list" ) (
437502 function * ( ) {
438503 const incompleteBeforeQuery = hasIncompleteInitialScan ( ) ;
439- const result = yield * runSearch ( "" , WORKSPACE_INDEX_PAGE_SIZE , "mixedSearch" , ( ) =>
440- finder . mixedSearch ( "" , { pageSize : WORKSPACE_INDEX_PAGE_SIZE } ) ,
441- ) ;
442- const mapped = mapMixedSearchResult ( result , WORKSPACE_INDEX_MAX_ENTRIES ) ;
504+ // fff's mixed search returns only directories for a constraint-only query; list files and
505+ // derive their folders instead.
506+ const mapped = exclusionConstraints
507+ ? mapFileSearchResult (
508+ yield * runSearch ( "" , WORKSPACE_INDEX_PAGE_SIZE , "fileSearch" , ( ) =>
509+ finder . fileSearch ( exclusionConstraints , { pageSize : WORKSPACE_INDEX_PAGE_SIZE } ) ,
510+ ) ,
511+ WORKSPACE_INDEX_MAX_ENTRIES ,
512+ )
513+ : mapMixedSearchResult (
514+ yield * runSearch ( "" , WORKSPACE_INDEX_PAGE_SIZE , "mixedSearch" , ( ) =>
515+ finder . mixedSearch ( "" , { pageSize : WORKSPACE_INDEX_PAGE_SIZE } ) ,
516+ ) ,
517+ WORKSPACE_INDEX_MAX_ENTRIES ,
518+ ) ;
443519 const sortedEntries = withDirectoryAncestors ( mapped . entries ) . toSorted ( ( left , right ) =>
444520 left . path . localeCompare ( right . path ) ,
445521 ) ;
@@ -457,22 +533,24 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
457533 ) ( function * ( query , limit , kind , imageOnly ) {
458534 const pageSize = imageOnly ? WORKSPACE_INDEX_PAGE_SIZE : Math . max ( 1 , limit + 1 ) ;
459535 const incompleteBeforeQuery = hasIncompleteInitialScan ( ) ;
460- if ( kind === "file" || imageOnly ) {
536+ const constrainedQuery = withExclusions ( query ) ;
537+ // Mixed search drops files when the query holds only exclusion constraints.
538+ if ( kind === "file" || imageOnly || ( kind === undefined && ! query && exclusionConstraints ) ) {
461539 const result = yield * runSearch ( query , pageSize , "fileSearch" , ( ) =>
462- finder . fileSearch ( query , { pageSize } ) ,
540+ finder . fileSearch ( constrainedQuery , { pageSize } ) ,
463541 ) ;
464542 const mapped = mapFileSearchResult ( result , limit , imageOnly ) ;
465543 return { ...mapped , truncated : incompleteBeforeQuery || mapped . truncated } ;
466544 }
467545 if ( kind === "directory" ) {
468546 const result = yield * runSearch ( query , pageSize , "directorySearch" , ( ) =>
469- finder . directorySearch ( query , { pageSize } ) ,
547+ finder . directorySearch ( constrainedQuery , { pageSize } ) ,
470548 ) ;
471549 const mapped = mapDirectorySearchResult ( result , limit ) ;
472550 return { ...mapped , truncated : incompleteBeforeQuery || mapped . truncated } ;
473551 }
474552 const result = yield * runSearch ( query , pageSize , "mixedSearch" , ( ) =>
475- finder . mixedSearch ( query , { pageSize } ) ,
553+ finder . mixedSearch ( constrainedQuery , { pageSize } ) ,
476554 ) ;
477555 const mapped = mapMixedSearchResult ( result , limit ) ;
478556 return { ...mapped , truncated : incompleteBeforeQuery || mapped . truncated } ;
@@ -494,7 +572,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
494572 do {
495573 const remainingTimeBudgetMs = Math . max ( 1 , Math . ceil ( deadline - performance . now ( ) ) ) ;
496574 const result = yield * runSearch ( input . query , input . limit , "grep" , ( ) =>
497- finder . grep ( searchQuery , {
575+ finder . grep ( withExclusions ( searchQuery ) , {
498576 mode : regexMode ? "regex" : "plain" ,
499577 smartCase : ! input . caseSensitive && ! regexMode ,
500578 maxMatchesPerFile : Math . min ( CONTENT_SEARCH_MAX_MATCHES_PER_FILE , rawPageSize ) ,
0 commit comments