Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terminate process gracefully on error in diff() #685

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 25 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ pub mod errors {
}

#[cfg(not(tarpaulin_include))]
/// `Ok` of the `Result` contains with the exit code value
// An Ok result contains the desired process exit code. Note that 1 is used to
// report that two files differ when delta is called with two positional
// arguments and without standard input; 2 is used to report a real problem.
fn run_app() -> std::io::Result<i32> {
let assets = HighlightingAssets::new();
let opt = cli::Opt::from_args_and_git_config(&mut git_config::GitConfig::try_create(), assets);
Expand Down Expand Up @@ -120,31 +122,38 @@ fn diff(
writer: &mut dyn Write,
) -> i32 {
use std::io::BufReader;
let die = || {
if minus_file.is_none() || plus_file.is_none() {
eprintln!(
"\
The main way to use delta is to configure it as the pager for git: \
see https://github.com/dandavison/delta#configuration. \
You can also use delta to diff two files: `delta file_A file_B`."
);
process::exit(config.error_exit_code);
};
return config.error_exit_code;
}
let minus_file = minus_file.unwrap();
let plus_file = plus_file.unwrap();

let diff_command = "git";
let minus_file = minus_file.unwrap_or_else(die);
let plus_file = plus_file.unwrap_or_else(die);
let diff_command_path = match grep_cli::resolve_binary(PathBuf::from(diff_command)) {
Ok(path) => path,
Err(_) => return config.error_exit_code,
Err(err) => {
eprintln!("Failed to resolve command '{}': {}", diff_command, err);
return config.error_exit_code;
}
};
let mut diff_process = process::Command::new(diff_command_path)

let diff_process = process::Command::new(diff_command_path)
.args(&["diff", "--no-index"])
.args(&[minus_file, plus_file])
.stdout(process::Stdio::piped())
.spawn()
.unwrap_or_else(|err| {
eprintln!("Failed to execute the command '{}': {}", diff_command, err);
process::exit(config.error_exit_code);
});
.spawn();

if let Err(err) = diff_process {
eprintln!("Failed to execute the command '{}': {}", diff_command, err);
return config.error_exit_code;
}
let mut diff_process = diff_process.unwrap();

let exit_code = diff_process
.wait()
Expand All @@ -154,7 +163,7 @@ You can also use delta to diff two files: `delta file_A file_B`."
.code()
.unwrap_or_else(|| {
eprintln!("'{}' process terminated without exit status.", diff_command);
process::exit(config.error_exit_code);
config.error_exit_code
});

if let Err(error) = delta(
Expand All @@ -163,10 +172,10 @@ You can also use delta to diff two files: `delta file_A file_B`."
&config,
) {
match error.kind() {
ErrorKind::BrokenPipe => process::exit(0),
ErrorKind::BrokenPipe => return 0,
_ => {
eprintln!("{}", error);
process::exit(config.error_exit_code);
return config.error_exit_code;
}
}
};
Expand Down