Skip to content

Commit 7c6598c

Browse files
committed
refactor: replace repository_walk with discover_repositories
1 parent e7f942c commit 7c6598c

1 file changed

Lines changed: 28 additions & 19 deletions

File tree

src/main.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,15 @@ fn main() -> ExitCode {
6666
/// Run a command in each git repository in the given directory and its
6767
/// subdirectories.
6868
fn 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.
@@ -186,11 +196,11 @@ mod test {
186196
let visible = create_repository(directory.path(), "visible");
187197
let hidden = create_repository(directory.path(), ".hidden");
188198

189-
let repositories: Vec<PathBuf> = discovered_repositories(directory.path(), false, false);
199+
let repositories: Vec<PathBuf> = repository_paths(directory.path(), false, false);
190200
assert!(repositories.contains(&visible));
191201
assert!(!repositories.contains(&hidden));
192202

193-
let repositories = discovered_repositories(directory.path(), true, false);
203+
let repositories = repository_paths(directory.path(), true, false);
194204
assert!(repositories.contains(&visible));
195205
assert!(repositories.contains(&hidden));
196206
}
@@ -203,8 +213,8 @@ mod test {
203213
fs::write(directory.path().join(".gitignore"), "ignored/\n")
204214
.expect("failed to create .gitignore");
205215

206-
assert!(!discovered_repositories(directory.path(), false, false).contains(&ignored));
207-
assert!(discovered_repositories(directory.path(), false, true).contains(&ignored));
216+
assert!(!repository_paths(directory.path(), false, false).contains(&ignored));
217+
assert!(repository_paths(directory.path(), false, true).contains(&ignored));
208218
}
209219

210220
fn create_repository(parent: &Path, name: &str) -> PathBuf {
@@ -213,7 +223,7 @@ mod test {
213223
path
214224
}
215225

216-
fn discovered_repositories(directory: &Path, hidden: bool, no_ignore: bool) -> Vec<PathBuf> {
226+
fn repository_paths(directory: &Path, hidden: bool, no_ignore: bool) -> Vec<PathBuf> {
217227
let options = Options {
218228
quiet: false,
219229
directory: directory.to_owned(),
@@ -223,9 +233,8 @@ mod test {
223233
command: vec!["true".to_owned()],
224234
};
225235

226-
repository_walk(&options)
227-
.map(|entry| entry.expect("failed to walk test directory").into_path())
228-
.filter(|path| path.is_dir() && path.join(".git").exists())
236+
discover_repositories(&options)
237+
.map(|repository| repository.expect("failed to walk test directory"))
229238
.collect()
230239
}
231240
}

0 commit comments

Comments
 (0)