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
43 changes: 28 additions & 15 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,35 +155,48 @@ 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 => match ed_diff::diff(&from_content, &to_content, params) {
Ok(result) => result,
Err(error) => {
eprintln!("{error}");
return Outcome::Trouble;
}
},
// Side by side output is streamed, unless a header may have to come
// first.
Format::Ed => {
let result = match ed_diff::diff(&from_content, &to_content, params) {
Ok(result) => result,
Err(error) => {
eprintln!("{error}");
return Outcome::Trouble;
}
};

result
}
Format::SideBySide => match header {
None => side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params),
None => {
side_by_side_different =
side_diff::diff(&from_content, &to_content, &mut stdout().lock(), params);
Vec::new()
}
Some(header) => {
let mut output = Vec::new();
let result = side_diff::diff(&from_content, &to_content, &mut output, params);
if !result.is_empty() {

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

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

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

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 All @@ -210,7 +223,7 @@ 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
30 changes: 16 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 @@ -308,14 +308,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: Vec<&[u8]> = from_file.split_inclusive(|&c| c == b'\n').collect();
let mut right_lines: Vec<&[u8]> = to_file.split_inclusive(|&c| c == b'\n').collect();

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 keep the blank lines, unrelated change

let config = Config::new(params.width, params.tabsize, params.expand_tabs);
Expand All @@ -327,7 +321,6 @@ pub fn diff<T: Write>(
if right_lines.last() == Some(&&b""[..]) {
right_lines.pop();
}

/*
DISCLAIMER:
Currently the diff engine does not produce results like the diff engine used in GNU diff,
Expand All @@ -350,17 +343,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, b"", b'<', output, &config).unwrap(),
Result::Right(right_ln) => push_output(b"", right_ln, b'>', output, &config).unwrap(),
Result::Left(left_ln) => {
different = true;
push_output(left_ln, b"", b'<', output, &config).unwrap();
}
Result::Right(right_ln) => {
different = true;
push_output(b"", right_ln, b'>', output, &config).unwrap();
}
Result::Both(left_ln, right_ln) => {
push_output(left_ln, right_ln, b' ', output, &config).unwrap()
push_output(left_ln, right_ln, b' ', output, &config).unwrap();
}
}
}

vec![]
different
}

#[cfg(test)]
Expand Down
48 changes: 48 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,54 @@ 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_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_contains("Files file1 and file2 differ\n");
}

#[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("diff -r -y left/file right/file\n");
}

// 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