Only set the exit code if --exit-code is set

This is important for usage with git log, which terminates on non-zero
exit codes.
pull/454/head
Wilfred Hughes 2022-12-18 23:11:18 +07:00
parent b7cfff3f27
commit a2f22cb17c
4 changed files with 21 additions and 9 deletions

@ -17,9 +17,10 @@ interleaved output from different files.
### Command Line Interface
Difftastic now sets the exit code if it finds changes. See [usage in
the manual](https://difftastic.wilfred.me.uk/usage.html) for the full
list of exit codes used.
Difftastic now sets the exit code if it finds changes and
`--exit-code` is set. See [usage in the
manual](https://difftastic.wilfred.me.uk/usage.html) for the full list
of exit codes used.
## 0.38 (released 14th November 2022)

@ -56,11 +56,12 @@ where you are not invoking the `difft` binary directly.
## Exit Codes
0: Difftastic found no syntactic changes (in text files) or no byte
changes (in binary files).
2: Difftastic was given invalid arguments. This includes invalid usage
(e.g. the wrong number of arguments) as well as paths that difftastic
cannot read (e.g. non-existent paths or insufficient permissions).
1: Difftastic found syntactic changes (in text files) or byte changes
1: When called with `--exit-code`, difftastic will return an exit code
of 1 when it finds any syntactic changes (in text files) or byte changes
(in binary files).
2: Difftastic was given invalid arguments, such as file paths that it
couldn't read, or the wrong number of arguments.
0: All other cases.

@ -161,6 +161,7 @@ fn main() {
byte_limit,
display_options,
missing_as_empty,
set_exit_code,
language_override,
lhs_path,
rhs_path,
@ -239,7 +240,7 @@ fn main() {
}
}
let exit_code = if encountered_changes.load(Ordering::Relaxed) {
let exit_code = if set_exit_code && encountered_changes.load(Ordering::Relaxed) {
EXIT_FOUND_CHANGES
} else {
EXIT_SUCCESS

@ -152,6 +152,11 @@ fn app() -> clap::Command<'static> {
.default_value("on")
.help("Enable or disable syntax highlighting.")
)
.arg(
Arg::new("exit-code").long("exit-code")
.env("DFT_EXIT_CODE")
.help("Set the exit code to 1 if there are syntactic changes in any text files, or byte changes in any binary files.")
)
.arg(
Arg::new("skip-unchanged").long("skip-unchanged")
.help("Don't display anything if a file is unchanged.")
@ -259,6 +264,7 @@ pub enum Mode {
byte_limit: usize,
display_options: DisplayOptions,
missing_as_empty: bool,
set_exit_code: bool,
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.
@ -451,6 +457,8 @@ pub fn parse_args() -> Mode {
// file on all platforms?
let missing_as_empty = matches.is_present("missing-as-empty");
let set_exit_code = matches.is_present("exit-code");
let display_options = DisplayOptions {
background_color,
use_color,
@ -468,6 +476,7 @@ pub fn parse_args() -> Mode {
byte_limit,
display_options,
missing_as_empty,
set_exit_code,
language_override,
lhs_path,
rhs_path,