barad-dur 0.18.0

The all-seeing repository analyzer
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
use anyhow::{bail, Result};
use std::collections::{HashMap, HashSet};
use std::io::{self, BufRead, IsTerminal, Write};
use std::path::Path;

use crate::cache::storage::CACHE_DIR;
use crate::collector::Collector;
use crate::snapshot::TimeWindow;

const CONFIG_FILE: &str = "barad-dur.toml";

/// Translation/resource file extensions to detect.
const TRANSLATION_EXTENSIONS: &[&str] = &[
    "resx", "po", "pot", "xlf", "xliff", "strings", "arb", "lproj",
];

/// Generated code patterns to detect.
const GENERATED_PATTERNS: &[&str] = &[".generated.", ".designer.", ".g.cs"];

/// Vendored/third-party directory names.
const VENDOR_DIRS: &[&str] = &["vendor", "third_party", "node_modules", "bower_components"];

/// i18n-related directory names.
const I18N_DIRS: &[&str] = &["i18n", "l10n", "locales", "translations"];

/// Results of scanning the repo for smart defaults.
#[derive(Debug, Default)]
pub struct ScanResult {
    pub exclude_patterns: Vec<(String, usize)>,
    pub total_files: usize,
    pub total_commits: usize,
    pub distinct_authors: usize,
    pub suggest_skip_blame: bool,
}

/// Detect exclude patterns from a list of file paths.
pub fn detect_exclude_patterns(file_paths: &[&str]) -> Vec<String> {
    let mut patterns: Vec<String> = Vec::new();
    let mut seen: HashSet<String> = HashSet::new();

    // Count files by translation extension
    let mut ext_counts: HashMap<String, usize> = HashMap::new();
    for path in file_paths {
        if let Some(ext) = Path::new(path).extension().and_then(|e| e.to_str()) {
            let ext_lower = ext.to_lowercase();
            if TRANSLATION_EXTENSIONS.contains(&ext_lower.as_str()) {
                *ext_counts.entry(ext_lower).or_insert(0) += 1;
            }
        }
    }
    for ext in ext_counts.keys() {
        let pattern = format!("*.{}", ext);
        if seen.insert(pattern.clone()) {
            patterns.push(pattern);
        }
    }

    // Detect generated code patterns
    for gen_pat in GENERATED_PATTERNS {
        if file_paths.iter().any(|p| p.contains(gen_pat)) {
            let pattern = format!("*{}*", gen_pat);
            if seen.insert(pattern.clone()) {
                patterns.push(pattern);
            }
        }
    }

    // Detect vendor directories
    for dir in VENDOR_DIRS {
        if file_paths
            .iter()
            .any(|p| p.starts_with(&format!("{}/", dir)) || p.contains(&format!("/{}/", dir)))
        {
            let pattern = format!("{}/**", dir);
            if seen.insert(pattern.clone()) {
                patterns.push(pattern);
            }
        }
    }

    // Detect i18n directories
    for dir in I18N_DIRS {
        if file_paths
            .iter()
            .any(|p| p.contains(&format!("/{}/", dir)) || p.starts_with(&format!("{}/", dir)))
        {
            let pattern = format!("**/{}/**", dir);
            if seen.insert(pattern.clone()) {
                patterns.push(pattern);
            }
        }
    }

    patterns
}

/// Scan a repository and return smart defaults.
pub fn scan_repo(repo_path: &Path) -> Result<ScanResult> {
    let collector = Collector::open(repo_path, TimeWindow::default())?;
    let files = collector.collect_files()?;
    let collection = collector.collect_commits()?;

    let file_paths: Vec<String> = files
        .iter()
        .map(|f| f.path.to_string_lossy().to_string())
        .collect();
    let file_refs: Vec<&str> = file_paths.iter().map(|s| s.as_str()).collect();

    let raw_patterns = detect_exclude_patterns(&file_refs);

    // Count files matching each pattern
    let exclude_patterns: Vec<(String, usize)> = raw_patterns
        .into_iter()
        .map(|pattern| {
            let count = file_refs
                .iter()
                .filter(|p| glob_match::glob_match(&pattern, p))
                .count();
            (pattern, count)
        })
        .filter(|(_, count)| *count > 0)
        .collect();

    Ok(ScanResult {
        exclude_patterns,
        total_files: files.len(),
        total_commits: collection.commits.len(),
        distinct_authors: collection.authors.len(),
        suggest_skip_blame: files.len() > 10_000,
    })
}

