Matches https://github.com/ilyagr/pijul-clone as of now, might become out of sync
use std::path::PathBuf;

use clap::{Parser, ValueHint};
use libpijul::changestore::ChangeStore;
use libpijul::*;

use pijul_repository::*;

#[derive(Parser, Debug)]
pub struct Change {
    /// Use the repository at PATH instead of the current directory
    #[clap(long = "repository", value_name = "PATH", value_hint = ValueHint::DirPath)]
    repo_path: Option<PathBuf>,
    /// The hash of the change to show, or an unambiguous prefix thereof
    #[clap(value_name = "HASH")]
    hash: Option<String>,
}

impl Change {
    pub fn run(self) -> Result<(), anyhow::Error> {
        let repo = Repository::find_root(self.repo_path.clone())?;
        let txn = repo.pristine.txn_begin()?;
        let changes = repo.changes;

        let hash = if let Some(hash) = self.hash {
            if let Some(h) = Hash::from_base32(hash.as_bytes()) {
                h
            } else {
                txn.hash_from_prefix(&hash)?.0
            }
        } else {
            let channel_name = txn.current_channel().unwrap_or(libpijul::DEFAULT_CHANNEL);
            let channel = if let Some(channel) = txn.load_channel(&channel_name)? {
                channel
            } else {
                return Ok(());
            };
            let channel = channel.read();
            if let Some(h) = txn.reverse_log(&*channel, None)?.next() {
                (h?.1).0.into()
            } else {
                return Ok(());
            }
        };
        let change = changes.get_change(&hash)?;
        let colors = super::diff::is_colored(repo.config.pager.as_ref());
        change.write(
            &changes,
            Some(hash),
            true,
            super::diff::Colored {
                w: termcolor::StandardStream::stdout(termcolor::ColorChoice::Auto),
                colors,
            },
        )?;
        Ok(())
    }
}