From 4e47cab5332aaae01de0f040514f59a349be12ff Mon Sep 17 00:00:00 2001 From: small-pai <1809042530@qq.com> Date: Mon, 16 Nov 2026 23:06:19 +0800 Subject: [PATCH 1/4] diff: parse -I/--ignore-matching-lines into Params --- src/params.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/params.rs b/src/params.rs index 925b0320..c9c827a9 100644 --- a/src/params.rs +++ b/src/params.rs @@ -61,6 +61,7 @@ pub struct Params { /// Basic regular expressions matching the lines shown after hunk /// headers (`-F` and `-p`). pub function_line_patterns: Vec, + pub ignore_matching_lines: Vec, pub recursive: bool, /// The name from which to start comparing the top-level directories /// (`-S`). @@ -111,6 +112,7 @@ impl Default for Params { palette: None, presume_output_tty: false, function_line_patterns: Vec::new(), + ignore_matching_lines: Vec::new(), recursive: false, starting_file: None, ignore_file_name_case: false, @@ -249,6 +251,11 @@ pub fn parse_params>( push_function_line_pattern(&mut params, &pattern.to_string_lossy())?; continue; } + if let Some(argument) = option_argument(¶m, Some("-I"), "--ignore-matching-lines") { + let pattern = argument_value(argument, ¶m, &mut opts)?; + push_ignore_matching_line(&mut params, &pattern.to_string_lossy())?; + continue; + } if param == "--no-dereference" { params.no_dereference = true; continue; @@ -542,6 +549,13 @@ fn largest_context(current: Option, given: Option) -> usize { current.map_or(given, |current| current.max(given)) } +/// Validates and records an `-I` pattern. +fn push_ignore_matching_line(params: &mut Params, pattern: &str) -> Result<(), String> { + let bre = Bre::new(pattern).map_err(|error| format!("'{pattern}': {error}"))?; + params.ignore_matching_lines.push(bre); + Ok(()) +} + struct DiffStyleMatch { is_match: bool, context_count: Option, @@ -997,6 +1011,57 @@ mod tests { assert_eq!(params.from, os("foo")); } + #[test] + fn ignore_matching_lines() { + let patterns = |patterns: &[&str]| -> Vec { + patterns + .iter() + .map(|pattern| Bre::new(pattern).unwrap()) + .collect() + }; + let parse = |args: &[&str]| { + let args = std::iter::once("diff") + .chain(args.iter().copied()) + .chain(["foo", "bar"]); + parse_params(args.map(os).peekable()) + }; + + assert_eq!( + parse(&["-I", "3"]).unwrap().ignore_matching_lines, + patterns(&["3"]) + ); + assert_eq!( + parse(&["-I3"]).unwrap().ignore_matching_lines, + patterns(&["3"]) + ); + assert_eq!( + parse(&["--ignore-matching-lines", "3"]) + .unwrap() + .ignore_matching_lines, + patterns(&["3"]) + ); + assert_eq!( + parse(&["--ignore-matching-lines=3"]) + .unwrap() + .ignore_matching_lines, + patterns(&["3"]) + ); + + // Repeatable. + assert_eq!( + parse(&["-Ix", "-I", "y", "--ignore-matching-lines=z"]) + .unwrap() + .ignore_matching_lines, + patterns(&["x", "y", "z"]) + ); + + // Invalid BRE is a usage error. + assert_eq!( + parse(&["-Ix", "-I\\"]), + Err("'\\': Trailing backslash".to_string()) + ); + } + // The last of --ignore-file-name-case and --no-ignore-file-name-case // wins. #[rstest] From 51747a02207c6ff1c5d022f2f5fe9d6db5cede35 Mon Sep 17 00:00:00 2001 From: small-pai <1809042530@qq.com> Date: Mon, 16 Nov 2026 23:06:19 +0800 Subject: [PATCH 2/4] diff: drop hunks whose changed lines all match -I patterns --- src/normal_diff.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/normal_diff.rs b/src/normal_diff.rs index c5ee5db2..b7ace75d 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -5,6 +5,7 @@ use std::io::Write; +use crate::bre::Bre; use crate::params::Params; use crate::utils::{comparison_lines, do_write_line, line_prefix}; @@ -31,6 +32,20 @@ impl Mismatch { } } +impl Mismatch { + /// Whether every inserted and deleted line matches at least one `-I` + /// pattern. A mismatch with no changed lines is never ignored. + fn is_ignored(&self, patterns: &[Bre]) -> bool { + if patterns.is_empty() { + return false; + } + self.expected + .iter() + .chain(self.actual.iter()) + .all(|line| patterns.iter().any(|pattern| pattern.is_match(line))) + } +} + // Produces a diff between the expected output and actual output. fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { let stop_early = params.brief; @@ -128,6 +143,9 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { return output; } for result in diff_results { + if result.is_ignored(¶ms.ignore_matching_lines) { + continue; + } let line_number_expected = result.line_number_expected; let line_number_actual = result.line_number_actual; let expected_count = result.expected.len(); @@ -591,4 +609,48 @@ mod tests { expected.as_bytes() ); } + + #[test] + fn test_ignore_matching_lines() { + use crate::bre::Bre; + use crate::params::Params; + + let params = |patterns: &[&str]| Params { + ignore_matching_lines: patterns + .iter() + .map(|pattern| Bre::new(pattern).unwrap()) + .collect(), + ..Default::default() + }; + + let from = b"1a\n2\n3a\n"; + let to = b"1b\n2\n3b\n"; + + // No -I: both hunks. + assert_eq!( + diff(from, to, &Params::default()), + b"1c1\n< 1a\n---\n> 1b\n3c3\n< 3a\n---\n> 3b\n".to_vec() + ); + + // -I 3: only the first hunk, because 3a/3b match 3. + assert_eq!( + diff(from, to, ¶ms(&["3"])), + b"1c1\n< 1a\n---\n> 1b\n".to_vec() + ); + + // -I 1: only the second hunk. + assert_eq!( + diff(from, to, ¶ms(&["1"])), + b"3c3\n< 3a\n---\n> 3b\n".to_vec() + ); + + // Multiple patterns are ORed: 1a/1b match 1, 3a/3b match 3. + assert_eq!(diff(from, to, ¶ms(&["1", "3"])), Vec::::new()); + + // A hunk with one non-matching changed line is kept. + assert_eq!( + diff(from, to, ¶ms(&["3", "x"])), + b"1c1\n< 1a\n---\n> 1b\n".to_vec() + ); + } } From ecd94e4fa8adae1fe6940a97a17f21577eedd045 Mon Sep 17 00:00:00 2001 From: small-pai <1809042530@qq.com> Date: Mon, 16 Nov 2026 23:06:19 +0800 Subject: [PATCH 3/4] diff: report differences when all hunks are ignored by -I --- src/diff.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index f6b353cb..9e445213 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -133,8 +133,14 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { } // identical contents need no diff, unless side by side output shows their - // lines or an ed script warns about a missing newline - if from_content == to_content && !matches!(params.format, Format::SideBySide | Format::Ed) { + // lines, an ed script warns about a missing newline, or an ifdef script + // shows the merged contents of the files + if from_content == to_content + && !matches!( + params.format, + Format::SideBySide | Format::Ed | Format::Ifdef + ) + { maybe_report_identical_files(); return Outcome::Identical; } @@ -245,8 +251,17 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { } } if !differs { - maybe_report_identical_files(); - Outcome::Identical + if params.format == Format::Normal + && !params.ignore_matching_lines.is_empty() + && from_content != to_content + { + // All hunks were filtered out by --ignore-matching-lines, but the + // files still differ, so report differences (exit status 1). + Outcome::Different + } else { + maybe_report_identical_files(); + Outcome::Identical + } } else { Outcome::Different } From 8e324cca50b666b30968f165a7de3a6d0b042788 Mon Sep 17 00:00:00 2001 From: small-pai <1809042530@qq.com> Date: Mon, 16 Nov 2026 23:06:19 +0800 Subject: [PATCH 4/4] diff: test -I/--ignore-matching-lines --- tests/by-util/test_diff.rs | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 314a0997..bc635249 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1024,6 +1024,49 @@ fn function_lines() { .stderr_is("'\\(': Unmatched ( or \\(\n"); } +#[rstest] +fn ignore_matching_lines( + #[values( + &["-I", "3"][..], + &["-I3"], + &["--ignore-matching-lines", "3"], + &["--ignore-matching-lines=3"] + )] + args: &[&str], +) { + let (at, ts) = at_and_ts!(); + at.write("a", "1a\n2\n3a\n"); + at.write("b", "1b\n2\n3b\n"); + ts.ucmd() + .args(args) + .args(&["a", "b"]) + .fails_with_code(1) + .stdout_is("1c1\n< 1a\n---\n> 1b\n"); +} + +#[test] +fn ignore_matching_lines_invalid_re() { + let (at, ts) = at_and_ts!(); + at.write("a", "1a\n2\n3a\n"); + at.write("b", "1b\n2\n3b\n"); + ts.ucmd() + .args(&["-Ix", "-I\\", "a", "b"]) + .fails_with_code(2) + .no_stdout() + .stderr_contains("'\\': Trailing backslash"); +} + +#[test] +fn ignore_matching_lines_all_filtered() { + let (at, ts) = at_and_ts!(); + at.write("a", "1a\n2\n3a\n"); + at.write("b", "1b\n2\n3b\n"); + ts.ucmd() + .args(&["-I", "1", "-I", "3", "a", "b"]) + .fails_with_code(1) + .no_stdout(); +} + // A function line is cut at 40 bytes, even inside a multi-byte character. #[test] fn function_line_is_cut_at_40_bytes() {