Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ diff = "0.1.13"
itoa = "1.0.11"
regex = "1.10.4"
same-file = "1.0.6"
thiserror = "2.0.21"
unicode-width = "0.2.0"

[dev-dependencies]
Expand Down
67 changes: 31 additions & 36 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {

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

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),
Expand All @@ -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!(),

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

};
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 {
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod ed_diff;
pub mod macros;
pub mod normal_diff;
pub mod params;
pub mod sdiff;
pub mod side_diff;
pub mod unified_diff;
pub mod utils;
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod ed_diff;
mod macros;
mod normal_diff;
mod params;
mod sdiff;
mod side_diff;
mod unified_diff;
mod utils;
Expand All @@ -46,7 +47,7 @@ fn usage(name: &str) {
println!("{name} {VERSION} (multi-call binary)\n");
println!("Usage: {name} [function [arguments...]]\n");
println!("Currently defined functions:\n");
println!(" cmp, diff\n");
println!(" cmp, diff, sdiff\n");
}

fn second_arg_error(name: &OsStr) -> ! {
Expand All @@ -73,6 +74,7 @@ fn main() -> ExitCode {
};

match util_name.as_encoded_bytes() {
name if name.ends_with(b"sdiff") => sdiff::main(args),
name if name.ends_with(b"diff") => diff::main(args),
name if name.ends_with(b"cmp") => cmp::main(args),
name => {
Expand Down
6 changes: 6 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ pub struct Params {
pub to_missing: bool,
pub from_label: Option<OsString>,
pub to_label: Option<OsString>,
/// Print only the left column of two common lines.
pub left_column: bool,
/// Do not print common lines
pub suppress_common_lines: bool,
}

impl Params {
Expand Down Expand Up @@ -113,6 +117,8 @@ impl Default for Params {
to_missing: false,
from_label: None,
to_label: None,
left_column: false,
suppress_common_lines: false,
}
}
}
Expand Down
Loading
Loading