use clap::{SubCommand, ArgMatches, Arg};
use super::{StaticSubcommand, get_wd};
use error::Error;
use std::path::Path;
use libpijul::{Repository};
use libpijul::fs_representation::{pristine_dir, find_repo_root};
pub fn invocation() -> StaticSubcommand {
return SubCommand::with_name("branches")
.about("List all branches")
.arg(Arg::with_name("repository")
.long("repository")
.help("Local repository.")
.takes_value(true)
)
}
#[derive(Debug)]
pub struct Params<'a> {
pub repository: Option<&'a Path>,
}
pub fn parse_args<'a>(args: &'a ArgMatches) -> Params<'a> {
Params {
repository: args.value_of("repository").map(|x| Path::new(x)),
}
}
pub fn run<'a>(args: &Params<'a>) -> Result<(), Error> {
debug!("args {:?}", args);
let wd = try!(get_wd(args.repository));
match find_repo_root(&wd) {
None => return Err(Error::NotInARepository),
Some(ref repo_root) => {
let repo_dir = pristine_dir(repo_root);
let repo = try!(Repository::open(&repo_dir, None).map_err(Error::Repository));
let txn = repo.txn_begin()?;
for (branch, _) in txn.iter_branches(None) {
println!("{}", branch.as_str());
}
Ok(())
}
}
}