/// Generate the TOML config string from scan results.
pub fn generate_toml(scan: &ScanResult) -> String {
    generate_toml_inner(scan, "6months", true, "cli", false)
}

fn generate_toml_inner(
    scan: &ScanResult,
    since: &str,
    use_detected_excludes: bool,
    format: &str,
    auto_open: bool,
) -> String {
    let mut out = String::new();

    out.push_str("# .repository-analysis/barad-dur.toml — Barad-dur repository configuration\n");
    out.push_str(
        "# All barad-dur files live in .repository-analysis/ (config, blame cache, history).\n",
    );
    out.push_str("# Generated by `barad-dur init`. Edit freely.\n");
    out.push_str("# CLI flags override these values when explicitly passed.\n\n");

    // [analysis]
    out.push_str("# ──────────────────────────────────────────────────\n");
    out.push_str("# Analysis scope\n");
    out.push_str("# ──────────────────────────────────────────────────\n");
    out.push_str("[analysis]\n\n");
    out.push_str("# How far back to look at git history.\n");
    out.push_str(&format!("since = \"{}\"\n", since));
    if scan.suggest_skip_blame {
        out.push_str(&format!(
            "\n# Large repo ({} files) — consider skip_blame = true for faster iteration\n",
            scan.total_files
        ));
    }
    out.push_str("skip_blame = false\n\n");

    // [exclude]
    out.push_str("# ──────────────────────────────────────────────────\n");
    out.push_str("# File exclusions\n");
    out.push_str("# ──────────────────────────────────────────────────\n");
    out.push_str("[exclude]\n\n");
    out.push_str("use_defaults = true\n");

    if use_detected_excludes && !scan.exclude_patterns.is_empty() {
        out.push_str("\n# Detected patterns:\n");
        for (pattern, count) in &scan.exclude_patterns {
            out.push_str(&format!("#   {} ({} files)\n", pattern, count));
        }
        let patterns: Vec<String> = scan
            .exclude_patterns
            .iter()
            .map(|(p, _)| format!("\"{}\"", p))
            .collect();
        out.push_str(&format!("patterns = [{}]\n\n", patterns.join(", ")));
    } else {
        out.push_str("patterns = []\n\n");
    }

    // [weights]
    out.push_str("# ──────────────────────────────────────────────────\n");
    out.push_str("# Category weights (must sum to 100)\n");
    out.push_str("# ──────────────────────────────────────────────────\n");
    out.push_str("[weights]\n\n");
    if scan.distinct_authors > 0 {
        out.push_str(&format!(
            "# {} distinct authors detected in the time window\n",
            scan.distinct_authors
        ));
    }
    out.push_str("health    = 30\n");
    out.push_str("team      = 30\n");
    out.push_str("evolution = 20\n");
    out.push_str("hygiene   = 20\n\n");

    // [thresholds]
    out.push_str("# ──────────────────────────────────────────────────\n");
    out.push_str("# Scoring thresholds\n");
    out.push_str("# ──────────────────────────────────────────────────\n\n");
    out.push_str("[thresholds.health]\n");
    out.push_str("max_complexity       = 20\n");
    out.push_str("hotspot_top_n        = 10\n");
    out.push_str("coupling_min_commits = 5\n\n");
    out.push_str("[thresholds.team]\n");
    out.push_str("silo_max_owners      = 1\n");
    out.push_str("activity_window_days = 30\n\n");
    out.push_str("[thresholds.evolution]\n");
    out.push_str("growth_baseline_months = 3\n");
    out.push_str("refactor_ratio_target  = 0.1\n\n");
    out.push_str("[thresholds.hygiene]\n");
    out.push_str("min_message_length = 10\n");
    out.push_str("max_message_length = 72\n\n");

    // [output]
    out.push_str("# ──────────────────────────────────────────────────\n");
    out.push_str("# Output preferences\n");
    out.push_str("# ──────────────────────────────────────────────────\n");
    out.push_str("[output]\n\n");
    out.push_str(&format!("format = \"{}\"\n", format));
    out.push_str(&format!("auto_open = {}\n", auto_open));

    out
}

