Skip to main content

chronicle/cli/
sync.rs

1use std::path::PathBuf;
2
3use crate::error::Result;
4use crate::sync::{enable_sync, get_sync_config, get_sync_status, pull_notes};
5
6/// Run `git chronicle sync enable`.
7pub fn run_enable(remote: &str) -> Result<()> {
8    let repo_dir = repo_dir()?;
9    enable_sync(&repo_dir, remote)?;
10    eprintln!("sync enabled for remote '{remote}'");
11    eprintln!("  push refspec:  refs/notes/chronicle -> {remote}");
12    eprintln!("  fetch refspec: +refs/notes/chronicle:refs/notes/chronicle");
13    Ok(())
14}
15
16/// Run `git chronicle sync status`.
17pub fn run_status(remote: &str) -> Result<()> {
18    let repo_dir = repo_dir()?;
19    let config = get_sync_config(&repo_dir, remote)?;
20    let status = get_sync_status(&repo_dir, remote)?;
21
22    if config.is_enabled() {
23        println!("Notes sync: enabled");
24        if let Some(ref push) = config.push_refspec {
25            println!("  Push refspec:  {push} -> {remote}");
26        }
27        if let Some(ref fetch) = config.fetch_refspec {
28            println!("  Fetch refspec: {fetch}");
29        }
30    } else {
31        println!("Notes sync: not configured");
32        println!("  Run `git chronicle sync enable` to set up sync.");
33    }
34
35    println!("  Local notes:   {} annotated commits", status.local_count);
36    if let Some(rc) = status.remote_count {
37        println!(
38            "  Remote notes:  {} annotated commits ({} not yet pushed)",
39            rc, status.unpushed_count
40        );
41    } else {
42        println!("  Remote notes:  unknown (remote unreachable)");
43    }
44
45    Ok(())
46}
47
48/// Run `git chronicle sync pull`.
49pub fn run_pull(remote: &str) -> Result<()> {
50    let repo_dir = repo_dir()?;
51    pull_notes(&repo_dir, remote)?;
52    eprintln!("fetched notes from '{remote}'");
53    Ok(())
54}
55
56fn repo_dir() -> Result<PathBuf> {
57    std::env::current_dir().map_err(|e| crate::error::ChronicleError::Io {
58        source: e,
59        location: snafu::Location::default(),
60    })
61}