Skip to content

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

Open
yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:diff-ignore-blank-lines
Open

yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:diff-ignore-blank-lines

Conversation

@yunusarda-sudo

Copy link
Copy Markdown

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
/// 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

Comment thread src/unified_diff.rs
.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

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

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?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

diff - -B / --ignore-blank-lines is not implemented

2 participants