From 60b44049f5e5d280220c38d59006704bc100606b Mon Sep 17 00:00:00 2001 From: Yunus Ardaguney Date: Thu, 19 Nov 2026 01:39:42 +0300 Subject: [PATCH 1/2] diff: add -B/--ignore-blank-lines --- src/context_diff.rs | 25 +++++++++++++++++++++++-- src/ed_diff.rs | 11 +++++++++-- src/normal_diff.rs | 12 ++++++++++-- src/params.rs | 19 +++++++++++++++++++ src/unified_diff.rs | 24 ++++++++++++++++++++++-- src/utils.rs | 17 +++++++++++++++++ tests/by-util/test_diff.rs | 27 +++++++++++++++++++++++++++ 7 files changed, 127 insertions(+), 8 deletions(-) diff --git a/src/context_diff.rs b/src/context_diff.rs index 23c391e1..2f780339 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -7,7 +7,7 @@ use std::collections::VecDeque; use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, line_prefix}; +use crate::utils::{do_write_line, is_blank_line, line_prefix}; use crate::utils::{ get_modification_time, is_posix_locale, write_file_header, FunctionLineFinder, TimestampStyle, }; @@ -337,7 +337,28 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { params.to_label.as_deref(), &to_modified_time, ); - let diff_results = make_diff(expected, actual, params.context_count, params.brief); + let mut diff_results = make_diff( + expected, + actual, + params.context_count, + params.brief && !params.ignore_blank_lines, + ); + if params.ignore_blank_lines { + diff_results.retain(|mismatch| { + if mismatch.expected_missing_nl || mismatch.actual_missing_nl { + return true; + } + let expected_has_non_blank = mismatch.expected.iter().any(|line| match line { + DiffLine::Change(l) | DiffLine::Add(l) => !is_blank_line(l), + DiffLine::Context(_) => false, + }); + let actual_has_non_blank = mismatch.actual.iter().any(|line| match line { + DiffLine::Change(l) | DiffLine::Add(l) => !is_blank_line(l), + DiffLine::Context(_) => false, + }); + expected_has_non_blank || actual_has_non_blank + }); + } if diff_results.is_empty() { return Vec::new(); } diff --git a/src/ed_diff.rs b/src/ed_diff.rs index 2f75adfa..e81d078f 100644 --- a/src/ed_diff.rs +++ b/src/ed_diff.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::params::Params; -use crate::utils::do_write_line; +use crate::utils::{do_write_line, is_blank_line}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -112,7 +112,14 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result Result, DiffError> { let mut output = Vec::new(); - let diff_results = make_diff(expected, actual, params.brief)?; + let mut diff_results = make_diff(expected, actual, params.brief && !params.ignore_blank_lines)?; + if params.ignore_blank_lines { + diff_results.retain(|mismatch| { + let expected_blank = mismatch.expected.iter().all(|l| is_blank_line(l)); + let actual_blank = mismatch.actual.iter().all(|l| is_blank_line(l)); + !(expected_blank && actual_blank) + }); + } if params.brief && !diff_results.is_empty() { write!(&mut output, "\0").unwrap(); return Ok(output); diff --git a/src/normal_diff.rs b/src/normal_diff.rs index 0a6503fb..27a2a272 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, line_prefix}; +use crate::utils::{do_write_line, is_blank_line, line_prefix}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -121,7 +121,15 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { // See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Normal.html // for details on the syntax of the normal format. let mut output = Vec::new(); - let diff_results = make_diff(expected, actual, params.brief); + let mut diff_results = make_diff(expected, actual, params.brief && !params.ignore_blank_lines); + if params.ignore_blank_lines { + diff_results.retain(|mismatch| { + let expected_blank = mismatch.expected.iter().all(|l| is_blank_line(l)); + let actual_blank = mismatch.actual.iter().all(|l| is_blank_line(l)); + let missing_nl = mismatch.expected_missing_nl || mismatch.actual_missing_nl; + !(expected_blank && actual_blank && !missing_nl) + }); + } if params.brief && !diff_results.is_empty() { write!(&mut output, "\0").unwrap(); return output; diff --git a/src/params.rs b/src/params.rs index 73ea45e0..fa7351a6 100644 --- a/src/params.rs +++ b/src/params.rs @@ -64,6 +64,7 @@ pub struct Params { /// (`-S`). pub starting_file: Option, pub ignore_file_name_case: bool, + pub ignore_blank_lines: bool, pub no_dereference: bool, /// Set when `from` did not exist and was compared as an empty file. pub from_missing: bool, @@ -110,6 +111,7 @@ impl Default for Params { recursive: false, starting_file: None, ignore_file_name_case: false, + ignore_blank_lines: false, no_dereference: false, from_missing: false, to_missing: false, @@ -257,6 +259,10 @@ pub fn parse_params>( params.ignore_file_name_case = true; continue; } + if param == "-B" || param == "--ignore-blank-lines" { + params.ignore_blank_lines = true; + continue; + } if param == "--no-ignore-file-name-case" { params.ignore_file_name_case = false; continue; @@ -1511,4 +1517,17 @@ mod tests { assert_eq!(params.ignore_file_name_case, expected); } } + + #[test] + fn ignore_blank_lines() { + for arg in ["-B", "--ignore-blank-lines"] { + let params = parse_params( + [os("diff"), os(arg), os("foo"), os("bar")] + .into_iter() + .peekable(), + ) + .unwrap(); + assert!(params.ignore_blank_lines); + } + } } diff --git a/src/unified_diff.rs b/src/unified_diff.rs index 572e160d..4f22b6ff 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -7,7 +7,7 @@ use std::collections::VecDeque; use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, line_prefix}; +use crate::utils::{do_write_line, is_blank_line, line_prefix}; use crate::utils::{get_modification_time, write_file_header, FunctionLineFinder, TimestampStyle}; #[derive(Debug, PartialEq)] @@ -262,7 +262,27 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { params.to_label.as_deref(), &to_modified_time, ); - let diff_results = make_diff(expected, actual, params.context_count, params.brief); + let mut diff_results = make_diff( + expected, + actual, + params.context_count, + params.brief && !params.ignore_blank_lines, + ); + if params.ignore_blank_lines { + diff_results.retain(|mismatch| { + let has_missing_nl = mismatch + .lines + .iter() + .any(|l| matches!(l, DiffLine::MissingNL)); + if has_missing_nl { + return true; + } + mismatch.lines.iter().any(|line| match line { + DiffLine::Expected(l) | DiffLine::Actual(l) => !is_blank_line(l), + DiffLine::Context(_) | DiffLine::MissingNL => false, + }) + }); + } if diff_results.is_empty() { return Vec::new(); } diff --git a/src/utils.rs b/src/utils.rs index 522f32d0..b9b7817a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -75,6 +75,12 @@ pub fn line_prefix<'a>(prefix: &'a [u8], line: &[u8], params: &Params) -> &'a [u } } +/// Returns true if a line contains only ASCII spaces, tabs or carriage returns, or is empty. +#[inline] +pub fn is_blank_line(line: &[u8]) -> bool { + line.iter().all(|&c| c == b' ' || c == b'\t' || c == b'\r') +} + /// Quotes a file name for the headers of the diff output: a name containing /// spaces, double quotes, backslashes, control characters or non-ASCII bytes /// is written between double quotes with C-style escapes, other names are @@ -552,6 +558,17 @@ mod tests { assert_line_written("foo bar\tbaz", false, 8, "foo bar\tbaz"); assert_line_written("foo bar\tbaz", true, 8, "foo bar baz"); } + + #[test] + fn blank_lines() { + assert!(is_blank_line(b"")); + assert!(is_blank_line(b"\r")); + assert!(is_blank_line(b" ")); + assert!(is_blank_line(b"\t")); + assert!(is_blank_line(b" \t \r")); + assert!(!is_blank_line(b"a")); + assert!(!is_blank_line(b" a ")); + } } mod modification_time { diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index cbbbfa2b..72c3cf22 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1069,3 +1069,30 @@ fn label_errors(#[case] args: &[&str], #[case] error: &str) { .fails_with_code(2) .stderr_contains(error); } + +#[test] +fn ignore_blank_lines() { + let (at, ts) = at_and_ts!(); + at.write("a", "a\n\nb\n"); + at.write("b", "a\nb\n"); + for option in ["-B", "--ignore-blank-lines"] { + for format in [&[][..], &["-u"], &["-c"], &["-e"], &["-q"]] { + ts.ucmd() + .arg(option) + .args(format) + .args(&["a", "b"]) + .succeeds() + .no_stdout(); + } + } + // Bug#18402: non-blank changes without a final newline are still reported. + at.write("c", "a"); + at.write("d", "b"); + ts.ucmd() + .arg("-B") + .args(&["c", "d"]) + .fails_with_code(1) + .stdout_is( + "1c1\n< a\n\\ No newline at end of file\n---\n> b\n\\ No newline at end of file\n", + ); +} From a9e187c7b267203431c624d91cbac1a9cca5dcfc Mon Sep 17 00:00:00 2001 From: Yunus Ardaguney Date: Fri, 20 Nov 2026 11:57:43 +0300 Subject: [PATCH 2/2] refactor(diff): address review feedback for -B/--ignore-blank-lines - restrict is_blank_line to empty lines matching GNU diff behavior - extract has_non_blank_lines shared helper in utils.rs, deduplicating filter across normal, unified, context, and ed diffs - fix missing newline handling at EOF for common context lines under -u and -c - support -B in side-by-side (-y) diff format with '(' and ')' gutter markers - add regression tests for whitespace lines, trailing missing-newline context, mixed blank/real changes, and side-by-side format --- src/context_diff.rs | 31 +++++++++++---------- src/ed_diff.rs | 8 ++---- src/normal_diff.rs | 9 ++---- src/side_diff.rs | 28 +++++++++++++++++-- src/unified_diff.rs | 26 ++++++++---------- src/utils.rs | 24 ++++++++++++---- tests/by-util/test_diff.rs | 56 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 132 insertions(+), 50 deletions(-) diff --git a/src/context_diff.rs b/src/context_diff.rs index 2f780339..e7dda6aa 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -7,7 +7,7 @@ use std::collections::VecDeque; use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, is_blank_line, line_prefix}; +use crate::utils::{do_write_line, has_non_blank_lines, line_prefix}; use crate::utils::{ get_modification_time, is_posix_locale, write_file_header, FunctionLineFinder, TimestampStyle, }; @@ -19,6 +19,15 @@ pub enum DiffLine { Add(Vec), } +impl DiffLine { + fn changed_bytes(&self) -> Option<&[u8]> { + match self { + Self::Change(l) | Self::Add(l) => Some(l), + Self::Context(_) => None, + } + } +} + #[derive(Debug, PartialEq)] struct Mismatch { pub line_number_expected: usize, @@ -344,19 +353,13 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { params.brief && !params.ignore_blank_lines, ); if params.ignore_blank_lines { - diff_results.retain(|mismatch| { - if mismatch.expected_missing_nl || mismatch.actual_missing_nl { - return true; - } - let expected_has_non_blank = mismatch.expected.iter().any(|line| match line { - DiffLine::Change(l) | DiffLine::Add(l) => !is_blank_line(l), - DiffLine::Context(_) => false, - }); - let actual_has_non_blank = mismatch.actual.iter().any(|line| match line { - DiffLine::Change(l) | DiffLine::Add(l) => !is_blank_line(l), - DiffLine::Context(_) => false, - }); - expected_has_non_blank || actual_has_non_blank + diff_results.retain(|m| { + has_non_blank_lines( + m.expected + .iter() + .filter_map(DiffLine::changed_bytes) + .chain(m.actual.iter().filter_map(DiffLine::changed_bytes)), + ) }); } if diff_results.is_empty() { diff --git a/src/ed_diff.rs b/src/ed_diff.rs index e81d078f..d6118024 100644 --- a/src/ed_diff.rs +++ b/src/ed_diff.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, is_blank_line}; +use crate::utils::{do_write_line, has_non_blank_lines}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -114,11 +114,7 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Result, let mut output = Vec::new(); let mut diff_results = make_diff(expected, actual, params.brief && !params.ignore_blank_lines)?; if params.ignore_blank_lines { - diff_results.retain(|mismatch| { - let expected_blank = mismatch.expected.iter().all(|l| is_blank_line(l)); - let actual_blank = mismatch.actual.iter().all(|l| is_blank_line(l)); - !(expected_blank && actual_blank) - }); + diff_results.retain(|m| has_non_blank_lines(m.expected.iter().chain(&m.actual))); } if params.brief && !diff_results.is_empty() { write!(&mut output, "\0").unwrap(); diff --git a/src/normal_diff.rs b/src/normal_diff.rs index 27a2a272..b4e7dda5 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, is_blank_line, line_prefix}; +use crate::utils::{do_write_line, has_non_blank_lines, line_prefix}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -123,12 +123,7 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { let mut output = Vec::new(); let mut diff_results = make_diff(expected, actual, params.brief && !params.ignore_blank_lines); if params.ignore_blank_lines { - diff_results.retain(|mismatch| { - let expected_blank = mismatch.expected.iter().all(|l| is_blank_line(l)); - let actual_blank = mismatch.actual.iter().all(|l| is_blank_line(l)); - let missing_nl = mismatch.expected_missing_nl || mismatch.actual_missing_nl; - !(expected_blank && actual_blank && !missing_nl) - }); + diff_results.retain(|m| has_non_blank_lines(m.expected.iter().chain(&m.actual))); } if params.brief && !diff_results.is_empty() { write!(&mut output, "\0").unwrap(); diff --git a/src/side_diff.rs b/src/side_diff.rs index faebfd19..da1a4c01 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -9,6 +9,7 @@ use std::{io::Write, vec}; use unicode_width::UnicodeWidthStr; use crate::params::Params; +use crate::utils::is_blank_line; const GUTTER_WIDTH_MIN: usize = 3; @@ -350,17 +351,38 @@ pub fn diff( More studies are needed to cover GNU diff side by side with 100% accuracy, which is one of the goals of this project : ) */ + let mut differs = false; for result in diff::slice(&left_lines, &right_lines) { 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) => { + let is_blank = params.ignore_blank_lines + && is_blank_line(left_ln.strip_suffix(b"\n").unwrap_or(left_ln)); + if !is_blank { + differs = true; + } + let symbol = if is_blank { b'(' } else { b'<' }; + push_output(left_ln, b"", symbol, output, &config).unwrap(); + } + Result::Right(right_ln) => { + let is_blank = params.ignore_blank_lines + && is_blank_line(right_ln.strip_suffix(b"\n").unwrap_or(right_ln)); + if !is_blank { + differs = true; + } + let symbol = if is_blank { b')' } else { b'>' }; + push_output(b"", right_ln, symbol, output, &config).unwrap(); + } Result::Both(left_ln, right_ln) => { push_output(left_ln, right_ln, b' ', output, &config).unwrap() } } } - vec![] + if differs { + vec![1] + } else { + vec![] + } } #[cfg(test)] diff --git a/src/unified_diff.rs b/src/unified_diff.rs index 4f22b6ff..70b45a57 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -7,7 +7,7 @@ use std::collections::VecDeque; use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, is_blank_line, line_prefix}; +use crate::utils::{do_write_line, has_non_blank_lines, line_prefix}; use crate::utils::{get_modification_time, write_file_header, FunctionLineFinder, TimestampStyle}; #[derive(Debug, PartialEq)] @@ -18,6 +18,15 @@ pub enum DiffLine { MissingNL, } +impl DiffLine { + fn changed_bytes(&self) -> Option<&[u8]> { + match self { + Self::Expected(l) | Self::Actual(l) => Some(l), + Self::Context(_) | Self::MissingNL => None, + } + } +} + #[derive(Debug, PartialEq)] struct Mismatch { pub line_number_expected: u32, @@ -269,19 +278,8 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { params.brief && !params.ignore_blank_lines, ); if params.ignore_blank_lines { - diff_results.retain(|mismatch| { - let has_missing_nl = mismatch - .lines - .iter() - .any(|l| matches!(l, DiffLine::MissingNL)); - if has_missing_nl { - return true; - } - mismatch.lines.iter().any(|line| match line { - DiffLine::Expected(l) | DiffLine::Actual(l) => !is_blank_line(l), - DiffLine::Context(_) | DiffLine::MissingNL => false, - }) - }); + diff_results + .retain(|m| has_non_blank_lines(m.lines.iter().filter_map(DiffLine::changed_bytes))); } if diff_results.is_empty() { return Vec::new(); diff --git a/src/utils.rs b/src/utils.rs index b9b7817a..17e81066 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -75,10 +75,22 @@ pub fn line_prefix<'a>(prefix: &'a [u8], line: &[u8], params: &Params) -> &'a [u } } -/// Returns true if a line contains only ASCII spaces, tabs or carriage returns, or is empty. +/// Returns true if a line is empty. +/// +/// In GNU diff, `-B`/`--ignore-blank-lines` only ignores lines that are completely +/// empty unless space-ignoring options (like `-b` or `-w`) are also active. #[inline] pub fn is_blank_line(line: &[u8]) -> bool { - line.iter().all(|&c| c == b' ' || c == b'\t' || c == b'\r') + line.is_empty() +} + +/// Returns true if any line in the given iterator is non-blank. +pub fn has_non_blank_lines(lines: I) -> bool +where + I: IntoIterator, + T: AsRef<[u8]>, +{ + lines.into_iter().any(|l| !is_blank_line(l.as_ref())) } /// Quotes a file name for the headers of the diff output: a name containing @@ -562,10 +574,10 @@ mod tests { #[test] fn blank_lines() { assert!(is_blank_line(b"")); - assert!(is_blank_line(b"\r")); - assert!(is_blank_line(b" ")); - assert!(is_blank_line(b"\t")); - assert!(is_blank_line(b" \t \r")); + assert!(!is_blank_line(b"\r")); + assert!(!is_blank_line(b" ")); + assert!(!is_blank_line(b"\t")); + assert!(!is_blank_line(b" \t \r")); assert!(!is_blank_line(b"a")); assert!(!is_blank_line(b" a ")); } diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 72c3cf22..f2293b2e 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1096,3 +1096,59 @@ fn ignore_blank_lines() { "1c1\n< a\n\\ No newline at end of file\n---\n> b\n\\ No newline at end of file\n", ); } + +#[test] +fn ignore_blank_lines_whitespace_not_ignored() { + let (at, ts) = at_and_ts!(); + at.write("a", "a\n \nb\n"); + at.write("b", "a\nb\n"); + ts.ucmd() + .arg("-B") + .args(&["a", "b"]) + .fails_with_code(1) + .stdout_is("2d1\n< \n"); +} + +#[test] +fn ignore_blank_lines_missing_newline_at_eof() { + let (at, ts) = at_and_ts!(); + // Common context line at EOF with missing trailing newline. + at.write("a", "a\n\nb"); + at.write("b", "a\nb"); + for format in [&[][..], &["-u"], &["-c"]] { + ts.ucmd() + .arg("-B") + .args(format) + .args(&["a", "b"]) + .succeeds() + .no_stdout(); + } +} + +#[test] +fn ignore_blank_lines_with_real_change_in_same_hunk() { + let (at, ts) = at_and_ts!(); + at.write("a", "a\n\nb\n"); + at.write("b", "a\nx\n"); + ts.ucmd() + .arg("-B") + .args(&["a", "b"]) + .fails_with_code(1) + .stdout_is("2,3c2\n< \n< b\n---\n> x\n"); +} + +#[test] +fn ignore_blank_lines_side_by_side() { + let (at, ts) = at_and_ts!(); + at.write("a", "a\n\nb\n"); + at.write("b", "a\nb\n"); + ts.ucmd() + .args(&["-y", "-B", "a", "b"]) + .succeeds() + .stdout_contains("("); + + ts.ucmd() + .args(&["-y", "-B", "b", "a"]) + .succeeds() + .stdout_contains(")"); +}