@@ -66,19 +66,15 @@ fn main() -> ExitCode {
6666/// Run a command in each git repository in the given directory and its
6767/// subdirectories.
6868fn repository_foreach ( options : & Options ) -> Result < ( ) , Vec < Error > > {
69- let mut errors: Vec < _ > = repository_walk ( options)
69+ let mut errors: Vec < _ > = discover_repositories ( options)
7070 . par_bridge ( )
71- . filter_map ( |entry | {
72- let path = match entry {
73- Ok ( entry ) => entry . into_path ( ) ,
71+ . filter_map ( |repository | {
72+ let path = match repository {
73+ Ok ( path ) => path ,
7474 Err ( source) => return Some ( Error :: from ( source) ) ,
7575 } ;
7676
77- if path. is_dir ( ) && path. join ( ".git" ) . exists ( ) {
78- run_command_in_directory ( options, & path) . err ( )
79- } else {
80- None
81- }
77+ run_command_in_directory ( options, & path) . err ( )
8278 } )
8379 . collect ( ) ;
8480
@@ -90,12 +86,26 @@ fn repository_foreach(options: &Options) -> Result<(), Vec<Error>> {
9086 }
9187}
9288
93- fn repository_walk ( options : & Options ) -> ignore:: Walk {
89+ fn discover_repositories (
90+ options : & Options ,
91+ ) -> impl Iterator < Item = Result < PathBuf , ignore:: Error > > {
9492 WalkBuilder :: new ( & options. directory )
9593 . standard_filters ( !options. no_ignore )
9694 . hidden ( !options. hidden )
97- . filter_entry ( |entry| entry . file_name ( ) != OsStr :: new ( ".git" ) )
95+ . filter_entry ( is_not_git_dir )
9896 . build ( )
97+ . filter_map ( |entry| match entry {
98+ Ok ( entry) => is_repository ( & entry) . then ( || Ok ( entry. into_path ( ) ) ) ,
99+ Err ( source) => Some ( Err ( source) ) ,
100+ } )
101+ }
102+
103+ fn is_not_git_dir ( entry : & ignore:: DirEntry ) -> bool {
104+ entry. file_name ( ) != OsStr :: new ( ".git" )
105+ }
106+
107+ fn is_repository ( entry : & ignore:: DirEntry ) -> bool {
108+ entry. file_type ( ) . is_some_and ( |ft| ft. is_dir ( ) ) && entry. path ( ) . join ( ".git" ) . is_dir ( )
99109}
100110
101111/// Run a command in a directory.
@@ -185,11 +195,11 @@ mod test {
185195 let visible = create_repository ( directory. path ( ) , "visible" ) ;
186196 let hidden = create_repository ( directory. path ( ) , ".hidden" ) ;
187197
188- let repositories: Vec < PathBuf > = discovered_repositories ( directory. path ( ) , false , false ) ;
198+ let repositories: Vec < PathBuf > = repository_paths ( directory. path ( ) , false , false ) ;
189199 assert ! ( repositories. contains( & visible) ) ;
190200 assert ! ( !repositories. contains( & hidden) ) ;
191201
192- let repositories = discovered_repositories ( directory. path ( ) , true , false ) ;
202+ let repositories = repository_paths ( directory. path ( ) , true , false ) ;
193203 assert ! ( repositories. contains( & visible) ) ;
194204 assert ! ( repositories. contains( & hidden) ) ;
195205 }
@@ -202,8 +212,8 @@ mod test {
202212 fs:: write ( directory. path ( ) . join ( ".gitignore" ) , "ignored/\n " )
203213 . expect ( "failed to create .gitignore" ) ;
204214
205- assert ! ( !discovered_repositories ( directory. path( ) , false , false ) . contains( & ignored) ) ;
206- assert ! ( discovered_repositories ( directory. path( ) , false , true ) . contains( & ignored) ) ;
215+ assert ! ( !repository_paths ( directory. path( ) , false , false ) . contains( & ignored) ) ;
216+ assert ! ( repository_paths ( directory. path( ) , false , true ) . contains( & ignored) ) ;
207217 }
208218
209219 fn create_repository ( parent : & Path , name : & str ) -> PathBuf {
@@ -212,7 +222,7 @@ mod test {
212222 path
213223 }
214224
215- fn discovered_repositories ( directory : & Path , hidden : bool , no_ignore : bool ) -> Vec < PathBuf > {
225+ fn repository_paths ( directory : & Path , hidden : bool , no_ignore : bool ) -> Vec < PathBuf > {
216226 let options = Options {
217227 quiet : false ,
218228 directory : directory. to_owned ( ) ,
@@ -222,9 +232,8 @@ mod test {
222232 command : "true" . to_owned ( ) ,
223233 } ;
224234
225- repository_walk ( & options)
226- . map ( |entry| entry. expect ( "failed to walk test directory" ) . into_path ( ) )
227- . filter ( |path| path. is_dir ( ) && path. join ( ".git" ) . exists ( ) )
235+ discover_repositories ( & options)
236+ . map ( |repository| repository. expect ( "failed to walk test directory" ) )
228237 . collect ( )
229238 }
230239}
0 commit comments