From 1967b98905d1211d3cae8131fee7a7c611360ceb Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Mon, 16 Nov 2026 23:14:47 +0330 Subject: [PATCH 01/15] fix: return correct exit status for side-by-side diff Fix the exit status of side-by-side diff when files differ. Previously, side-by-side diff always returned an empty result, causing to incorrectly treat different files as identical. Introduce to track whether the input files differ while preserving the existing public API. Add regression tests to verify that: - Different files return exit code 1. - Identical files return exit code 0. --- src/diff.rs | 66 ++++++++++++++++++++++++++++++++++-------------- src/side_diff.rs | 44 ++++++++++++-------------------- 2 files changed, 63 insertions(+), 47 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index 55fd30e5..90e11931 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -155,35 +155,63 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { }; // run diff - let result: Vec = match params.format { - Format::Normal => normal_diff::diff(&from_content, &to_content, params), - Format::Unified => unified_diff::diff(&from_content, &to_content, params), - Format::Context => context_diff::diff(&from_content, &to_content, params), - Format::Ed => match ed_diff::diff(&from_content, &to_content, params) { - Ok(result) => result, - Err(error) => { - eprintln!("{error}"); - return Outcome::Trouble; - } - }, - // Side by side output is streamed, unless a header may have to come - // first. + let (result, different): (Vec, bool) = match params.format { + Format::Normal => { + let result = normal_diff::diff(&from_content, &to_content, params); + let different = !result.is_empty(); + (result, different) + } + Format::Unified => { + let result = unified_diff::diff(&from_content, &to_content, params); + let different = !result.is_empty(); + (result, different) + } + Format::Context => { + let result = context_diff::diff(&from_content, &to_content, params); + let different = !result.is_empty(); + (result, different) + } + Format::Ed => { + let result = match ed_diff::diff(&from_content, &to_content, params) { + Ok(result) => result, + Err(error) => { + eprintln!("{error}"); + return Outcome::Trouble; + } + }; + + let different = !result.is_empty(); + (result, different) + } Format::SideBySide => match header { - None => side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params), + None => { + let different = side_diff::diff_with_status( + &from_content, + &to_content, + &mut stdout().lock(), + params, + ); + (Vec::new(), different) + } Some(header) => { let mut output = Vec::new(); - let result = side_diff::diff(&from_content, &to_content, &mut output, params); - if !result.is_empty() { + + let different = + side_diff::diff_with_status(&from_content, &to_content, &mut output, params); + + if different { output.splice(0..0, header.iter().copied()); } + if stdout().write_all(&output).is_err() { return Outcome::Trouble; } - result + + (Vec::new(), different) } }, }; - if params.brief && !result.is_empty() { + if params.brief && different { print_files_differ("Files", params); } else if params.format != Format::SideBySide { let header = if result.is_empty() { @@ -210,7 +238,7 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { return Outcome::Trouble; } } - if result.is_empty() { + if !different { maybe_report_identical_files(); Outcome::Identical } else { diff --git a/src/side_diff.rs b/src/side_diff.rs index faebfd19..aa488eed 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -314,43 +314,32 @@ pub fn diff( output: &mut T, params: &Params, ) -> Vec { - // ^ The left file ^ The right file + diff_with_status(from_file, to_file, output, params); + vec![] +} +pub(crate) fn diff_with_status( + from_file: &[u8], + to_file: &[u8], + output: &mut T, + params: &Params, +) -> bool { + // ^ The left file ^ The right file let mut left_lines: Vec<&[u8]> = from_file.split_inclusive(|&c| c == b'\n').collect(); let mut right_lines: Vec<&[u8]> = to_file.split_inclusive(|&c| c == b'\n').collect(); let config = Config::new(params.width, params.tabsize, params.expand_tabs); - if left_lines.last() == Some(&&b""[..]) { left_lines.pop(); } - if right_lines.last() == Some(&&b""[..]) { right_lines.pop(); } + let results = diff::slice(&left_lines, &right_lines); + let different = results + .iter() + .any(|result| matches!(result, Result::Left(_) | Result::Right(_))); - /* - DISCLAIMER: - Currently the diff engine does not produce results like the diff engine used in GNU diff, - so some results may be inaccurate. For example, the line difference marker "|", according - to the GNU documentation, appears when the same lines (only the actual line, although the - relative line may change the result, so occasionally '|' markers appear with the same lines) - are different but exist in both files. In the current solution the same result cannot be - obtained because the diff engine does not return Both if both exist but are different, - but instead returns a Left and a Right for each one, implying that two lines were added - and deleted. Furthermore, the GNU diff program apparently stores some internal state - (this internal state is just a note about how the diff engine works) about the lines. - For example, an added or removed line directly counts in the line query of the original - lines to be printed in the output. Because of this imbalance caused by additions and - deletions, the characters ( and ) are introduced. They basically represent lines without - context, which have lost their pair in the other file due to additions or deletions. Anyway, - my goal with this disclaimer is to warn that for some reason, whether it's the diff engine's - inability to determine and predict/precalculate the result of GNU's sdiff, with this software it's - not possible to reproduce results that are 100% faithful to GNU's, however, the basic premise - e of side diff of showing added and removed lines and creating edit scripts is totally possible. - More studies are needed to cover GNU diff side by side with 100% accuracy, which is one of - the goals of this project : ) - */ - for result in diff::slice(&left_lines, &right_lines) { + for result in results { match result { Result::Left(left_ln) => push_output(left_ln, b"", b'<', output, &config).unwrap(), Result::Right(right_ln) => push_output(b"", right_ln, b'>', output, &config).unwrap(), @@ -359,8 +348,7 @@ pub fn diff( } } } - - vec![] + different } #[cfg(test)] From b2364b1993256b833322b348a656f9e14a4c1930 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Tue, 17 Nov 2026 14:12:05 +0330 Subject: [PATCH 02/15] fix: silence unused side diff warning --- src/side_diff.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/side_diff.rs b/src/side_diff.rs index aa488eed..c1bee00f 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -308,6 +308,7 @@ fn push_output( Ok(()) } +#[cfg_attr(not(test), allow(dead_code))] pub fn diff( from_file: &[u8], to_file: &[u8], From b5c6e52bdf841508a077e8cc16f79516df993fdc Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Tue, 17 Nov 2026 14:24:15 +0330 Subject: [PATCH 03/15] fix: scope dead code allowance to binary module Allow dead_code on the side_diff module in the binary to silence the unused diff warning. Keep the library's public API free of unnecessary lint allowances. --- src/main.rs | 1 + src/side_diff.rs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 2be28099..4fe3fb3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ mod ed_diff; mod macros; mod normal_diff; mod params; +#[allow(dead_code)] mod side_diff; mod unified_diff; mod utils; diff --git a/src/side_diff.rs b/src/side_diff.rs index c1bee00f..aa488eed 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -308,7 +308,6 @@ fn push_output( Ok(()) } -#[cfg_attr(not(test), allow(dead_code))] pub fn diff( from_file: &[u8], to_file: &[u8], From bcf68cfd4585e8a10e1c23e20a6d78b939c4d751 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Tue, 17 Nov 2026 15:03:13 +0330 Subject: [PATCH 04/15] docs: restore side-by-side diff disclaimer --- src/side_diff.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/side_diff.rs b/src/side_diff.rs index aa488eed..75124814 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -334,6 +334,28 @@ pub(crate) fn diff_with_status( if right_lines.last() == Some(&&b""[..]) { right_lines.pop(); } + /* + DISCLAIMER: + Currently the diff engine does not produce results like the diff engine used in GNU diff, + so some results may be inaccurate. For example, the line difference marker "|", according + to the GNU documentation, appears when the same lines (only the actual line, although the + relative line may change the result, so occasionally '|' markers appear with the same lines) + are different but exist in both files. In the current solution the same result cannot be + obtained because the diff engine does not return Both if both exist but are different, + but instead returns a Left and a Right for each one, implying that two lines were added + and deleted. Furthermore, the GNU diff program apparently stores some internal state + (this internal state is just a note about how the diff engine works) about the lines. + For example, an added or removed line directly counts in the line query of the original + lines to be printed in the output. Because of this imbalance caused by additions and + deletions, the characters ( and ) are introduced. They basically represent lines without + context, which have lost their pair in the other file due to additions or deletions. Anyway, + my goal with this disclaimer is to warn that for some reason, whether it's the diff engine's + inability to determine and predict/precalculate the result of GNU's sdiff, with this software it's + not possible to reproduce results that are 100% faithful to GNU's, however, the basic premise + e of side diff of showing added and removed lines and creating edit scripts is totally possible. + More studies are needed to cover GNU diff side by side with 100% accuracy, which is one of + the goals of this project : ) + */ let results = diff::slice(&left_lines, &right_lines); let different = results .iter() From f0265844238e29c9bedc2302b157eb3cd23f2794 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Tue, 17 Nov 2026 15:39:57 +0330 Subject: [PATCH 05/15] test: document identical side-by-side regression --- tests/by-util/test_diff.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 6173c199..640e9cf1 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -864,6 +864,19 @@ fn sdiff_gutter_marker_column(#[case] options: &[&str], #[case] expected: &str) .stdout_is(expected); } +// Regression test: identical files must return exit code 0 in side-by-side mode. +#[test] +fn sdiff_identical_files_exit_success() { + let (at, ts) = at_and_ts!(); + at.write("file1", "same\n"); + at.write("file2", "same\n"); + + ts.ucmd() + .args(&["-y", "file1", "file2"]) + .succeeds() + .no_stderr(); +} + #[rstest] #[case::delete_and_append( "north\neast\nsouth\nwest\n", From 57a6b326ec6690469f701a5e30cec3bd78b78dff Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Tue, 17 Nov 2026 21:43:10 +0330 Subject: [PATCH 06/15] test: cover different files exit status in side-by-side mode --- tests/by-util/test_diff.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 640e9cf1..88abfbd1 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -864,6 +864,15 @@ fn sdiff_gutter_marker_column(#[case] options: &[&str], #[case] expected: &str) .stdout_is(expected); } +#[test] +fn sdiff_different_files_exit_failure() { + let (at, ts) = at_and_ts!(); + at.write("file1", "first\n"); + at.write("file2", "second\n"); + + ts.ucmd().args(&["-y", "file1", "file2"]).fails_with_code(1); +} + // Regression test: identical files must return exit code 0 in side-by-side mode. #[test] fn sdiff_identical_files_exit_success() { From c3760f2ad1f7052e0530af39fb861a9a35082dc8 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:09:18 +0330 Subject: [PATCH 07/15] refactor: simplify side-by-side diff API --- src/side_diff.rs | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/side_diff.rs b/src/side_diff.rs index 75124814..0c6c81ea 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -5,7 +5,7 @@ use core::cmp::{max, min}; use diff::Result; -use std::{io::Write, vec}; +use std::io::Write; use unicode_width::UnicodeWidthStr; use crate::params::Params; @@ -308,23 +308,7 @@ fn push_output( Ok(()) } -pub fn diff( - from_file: &[u8], - to_file: &[u8], - output: &mut T, - params: &Params, -) -> Vec { - diff_with_status(from_file, to_file, output, params); - vec![] -} - -pub(crate) fn diff_with_status( - from_file: &[u8], - to_file: &[u8], - output: &mut T, - params: &Params, -) -> bool { - // ^ The left file ^ The right file +pub fn diff(from_file: &[u8], to_file: &[u8], output: &mut T, params: &Params) -> bool { let mut left_lines: Vec<&[u8]> = from_file.split_inclusive(|&c| c == b'\n').collect(); let mut right_lines: Vec<&[u8]> = to_file.split_inclusive(|&c| c == b'\n').collect(); let config = Config::new(params.width, params.tabsize, params.expand_tabs); From df00ca2f36cf22e655ba5727715dad34b21befc1 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:09:23 +0330 Subject: [PATCH 08/15] refactor: simplify diff status handling --- src/diff.rs | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index 90e11931..fc3128c9 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -155,21 +155,18 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { }; // run diff - let (result, different): (Vec, bool) = match params.format { + let (result, side_by_side_different): (Vec, Option) = match params.format { Format::Normal => { let result = normal_diff::diff(&from_content, &to_content, params); - let different = !result.is_empty(); - (result, different) + (result, None) } Format::Unified => { let result = unified_diff::diff(&from_content, &to_content, params); - let different = !result.is_empty(); - (result, different) + (result, None) } Format::Context => { let result = context_diff::diff(&from_content, &to_content, params); - let different = !result.is_empty(); - (result, different) + (result, None) } Format::Ed => { let result = match ed_diff::diff(&from_content, &to_content, params) { @@ -180,24 +177,18 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { } }; - let different = !result.is_empty(); - (result, different) + (result, None) } Format::SideBySide => match header { None => { - let different = side_diff::diff_with_status( - &from_content, - &to_content, - &mut stdout().lock(), - params, - ); - (Vec::new(), different) + let different = + side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params); + (Vec::new(), Some(different)) } Some(header) => { let mut output = Vec::new(); - let different = - side_diff::diff_with_status(&from_content, &to_content, &mut output, params); + let different = side_diff::diff(&from_content, &to_content, &mut output, params); if different { output.splice(0..0, header.iter().copied()); @@ -207,10 +198,11 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { return Outcome::Trouble; } - (Vec::new(), different) + (Vec::new(), Some(different)) } }, }; + let different = side_by_side_different.unwrap_or(!result.is_empty()); if params.brief && different { print_files_differ("Files", params); } else if params.format != Format::SideBySide { From 049b5e7b6a14dd2d3b81ac39ce5ff54e49456c5e Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:29:43 +0330 Subject: [PATCH 09/15] refactor: simplify diff result handling --- src/diff.rs | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index fc3128c9..2e0ae11b 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -155,19 +155,11 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { }; // run diff - let (result, side_by_side_different): (Vec, Option) = match params.format { - Format::Normal => { - let result = normal_diff::diff(&from_content, &to_content, params); - (result, None) - } - Format::Unified => { - let result = unified_diff::diff(&from_content, &to_content, params); - (result, None) - } - Format::Context => { - let result = context_diff::diff(&from_content, &to_content, params); - (result, None) - } + let mut side_by_side_different = false; + let result: Vec = match params.format { + Format::Normal => normal_diff::diff(&from_content, &to_content, params), + Format::Unified => unified_diff::diff(&from_content, &to_content, params), + Format::Context => context_diff::diff(&from_content, &to_content, params), Format::Ed => { let result = match ed_diff::diff(&from_content, &to_content, params) { Ok(result) => result, @@ -177,20 +169,21 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { } }; - (result, None) + result } Format::SideBySide => match header { None => { - let different = + side_by_side_different = side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params); - (Vec::new(), Some(different)) + Vec::new() } Some(header) => { let mut output = Vec::new(); - let different = side_diff::diff(&from_content, &to_content, &mut output, params); + side_by_side_different = + side_diff::diff(&from_content, &to_content, &mut output, params); - if different { + if side_by_side_different { output.splice(0..0, header.iter().copied()); } @@ -198,11 +191,11 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { return Outcome::Trouble; } - (Vec::new(), Some(different)) + Vec::new() } }, }; - let different = side_by_side_different.unwrap_or(!result.is_empty()); + let different = side_by_side_different || !result.is_empty(); if params.brief && different { print_files_differ("Files", params); } else if params.format != Format::SideBySide { From 90af07236dd7e1dd75a616c11a167f5a5b3295b7 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:35:19 +0330 Subject: [PATCH 10/15] perf: avoid repeated side-by-side diff iteration --- src/side_diff.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/side_diff.rs b/src/side_diff.rs index 0c6c81ea..520bf7bd 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -341,16 +341,19 @@ pub fn diff(from_file: &[u8], to_file: &[u8], output: &mut T, params: the goals of this project : ) */ let results = diff::slice(&left_lines, &right_lines); - let different = results - .iter() - .any(|result| matches!(result, Result::Left(_) | Result::Right(_))); - + let mut different = false; for result in results { match result { - Result::Left(left_ln) => push_output(left_ln, b"", b'<', output, &config).unwrap(), - Result::Right(right_ln) => push_output(b"", right_ln, b'>', output, &config).unwrap(), + Result::Left(left_ln) => { + different = true; + push_output(left_ln, b"", b'<', output, &config).unwrap(); + } + Result::Right(right_ln) => { + different = true; + push_output(b"", right_ln, b'>', output, &config).unwrap(); + } Result::Both(left_ln, right_ln) => { - push_output(left_ln, right_ln, b' ', output, &config).unwrap() + push_output(left_ln, right_ln, b' ', output, &config).unwrap(); } } } From cb854ac1048d4dd4e7f1a38e135d8133033db431 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:44:57 +0330 Subject: [PATCH 11/15] style: preserve side-by-side diff formatting --- src/main.rs | 1 - src/side_diff.rs | 5 +++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 4fe3fb3a..2be28099 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,6 @@ mod ed_diff; mod macros; mod normal_diff; mod params; -#[allow(dead_code)] mod side_diff; mod unified_diff; mod utils; diff --git a/src/side_diff.rs b/src/side_diff.rs index 520bf7bd..1822ea0f 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -309,12 +309,15 @@ fn push_output( } pub fn diff(from_file: &[u8], to_file: &[u8], output: &mut T, params: &Params) -> bool { + // ^ The left file ^ The right file let mut left_lines: Vec<&[u8]> = from_file.split_inclusive(|&c| c == b'\n').collect(); let mut right_lines: Vec<&[u8]> = to_file.split_inclusive(|&c| c == b'\n').collect(); let config = Config::new(params.width, params.tabsize, params.expand_tabs); + if left_lines.last() == Some(&&b""[..]) { left_lines.pop(); } + if right_lines.last() == Some(&&b""[..]) { right_lines.pop(); } @@ -342,6 +345,7 @@ pub fn diff(from_file: &[u8], to_file: &[u8], output: &mut T, params: */ let results = diff::slice(&left_lines, &right_lines); let mut different = false; + for result in results { match result { Result::Left(left_ln) => { @@ -357,6 +361,7 @@ pub fn diff(from_file: &[u8], to_file: &[u8], output: &mut T, params: } } } + different } From 8461c70d31429ab28c1fa2d8febe19c1066e1485 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:52:36 +0330 Subject: [PATCH 12/15] test: cover side-by-side diff exit status --- tests/by-util/test_diff.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 88abfbd1..a2275655 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -873,6 +873,32 @@ fn sdiff_different_files_exit_failure() { ts.ucmd().args(&["-y", "file1", "file2"]).fails_with_code(1); } +#[test] +fn sdiff_brief_different_files_exit_failure() { + let (at, ts) = at_and_ts!(); + at.write("file1", "first\n"); + at.write("file2", "second\n"); + + ts.ucmd() + .args(&["-q", "-y", "file1", "file2"]) + .fails_with_code(1) + .stdout_contains("Files file1 and file2 differ\n"); +} + +#[test] +fn sdiff_recursive_header() { + let (at, ts) = at_and_ts!(); + at.mkdir("left"); + at.mkdir("right"); + at.write("left/file", "first\n"); + at.write("right/file", "second\n"); + + ts.ucmd() + .args(&["-r", "-y", "left", "right"]) + .fails_with_code(1) + .stdout_contains("diff -r -y left/file right/file\n"); +} + // Regression test: identical files must return exit code 0 in side-by-side mode. #[test] fn sdiff_identical_files_exit_success() { From 45e3b3e6e24f1baab53faa331ccceb154214d180 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 21:30:13 +0330 Subject: [PATCH 13/15] test: handle platform-specific path separators --- tests/by-util/test_diff.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index a2275655..17e42a3d 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -896,7 +896,10 @@ fn sdiff_recursive_header() { ts.ucmd() .args(&["-r", "-y", "left", "right"]) .fails_with_code(1) - .stdout_contains("diff -r -y left/file right/file\n"); + .stdout_contains(format!( + "diff -r -y left{0}file right{0}file\n", + std::path::MAIN_SEPARATOR + )); } // Regression test: identical files must return exit code 0 in side-by-side mode. From 6d34d9df46ef2433f5ccbe4b27e9872ae1bbdc07 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Fri, 20 Nov 2026 13:53:00 +0330 Subject: [PATCH 14/15] fix: suppress side-by-side output in brief mode Suppress side-by-side diff output when --brief is enabled. Print only the file comparison summary and preserve the correct exit status for identical and different files, including recursive comparisons. Add regression tests for identical, different, and recursive brief comparisons. --- src/diff.rs | 43 ++++++++++++++++++++++---------------- tests/by-util/test_diff.rs | 28 ++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index 2e0ae11b..aa70393d 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -171,29 +171,36 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { result } - Format::SideBySide => match header { - None => { - side_by_side_different = - side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params); - Vec::new() - } - Some(header) => { - let mut output = Vec::new(); + Format::SideBySide => { + let different = if params.brief { + side_diff::diff(&from_content, &to_content, &mut std::io::sink(), params) + } else { + match header { + None => { + side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params) + } + Some(header) => { + let mut output = Vec::new(); - side_by_side_different = - side_diff::diff(&from_content, &to_content, &mut output, params); + let different = + side_diff::diff(&from_content, &to_content, &mut output, params); - if side_by_side_different { - output.splice(0..0, header.iter().copied()); - } + if different { + output.splice(0..0, header.iter().copied()); + } - if stdout().write_all(&output).is_err() { - return Outcome::Trouble; + if stdout().write_all(&output).is_err() { + return Outcome::Trouble; + } + + different + } } + }; - Vec::new() - } - }, + side_by_side_different = different; + Vec::new() + } }; let different = side_by_side_different || !result.is_empty(); if params.brief && different { diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 17e42a3d..c80e54fb 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -873,6 +873,18 @@ fn sdiff_different_files_exit_failure() { ts.ucmd().args(&["-y", "file1", "file2"]).fails_with_code(1); } +#[test] +fn sdiff_brief_identical_files() { + let (at, ts) = at_and_ts!(); + at.write("file1", "same\n"); + at.write("file2", "same\n"); + + ts.ucmd() + .args(&["-q", "-y", "file1", "file2"]) + .succeeds() + .stdout_is(""); +} + #[test] fn sdiff_brief_different_files_exit_failure() { let (at, ts) = at_and_ts!(); @@ -882,7 +894,21 @@ fn sdiff_brief_different_files_exit_failure() { ts.ucmd() .args(&["-q", "-y", "file1", "file2"]) .fails_with_code(1) - .stdout_contains("Files file1 and file2 differ\n"); + .stdout_is("Files file1 and file2 differ\n"); +} + +#[test] +fn sdiff_recursive_brief_different_files() { + let (at, ts) = at_and_ts!(); + at.mkdir("left"); + at.mkdir("right"); + at.write("left/file", "first\n"); + at.write("right/file", "second\n"); + + ts.ucmd() + .args(&["-q", "-r", "-y", "left", "right"]) + .fails_with_code(1) + .stdout_is("Files left/file and right/file differ\n"); } #[test] From 8e6ab291196e10411d33062cfad4fe39d8e5a1d3 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Fri, 20 Nov 2026 14:14:02 +0330 Subject: [PATCH 15/15] test: handle platform-specific path separators --- tests/by-util/test_diff.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index c80e54fb..4e32d8fe 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -908,7 +908,10 @@ fn sdiff_recursive_brief_different_files() { ts.ucmd() .args(&["-q", "-r", "-y", "left", "right"]) .fails_with_code(1) - .stdout_is("Files left/file and right/file differ\n"); + .stdout_is(format!( + "Files left{sep}file and right{sep}file differ\n", + sep = std::path::MAIN_SEPARATOR + )); } #[test]