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
51 changes: 34 additions & 17 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,29 +155,45 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome {
};

// run diff
let mut side_by_side_different = false;
let result: Vec<u8> = match params.format {
Format::Normal => normal_diff::diff(&from_content, &to_content, params),
Format::Unified => unified_diff::diff(&from_content, &to_content, params),
Format::Context => context_diff::diff(&from_content, &to_content, params),
Format::Ed => ed_diff::diff(&from_content, &to_content, params),
// 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;
Format::SideBySide => {
let different = if params.brief {
side_diff::diff(&from_content, &to_content, &mut std::io::sink(), params)
} else {
match header {
None => {
side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params)
}
Some(header) => {
let mut output = Vec::new();

let different =
side_diff::diff(&from_content, &to_content, &mut output, params);

if different {
output.splice(0..0, header.iter().copied());
}

if stdout().write_all(&output).is_err() {
return Outcome::Trouble;
}

different
}
}
result
}
},
};

side_by_side_different = different;
Vec::new()
}
};
if params.brief && !result.is_empty() {
let different = side_by_side_different || !result.is_empty();
if params.brief && different {
print_files_differ("Files", params);
} else if params.format != Format::SideBySide {
let header = if result.is_empty() {
Expand Down Expand Up @@ -223,7 +239,8 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome {
return Outcome::Trouble;
}
}
if result.is_empty() {

if !different {
maybe_report_identical_files();
Outcome::Identical
} else {
Expand Down
28 changes: 14 additions & 14 deletions src/side_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use core::cmp::{max, min};
use diff::Result;
use std::{io::Write, vec};
use std::io::Write;
use unicode_width::UnicodeWidthStr;

use crate::params::Params;
Expand Down Expand Up @@ -309,14 +309,8 @@ fn push_output<T: Write>(
Ok(())
}

pub fn diff<T: Write>(
from_file: &[u8],
to_file: &[u8],
output: &mut T,
params: &Params,
) -> Vec<u8> {
pub fn diff<T: Write>(from_file: &[u8], to_file: &[u8], output: &mut T, params: &Params) -> bool {
// ^ The left file ^ The right file

let mut left_lines = comparison_lines_inclusive(from_file, params);
let mut right_lines = comparison_lines_inclusive(to_file, params);
let config = Config::new(params.width, params.tabsize, params.expand_tabs);
Expand All @@ -328,7 +322,6 @@ pub fn diff<T: Write>(
if right_lines.last().is_some_and(|l| l.raw.is_empty()) {
right_lines.pop();
}

/*
DISCLAIMER:
Currently the diff engine does not produce results like the diff engine used in GNU diff,
Expand All @@ -351,19 +344,26 @@ pub fn diff<T: Write>(
More studies are needed to cover GNU diff side by side with 100% accuracy, which is one of
the goals of this project : )
*/
for result in diff::slice(&left_lines, &right_lines) {
let results = diff::slice(&left_lines, &right_lines);
let mut different = false;

for result in results {
match result {
Result::Left(left_ln) => push_output(left_ln.raw, b"", b'<', output, &config).unwrap(),
Result::Left(left_ln) => {
different = true;
push_output(left_ln.raw, b"", b'<', output, &config).unwrap();
}
Result::Right(right_ln) => {
push_output(b"", right_ln.raw, b'>', output, &config).unwrap()
different = true;
push_output(b"", right_ln.raw, b'>', output, &config).unwrap();
}
Result::Both(left_ln, right_ln) => {
push_output(left_ln.raw, right_ln.raw, b' ', output, &config).unwrap()
push_output(left_ln.raw, right_ln.raw, b' ', output, &config).unwrap();
}
}
}

vec![]
different
}

#[cfg(test)]
Expand Down
80 changes: 80 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,86 @@ fn sdiff_gutter_marker_column(#[case] options: &[&str], #[case] expected: &str)
.stdout_is(expected);
}

#[test]
fn sdiff_different_files_exit_failure() {
let (at, ts) = at_and_ts!();
at.write("file1", "first\n");
at.write("file2", "second\n");

ts.ucmd().args(&["-y", "file1", "file2"]).fails_with_code(1);
}

#[test]
fn sdiff_brief_identical_files() {
let (at, ts) = at_and_ts!();
at.write("file1", "same\n");
at.write("file2", "same\n");

ts.ucmd()
.args(&["-q", "-y", "file1", "file2"])
.succeeds()
.stdout_is("");
}

#[test]
fn sdiff_brief_different_files_exit_failure() {
let (at, ts) = at_and_ts!();
at.write("file1", "first\n");
at.write("file2", "second\n");

ts.ucmd()
.args(&["-q", "-y", "file1", "file2"])
.fails_with_code(1)
.stdout_is("Files file1 and file2 differ\n");
}

#[test]
fn sdiff_recursive_brief_different_files() {
let (at, ts) = at_and_ts!();
at.mkdir("left");
at.mkdir("right");
at.write("left/file", "first\n");
at.write("right/file", "second\n");

ts.ucmd()
.args(&["-q", "-r", "-y", "left", "right"])
.fails_with_code(1)
.stdout_is(format!(
"Files left{sep}file and right{sep}file differ\n",
sep = std::path::MAIN_SEPARATOR
));
}

#[test]
fn sdiff_recursive_header() {
let (at, ts) = at_and_ts!();
at.mkdir("left");
at.mkdir("right");
at.write("left/file", "first\n");
at.write("right/file", "second\n");

ts.ucmd()
.args(&["-r", "-y", "left", "right"])
.fails_with_code(1)
.stdout_contains(format!(
"diff -r -y left{0}file right{0}file\n",
std::path::MAIN_SEPARATOR
));
}

// Regression test: identical files must return exit code 0 in side-by-side mode.
#[test]
fn sdiff_identical_files_exit_success() {

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 also add a test for -q -y with different files, and for the header with -r -y

let (at, ts) = at_and_ts!();
at.write("file1", "same\n");
at.write("file2", "same\n");

ts.ucmd()
.args(&["-y", "file1", "file2"])
.succeeds()
.no_stderr();
}

#[rstest]
#[case::delete_and_append(
"north\neast\nsouth\nwest\n",
Expand Down
Loading