Skip to main content

chronicle/cli/
history.rs

1use crate::error::Result;
2use crate::git::CliOps;
3use crate::read::history::{build_timeline, HistoryQuery};
4
5pub fn run(
6    path: String,
7    anchor: Option<String>,
8    limit: u32,
9    format: String,
10    compact: bool,
11) -> Result<()> {
12    let repo_dir = std::env::current_dir().map_err(|e| crate::error::ChronicleError::Io {
13        source: e,
14        location: snafu::Location::default(),
15    })?;
16    let git_ops = CliOps::new(repo_dir);
17
18    let query = HistoryQuery {
19        file: path,
20        anchor,
21        limit,
22    };
23
24    let result =
25        build_timeline(&git_ops, &query).map_err(|e| crate::error::ChronicleError::Git {
26            source: e,
27            location: snafu::Location::default(),
28        })?;
29
30    let json = if compact {
31        let compact_out = serde_json::json!({
32            "timeline": result.timeline,
33        });
34        serde_json::to_string_pretty(&compact_out)
35    } else if format == "pretty" {
36        serde_json::to_string_pretty(&result)
37    } else {
38        serde_json::to_string(&result)
39    }
40    .map_err(|e| crate::error::ChronicleError::Json {
41        source: e,
42        location: snafu::Location::default(),
43    })?;
44
45    println!("{json}");
46    Ok(())
47}