From 54d36fafcbf06c6c48a69ecfc367d59826881951 Mon Sep 17 00:00:00 2001 From: Wynd Date: Sat, 18 Jan 2025 21:13:26 +0200 Subject: [PATCH] Using rayon for discovering repos via dir walking --- src/lib.rs | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a7e9fc6..778c213 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ use std::{ cmp::Reverse, collections::HashSet, - path::{self, Path, PathBuf}, + path::{self, PathBuf}, sync::OnceLock, }; @@ -504,34 +504,44 @@ pub fn get_commits( // .sorted_by_cached_key(|a| Reverse(a.time)) // .collect_vec(); - Ok((0, 0, commits)) - // Ok((repos_count, branches_count, commits)) + Ok((repos_count, branches_count, commits)) } fn find_git_repos(scan_path: &path::Path, repos: &mut Vec, ignored_repos: &Vec) { + if let Some(path) = walk_dir(scan_path, ignored_repos) { + repos.extend(path) + } +} + +pub fn walk_dir(scan_path: &path::Path, ignored_repos: &Vec) -> Option> { let Ok(dirs) = scan_path.read_dir() else { - return; + return None; }; - let dirs: Vec<_> = dirs + let dirs: Vec = dirs + .par_bridge() .filter_map(|d| d.ok()) .filter(|d| { let dir_name = d.file_name().to_string_lossy().to_string(); !ignored_repos.contains(&dir_name) }) .filter(|d| d.file_type().is_ok_and(|t| t.is_dir())) - .collect_vec(); + .filter_map(|d| { + let dir = d.path(); + let filename = dir.file_name().unwrap_or_default().to_string_lossy(); - let dirs = dirs.iter().map(|d| d.path()); + match filename.as_ref() { + ".git" => Some(vec![dir]), + _ => walk_dir(&dir, ignored_repos), + } + }) + .reduce(Vec::new, |mut c, n| { + c.extend(n); + c + }); - for dir in dirs { - let filename = dir.file_name().unwrap_or_default().to_string_lossy(); - match filename.as_ref() { - ".git" => repos.push(dir), - _ => find_git_repos(&dir, repos, ignored_repos), - } - } + Some(dirs) } pub fn get_default_until(since: NaiveDate) -> String {