From ceca2541645ba11885fda92eef8291f0a1557240 Mon Sep 17 00:00:00 2001 From: qodelicious <162776175+qodelicious@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:41:42 +0000 Subject: [PATCH 1/3] diff: use the largest context length given, like GNU diff --- src/params.rs | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/params.rs b/src/params.rs index 73ea45e0..de2d82e3 100644 --- a/src/params.rs +++ b/src/params.rs @@ -348,9 +348,7 @@ pub fn parse_params>( }) => { if is_match { format = Some(Format::Context); - if context_count.is_some() { - context = context_count; - } + context = Some(largest_context(context, context_count)); if next_param_consumed { opts.next(); } @@ -367,9 +365,7 @@ pub fn parse_params>( }) => { if is_match { format = Some(Format::Unified); - if context_count.is_some() { - context = context_count; - } + context = Some(largest_context(context, context_count)); if next_param_consumed { opts.next(); } @@ -521,6 +517,13 @@ fn push_function_line_pattern(params: &mut Params, pattern: &str) -> Result<(), Ok(()) } +/// GNU diff uses the largest context length given, and a bare -c or -u +/// counts as a request for 3 lines. +fn largest_context(current: Option, given: Option) -> usize { + let given = given.unwrap_or(3); + current.map_or(given, |current| current.max(given)) +} + struct DiffStyleMatch { is_match: bool, context_count: Option, @@ -1511,4 +1514,28 @@ mod tests { assert_eq!(params.ignore_file_name_case, expected); } } + + #[test] + fn largest_context_count_wins() { + for (args, expected) in [ + (vec!["-U1", "-u"], 3), + (vec!["-u", "-U1"], 3), + (vec!["-U0", "-u"], 3), + (vec!["-U5", "-u"], 5), + (vec!["-U1", "-U2"], 2), + (vec!["-U2", "-U1"], 2), + (vec!["-C1", "-c"], 3), + (vec!["-c", "-C1"], 3), + (vec!["-C", "7", "-C2"], 7), + (vec!["--unified=1", "--unified"], 3), + (vec!["-U1"], 1), + (vec!["-C0"], 0), + ] { + let mut params = vec!["diff"]; + params.extend(&args); + params.extend(["foo", "bar"]); + let parsed = parse_params(params.iter().map(|x| os(x)).peekable()).unwrap(); + assert_eq!(parsed.context_count, expected, "{args:?}"); + } + } } From 907ca7f44e69ff3540103c49fce82d3b673f18f0 Mon Sep 17 00:00:00 2001 From: qodelicious <162776175+qodelicious@users.noreply.github.com> Date: Sun, 8 Nov 2026 01:26:40 +0000 Subject: [PATCH 2/3] diff: move the context length tests to test_diff.rs Cover the -U1 -u case from #340, --context=1 --context and the -5u form, with the expected hunks checked against GNU diff 3.10. --- src/params.rs | 24 ------------------------ tests/by-util/test_diff.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/params.rs b/src/params.rs index de2d82e3..f27b376d 100644 --- a/src/params.rs +++ b/src/params.rs @@ -1514,28 +1514,4 @@ mod tests { assert_eq!(params.ignore_file_name_case, expected); } } - - #[test] - fn largest_context_count_wins() { - for (args, expected) in [ - (vec!["-U1", "-u"], 3), - (vec!["-u", "-U1"], 3), - (vec!["-U0", "-u"], 3), - (vec!["-U5", "-u"], 5), - (vec!["-U1", "-U2"], 2), - (vec!["-U2", "-U1"], 2), - (vec!["-C1", "-c"], 3), - (vec!["-c", "-C1"], 3), - (vec!["-C", "7", "-C2"], 7), - (vec!["--unified=1", "--unified"], 3), - (vec!["-U1"], 1), - (vec!["-C0"], 0), - ] { - let mut params = vec!["diff"]; - params.extend(&args); - params.extend(["foo", "bar"]); - let parsed = parse_params(params.iter().map(|x| os(x)).peekable()).unwrap(); - assert_eq!(parsed.context_count, expected, "{args:?}"); - } - } } diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 6173c199..4fa50317 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -915,6 +915,43 @@ fn ed_format(#[case] old: &str, #[case] new: &str, #[case] expected: &str) { .stdout_is(expected); } +#[test] +fn largest_context_length_wins_issue_340() { + // The case from issue #340, GNU diff merges both changes into one hunk. + let (at, ts) = at_and_ts!(); + at.write("old", "a\nb\nc\nd\ne\nf\ng\nh\n"); + at.write("new", "a\nB\nc\nd\ne\nf\nG\nh\n"); + ts.ucmd() + .args(&["-U1", "-u", "old", "new"]) + .fails_with_code(1) + .stdout_contains("\n@@ -1,8 +1,8 @@\n"); +} + +// With one changed line in the middle, the hunk header shows how many lines +// of context were used. +#[rstest] +#[case::unified_then_bare(&["-U1", "-u"], "\n@@ -7,7 +7,7 @@\n")] +#[case::bare_then_unified(&["-u", "-U1"], "\n@@ -7,7 +7,7 @@\n")] +#[case::larger_unified(&["-U5", "-u"], "\n@@ -5,11 +5,11 @@\n")] +#[case::two_unified(&["-U2", "-U1"], "\n@@ -8,5 +8,5 @@\n")] +#[case::long_unified(&["--unified=1", "--unified"], "\n@@ -7,7 +7,7 @@\n")] +#[case::number_u(&["-5u"], "\n@@ -5,11 +5,11 @@\n")] +#[case::context_then_bare(&["-C1", "-c"], "\n*** 7,13 ****\n")] +#[case::long_context_then_bare(&["--context=1", "--context"], "\n*** 7,13 ****\n")] +#[case::bare_then_long_context(&["--context", "--context=1"], "\n*** 7,13 ****\n")] +#[case::number_c(&["-5c"], "\n*** 5,15 ****\n")] +fn largest_context_length_wins(#[case] options: &[&str], #[case] header: &str) { + let (at, ts) = at_and_ts!(); + let lines: String = (1..=20).map(|i| format!("{i}\n")).collect(); + at.write("old", &lines); + at.write("new", &lines.replace("\n10\n", "\nX\n")); + ts.ucmd() + .args(options) + .args(&["old", "new"]) + .fails_with_code(1) + .stdout_contains(header); +} + #[test] fn function_lines() { let (at, ts) = at_and_ts!(); From 806fe329324591c613afd0b23eba1c436eda5c39 Mon Sep 17 00:00:00 2001 From: qodelicious <162776175+qodelicious@users.noreply.github.com> Date: Fri, 20 Nov 2026 00:49:59 +0000 Subject: [PATCH 3/3] diff: take the default context length from Params::default() --- src/params.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/params.rs b/src/params.rs index f27b376d..98bc1ba8 100644 --- a/src/params.rs +++ b/src/params.rs @@ -518,9 +518,9 @@ fn push_function_line_pattern(params: &mut Params, pattern: &str) -> Result<(), } /// GNU diff uses the largest context length given, and a bare -c or -u -/// counts as a request for 3 lines. +/// counts as a request for the default length. fn largest_context(current: Option, given: Option) -> usize { - let given = given.unwrap_or(3); + let given = given.unwrap_or_else(|| Params::default().context_count); current.map_or(given, |current| current.max(given)) }