pijul_org / pijul
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)
}