Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
}) => {
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();
}
Expand All @@ -320,9 +318,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
}) => {
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();
}
Expand Down Expand Up @@ -474,6 +470,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<usize>, given: Option<usize>) -> usize {
let given = given.unwrap_or(3);
current.map_or(given, |current| current.max(given))
}

struct DiffStyleMatch {
is_match: bool,
context_count: Option<usize>,
Expand Down
41 changes: 41 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,47 @@ fn ed_format_dot_line_ends_text_once() -> Result<(), Box<dyn std::error::Error>>
Ok(())
}

#[test]
fn largest_context_length_wins() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
let old = tmp_dir.path().join("old");
let new = tmp_dir.path().join("new");

// The case from issue #340, GNU diff merges both changes into one hunk.
std::fs::write(&old, "a\nb\nc\nd\ne\nf\ng\nh\n")?;
std::fs::write(&new, "a\nB\nc\nd\ne\nf\nG\nh\n")?;
let mut cmd = cargo_bin_cmd!("diffutils");
cmd.arg("diff").arg("-U1").arg("-u").arg(&old).arg(&new);
cmd.assert()
.code(predicate::eq(1))
.stdout(predicate::str::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.
let lines: String = (1..=20).map(|i| format!("{i}\n")).collect();
std::fs::write(&old, &lines)?;
std::fs::write(&new, lines.replace("\n10\n", "\nX\n"))?;
for (args, header) in [
(vec!["-U1", "-u"], "\n@@ -7,7 +7,7 @@\n"),
(vec!["-u", "-U1"], "\n@@ -7,7 +7,7 @@\n"),
(vec!["-U5", "-u"], "\n@@ -5,11 +5,11 @@\n"),
(vec!["-U2", "-U1"], "\n@@ -8,5 +8,5 @@\n"),
(vec!["--unified=1", "--unified"], "\n@@ -7,7 +7,7 @@\n"),
(vec!["-5u"], "\n@@ -5,11 +5,11 @@\n"),
(vec!["-C1", "-c"], "\n*** 7,13 ****\n"),
(vec!["--context=1", "--context"], "\n*** 7,13 ****\n"),
(vec!["--context", "--context=1"], "\n*** 7,13 ****\n"),
(vec!["-5c"], "\n*** 5,15 ****\n"),
] {
let mut cmd = cargo_bin_cmd!("diffutils");
cmd.arg("diff").args(&args).arg(&old).arg(&new);
cmd.assert()
.code(predicate::eq(1))
.stdout(predicate::str::contains(header));
}
Ok(())
}

#[test]
fn function_lines() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
Expand Down
Loading