fn prompt(question: &str, default: &str) -> String {
    eprint!("     ? {} [{}]: ", question, default);
    let _ = io::stderr().flush();
    let mut input = String::new();
    if io::stdin().lock().read_line(&mut input).is_err() {
        return default.to_string(); // gracefully return default on I/O failure
    }
    let trimmed = input.trim();
    if trimmed.is_empty() {
        default.to_string()
    } else {
        trimmed.to_string()
    }
}

fn prompt_yn(question: &str, default_yes: bool) -> bool {
    let hint = if default_yes { "Y/n" } else { "y/N" };
    let answer = prompt(question, hint);
    match answer.to_lowercase().as_str() {
        "y" | "yes" => true,
        "n" | "no" => false,
        _ => default_yes,
    }
}

fn run_wizard(scan: &ScanResult) -> Result<String> {
    eprintln!("\n  barad-dur config wizard");
    eprintln!("  ───────────────────────\n");

    let mode = prompt("Configuration mode: [S]imple or [A]dvanced", "S");
    let advanced = mode.to_lowercase().starts_with('a');

    if advanced {
        run_advanced_wizard(scan)
    } else {
        run_simple_wizard(scan)
    }
}

fn run_simple_wizard(scan: &ScanResult) -> Result<String> {
    // Q1: Time window
    eprintln!(
        "     Detected: {} commits, {} files",
        scan.total_commits, scan.total_files
    );
    let since = prompt("Analysis window", "6months");

    // Q2: Exclusions
    let use_detected_excludes = if !scan.exclude_patterns.is_empty() {
        eprintln!("     Detected exclude candidates:");
        for (pattern, count) in &scan.exclude_patterns {
            eprintln!("       - {} ({} files)", pattern, count);
        }
        prompt_yn("Exclude these from analysis?", true)
    } else {
        false
    };

    // Q3: Output format
    let format = prompt("Default output format (cli/html/json)", "cli");

    Ok(generate_toml_inner(
        scan,
        &since,
        use_detected_excludes,
        &format,
        false,
    ))
}

fn run_advanced_wizard(scan: &ScanResult) -> Result<String> {
    // Q1: Time window
    eprintln!(
        "     Detected: {} commits, {} files, {} authors",
        scan.total_commits, scan.total_files, scan.distinct_authors
    );
    let since = prompt("Analysis window", "6months");

    // Q2: Exclusions
    let use_detected_excludes = if !scan.exclude_patterns.is_empty() {
        eprintln!("     Detected exclude candidates:");
        for (pattern, count) in &scan.exclude_patterns {
            eprintln!("       - {} ({} files)", pattern, count);
        }
        prompt_yn("Add these exclusions?", true)
    } else {
        false
    };

    // Q3: Weights
    let adjust_weights = prompt_yn("Adjust category weights? (default: 30/30/20/20)", false);
    // For now, just use defaults if they don't want to adjust
    let _ = adjust_weights;

    // Q4: Thresholds
    let _ = prompt_yn(
        "Skip detailed threshold configuration? (use defaults)",
        true,
    );

    // Q5: Output
    let format = prompt("Default output format (cli/html/json)", "cli");
    let auto_open = prompt_yn("Auto-open HTML reports in browser?", false);

    Ok(generate_toml_inner(
        scan,
        &since,
        use_detected_excludes,
        &format,
        auto_open,
    ))
}

