Skip to content

Implement -q/--brief option (fixes #19) #20

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

Merged
merged 4 commits into from
Mar 19, 2024
Merged
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
124 changes: 113 additions & 11 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ impl Mismatch {
}

// Produces a diff between the expected output and actual output.
fn make_diff(expected: &[u8], actual: &[u8], context_size: usize) -> Vec<Mismatch> {
fn make_diff(
expected: &[u8],
actual: &[u8],
context_size: usize,
stop_early: bool,
) -> Vec<Mismatch> {
let mut line_number_expected = 1;
let mut line_number_actual = 1;
let mut context_queue: VecDeque<&[u8]> = VecDeque::with_capacity(context_size);
Expand Down Expand Up @@ -191,6 +196,10 @@ fn make_diff(expected: &[u8], actual: &[u8], context_size: usize) -> Vec<Mismatc
line_number_actual += 1;
}
}
if stop_early && !results.is_empty() {
// Optimization: stop analyzing the files as soon as there are any differences
return results;
}
}

results.push(mismatch);
Expand Down Expand Up @@ -260,12 +269,16 @@ pub fn diff(
actual: &[u8],
actual_filename: &str,
context_size: usize,
stop_early: bool,
) -> Vec<u8> {
let mut output = format!("*** {expected_filename}\t\n--- {actual_filename}\t\n").into_bytes();
let diff_results = make_diff(expected, actual, context_size);
let diff_results = make_diff(expected, actual, context_size, stop_early);
if diff_results.is_empty() {
return Vec::new();
};
}
if stop_early {
return output;
}
for result in diff_results {
let mut line_number_expected = result.line_number_expected;
let mut line_number_actual = result.line_number_actual;
Expand Down Expand Up @@ -404,8 +417,14 @@ mod tests {
}
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff =
diff(&alef, "a/alef", &bet, &format!("{target}/alef"), 2);
let diff = diff(
&alef,
"a/alef",
&bet,
&format!("{target}/alef"),
2,
false,
);
File::create(&format!("{target}/ab.diff"))
.unwrap()
.write_all(&diff)
Expand Down Expand Up @@ -477,8 +496,14 @@ mod tests {
}
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff =
diff(&alef, "a/alef_", &bet, &format!("{target}/alef_"), 2);
let diff = diff(
&alef,
"a/alef_",
&bet,
&format!("{target}/alef_"),
2,
false,
);
File::create(&format!("{target}/ab_.diff"))
.unwrap()
.write_all(&diff)
Expand Down Expand Up @@ -553,8 +578,14 @@ mod tests {
};
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff =
diff(&alef, "a/alefx", &bet, &format!("{target}/alefx"), 2);
let diff = diff(
&alef,
"a/alefx",
&bet,
&format!("{target}/alefx"),
2,
false,
);
File::create(&format!("{target}/abx.diff"))
.unwrap()
.write_all(&diff)
Expand Down Expand Up @@ -632,8 +663,14 @@ mod tests {
}
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff =
diff(&alef, "a/alefr", &bet, &format!("{target}/alefr"), 2);
let diff = diff(
&alef,
"a/alefr",
&bet,
&format!("{target}/alefr"),
2,
false,
);
File::create(&format!("{target}/abr.diff"))
.unwrap()
.write_all(&diff)
Expand Down Expand Up @@ -662,4 +699,69 @@ mod tests {
}
}
}

#[test]
fn test_stop_early() {
let from_filename = "foo";
let from = vec!["a", "b", "c", ""].join("\n");
let to_filename = "bar";
let to = vec!["a", "d", "c", ""].join("\n");
let context_size: usize = 3;

let diff_full = diff(
from.as_bytes(),
from_filename,
to.as_bytes(),
to_filename,
context_size,
false,
);
let expected_full = vec![
"*** foo\t",
"--- bar\t",
"***************",
"*** 1,3 ****",
" a",
"! b",
" c",
"--- 1,3 ----",
" a",
"! d",
" c",
"",
]
.join("\n");
assert_eq!(diff_full, expected_full.as_bytes());

let diff_brief = diff(
from.as_bytes(),
from_filename,
to.as_bytes(),
to_filename,
context_size,
true,
);
let expected_brief = vec!["*** foo\t", "--- bar\t", ""].join("\n");
assert_eq!(diff_brief, expected_brief.as_bytes());

let nodiff_full = diff(
from.as_bytes(),
from_filename,
from.as_bytes(),
to_filename,
context_size,
false,
);
assert!(nodiff_full.is_empty());

let nodiff_brief = diff(
from.as_bytes(),
from_filename,
from.as_bytes(),
to_filename,
context_size,
true,
);
assert!(nodiff_brief.is_empty());
}
}
38 changes: 33 additions & 5 deletions src/ed_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Mismatch {
}

