Skip to content

diff: add -B/--ignore-blank-lines - #419

Open
yunusarda-sudo wants to merge 2 commits into
uutils:mainfrom
yunusarda-sudo:diff-ignore-blank-lines
Open

yunusarda-sudo wants to merge 2 commits into
uutils:mainfrom
yunusarda-sudo:diff-ignore-blank-lines

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

Add -B / --ignore-blank-lines to ignore changes that consist entirely of inserted or deleted blank lines across normal, unified, context, and ed output formats.

Closes #407

Comment thread src/utils.rs Outdated
/// 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')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GNU -B only ignores empty lines, unless -b or -w is given too (see info diff, "Blank Lines").
with this, a\n \nb\n vs a\nb\n exits 0, GNU exits 1.
please add a test for it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit a9e187c: restricted is_blank_line to empty lines only (matching GNU diff), and added a test showing a\n \nb\n vs a\nb\n fails with code 1 unless -b/-w is passed.

Comment thread src/unified_diff.rs Outdated
.lines
.iter()
.any(|l| matches!(l, DiffLine::MissingNL));
if has_missing_nl {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also matches the MissingNL after a common last context line.
printf 'a\n\nb' vs printf 'a\nb' with -u -B prints a hunk, but the normal format prints nothing.
same in context_diff.rs. please add a test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit a9e187c: common context line missing newline is no longer incorrectly treated as a blank change, and a test for printf "a\n\nb" vs printf "a\nb" was added.

Comment thread src/normal_diff.rs Outdated
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once blank means empty, can this missing_nl case still happen?
it isn't checked in ed_diff.rs either

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in commit a9e187c: the missing_nl check is cleaned up and unified across all diff formatters.

Comment thread src/context_diff.rs
DiffLine::Context(_) => false,
});
expected_has_non_blank || actual_has_non_blank
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost the same filter 4 times, could it be a shared helper in utils.rs?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit a9e187c: extracted has_non_blank_lines helper in src/utils.rs shared across normal, unified, context, and ed formats.

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"]] {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about -y? -B is silently ignored there.
please also add a test where a blank change and a real change are in the same hunk

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit a9e187c: added support for -B in side-by-side (-y) format (using ( and ) gutter markers for suppressed blank changes matching GNU diff), and added a test with both a blank insertion and a real change in the same hunk.

@yunusarda-sudo
yunusarda-sudo force-pushed the diff-ignore-blank-lines branch from b4150ad to 4605e4d Compare November 22, 2026 01:47
@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

Thanks for the review — these points are in the current patch:

  • -B only ignores completely empty lines, unless -b or -w is also given. There is a test where a\n \nb\n vs a\nb\n exits 1 and shows the space-only line.
  • Unified and context formats treat a missing newline after a common last context line the same way as the normal format; there is a test for that (printf 'a\n\nb' vs printf 'a\nb' with -u/-c/ ).
  • Blank-line filtering is shared through is_blank_line in utils.rs.
  • -y honors -B, and there is a test where a blank change and a real change share a hunk.
@yunusarda-sudo
yunusarda-sudo force-pushed the diff-ignore-blank-lines branch from c2be386 to c543fcc Compare November 26, 2026 19:14
@github-actions

Copy link
Copy Markdown

GNU diffutils testsuite comparison:

Test results comparison:
  Current:   TOTAL: 33 / PASSED: 22 / FAILED: 7 / SKIPPED: 4
  Reference: TOTAL: 33 / PASSED: 21 / FAILED: 8 / SKIPPED: 4

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + no-newline-at-eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants