mod report;
use std::collections::HashMap;
use std::error::Error;
use std::path::{Path, PathBuf};
use crate::git::GitRepo;
use crate::util::parse_since;
use crate::walk::{self, WalkConfig};
use report::{print_json, print_report};
pub struct FileHotspot {
pub path: PathBuf,
pub language: String,
pub commits: usize,
pub complexity: usize,
pub score: usize,
}
fn compute_complexity(
file_path: &Path,
spec: &crate::loc::language::LanguageSpec,
metric: &str,
) -> Result<Option<usize>, Box<dyn Error>> {
match metric {
"cycom" => match crate::cycom::analyze_file(file_path, spec)? {
Some(c) => Ok(Some(c.total_complexity)),
None => Ok(None),
},
"cogcom" => match crate::cogcom::analyze_file(file_path, spec)? {
Some(c) => Ok(Some(c.total_complexity)),
None => Ok(None),
},
_ => match crate::indent::analyze_file(file_path, spec)? {
Some(m) => Ok(Some(m.total_indent)),
None => Ok(None),
},
}
}
pub fn run(
cfg: &WalkConfig<'_>,
json: bool,
top: usize,
sort_by: &str,
since: Option<&str>,
complexity_metric: &str,
) -> Result<(), Box<dyn Error>> {
let git_repo = GitRepo::open(cfg.path)
.map_err(|e| format!("not a git repository (or any parent): {e}"))?;
let since_ts = since.map(parse_since).transpose()?;
let freqs = git_repo.file_frequencies(since_ts)?;
if freqs.is_empty() {
if since.is_some() {
eprintln!("No commits found in the specified time range.");
} else {
eprintln!("No commits found in the repository.");
}
return Ok(());
}
let freq_map: HashMap<PathBuf, usize> =
freqs.into_iter().map(|f| (f.path, f.commits)).collect();
let (walk_root, walk_prefix) = git_repo.walk_prefix(cfg.path)?;
let mut results: Vec<FileHotspot> = Vec::new();
for (file_path, spec) in walk::source_files(&walk_root, cfg.exclude_tests(), cfg.filter) {
let rel_path = GitRepo::to_git_path(&walk_root, &walk_prefix, &file_path);
let commits = match freq_map.get(&rel_path) {
Some(&c) => c,
None => continue,
};
let complexity = match compute_complexity(&file_path, spec, complexity_metric) {
Ok(Some(c)) => c,
Ok(None) => continue,
Err(err) => {
eprintln!("warning: {}: {err}", file_path.display());
continue;
}
};
let score = commits * complexity;
results.push(FileHotspot {
path: rel_path,
language: spec.name.to_string(),
commits,
complexity,
score,
});
}
match sort_by {
"commits" => results.sort_by(|a, b| b.commits.cmp(&a.commits)),
"complexity" => results.sort_by(|a, b| b.complexity.cmp(&a.complexity)),
_ => results.sort_by(|a, b| b.score.cmp(&a.score)),
}
results.truncate(top);
if json {
print_json(&results, complexity_metric)?;
} else {
print_report(&results, complexity_metric);
}
Ok(())
}
#[cfg(test)]
#[path = "mod_test.rs"]
mod tests;