pijul 1.0.0-alpha

The sound distributed version control system.
use crate::repository::Repository;
use crate::Error;
use libanu::changestore::*;
use libanu::pristine::Base32;
use libanu::TxnTExt;
use std::path::PathBuf;

#[derive(Clap, Debug)]
pub struct Log {
    #[clap(long = "repository")]
    repo_path: Option<PathBuf>,
    #[clap(long = "channel")]
    channel: Option<String>,
    #[clap(long = "hash-only")]
    hash_only: bool,
    #[clap(long = "states")]
    states: bool,
}

impl Log {
    pub fn run(self) -> Result<(), anyhow::Error> {
        let repo = Repository::find_root(self.repo_path)?;
        let txn = repo.pristine.txn_begin()?;
        use libanu::pristine::TxnT;
        let channel_name = repo.config.get_current_channel(self.channel.as_ref());
        let channel = if let Some(channel) = txn.load_channel(channel_name) {
            channel
        } else {
            return Err((Error::NoSuchChannel {
                channel: channel_name.to_string(),
            })
            .into());
        };
        let changes = repo.changes;
        if self.hash_only {
            for h in libanu::change::full_dependencies(&txn, &channel).0 {
                println!("{}", h.to_base32())
            }
        } else {
            let states = self.states;
            /* || {
                if let Ok(global) = crate::config::Global::load() {
                    global.log_states
                } else {
                    false
                }
            };*/
            for (_, (h, mrk)) in txn.reverse_log(&channel.borrow(), None) {
                let change = changes.get_change(&h)?;
                println!("Change {}", h.to_base32());
                println!("Author: {:?}", change.header.authors);
                println!("Date: {}", change.header.timestamp);
                if states {
                    println!("State: {}", mrk.to_base32());
                }
                println!("\n    {}\n", change.header.message);
            }
        }
        Ok(())
    }
}