reponest 0.1.0-alpha

A TUI/CLI tool for managing multiple git repositories written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! This module provides asynchronous directory traversal to discover Git repositories.

use anyhow::Result;
use std::path::PathBuf;

use crate::config::AppConfig;

/// Scan a single directory for Git repositories
pub async fn scan_directory(base_path: &str, cfg: &AppConfig) -> Result<Vec<PathBuf>> {
    let base = PathBuf::from(base_path);
    let mut paths = Vec::new();
    scan_recursive(base, cfg, 0, &mut paths).await?;
    Ok(paths)
}

/// Scan multiple directories for Git repositories
pub async fn scan_directories(base_paths: &[String], cfg: &AppConfig) -> Result<Vec<PathBuf>> {
    let mut all_paths = Vec::new();
    for base in base_paths {
        if let Ok(mut paths) = scan_directory(base, cfg).await {
            all_paths.append(&mut paths);
        }
    }
    Ok(all_paths)
}

/// Recursively traverse directory tree to find Git repositories
fn scan_recursive<'a>(
    path: PathBuf,
    cfg: &'a AppConfig,
    depth: usize,
    paths: &'a mut Vec<PathBuf>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
    Box::pin(async move {
        if cfg.main.max_depth > 0 && depth >= cfg.main.max_depth {
            return Ok(());
        }

        let mut entries = tokio::fs::read_dir(&path).await?;

        while let Some(entry) = entries.next_entry().await? {
            let entry_path = entry.path();
            if !entry_path.is_dir() {
                continue;
            }

            let file_name = entry_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("");

            // If we find a .git directory, record the parent as a Git repository.
            // After that, we will continue scanning other directories, thus finding nested repos.
            if file_name == ".git" {
                if let Some(repo_path) = entry_path.parent() {
                    paths.push(repo_path.to_path_buf());
                }
                continue;
            }

            if is_excluded(file_name, &cfg.internal.exclude_dirs) {
                continue;
            }
            let _ = scan_recursive(entry_path, cfg, depth + 1, paths).await;
        }

        Ok(())
    })
}

/// Check if a directory should be excluded from scanning
#[inline]
fn is_excluded(dir_name: &str, exclude_patterns: &[String]) -> bool {
    // Skip all hidden directories
    if dir_name.starts_with('.') {
        return true;
    }

    exclude_patterns
        .iter()
        .any(|pattern| matches_wildcard(dir_name, pattern))
}

