|
|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
//! CLI option parsing.
|
|
|
|
|
|
|
|
|
|
use std::{borrow::Borrow, env, ffi::OsString};
|
|
|
|
|
use std::{borrow::Borrow, env, ffi::OsStr, path::PathBuf};
|
|
|
|
|
|
|
|
|
|
use atty::Stream;
|
|
|
|
|
use clap::{crate_authors, crate_description, crate_version, Arg, Command};
|
|
|
|
|
@ -178,6 +178,41 @@ pub enum DisplayMode {
|
|
|
|
|
SideBySideShowBoth,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Debug)]
|
|
|
|
|
pub enum FileArgument {
|
|
|
|
|
NamedPath(std::path::PathBuf),
|
|
|
|
|
Stdin,
|
|
|
|
|
DevNull,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FileArgument {
|
|
|
|
|
/// Return a `FileArgument` representing this command line
|
|
|
|
|
/// argument.
|
|
|
|
|
pub fn from_cli_argument(arg: &OsStr) -> Self {
|
|
|
|
|
if arg == "/dev/null" {
|
|
|
|
|
FileArgument::DevNull
|
|
|
|
|
} else if arg == "-" {
|
|
|
|
|
FileArgument::Stdin
|
|
|
|
|
} else {
|
|
|
|
|
FileArgument::NamedPath(PathBuf::from(arg))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Return a `FileArgument` that always represents a path that
|
|
|
|
|
/// exists.
|
|
|
|
|
pub fn from_path_argument(arg: &OsStr) -> Self {
|
|
|
|
|
FileArgument::NamedPath(PathBuf::from(arg))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn display(&self) -> String {
|
|
|
|
|
match self {
|
|
|
|
|
FileArgument::NamedPath(path) => path.display().to_string(),
|
|
|
|
|
FileArgument::Stdin => "(stdin)".to_string(),
|
|
|
|
|
FileArgument::DevNull => "/dev/null".to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum Mode {
|
|
|
|
|
Diff {
|
|
|
|
|
graph_limit: usize,
|
|
|
|
|
@ -187,11 +222,13 @@ pub enum Mode {
|
|
|
|
|
language_override: Option<guess_language::Language>,
|
|
|
|
|
/// The path where we can read the LHS file. This is often a
|
|
|
|
|
/// temporary file generated by source control.
|
|
|
|
|
lhs_path: OsString,
|
|
|
|
|
lhs_path: FileArgument,
|
|
|
|
|
/// The path where we can read the RHS file. This is often a
|
|
|
|
|
/// temporary file generated by source control.
|
|
|
|
|
rhs_path: OsString,
|
|
|
|
|
/// The path that we should display for the LHS file.
|
|
|
|
|
rhs_path: FileArgument,
|
|
|
|
|
/// The path that we should display for the LHS file. This is
|
|
|
|
|
/// usually the same as `lhs_path`, but the six argument form
|
|
|
|
|
/// of git-diff specifies this separately.
|
|
|
|
|
lhs_display_path: String,
|
|
|
|
|
/// The path that we should display for the RHS file.
|
|
|
|
|
rhs_display_path: String,
|
|
|
|
|
@ -268,8 +305,8 @@ pub fn parse_args() -> Mode {
|
|
|
|
|
[lhs_path, rhs_path] => (
|
|
|
|
|
lhs_path.to_owned(),
|
|
|
|
|
rhs_path.to_owned(),
|
|
|
|
|
lhs_path.to_owned(),
|
|
|
|
|
rhs_path.to_owned(),
|
|
|
|
|
FileArgument::from_cli_argument(lhs_path),
|
|
|
|
|
FileArgument::from_cli_argument(rhs_path),
|
|
|
|
|
false,
|
|
|
|
|
),
|
|
|
|
|
[display_path, lhs_tmp_file, _lhs_hash, _lhs_mode, rhs_tmp_file, _rhs_hash, _rhs_mode] => {
|
|
|
|
|
@ -277,8 +314,8 @@ pub fn parse_args() -> Mode {
|
|
|
|
|
(
|
|
|
|
|
display_path.to_owned(),
|
|
|
|
|
display_path.to_owned(),
|
|
|
|
|
lhs_tmp_file.to_owned(),
|
|
|
|
|
rhs_tmp_file.to_owned(),
|
|
|
|
|
FileArgument::from_path_argument(lhs_tmp_file),
|
|
|
|
|
FileArgument::from_path_argument(rhs_tmp_file),
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
@ -289,8 +326,8 @@ pub fn parse_args() -> Mode {
|
|
|
|
|
(
|
|
|
|
|
old_name.to_owned(),
|
|
|
|
|
new_name.to_owned(),
|
|
|
|
|
lhs_tmp_file.to_owned(),
|
|
|
|
|
rhs_tmp_file.to_owned(),
|
|
|
|
|
FileArgument::from_path_argument(lhs_tmp_file),
|
|
|
|
|
FileArgument::from_path_argument(rhs_tmp_file),
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
@ -381,8 +418,8 @@ pub fn parse_args() -> Mode {
|
|
|
|
|
display_options,
|
|
|
|
|
missing_as_empty,
|
|
|
|
|
language_override,
|
|
|
|
|
lhs_path: lhs_path.to_owned(),
|
|
|
|
|
rhs_path: rhs_path.to_owned(),
|
|
|
|
|
lhs_path,
|
|
|
|
|
rhs_path,
|
|
|
|
|
lhs_display_path: lhs_display_path.to_string_lossy().to_string(),
|
|
|
|
|
rhs_display_path: rhs_display_path.to_string_lossy().to_string(),
|
|
|
|
|
}
|
|
|
|
|
|