@@ -760,36 +760,46 @@ impl IndexingContext {
760760 if let Some ( ignored) = ignored {
761761 // The index is authoritative for tracked descendants; check-ignore on a directory
762762 // alone does not protect tracked files below an otherwise ignored directory.
763+ // A lazy tree listing has no scan prefetch. Ask for its ignored siblings together
764+ // instead of starting one process per directory; keep the native fallback when
765+ // Git cannot map a batched answer reliably (for example, normalized Unicode).
766+ let ignored_directories = candidates
767+ . iter ( )
768+ . filter ( |name| ignored. contains ( * name) )
769+ . cloned ( )
770+ . collect :: < Vec < _ > > ( ) ;
771+ let tracked = lithe_git:: tracked_directories ( parent, & ignored_directories) ?;
763772 for name in candidates {
764- let excluded = if parent. join ( & name) . join ( ".git" ) . exists ( ) {
765- // A nested repository owns its ignore rules and tracked content.
766- false
767- } else if ignored. contains ( & name) {
768- let response = lithe_git:: execute_git_readonly (
769- & parent. to_string_lossy ( ) ,
770- & [
771- "--literal-pathspecs" . into ( ) ,
772- "ls-files" . into ( ) ,
773- "-z" . into ( ) ,
774- "--" . into ( ) ,
775- name. clone ( ) ,
776- ] ,
777- None ,
778- ) ;
779- self . check ( ) ?;
780- let Ok ( response) = response else {
781- self . git_failed . store ( true , Ordering :: Release ) ;
782- continue ;
783- } ;
784- if response. exit_code != 0 {
785- self . git_failed . store ( true , Ordering :: Release ) ;
773+ let excluded =
774+ if parent. join ( & name) . join ( ".git" ) . exists ( ) || !ignored. contains ( & name) {
775+ // A nested repository owns its ignore rules and tracked content.
786776 false
777+ } else if let Some ( tracked) = & tracked {
778+ !tracked. contains ( & name)
787779 } else {
788- response. stdout . is_empty ( )
789- }
790- } else {
791- false
792- } ;
780+ let response = lithe_git:: execute_git_readonly (
781+ & parent. to_string_lossy ( ) ,
782+ & [
783+ "--literal-pathspecs" . into ( ) ,
784+ "ls-files" . into ( ) ,
785+ "-z" . into ( ) ,
786+ "--" . into ( ) ,
787+ name. clone ( ) ,
788+ ] ,
789+ None ,
790+ ) ;
791+ self . check ( ) ?;
792+ let Ok ( response) = response else {
793+ self . git_failed . store ( true , Ordering :: Release ) ;
794+ continue ;
795+ } ;
796+ if response. exit_code != 0 {
797+ self . git_failed . store ( true , Ordering :: Release ) ;
798+ false
799+ } else {
800+ response. stdout . is_empty ( )
801+ }
802+ } ;
793803 decisions. insert ( relative_path ( & parent. join ( name) , & self . root ) , excluded) ;
794804 }
795805 } else {
@@ -1955,6 +1965,205 @@ mod tests {
19551965 ) ;
19561966 }
19571967
1968+ /// Listing one expanded directory must not start one Git process per ignored sibling.
1969+ /// This exercises the lazy tree path before a snapshot or provider scan warms its cache.
1970+ #[ test]
1971+ fn directory_listing_batches_tracked_checks_for_ignored_siblings ( ) {
1972+ use lithe_git:: console:: QueryCategory ;
1973+ let _deadline = lithe_protocol:: cancellation:: Scope :: begin ( None , Some ( 10_000 ) ) ;
1974+ let workspace = Workspace :: new ( ) ;
1975+ workspace. git ( & [ "init" , "--quiet" ] ) ;
1976+ fs:: write ( workspace. 0 . join ( ".gitignore" ) , "generated*/\n " ) . unwrap ( ) ;
1977+ for index in 0 ..64 {
1978+ fs:: create_dir ( workspace. 0 . join ( format ! ( "generated {index:03}" ) ) ) . unwrap ( ) ;
1979+ }
1980+ fs:: write ( workspace. 0 . join ( "generated 000/kept.txt" ) , "tracked" ) . unwrap ( ) ;
1981+ workspace. git ( & [ "add" , "-f" , "generated 000/kept.txt" ] ) ;
1982+ for name in [ "generated [literal]" , "generated literal" ] {
1983+ fs:: create_dir ( workspace. 0 . join ( name) ) . unwrap ( ) ;
1984+ }
1985+ fs:: write ( workspace. 0 . join ( "generated [literal]/kept.txt" ) , "tracked" ) . unwrap ( ) ;
1986+ workspace. git ( & [ "add" , "-f" , "generated [literal]/kept.txt" ] ) ;
1987+ let nested = workspace. 0 . join ( "generated nested" ) ;
1988+ fs:: create_dir ( & nested) . unwrap ( ) ;
1989+ let initialized = lithe_git:: execute_git_readonly (
1990+ & nested. to_string_lossy ( ) ,
1991+ & [ "init" . into ( ) , "--quiet" . into ( ) ] ,
1992+ None ,
1993+ )
1994+ . unwrap ( ) ;
1995+ assert_eq ! ( initialized. exit_code, 0 , "{}" , initialized. stderr) ;
1996+ workspace. configure ( & [ ] , & [ ] , true ) ;
1997+
1998+ let calls = Arc :: new ( Mutex :: new ( Vec :: < String > :: new ( ) ) ) ;
1999+ let recorded = calls. clone ( ) ;
2000+ let listings = lithe_request_context:: events:: with_sink (
2001+ Arc :: new ( move |event| {
2002+ let event: serde_json:: Value = serde_json:: from_str ( event) . unwrap ( ) ;
2003+ if event[ "type" ] == "started" {
2004+ for argument in event[ "arguments" ] . as_array ( ) . unwrap ( ) {
2005+ if let Some ( command @ ( "check-ignore" | "ls-files" ) ) = argument. as_str ( ) {
2006+ recorded. lock ( ) . unwrap ( ) . push ( command. to_owned ( ) ) ;
2007+ }
2008+ }
2009+ }
2010+ } ) ,
2011+ || {
2012+ let _policy = lithe_git:: execution_policy:: Scope :: begin ( Some (
2013+ lithe_git:: execution_policy:: GitExecutionOptions {
2014+ console_queries : vec ! [ QueryCategory :: Checks ] ,
2015+ query_category : Some ( QueryCategory :: Checks ) ,
2016+ ..Default :: default ( )
2017+ } ,
2018+ ) )
2019+ . unwrap ( ) ;
2020+ let request =
2021+ || serde_json:: from_value ( json ! ( { "root" : workspace. 0 , "paths" : [ "" ] } ) ) . unwrap ( ) ;
2022+ let first = files:: list_directories ( request ( ) ) . unwrap ( ) ;
2023+ let second = files:: list_directories ( request ( ) ) . unwrap ( ) ;
2024+ assert_eq ! (
2025+ first. directories[ 0 ] . children. len( ) ,
2026+ second. directories[ 0 ] . children. len( )
2027+ ) ;
2028+ first
2029+ } ,
2030+ ) ;
2031+ let children = & listings. directories [ 0 ] . children ;
2032+ for index in 0 ..64 {
2033+ let name = format ! ( "generated {index:03}" ) ;
2034+ assert ! ( children. iter( ) . any( |node| node. name == name) ) ;
2035+ }
2036+ let policy = context ( & workspace. 0 ) . unwrap ( ) ;
2037+ assert_eq ! ( policy. reason( "generated 000" , true ) . unwrap( ) , None ) ;
2038+ assert_eq ! ( policy. reason( "generated 001" , true ) . unwrap( ) , Some ( "git" ) ) ;
2039+ assert_eq ! ( policy. reason( "generated [literal]" , true ) . unwrap( ) , None ) ;
2040+ assert_eq ! (
2041+ policy. reason( "generated literal" , true ) . unwrap( ) ,
2042+ Some ( "git" )
2043+ ) ;
2044+ assert_eq ! ( policy. reason( "generated nested" , true ) . unwrap( ) , None ) ;
2045+ assert ! ( !policy. git_failed. load( Ordering :: Acquire ) ) ;
2046+ let calls = calls. lock ( ) . unwrap ( ) ;
2047+ let count = |command : & str | calls. iter ( ) . filter ( |value| * value == command) . count ( ) ;
2048+ assert_eq ! ( count( "check-ignore" ) , 1 , "queries: {calls:?}" ) ;
2049+ assert_eq ! ( count( "ls-files" ) , 1 , "queries: {calls:?}" ) ;
2050+ }
2051+
2052+ /// A failed or unmatchable batch still uses the native per-directory answer.
2053+ #[ cfg( unix) ]
2054+ #[ test]
2055+ fn directory_listing_falls_back_when_batched_tracked_checks_are_unavailable ( ) {
2056+ use std:: os:: unix:: fs:: PermissionsExt ;
2057+ let _deadline = lithe_protocol:: cancellation:: Scope :: begin ( None , Some ( 10_000 ) ) ;
2058+ for batch_result in [ "exit 128" , "printf 'outside-request/file\\ 000'; exit 0" ] {
2059+ let workspace = Workspace :: new ( ) ;
2060+ workspace. git ( & [ "init" , "--quiet" ] ) ;
2061+ fs:: write (
2062+ workspace. 0 . join ( ".gitignore" ) ,
2063+ "ignored/\n other/\n tracked/\n " ,
2064+ )
2065+ . unwrap ( ) ;
2066+ fs:: create_dir ( workspace. 0 . join ( "other" ) ) . unwrap ( ) ;
2067+ fs:: create_dir ( workspace. 0 . join ( "tracked" ) ) . unwrap ( ) ;
2068+ fs:: write ( workspace. 0 . join ( "tracked/kept.txt" ) , "tracked" ) . unwrap ( ) ;
2069+ workspace. git ( & [ "add" , "-f" , "tracked/kept.txt" ] ) ;
2070+ let wrapper = workspace. 0 . join ( ".git/listing-test-git" ) ;
2071+ fs:: write (
2072+ & wrapper,
2073+ format ! (
2074+ "#!/bin/sh\n tracked=0\n paths=0\n count=0\n \
2075+ for argument in \" $@\" ; do\n \
2076+ if [ \" $argument\" = ls-files ]; then tracked=1; fi\n \
2077+ if [ \" $paths\" = 1 ]; then count=$((count + 1)); fi\n \
2078+ if [ \" $argument\" = -- ]; then paths=1; fi\n \
2079+ done\n \
2080+ if [ \" $tracked\" = 1 ] && [ \" $count\" -gt 1 ]; then\n \
2081+ printf 'batch\\ n' >> \" $0.attempts\" \n \
2082+ {batch_result}\n \
2083+ fi\n \
2084+ exec git \" $@\" \n "
2085+ ) ,
2086+ )
2087+ . unwrap ( ) ;
2088+ fs:: set_permissions ( & wrapper, fs:: Permissions :: from_mode ( 0o755 ) ) . unwrap ( ) ;
2089+ workspace. configure ( & [ ] , & [ ] , true ) ;
2090+ let _policy = lithe_git:: execution_policy:: Scope :: begin ( Some (
2091+ lithe_git:: execution_policy:: GitExecutionOptions {
2092+ executable : Some ( wrapper. to_string_lossy ( ) . into_owned ( ) ) ,
2093+ ..Default :: default ( )
2094+ } ,
2095+ ) )
2096+ . unwrap ( ) ;
2097+ let listing = files:: list_directories (
2098+ serde_json:: from_value ( json ! ( { "root" : workspace. 0 , "paths" : [ "" ] } ) ) . unwrap ( ) ,
2099+ )
2100+ . unwrap ( ) ;
2101+ assert ! ( listing. directories[ 0 ]
2102+ . children
2103+ . iter( )
2104+ . any( |node| node. name == "ignored" ) ) ;
2105+ let policy = context ( & workspace. 0 ) . unwrap ( ) ;
2106+ assert_eq ! ( policy. reason( "ignored" , true ) . unwrap( ) , Some ( "git" ) ) ;
2107+ assert_eq ! ( policy. reason( "other" , true ) . unwrap( ) , Some ( "git" ) ) ;
2108+ assert_eq ! ( policy. reason( "tracked" , true ) . unwrap( ) , None ) ;
2109+ assert ! ( !policy. git_failed. load( Ordering :: Acquire ) ) ;
2110+ assert_eq ! (
2111+ fs:: read_to_string( wrapper. with_extension( "attempts" ) ) . unwrap( ) ,
2112+ "batch\n "
2113+ ) ;
2114+ }
2115+ }
2116+
2117+ /// Cancelling a tracked query must not start the per-directory fallback or cache answers.
2118+ #[ test]
2119+ fn directory_listing_tracked_check_cancellation_does_not_fall_back ( ) {
2120+ use lithe_git:: console:: QueryCategory ;
2121+ let workspace = Workspace :: new ( ) ;
2122+ workspace. git ( & [ "init" , "--quiet" ] ) ;
2123+ fs:: write ( workspace. 0 . join ( ".gitignore" ) , "ignored/\n other/\n " ) . unwrap ( ) ;
2124+ fs:: create_dir ( workspace. 0 . join ( "other" ) ) . unwrap ( ) ;
2125+ workspace. configure ( & [ ] , & [ ] , true ) ;
2126+ let operation = format ! ( "listing-cancel-{}" , workspace. 0 . display( ) ) ;
2127+ let _deadline =
2128+ lithe_protocol:: cancellation:: Scope :: begin ( Some ( operation. clone ( ) ) , Some ( 5_000 ) ) ;
2129+ let attempts = Arc :: new ( AtomicU64 :: new ( 0 ) ) ;
2130+ let recorded = attempts. clone ( ) ;
2131+ let result = lithe_request_context:: events:: with_sink (
2132+ Arc :: new ( move |event| {
2133+ let event: serde_json:: Value = serde_json:: from_str ( event) . unwrap ( ) ;
2134+ if event[ "type" ] == "started"
2135+ && event[ "arguments" ]
2136+ . as_array ( )
2137+ . unwrap ( )
2138+ . iter ( )
2139+ . any ( |arg| arg == "ls-files" )
2140+ {
2141+ recorded. fetch_add ( 1 , Ordering :: Relaxed ) ;
2142+ assert ! ( lithe_protocol:: cancellation:: cancel( & operation) ) ;
2143+ }
2144+ } ) ,
2145+ || {
2146+ let _policy = lithe_git:: execution_policy:: Scope :: begin ( Some (
2147+ lithe_git:: execution_policy:: GitExecutionOptions {
2148+ console_queries : vec ! [ QueryCategory :: Checks ] ,
2149+ query_category : Some ( QueryCategory :: Checks ) ,
2150+ ..Default :: default ( )
2151+ } ,
2152+ ) )
2153+ . unwrap ( ) ;
2154+ context ( & workspace. 0 ) . unwrap ( ) . reason ( "ignored" , true )
2155+ } ,
2156+ ) ;
2157+ assert ! ( matches!( result. unwrap_err( ) . code, ErrorCode :: Cancelled ) ) ;
2158+ assert_eq ! ( attempts. load( Ordering :: Relaxed ) , 1 ) ;
2159+ assert ! ( context( & workspace. 0 )
2160+ . unwrap( )
2161+ . decisions
2162+ . lock( )
2163+ . unwrap( )
2164+ . is_empty( ) ) ;
2165+ }
2166+
19582167 /// Regression for Lithe-IDEA#1994: preparing provider exclusions asked Git once per
19592168 /// parent directory, which delayed gopls by about 30 s on Kubernetes. A wrapper
19602169 /// executable counts the Git processes: they must grow with directory depth and the
0 commit comments