/// Run the init command.
pub fn run_init(target: &Path, force: bool, interactive: bool) -> Result<()> {
    let config_path = target.join(CACHE_DIR).join(CONFIG_FILE);
    if config_path.exists() && !force {
        bail!(
            "Config already exists at {}. Use --force to overwrite.",
            config_path.display()
        );
    }

    eprintln!("  Scanning repository...");
    let scan = scan_repo(target)?;

    let toml_content = if interactive && io::stdin().is_terminal() {
        run_wizard(&scan)?
    } else {
        if interactive {
            eprintln!("Warning: stdin is not a terminal, falling back to auto-detect mode.");
        }
        generate_toml(&scan)
    };

    std::fs::create_dir_all(target.join(CACHE_DIR))?;
    std::fs::write(&config_path, &toml_content)?;
    eprintln!("  Config written to {}", config_path.display());
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn detect_translation_extensions() {
        let files = vec!["src/main.rs", "Resources/Strings.resx", "i18n/fr.po"];
        let patterns = detect_exclude_patterns(&files);
        assert!(patterns.iter().any(|p| p.contains("resx")));
        assert!(patterns.iter().any(|p| p.contains("po")));
    }

    #[test]
    fn detect_i18n_directories() {
        let files = vec!["src/assets/i18n/en.ts", "src/assets/i18n/fr.ts"];
        let patterns = detect_exclude_patterns(&files);
        assert!(patterns.iter().any(|p| p.contains("i18n")));
    }

    #[test]
    fn detect_generated_code() {
        let files = vec!["Models/Foo.generated.cs", "Views/Bar.designer.cs"];
        let patterns = detect_exclude_patterns(&files);
        assert!(patterns.iter().any(|p| p.contains("generated")));
    }

    #[test]
    fn detect_vendor_dirs() {
        let files = vec!["vendor/lib/foo.go", "node_modules/pkg/index.js"];
        let patterns = detect_exclude_patterns(&files);
        assert!(patterns.iter().any(|p| p.contains("vendor")));
        assert!(patterns.iter().any(|p| p.contains("node_modules")));
    }

    #[test]
    fn detect_no_false_positives() {
        let files = vec!["src/main.rs", "src/lib.rs", "tests/test.rs"];
        let patterns = detect_exclude_patterns(&files);
        assert!(patterns.is_empty());
    }

    #[test]
    fn generate_toml_is_valid() {
        let scan = ScanResult::default();
        let toml_str = generate_toml(&scan);
        assert!(toml_str.contains("[analysis]"));
        assert!(toml_str.contains("[weights]"));
        assert!(toml_str.contains("since ="));
        assert!(toml_str.contains("[output]"));
        // Verify it parses as valid TOML
        assert!(toml_str.parse::<toml::Value>().is_ok());
    }

    #[test]
    fn generate_toml_with_detected_patterns() {
        let scan = ScanResult {
            exclude_patterns: vec![("*.resx".to_string(), 5), ("**/i18n/**".to_string(), 3)],
            ..Default::default()
        };
        let toml_str = generate_toml(&scan);
        assert!(toml_str.contains("*.resx"));
        assert!(toml_str.contains("**/i18n/**"));
        assert!(toml_str.contains("5 files"));
        // Verify it parses as valid TOML
        assert!(toml_str.parse::<toml::Value>().is_ok());
    }

    #[test]
    fn generate_toml_suggests_skip_blame_for_large_repos() {
        let scan = ScanResult {
            total_files: 15_000,
            suggest_skip_blame: true,
            ..Default::default()
        };
        let toml_str = generate_toml(&scan);
        assert!(toml_str.contains("skip_blame"));
        assert!(toml_str.contains("15000 files"));
    }
}