use clap::{Arg, ArgMatches, SubCommand};
use commands::{BasicOptions, StaticSubcommand};
use error::Error;
use libpijul::fs_representation::in_repo_root;
use libpijul::status::detailed_status;
use rand;
use std::fs::canonicalize;
use std::io::{stderr, Write};
use std::process::exit;
pub fn invocation() -> StaticSubcommand {
return SubCommand::with_name("diff")
.about("Show what would be recorded if record were called")
.arg(
Arg::with_name("repository")
.long("repository")
.help("The repository to show, defaults to the current directory.")
.takes_value(true)
.required(false),
)
.arg(
Arg::with_name("branch")
.long("branch")
.help("The branch to show, defaults to the current branch.")
.takes_value(true)
.required(false),
)
.arg(
Arg::with_name("prefix")
.help("Prefix to start from")
.takes_value(true)
.multiple(true),
)
.arg(
Arg::with_name("patience")
.long("patience")
.help("Use patience diff instead of the default (Myers diff)")
.conflicts_with("myers")
.takes_value(false),
)
.arg(
Arg::with_name("myers")
.long("myers")
.help("Use Myers diff")
.conflicts_with("patience")
.takes_value(false),
);
}
pub fn run(args: &ArgMatches) -> Result<(), Error> {
let opts = BasicOptions::from_args(args)?;
let repo = opts.open_repo()?;
let mut txn = repo.mut_txn_begin(rand::thread_rng())?;
let prefix = if let Some(prefix) = args.value_of("prefix") {
let p = canonicalize(opts.cwd.join(prefix))?;
opts.repo_root.relativize(&p)?.to_owned()
} else {
in_repo_root().to_owned()
};
let diff_algorithm = if args.is_present("patience") {
libpijul::DiffAlgorithm::Patience
} else {
libpijul::DiffAlgorithm::Myers
};
let status = detailed_status(
&mut txn,
&opts.repo_root,
&opts.branch(),
diff_algorithm,
&prefix,
)?;
super::ask::print_diff(&opts.repo_root, &txn, &opts.cwd, status);
Ok(())
}
pub fn explain(res: Result<(), Error>) {
match res {
Ok(_) => (),
Err(e) => {
write!(stderr(), "error: {}", e).unwrap();
exit(1)
}
}
}