use clap::{SubCommand, ArgMatches, Arg};
use super::{BasicOptions, StaticSubcommand, set_current_branch, get_current_branch, default_explain};
use error::{ErrorKind, Result};
use libpijul::patch::UnsignedPatch;
use rand;
pub fn invocation() -> StaticSubcommand {
return SubCommand::with_name("checkout")
.about("Change the current branch")
.arg(Arg::with_name("repository")
.long("repository")
.help("Local repository.")
.takes_value(true)
)
.arg(Arg::with_name("branch")
.help("Branch to switch to.")
.takes_value(true)
)
}
pub fn run(args: &ArgMatches) -> Result<()> {
let opts = BasicOptions::from_args(args)?;
if let Some(branch) = args.value_of("branch") {
let repo = opts.open_repo()?;
let mut txn = try!(repo.mut_txn_begin(rand::thread_rng()));
if txn.get_branch(branch).is_some() {
txn.output_repository(&branch, &opts.repo_root,
&UnsignedPatch::empty().leave_unsigned(), &Vec::new())?;
txn.commit()?;
set_current_branch(&opts.repo_root, branch)?;
Ok(())
} else {
Err(ErrorKind::NoSuchBranch.into())
}
} else {
println!("Current branch: {:?}", get_current_branch(&opts.repo_root)?);
Ok(())
}
}
pub fn explain(res: Result<()>) {
default_explain(res)
}