Repository navigation
sdiff: implement the sdiff utility
#399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shadowmkj
wants to merge
1
commit into
uutils:main
Choose a base branch
from
shadowmkj:feat/sdiff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,146
−55
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,13 @@ | |
|
|
||
| use crate::color::{colorize, ColorWhen, Palette, PaletteError}; | ||
| use crate::params::{parse_params, Format, MissingFile, Params}; | ||
| use crate::utils::report_failure_to_read_input_file; | ||
| use crate::utils::{is_binary, read_file_contents, report_failure_to_read_input_file}; | ||
| use crate::{context_diff, dir_diff, ed_diff, normal_diff, side_diff, unified_diff}; | ||
| use std::env::ArgsOs; | ||
| use std::ffi::OsString; | ||
| #[cfg(any(target_os = "linux", target_os = "android"))] | ||
| use std::fs; | ||
| use std::io::{self, stdout, IsTerminal, Read, Write}; | ||
| use std::io::{self, stdout, IsTerminal, Write}; | ||
| use std::iter::Peekable; | ||
| use std::path::Path; | ||
| use std::process::ExitCode; | ||
|
|
@@ -88,15 +89,6 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { | |
| return Outcome::Identical; | ||
| } | ||
|
|
||
| // read files | ||
| fn read_file_contents(filepath: &OsString) -> io::Result<Vec<u8>> { | ||
| if filepath == "-" { | ||
| let mut content = Vec::new(); | ||
| io::stdin().read_to_end(&mut content).and(Ok(content)) | ||
| } else { | ||
| fs::read(filepath) | ||
| } | ||
| } | ||
| let mut io_error = false; | ||
| let mut from_missing = false; | ||
| let mut to_missing = false; | ||
|
|
@@ -154,6 +146,32 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { | |
| ..params.clone() | ||
| }; | ||
|
|
||
| if params.format == Format::SideBySide { | ||
| let has_diff = match header { | ||
| None => side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params), | ||
| Some(header) => { | ||
| let mut output = Vec::new(); | ||
| let has_diff = side_diff::diff(&from_content, &to_content, &mut output, params); | ||
| if has_diff { | ||
| output.splice(0..0, header.iter().copied()); | ||
| } | ||
| if stdout().write_all(&output).is_err() { | ||
| return Outcome::Trouble; | ||
| } | ||
| has_diff | ||
| } | ||
| }; | ||
| return if has_diff { | ||
| if params.brief { | ||
| print_files_differ("Files", params); | ||
| } | ||
| Outcome::Different | ||
| } else { | ||
| maybe_report_identical_files(); | ||
| Outcome::Identical | ||
| }; | ||
| } | ||
|
|
||
| // run diff | ||
| let result: Vec<u8> = match params.format { | ||
| Format::Normal => normal_diff::diff(&from_content, &to_content, params), | ||
|
|
@@ -166,26 +184,11 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { | |
| return Outcome::Trouble; | ||
| } | ||
| }, | ||
| // Side by side output is streamed, unless a header may have to come | ||
| // first. | ||
| Format::SideBySide => match header { | ||
| None => side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params), | ||
| Some(header) => { | ||
| let mut output = Vec::new(); | ||
| let result = side_diff::diff(&from_content, &to_content, &mut output, params); | ||
| if !result.is_empty() { | ||
| output.splice(0..0, header.iter().copied()); | ||
| } | ||
| if stdout().write_all(&output).is_err() { | ||
| return Outcome::Trouble; | ||
| } | ||
| result | ||
| } | ||
| }, | ||
| Format::SideBySide => unreachable!(), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we avoid this |
||
| }; | ||
| if params.brief && !result.is_empty() { | ||
| print_files_differ("Files", params); | ||
| } else if params.format != Format::SideBySide { | ||
| } else { | ||
| let header = if result.is_empty() { | ||
| &[][..] | ||
| } else { | ||
|
|
@@ -249,14 +252,6 @@ fn output_palette(params: &Params) -> Option<Palette> { | |
| } | ||
| } | ||
|
|
||
| /// Number of leading bytes inspected when deciding whether an input is binary. | ||
| const BINARY_PROBE_LEN: usize = 16 * 1024; | ||
|
|
||
| /// An input is treated as binary when a NUL byte occurs near its start. | ||
| fn is_binary(content: &[u8]) -> bool { | ||
| content[..content.len().min(BINARY_PROBE_LEN)].contains(&0) | ||
| } | ||
|
|
||
| /// Print the one-line summary used instead of the differences, `kind` naming | ||
| /// what was compared. | ||
| fn print_files_differ(kind: &str, params: &Params) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with
-y -qthe 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 -qtoo