pijul 0.3.2

A patch-based distributed version control system, easy to use and fast. Command-line interface.
use std::path::Path;
use clap::{SubCommand, Arg, ArgMatches};

use commands;
use libpijul::fs_representation::{pristine_dir, find_repo_root, repo_dir};
use libpijul::{Repository};
use error::Error;
use commands::get_wd;
use std::fs::File;
use super::{get_current_branch};

pub struct Params<'a> {
    pub repository: Option<&'a Path>,
    pub debug: bool
}

pub fn invocation() -> commands::StaticSubcommand {
    return SubCommand::with_name("info")
        .about("Get information about the current repository, if any")
        .arg(Arg::with_name("dir")
             .help("Pijul info will be given about this directory.")
             .required(false))
        .arg(Arg::with_name("debug")
             .long("--debug")
             .help("Pijul info will be given about this directory.")
             .required(false));

}

pub fn parse_args<'a>(args: &'a ArgMatches) -> Params<'a> {
    Params {
        repository: args.value_of("dir").and_then(|x| Some(Path::new(x))),
        debug: args.is_present("debug")
    }
}

pub fn run(args: &Params) -> Result<(), Error> {
    let wd = try!(get_wd(args.repository));
    match find_repo_root(&wd) {
        Some(ref r) => {
            println!("Current repository root: {:?}", r);
            let repo_dir = repo_dir(r);
            println!("Current branch: {:?}", get_current_branch(&repo_dir)?);
            if args.debug {
                let pristine_dir = pristine_dir(r);
                let repo = try!(Repository::open(&pristine_dir, None).map_err(Error::Repository));
                let txn = repo.txn_begin()?;
                txn.dump("dump");
                for (branch, _) in txn.iter_branches(None) {
                    let mut f = try!(File::create(format!("debug_{}", branch.as_str())));
                    txn.debug(branch.as_str(), &mut f);
                }
            }
            Ok(())
        }
        None => Err(Error::NotInARepository),
    }
}