Skip to content
Closed
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
16 changes: 15 additions & 1 deletion src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,21 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
params.to_label.as_deref(),
&to_modified_time,
);
let diff_results = make_diff(expected, actual, params.context_count, params.brief);
let stop_early = params.brief && !params.ignore_blank_lines;
let mut diff_results = make_diff(expected, actual, params.context_count, stop_early);
if params.ignore_blank_lines {
diff_results.retain(|result| {
let all_expected_blank = result.expected.iter().all(|line| match line {
DiffLine::Change(l) | DiffLine::Add(l) => l.is_empty(),
DiffLine::Context(_) => true,
});
let all_actual_blank = result.actual.iter().all(|line| match line {
DiffLine::Change(l) | DiffLine::Add(l) => l.is_empty(),
DiffLine::Context(_) => true,
});
!(all_expected_blank && all_actual_blank)
});
}
if diff_results.is_empty() {
return Vec::new();
}
Expand Down
10 changes: 9 additions & 1 deletion src/ed_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,15 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result<Vec<Mis

pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Result<Vec<u8>, DiffError> {
let mut output = Vec::new();
let diff_results = make_diff(expected, actual, params.brief)?;
let stop_early = params.brief && !params.ignore_blank_lines;
let mut diff_results = make_diff(expected, actual, stop_early)?;
if params.ignore_blank_lines {
diff_results.retain(|result| {
let all_expected_blank = result.expected.iter().all(|l| l.is_empty());
let all_actual_blank = result.actual.iter().all(|l| l.is_empty());
!(all_expected_blank && all_actual_blank)
});
}
if params.brief && !diff_results.is_empty() {
write!(&mut output, "\0").unwrap();
return Ok(output);
Expand Down
10 changes: 9 additions & 1 deletion src/normal_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,15 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
// 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 stop_early = params.brief && !params.ignore_blank_lines;
let mut diff_results = make_diff(expected, actual, stop_early);
if params.ignore_blank_lines {
diff_results.retain(|result| {
let all_expected_blank = result.expected.iter().all(|l| l.is_empty());
let all_actual_blank = result.actual.iter().all(|l| l.is_empty());
!(all_expected_blank && all_actual_blank)
});
}
if params.brief && !diff_results.is_empty() {
write!(&mut output, "\0").unwrap();
return output;
Expand Down
16 changes: 16 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub struct Params {
/// (`-S`).
pub starting_file: Option<OsString>,
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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -281,6 +283,10 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
params.suppress_blank_empty = true;
continue;
}
if param == "-B" || param == "--ignore-blank-lines" {
params.ignore_blank_lines = true;
continue;
}
if param == "--normal" {
if format.is_some() && format != Some(Format::Normal) {
return Err("Conflicting output style options".to_string());
Expand Down Expand Up @@ -1511,4 +1517,14 @@ mod tests {
assert_eq!(params.ignore_file_name_case, expected);
}
}
#[test]
fn ignore_blank_lines() {
for args in [&["-B"][..], &["--ignore-blank-lines"]] {
let args = std::iter::once("diff")
.chain(args.iter().copied())
.chain(["foo", "bar"]);
let params = parse_params(args.map(os).peekable()).unwrap();
assert!(params.ignore_blank_lines);
}
}
}
16 changes: 15 additions & 1 deletion src/unified_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,21 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
params.to_label.as_deref(),
&to_modified_time,
);
let diff_results = make_diff(expected, actual, params.context_count, params.brief);
let stop_early = params.brief && !params.ignore_blank_lines;
let mut diff_results = make_diff(expected, actual, params.context_count, stop_early);
if params.ignore_blank_lines {
diff_results.retain(|result| {
let all_expected_blank = result.lines.iter().all(|line| match line {
DiffLine::Expected(l) => l.is_empty(),
_ => true,
});
let all_actual_blank = result.lines.iter().all(|line| match line {
DiffLine::Actual(l) => l.is_empty(),
_ => true,
});
!(all_expected_blank && all_actual_blank)
});
}
if diff_results.is_empty() {
return Vec::new();
}
Expand Down
39 changes: 39 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,3 +1131,42 @@ 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("b1", "a\n\nb\n");
at.write("b2", "a\nb\n");
for option in ["-B", "--ignore-blank-lines"] {
ts.ucmd()
.args(&[option, "b1", "b2"])
.succeeds()
.stdout_is("");
ts.ucmd()
.args(&["-u", option, "b1", "b2"])
.succeeds()
.stdout_is("");
ts.ucmd()
.args(&["-c", option, "b1", "b2"])
.succeeds()
.stdout_is("");
ts.ucmd()
.args(&["-e", option, "b1", "b2"])
.succeeds()
.stdout_is("");
ts.ucmd()
.args(&["-q", option, "b1", "b2"])
.succeeds()
.stdout_is("");
}

// Bug#18402: non-blank changes without a final newline are still reported.
at.write("b5", "a");
at.write("b6", "b");
ts.ucmd()
.args(&["-B", "b5", "b6"])
.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",
);
}
Loading