// Produces a diff between the expected output and actual output.
fn make_diff(expected: &[u8], actual: &[u8]) -> Result<Vec<Mismatch>, DiffError> {
fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result<Vec<Mismatch>, DiffError> {
let mut line_number_expected = 1;
let mut line_number_actual = 1;
let mut results = Vec::new();
Expand Down Expand Up @@ -94,6 +94,10 @@ fn make_diff(expected: &[u8], actual: &[u8]) -> Result<Vec<Mismatch>, DiffError>
}
}
}
if stop_early && !results.is_empty() {
// Optimization: stop analyzing the files as soon as there are any differences
return Ok(results);
}
}

if !mismatch.actual.is_empty() || !mismatch.expected.is_empty() {
Expand All @@ -103,9 +107,13 @@ fn make_diff(expected: &[u8], actual: &[u8]) -> Result<Vec<Mismatch>, DiffError>
Ok(results)
}

pub fn diff(expected: &[u8], actual: &[u8]) -> Result<Vec<u8>, DiffError> {
pub fn diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result<Vec<u8>, DiffError> {
let mut output = Vec::new();
let diff_results = make_diff(expected, actual)?;
let diff_results = make_diff(expected, actual, stop_early)?;
if stop_early && !diff_results.is_empty() {
write!(&mut output, "\0").unwrap();
return Ok(output);
}
let mut lines_offset = 0;
for result in diff_results {
let line_number_expected: isize = result.line_number_expected as isize + lines_offset;
Expand Down Expand Up @@ -152,7 +160,7 @@ mod tests {
use super::*;
use pretty_assertions::assert_eq;
pub fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Result<Vec<u8>, DiffError> {
let mut output = diff(expected, actual)?;
let mut output = diff(expected, actual, false)?;
writeln!(&mut output, "w {filename}").unwrap();
Ok(output)
}
Expand All @@ -161,7 +169,7 @@ mod tests {
fn test_basic() {
let from = b"a\n";
let to = b"b\n";
let diff = diff(from, to).unwrap();
let diff = diff(from, to, false).unwrap();
let expected = vec!["1c", "b", ".", ""].join("\n");
assert_eq!(diff, expected.as_bytes());
}
Expand Down Expand Up @@ -390,4 +398,24 @@ mod tests {
}
}
}

#[test]
fn test_stop_early() {
let from = vec!["a", "b", "c", ""].join("\n");
let to = vec!["a", "d", "c", ""].join("\n");

let diff_full = diff(from.as_bytes(), to.as_bytes(), false).unwrap();
let expected_full = vec!["2c", "d", ".", ""].join("\n");
assert_eq!(diff_full, expected_full.as_bytes());

let diff_brief = diff(from.as_bytes(), to.as_bytes(), true).unwrap();
let expected_brief = "\0".as_bytes();
assert_eq!(diff_brief, expected_brief);

let nodiff_full = diff(from.as_bytes(), from.as_bytes(), false).unwrap();
assert!(nodiff_full.is_empty());

let nodiff_brief = diff(from.as_bytes(), from.as_bytes(), true).unwrap();
assert!(nodiff_brief.is_empty());
}
}
17 changes: 14 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn main() -> ExitCode {
context_count,
format,
report_identical_files,
brief,
} = parse_params(opts).unwrap_or_else(|error| {
eprintln!("{error}");
exit(2);
Expand Down Expand Up @@ -64,27 +65,37 @@ fn main() -> ExitCode {
};
// run diff
let result: Vec<u8> = match format {
Format::Normal => normal_diff::diff(&from_content, &to_content),
Format::Normal => normal_diff::diff(&from_content, &to_content, brief),
Format::Unified => unified_diff::diff(
&from_content,
&from.to_string_lossy(),
&to_content,
&to.to_string_lossy(),
context_count,
brief,
),
Format::Context => context_diff::diff(
&from_content,
&from.to_string_lossy(),
&to_content,
&to.to_string_lossy(),
context_count,
brief,
),
Format::Ed => ed_diff::diff(&from_content, &to_content).unwrap_or_else(|error| {
Format::Ed => ed_diff::diff(&from_content, &to_content, brief).unwrap_or_else(|error| {
eprintln!("{error}");
exit(2);
}),
};
io::stdout().write_all(&result).unwrap();
if brief && !result.is_empty() {
println!(
"Files {} and {} differ",
from.to_string_lossy(),
to.to_string_lossy()
);
} else {
io::stdout().write_all(&result).unwrap();
}
if result.is_empty() {
maybe_report_identical_files();
ExitCode::SUCCESS
Expand Down
Loading