/// Match a name against a pattern with wildcard support
#[inline]
fn matches_wildcard(name: &str, pattern: &str) -> bool {
    if !pattern.contains('*') {
        return name == pattern;
    }

    let parts: Vec<&str> = pattern.split('*').collect();

    match parts.len() {
        1 => true, // pattern is just "*"
        2 => {
            let (prefix, suffix) = (parts[0], parts[1]);
            match (prefix.is_empty(), suffix.is_empty()) {
                (true, false) => name.ends_with(suffix),   // "*suffix"
                (false, true) => name.starts_with(prefix), // "prefix*"
                (false, false) => {
                    // "prefix*suffix"
                    name.starts_with(prefix)
                        && name.ends_with(suffix)
                        && name.len() >= prefix.len() + suffix.len()
                }
                (true, true) => true, // "*"
            }
        }
        _ => name == pattern, // complex patterns fallback to exact match
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    /// Create a test git repository
    fn create_git_repo(path: &std::path::Path) {
        fs::create_dir_all(path).unwrap();
        fs::create_dir_all(path.join(".git")).unwrap();
        fs::write(path.join(".git/config"), "[core]").unwrap();
    }

    /// Create a regular directory (not a git repo)
    fn create_dir(path: &std::path::Path) {
        fs::create_dir_all(path).unwrap();
    }

    #[tokio::test]
    async fn test_scan_empty_directory() {
        let temp_dir = TempDir::new().unwrap();
        let config = AppConfig::default();

        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        assert_eq!(result.len(), 0);
    }

    #[tokio::test]
    async fn test_scan_single_repo() {
        let temp_dir = TempDir::new().unwrap();
        let repo_path = temp_dir.path().join("repo1");
        create_git_repo(&repo_path);

        let config = AppConfig::default();
        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0], repo_path);
    }

    #[tokio::test]
    async fn test_scan_multiple_repos() {
        let temp_dir = TempDir::new().unwrap();

        // Create 3 repos
        for i in 1..=3 {
            let repo_path = temp_dir.path().join(format!("repo{}", i));
            create_git_repo(&repo_path);
        }

        let config = AppConfig::default();
        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        assert_eq!(result.len(), 3);
    }

    #[tokio::test]
    async fn test_scan_nested_repos() {
        let temp_dir = TempDir::new().unwrap();

        // Create nested structure: parent/child1 and parent/child2
        let parent = temp_dir.path().join("parent");
        create_git_repo(&parent);

        let child1 = parent.join("child1");
        create_git_repo(&child1);

        let child2 = parent.join("child2");
        create_git_repo(&child2);

        let config = AppConfig::default();
        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        // Should find all 3 repos
        assert_eq!(result.len(), 3);
    }

    #[tokio::test]
    async fn test_scan_with_max_depth() {
        let temp_dir = TempDir::new().unwrap();

        // Create structure with depth: temp_dir/level1/level2/level3
        let level1 = temp_dir.path().join("level1");
        create_git_repo(&level1);

        let level2 = level1.join("level2");
        create_git_repo(&level2);

        let level3 = level2.join("level3");
        create_git_repo(&level3);

        // Test with max_depth = 2 (should find only level1, depth 1 from base)
        let mut config = AppConfig::default();
        config.main.max_depth = 2;

        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        assert_eq!(result.len(), 1);
        assert!(result[0].ends_with("level1"));

        // Test with max_depth = 3 (should find level1 and level2)
        config.main.max_depth = 3;
        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        assert_eq!(result.len(), 2);

        // Test with max_depth = 0 (unlimited)
        config.main.max_depth = 0;
        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        assert_eq!(result.len(), 3);
    }

    #[tokio::test]
    async fn test_scan_excludes_hidden_dirs() {
        let temp_dir = TempDir::new().unwrap();

        // Create regular repo
        let normal_repo = temp_dir.path().join("normal");
        create_git_repo(&normal_repo);

        // Create repo in hidden directory (should be excluded)
        let hidden_dir = temp_dir.path().join(".hidden");
        create_git_repo(&hidden_dir);

        let config = AppConfig::default();
        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        // Should only find the normal repo, not the hidden one
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], normal_repo);
    }

    #[tokio::test]
    async fn test_scan_with_exclude_patterns() {
        let temp_dir = TempDir::new().unwrap();

        // Create various repos
        create_git_repo(&temp_dir.path().join("repo1"));
        create_git_repo(&temp_dir.path().join("node_modules"));
        create_git_repo(&temp_dir.path().join("target"));
        create_git_repo(&temp_dir.path().join("build"));

        // Configure exclusions
        let mut config = AppConfig::default();
        config.internal.exclude_dirs = vec![
            "node_modules".to_string(),
            "target".to_string(),
            "build".to_string(),
        ];

        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        // Should only find repo1
        assert_eq!(result.len(), 1);
        assert!(result[0].ends_with("repo1"));
    }

    #[tokio::test]
    async fn test_scan_ignores_non_git_dirs() {
        let temp_dir = TempDir::new().unwrap();

        // Create git repo
        let repo_path = temp_dir.path().join("repo");
        create_git_repo(&repo_path);

        // Create non-git directories
        create_dir(&temp_dir.path().join("not_a_repo"));
        create_dir(&temp_dir.path().join("another_dir"));

        let config = AppConfig::default();
        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        // Should only find the actual git repo
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], repo_path);
    }

    #[tokio::test]
    async fn test_scan_multiple_directories() {
        let temp_dir1 = TempDir::new().unwrap();
        let temp_dir2 = TempDir::new().unwrap();

        // Create repos in both directories
        create_git_repo(&temp_dir1.path().join("repo1"));
        create_git_repo(&temp_dir1.path().join("repo2"));
        create_git_repo(&temp_dir2.path().join("repo3"));

        let paths = vec![
            temp_dir1.path().to_str().unwrap().to_string(),
            temp_dir2.path().to_str().unwrap().to_string(),
        ];

        let config = AppConfig::default();
        let result = scan_directories(&paths, &config).await.unwrap();

        assert_eq!(result.len(), 3);
    }

    #[test]
    fn test_matches_wildcard_exact() {
        assert!(matches_wildcard("node_modules", "node_modules"));
        assert!(!matches_wildcard("node_modules", "target"));
    }

    #[test]
    fn test_matches_wildcard_prefix() {
        assert!(matches_wildcard("test_file", "test*"));
        assert!(matches_wildcard("test", "test*"));
        assert!(!matches_wildcard("other", "test*"));
    }

    #[test]
    fn test_matches_wildcard_suffix() {
        assert!(matches_wildcard("file.txt", "*.txt"));
        assert!(matches_wildcard(".txt", "*.txt"));
        assert!(!matches_wildcard("file.rs", "*.txt"));
    }

    #[test]
    fn test_matches_wildcard_prefix_suffix() {
        assert!(matches_wildcard("test_file.txt", "test*.txt"));
        assert!(matches_wildcard("test.txt", "test*.txt"));
        assert!(!matches_wildcard("other_file.txt", "test*.txt"));
        assert!(!matches_wildcard("test", "test*.txt"));
    }

    #[test]
    fn test_matches_wildcard_star_only() {
        assert!(matches_wildcard("anything", "*"));
        assert!(matches_wildcard("", "*"));
    }

    #[test]
    fn test_is_excluded_hidden_dirs() {
        let patterns = vec![];
        assert!(is_excluded(".hidden", &patterns));
        assert!(is_excluded(".git", &patterns));
        assert!(!is_excluded("normal", &patterns));
    }

    #[test]
    fn test_is_excluded_with_patterns() {
        let patterns = vec![
            "node_modules".to_string(),
            "target".to_string(),
            "*.tmp".to_string(),
        ];

        assert!(is_excluded("node_modules", &patterns));
        assert!(is_excluded("target", &patterns));
        assert!(is_excluded("file.tmp", &patterns));
        assert!(!is_excluded("src", &patterns));
    }

    #[tokio::test]
    async fn test_scan_complex_structure() {
        let temp_dir = TempDir::new().unwrap();

        // Create a complex structure mimicking real projects
        // project1/
        //   .git/
        //   src/
        //   subproject/
        //     .git/
        // project2/
        //   .git/

        let project1 = temp_dir.path().join("project1");
        create_git_repo(&project1);
        create_dir(&project1.join("src"));

        let subproject = project1.join("subproject");
        create_git_repo(&subproject);

        let project2 = temp_dir.path().join("project2");
        create_git_repo(&project2);
        create_dir(&project2.join("build"));

        let config = AppConfig::default();
        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        // Should find project1, subproject, and project2
        assert_eq!(result.len(), 3);
    }

    #[tokio::test]
    async fn test_scan_deep_nesting() {
        let temp_dir = TempDir::new().unwrap();

        // Create nested structure at moderate depth
        let level1 = temp_dir.path().join("a");
        let level2 = level1.join("b");
        let level3 = level2.join("c");

        fs::create_dir_all(&level3).unwrap();
        let repo_path = level3.join("deep_repo");
        create_git_repo(&repo_path);

        // Test with no max_depth (should find it)
        let config = AppConfig::default();
        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0], repo_path);

        // Test with max_depth = 2 (should not find it at depth 4)
        let mut config = AppConfig::default();
        config.main.max_depth = 2;
        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        assert_eq!(result.len(), 0);

        // Test with max_depth = 5 (should find it)
        let mut config = AppConfig::default();
        config.main.max_depth = 5;
        let result = scan_directory(temp_dir.path().to_str().unwrap(), &config)
            .await
            .unwrap();

        assert_eq!(result.len(), 1);
    }
}