1use std::path::{Path, PathBuf};
6use std::sync::Mutex;
7
8use rayon::prelude::*;
9
10pub fn find_repos(root: &Path, max_depth: u32) -> Vec<PathBuf> {
11 let out = Mutex::new(Vec::new());
12 walk(root, 0, max_depth, &out);
13 let mut v = out.into_inner().unwrap_or_default();
14 v.sort();
15 v
16}
17
18fn walk(path: &Path, depth: u32, max_depth: u32, out: &Mutex<Vec<PathBuf>>) {
19 if depth > max_depth {
20 return;
21 }
22 let Ok(entries) = std::fs::read_dir(path) else {
23 return;
24 };
25
26 let children: Vec<PathBuf> = entries
27 .filter_map(|e| e.ok())
28 .filter_map(|e| {
29 let path = e.path();
30 let name = path.file_name()?.to_str()?.to_string();
31 if name.starts_with('.') {
32 return None;
33 }
34 let meta = std::fs::symlink_metadata(&path).ok()?;
35 if !meta.file_type().is_dir() {
36 return None;
37 }
38 Some(path)
39 })
40 .collect();
41
42 children.into_par_iter().for_each(|entry| {
43 if entry.join(".git").exists() {
44 if let Ok(mut guard) = out.lock() {
45 guard.push(entry);
46 }
47 return;
48 }
49 walk(&entry, depth + 1, max_depth, out);
50 });
51}
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56 use std::fs;
57 use tempfile::TempDir;
58
59 fn touch(path: &Path) {
60 fs::create_dir_all(path.parent().unwrap()).unwrap();
61 fs::write(path, "").unwrap();
62 }
63
64 #[test]
65 fn finds_repos_at_various_depths() {
66 let tmp = TempDir::new().unwrap();
67 let root = tmp.path();
68
69 fs::create_dir_all(root.join("a/.git")).unwrap();
71 fs::create_dir_all(root.join("b/c/.git")).unwrap();
73 fs::create_dir_all(root.join("plain")).unwrap();
75 fs::create_dir_all(root.join("d")).unwrap();
77 touch(&root.join("d/.git"));
78
79 let repos = find_repos(root, 4);
80 let names: Vec<String> = repos
81 .iter()
82 .map(|p| p.file_name().unwrap().to_string_lossy().into_owned())
83 .collect();
84
85 assert!(names.contains(&"a".to_string()));
86 assert!(names.contains(&"c".to_string()));
87 assert!(names.contains(&"d".to_string()));
88 assert!(!names.contains(&"plain".to_string()));
89 }
90
91 #[test]
92 fn respects_max_depth() {
93 let tmp = TempDir::new().unwrap();
94 let root = tmp.path();
95 fs::create_dir_all(root.join("l1/l2/l3/deep/.git")).unwrap();
96 let shallow = find_repos(root, 1);
97 assert!(shallow.is_empty(), "depth 1 should not reach l1/l2/l3/deep");
98 let deep = find_repos(root, 4);
99 assert_eq!(deep.len(), 1);
100 }
101
102 #[test]
103 fn skips_hidden_directories() {
104 let tmp = TempDir::new().unwrap();
105 let root = tmp.path();
106 fs::create_dir_all(root.join(".hidden/.git")).unwrap();
107 fs::create_dir_all(root.join("visible/.git")).unwrap();
108 let repos = find_repos(root, 4);
109 assert_eq!(repos.len(), 1);
110 }
111
112 #[test]
113 fn does_not_descend_into_a_repo() {
114 let tmp = TempDir::new().unwrap();
117 let root = tmp.path();
118 fs::create_dir_all(root.join("outer/.git")).unwrap();
119 fs::create_dir_all(root.join("outer/inner/.git")).unwrap();
120 let repos = find_repos(root, 4);
121 assert_eq!(repos.len(), 1);
122 assert!(repos[0].ends_with("outer"));
123 }
124}