Repository navigation
Conversation
| if config.suppress_common_lines { | ||
| continue; | ||
| } | ||
| if config.left_column { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
| // `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`. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Changed return type to bool and added tests
| continue; | ||
| } | ||
|
|
||
| if param == "-w" || param == "--width" { |
There was a problem hiding this comment.
the width parsing is almost the same code 3 times, could be dedup, no?
and params.rs already parses --width / --tabsize
There was a problem hiding this comment.
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
| format!("Usage: {} <from> <to>", executable.to_string_lossy()) | ||
| } | ||
|
|
||
| fn is_binary(content: &[u8]) -> bool { |
There was a problem hiding this comment.
duplicate of is_binary in diff.rs, please share it
There was a problem hiding this comment.
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
| } | ||
|
|
||
| let mut io_error = false; | ||
| let from_content = match fs::read(¶ms.from) { |
There was a problem hiding this comment.
- (stdin) isn't supported here; diff.rs already has read_file_contents, could it be reused?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| return Outcome::Identical; | ||
| } | ||
| eprintln!( | ||
| "Binary files {} and {} differ", |
There was a problem hiding this comment.
does GNU write this to stderr?
There was a problem hiding this comment.
Just checked it, GNU writes it to stdout. fixing it now
| /// 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> { |
There was a problem hiding this comment.
please add integration tests in tests/by-util/test_sdiff.rs :)
| params.width = val_str | ||
| .parse::<usize>() | ||
| .map_err(|_| format!("invalid width '{val_str}'"))?; | ||
| if params.width < 7 { |
There was a problem hiding this comment.
please use a thiserror enum instead of String errors
There was a problem hiding this comment.
Will do, I will have to add thiserror to the dependencies as the project does not use it at all.
|
thanks for picking this up! |
| } | ||
| }; | ||
| return if has_diff { | ||
| if params.brief { |
There was a problem hiding this comment.
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
| result | ||
| } | ||
| }, | ||
| Format::SideBySide => unreachable!(), |
There was a problem hiding this comment.
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
| } | ||
|
|
||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| pub enum Outcome { |
There was a problem hiding this comment.
this is the same Outcome + From impl as in diff.rs, could be shared, no?
| continue; | ||
| } | ||
|
|
||
| if param_str.starts_with("-w") && param_str.len() > 2 { |
There was a problem hiding this comment.
grouped short options like -sl or -tw80 are rejected as unrecognized, GNU accepts them
could you please handle that and add a test?
There was a problem hiding this comment.
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
|
thanks for working on this! |
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
7c13a8b to
f18db1d
Compare
Issue: #16
Initial implementation of
sdiff. It utilizes the existingside_diffformatter to print diff side by side.Also added 2 extra options
--suppress-common-linesand--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
sdiffcommand-line parsing and file comparison controller insrc/sdiff.rs.-w/--width,-l/--left-column,-s/--suppress-common-lines,-t/--expand-tabs,-a/--text, and--tabsize.side_diff::diffto track line differences so callers return exit code 1 when differences exist and 0 when files match.sdiffin the multi-call binary dispatcher (src/main.rs) and exposed the module insrc/lib.rs.