Skip to content

diff: add -D/--ifdef format support - #422

Open
yunusarda-sudo wants to merge 4 commits into
uutils:mainfrom
yunusarda-sudo:diff-ifdef
Open

yunusarda-sudo wants to merge 4 commits into
uutils:mainfrom
yunusarda-sudo:diff-ifdef

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

Implement -D NAME and --ifdef=NAME to output merged files with C preprocessor conditional directives (#ifndef, #ifdef, #else, and #endif), matching GNU diff.

Closes #410

@github-actions

github-actions Bot commented Nov 19, 2026 •

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):
  + ifdef

Comment thread src/ifdef_diff.rs
&mut differs,
);
do_write_line(&mut output, str1, params.expand_tabs, params.tabsize).unwrap();
output.push(b'\n');

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 always adds a '\n', so identical files a\nb (no final newline) give a\nb\n.
GNU keeps the file as is, no?
please add a test with a missing newline at EOF, on one side and on both

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.

I checked GNU diff 3.10 on this. diff -D FOO on identical files whose last line has no newline (a\nb) writes a\nb\n and exits 0 — it does not keep the raw missing-newline bytes. The same extra newline is used when both sides lack a newline but the last lines differ. When only one side has a newline, GNU wraps that last line in #ifndef / #else, and ifdef_diff_missing_newline covers all three cases.

Comment thread src/diff.rs Outdated
let (result, differs) = match params.format {
Format::Normal => {
let res = normal_diff::diff(&from_content, &to_content, params);
let differs = !res.is_empty();

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 code 5 times :)
could the match keep returning a Vec<u8>, with differs only special-cased for Ifdef?

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 — compare_files now keeps the match returning a Vec<u8>, and differs is only special-cased for Ifdef.

Comment thread src/diff.rs Outdated
if !result.is_empty() {
let res = side_diff::diff(&from_content, &to_content, &mut output, params);
let differs = !res.is_empty();
if !res.is_empty() {

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.

if differs?

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 — the side-by-side header path now uses if differs.

Comment thread src/ifdef_diff.rs Outdated
match (!current_expected.is_empty(), !current_actual.is_empty()) {
(true, false) => {
writeln!(output, "#ifndef {ifdef_name}").unwrap();
for line in current_expected.drain(..) {

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.

the same loop 4 times, could you please move it into a small helper?

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 — the four directive/line loops now go through flush_hunk, write_directive and write_lines.

Comment thread src/ifdef_diff.rs Outdated
let mut line_number_actual = 1;
let mut differs = false;

let mut current_expected: Vec<Vec<u8>> = Vec::new();

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.

why copy each line? Vec<&[u8]> should be enough

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 — the hunk buffers are Vec<&[u8]> now.

Comment thread src/params.rs Outdated
}
format = Some(Format::Ifdef);
let val = argument_value(argument, &param, &mut opts)?;
params.ifdef_name = Some(val.to_string_lossy().into_owned());

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.

please keep it as OsString (or bytes), a non-UTF-8 name gets mangled here

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 — ifdef_name is an OsString, and the output uses OsStr::as_encoded_bytes. ifdef_diff_non_utf8_macro covers a non-UTF-8 -D value.

@sylvestre

Copy link
Copy Markdown
Collaborator

Could you please verify the output of your LLM ?
it isn't great here :(

@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

Updated and rebased onto latest main. Fixed compatibility with the newly introduced do_write_line marker parameter from #384, refactored chunk emission into a clean helper, and verified all 358 tests (170 unit + 188 integration) pass cleanly along with clippy and formatting.

@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

Thanks for the review — the current patch takes the inline comments:

  • Identical files without a final newline are covered by a test. I also checked GNU diff 3.10: diff -D FOO on two copies of a\nb (no final newline) prints a\nb\n, so this matches that behavior.
  • The format dispatch keeps returning a Vec<u8>, with differs special-cased for ifdef.
  • The four directive loops share write_directive / write_lines / flush_hunk.
  • Line lists are Vec<&[u8]> (no per-line copy).
  • The macro name is kept as OsString; a non-UTF-8 -D name is covered by a test.

Comment thread src/diff.rs Outdated
}
}
if result.is_empty() {
let differs = if params.format == Format::Ifdef {

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.

differs is already computed above, could you reuse 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 — the exit path now reuses the differs computed after the format match.

Comment thread src/ifdef_diff.rs Outdated
write_lines(output, current_actual, expand_tabs, tabsize);
write_directive(output, b"#endif /* ", ifdef_name, Some(b" */"));
}
(false, false) => unreachable!(),

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.

with the early return above, could (false, false) => return false replace both?

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 — the empty case is handled by (false, false) => return false, and the early return is gone.

Comment thread src/ifdef_diff.rs Outdated

fn write_lines(output: &mut Vec<u8>, lines: &[&[u8]], expand_tabs: bool, tabsize: usize) {
for &line in lines {
do_write_line(output, line, expand_tabs, tabsize, b"").unwrap();

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.

please avoid unwrap(), unified_diff uses .expect("write to Vec is infallible")

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 — writes to the Vec now use .expect("write to Vec is infallible").

Comment thread src/params.rs Outdated
continue;
}
// -D NAME, -DNAME, --ifdef NAME or --ifdef=NAME
let ifdef = if bytes == b"-D" || bytes == b"--ifdef" {

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 code as the -L block above, could it be a small helper?

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 — -L/--label and -D/--ifdef now share option_os_value.

yunusarda-sudo and others added 4 commits November 25, 2026 07:58
Reuse the already-computed differs flag for the exit outcome.
In flush_hunk, return false from the (false, false) arm and drop
the matching early return.
Parse -L/--label and -D/--ifdef through a shared helper, and use
expect("write to Vec is infallible") for ifdef output writes.
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 - -D / --ifdef output format is not implemented

2 participants