From 4c981f747b72e67fc98849160a2297b93f82fde1 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Mon, 16 Nov 2026 23:14:47 +0330 Subject: [PATCH 01/12] 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 0a55fc60555f76848ffc2a6173c67fadb1ce5b0e Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Tue, 17 Nov 2026 14:12:05 +0330 Subject: [PATCH 02/12] 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 d8c09697e48b731715c62f291a9e71d679807598 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Tue, 17 Nov 2026 14:24:15 +0330 Subject: [PATCH 03/12] 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 39e75deba3f186ca82074bd93ceb55d52d4ef669 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Tue, 17 Nov 2026 15:03:13 +0330 Subject: [PATCH 04/12] 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 34f5bb0f0c0cd4fcd030e199511dd6f0fa278bb2 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Tue, 17 Nov 2026 15:39:57 +0330 Subject: [PATCH 05/12] 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 c251c39b..3e08d991 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -802,6 +802,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 64324e7a6417a9d24c5caeb8ffcb9723686354ff Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Tue, 17 Nov 2026 21:43:10 +0330 Subject: [PATCH 06/12] 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 3e08d991..d3e9060f 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -802,6 +802,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 0f54b924f4dd0cb6102eae90951a04763aeb5c5e Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:09:18 +0330 Subject: [PATCH 07/12] 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 e83d30e1d16cea601d9a7adfad937bd799aed8c1 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:09:23 +0330 Subject: [PATCH 08/12] 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 da380d420875c7cacca2e50aa299c4803a769241 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:29:43 +0330 Subject: [PATCH 09/12] 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 3862c137f506c0470f9ff6aef6a60fd145932854 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:35:19 +0330 Subject: [PATCH 10/12] 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 3eadee7f7441bd02fdef71b5fff60288dc9b2894 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:44:57 +0330 Subject: [PATCH 11/12] 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 f3d9089b97fde8af33adf58cc2a4c47eddc03d32 Mon Sep 17 00:00:00 2001 From: dex0o0 Date: Wed, 18 Nov 2026 14:52:36 +0330 Subject: [PATCH 12/12] 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 d3e9060f..4374927c 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -811,6 +811,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() {