mod code;
mod git;
mod walk;
use std::collections::HashMap;
use std::path::{Component, Path, PathBuf};
use crate::model::{ChurnData, CodeData, Lens, StatusData};
pub use git::{head_short_hash, is_repo};
pub use walk::walk;
pub enum LayerResult {
Code {
files: HashMap<PathBuf, CodeData>,
inaccurate: bool,
},
Churn(HashMap<PathBuf, ChurnData>),
Status(HashMap<PathBuf, StatusData>),
}
impl LayerResult {
pub fn lens(&self) -> Lens {
match self {
LayerResult::Code { .. } => Lens::Code,
LayerResult::Churn(_) => Lens::Churn,
LayerResult::Status(_) => Lens::Status,
}
}
}
pub fn compute(lens: Lens, root: &Path) -> LayerResult {
match lens {
Lens::Code => {
let (files, inaccurate) = code::collect_code(root);
LayerResult::Code { files, inaccurate }
}
Lens::Churn => LayerResult::Churn(git::churn(root)),
Lens::Status => LayerResult::Status(git::status(root)),
Lens::Size => unreachable!("the size lens reads node bytes; it never computes a layer"),
}
}
pub(crate) fn relative_path(path: &Path, root: &Path) -> PathBuf {
let rel = path.strip_prefix(root).unwrap_or(path);
rel.components()
.filter_map(|c| match c {
Component::Normal(s) => Some(s),
_ => None,
})
.collect()
}