pijul 0.12.2

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

use super::{default_explain, BasicOptions, StaticSubcommand};

pub fn invocation() -> StaticSubcommand {
    return SubCommand::with_name("prune")
        .about("Delete a branch in the local repository")
        .arg(
            Arg::with_name("repository")
                .long("repository")
                .help("Local repository.")
                .takes_value(true),
        )
        .arg(
            Arg::with_name("branch")
                .help("Branch to delete.")
                .takes_value(true)
                .required(true),
        );
}

pub fn run(args: &ArgMatches) -> Result<(), Error> {
    debug!("args {:?}", args);
    let opts = BasicOptions::from_args(args)?;
    let branch = args.value_of("branch").unwrap();
    let current_branch = opts.repo_root.get_current_branch()?;
    if current_branch == branch {
        return Err(Error::CannotDeleteCurrentBranch);
    }
    let repo = opts.open_repo()?;
    let mut txn = repo.mut_txn_begin(rand::thread_rng())?;
    let at_least_two_branches = {
        let mut it = txn.iter_branches(None);
        it.next();
        it.next().is_some()
    };
    if at_least_two_branches {
        if !txn.drop_branch(&branch)? {
            return Err(Error::NoSuchBranch);
        };
        txn.commit()?;
        Ok(())
    } else {
        if txn.get_branch(&branch).is_none() {
            Err(Error::NoSuchBranch)
        } else {
            Err(Error::CannotDeleteCurrentBranch)
        }
    }
}

pub fn explain(res: Result<(), Error>) {
    default_explain(res)
}