use std::path::PathBuf;
use clap::Clap;
use libpijul::{MutTxnT, MutTxnTExt, TxnT};
use log::debug;
use crate::repository::Repository;
#[derive(Clap, Debug)]
pub struct Fork {
#[clap(long = "repository")]
repo_path: Option<PathBuf>,
#[clap(long = "channel", conflicts_with = "change")]
channel: Option<String>,
#[clap(long = "change", conflicts_with = "channel")]
change: Option<String>,
to: String,
}
impl Fork {
pub fn run(self) -> Result<(), anyhow::Error> {
let repo = Repository::find_root(self.repo_path)?;
debug!("{:?}", repo.config);
let mut txn = repo.pristine.mut_txn_begin();
if let Some(ref ch) = self.change {
let (hash, _) = txn.hash_from_prefix(ch)?;
let mut channel = txn.open_or_create_channel(&self.to)?;
txn.apply_change_rec(&repo.changes, &mut channel, hash)?
} else {
let channel_name = repo.config.get_current_channel(self.channel.as_ref());
if let Some(channel) = txn.load_channel(channel_name) {
txn.fork(&channel, &self.to)?;
}
}
txn.commit()?;
Ok(())
}
}