Skip to content

sdiff: implement the sdiff utility - #399

Open
shadowmkj wants to merge 1 commit into
uutils:mainfrom
shadowmkj:feat/sdiff
Open

shadowmkj wants to merge 1 commit into
uutils:mainfrom
shadowmkj:feat/sdiff

Conversation

@shadowmkj

Copy link
Copy Markdown
Contributor

Issue: #16

Initial implementation of sdiff. It utilizes the existing side_diff formatter to print diff side by side.
Also added 2 extra options --suppress-common-lines and --left-column.

About 500 lines changes has been made, so I thought about opening this PR to get feedback on whether I'm going in the right direction.

Next phases would be to add integration tests and support the interactive diff merging workflow in GNU sdiff.

Changes

  • Implemented sdiff command-line parsing and file comparison controller in src/sdiff.rs.
  • Added support for -w/--width, -l/--left-column, -s/--suppress-common-lines, -t/--expand-tabs, -a/--text, and --tabsize.
  • Updated side_diff::diff to track line differences so callers return exit code 1 when differences exist and 0 when files match.
  • Registered sdiff in the multi-call binary dispatcher (src/main.rs) and exposed the module in src/lib.rs.
  • Added unit tests covering option parsing, tab size / width validation, exit code outcomes, and side-by-side formatting.

@shadowmkj
shadowmkj marked this pull request as ready for review November 15, 2026 15:51
Comment thread src/side_diff.rs
if config.suppress_common_lines {
continue;
}
if config.left_column {

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.

i think GNU still prints the ( marker in the gutter with --left-column, not just the bare line
could you please check against GNU's output and 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.

You are right, but I found there is some issue in the diff engine itself.
https://github.com/uutils/diffutils/blob/main/src/side_diff.rs#L311-L352
There is also a disclaimer in the file stating the output is different.

This should be a separate issue?

Comment thread src/side_diff.rs Outdated
// `side_diff::diff` streams its output directly into the `output` writer.
// Callers (such as `diff -y` and `sdiff`) check `result.is_empty()` to
// determine whether differences were detected and return exit code 1 vs 0.
// Returning `vec![0]` when differences were found signals `Outcome::Different`.

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.

a vec![0] sentinel is hacky, could diff return a bool instead?
also this changes diff -y (exit code 1, headers), please add a test for that in tests/by-util/test_diff.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.

Changed return type to bool and added tests

Comment thread src/sdiff.rs
continue;
}

if param == "-w" || param == "--width" {

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 width parsing is almost the same code 3 times, could be dedup, no?
and params.rs already parses --width / --tabsize

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.

But the parameters for diff and sdiff is different in GNU.

Example:
diff uses -W for width and -w for ignoring whitespaces
sdiff uses -W to ignore whitespace and -w for width

🤷‍♂️
So I would need to parse width again right? If there is another solution you have in mind, pls let me know

Comment thread src/sdiff.rs Outdated
format!("Usage: {} <from> <to>", executable.to_string_lossy())
}

fn is_binary(content: &[u8]) -> bool {

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.

duplicate of is_binary in diff.rs, please share 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.

I moved is_binary and read_file_contents functions from src/diff.rs to src/utils.rs so it can be shared between sdiff and diff

Comment thread src/sdiff.rs Outdated
}

let mut io_error = false;
let from_content = match fs::read(&params.from) {

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.

- (stdin) isn't supported here; diff.rs already has read_file_contents, could it be reused?

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 found out that GNU sdiff does not support reading from stdin.
So I did not reuse this because of that. Should I reuse this part and make sdiff support reading from stdin?

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.

My bad sorry! https://man7.org/linux/man-pages/man1/sdiff.1.html this says that - can be used.

I referred to the documentation given by the person who made the initial issue #16
https://www.gnu.org/software/diffutils/manual/html_node/Invoking-sdiff.html

In this doc it says

You may not use - as an input file.

Comment thread src/sdiff.rs
return Outcome::Identical;
}
eprintln!(
"Binary files {} and {} differ",

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.

does GNU write this to stderr?

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.

Just checked it, GNU writes it to stdout. fixing it now

Comment thread src/sdiff.rs Outdated
/// for example: `-w` sets the columns in `sdiff` but in `diff` it ignores whitespace
/// https://www.gnu.org/software/diffutils/manual/html_node/diff-Options.html
/// https://www.gnu.org/software/diffutils/manual/html_node/sdiff-Options.html
pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Result<Params, String> {

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 add integration tests in tests/by-util/test_sdiff.rs :)

Comment thread src/sdiff.rs Outdated
params.width = val_str
.parse::<usize>()
.map_err(|_| format!("invalid width '{val_str}'"))?;
if params.width < 7 {

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 use a thiserror enum instead of String errors

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.

Will do, I will have to add thiserror to the dependencies as the project does not use it at all.

@sylvestre

Copy link
Copy Markdown
Collaborator

thanks for picking this up!
The direction is fine, but sdiff.rs redoes what diff.rs and params.rs already do (reading files, stdin, binary detection, parsing --width/--tabsize). Please reuse that code.

Comment thread src/diff.rs
}
};
return if has_diff {
if params.brief {

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 -y -q the side-by-side output is already streamed before this, so we print both the diff and "Files differ"
GNU prints only the line, no?
please add a test for diff -y -q too

Comment thread src/diff.rs
result
}
},
Format::SideBySide => 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.

could we avoid this unreachable!()? e.g. keep side by side as an arm of the match
or at least add a comment explaining why

Comment thread src/sdiff.rs
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Outcome {

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 is the same Outcome + From impl as in diff.rs, could be shared, no?

Comment thread src/sdiff.rs
continue;
}

if param_str.starts_with("-w") && param_str.len() > 2 {

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.

grouped short options like -sl or -tw80 are rejected as unrecognized, GNU accepts them
could you please handle that and 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.

Yeah, I've seen a lot of issues that popped up because of the way GNU handles options.
I left a comment on an issue #396 (comment)
It is about using clap, please give me a response to that when you can, thanks

@sylvestre

Copy link
Copy Markdown
Collaborator

thanks for working on this!
cargo test (windows-latest) is red and it looks like the new sdiff tests, please fix it.
Please also squash the 7 commits into one.

sdiff: add support for left-column and suppress-common-lines options

test(side_diff): add unit tests for side diff features

sdiff: add sdiff utility implementation

- register the new sdiff module in lib.rs and main.rs
- update binary dispatch and usage text to support the sdiff command

side_diff: render `(` when --left-column is set

sdiff: change diff return type from Vec<u8> to bool

refactor: extract file reading and binary detection utilities

- Move `read_file_contents` and `is_binary` functions from `src/diff.rs` to `src/utils.rs` to improve code reuse
- Add the `thiserror` dependency to `Cargo.toml`

sdiff: add test suite for sdiff utility
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.

2 participants