Skip to content

Commit

Permalink
Merge branch 'main' into context-diff-modification-time
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmayPatil105 committed Apr 1, 2024
2 parents 80c9944 + b135b6f commit 88a7568
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 237 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[![CodeCov](https://codecov.io/gh/uutils/diffutils/branch/master/graph/badge.svg)](https://codecov.io/gh/uutils/diffutils)

The goal of this package is to be a dropped in replacement for the [diffutils commands](https://www.gnu.org/software/diffutils/) in Rust.
The goal of this package is to be a drop-in replacement for the [diffutils commands](https://www.gnu.org/software/diffutils/) in Rust.

Based on the incomplete diff generator in https://github.com/rust-lang/rust/blob/master/src/tools/compiletest/src/runtest.rs, and made to be compatible with GNU's diff and patch tools.

Expand Down
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/fuzz_ed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
extern crate libfuzzer_sys;
use diffutilslib::ed_diff;
use diffutilslib::ed_diff::DiffError;
use diffutilslib::params::Params;
use std::fs::{self, File};
use std::io::Write;
use std::process::Command;

fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Result<Vec<u8>, DiffError> {
let mut output = ed_diff::diff(expected, actual, false, false, 8)?;
let mut output = ed_diff::diff(expected, actual, &Params::default())?;
writeln!(&mut output, "w {filename}").unwrap();
Ok(output)
}
Expand Down
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/fuzz_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#[macro_use]
extern crate libfuzzer_sys;
use diffutilslib::normal_diff;
use diffutilslib::params::Params;

use std::fs::{self, File};
use std::io::Write;
Expand All @@ -21,7 +22,7 @@ fuzz_target!(|x: (Vec<u8>, Vec<u8>)| {
} else {
return
}*/
let diff = normal_diff::diff(&from, &to, false, false, 8);
let diff = normal_diff::diff(&from, &to, &Params::default());
File::create("target/fuzz.file.original")
.unwrap()
.write_all(&from)
Expand Down
13 changes: 7 additions & 6 deletions fuzz/fuzz_targets/fuzz_patch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_main]
#[macro_use]
extern crate libfuzzer_sys;
use diffutilslib::params::Params;
use diffutilslib::unified_diff;
use std::fs::{self, File};
use std::io::Write;
Expand All @@ -22,13 +23,13 @@ fuzz_target!(|x: (Vec<u8>, Vec<u8>, u8)| {
}*/
let diff = unified_diff::diff(
&from,
"a/fuzz.file",
&to,
"target/fuzz.file",
context as usize,
false,
false,
8,
&Params {
from: "a/fuzz.file".into(),
to: "target/fuzz.file".into(),
context_count: context as usize,
..Default::default()
}
);
File::create("target/fuzz.file.original")
.unwrap()
Expand Down
143 changes: 72 additions & 71 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use std::collections::VecDeque;
use std::io::Write;

use crate::params::Params;
use crate::utils::do_write_line;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -282,24 +283,22 @@ fn get_modification_time(file_path: &str) -> String {

#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn diff(
expected: &[u8],
expected_filename: &str,
actual: &[u8],
actual_filename: &str,
context_size: usize,
stop_early: bool,
expand_tabs: bool,
tabsize: usize,
) -> Vec<u8> {
let expected_file_modified_time = get_modification_time(expected_filename);
let actual_file_modified_time = get_modification_time(actual_filename);
let mut output = format!("*** {expected_filename}\t{expected_file_modified_time}\n--- {actual_filename}\t{actual_file_modified_time}\n").into_bytes();
let diff_results = make_diff(expected, actual, context_size, stop_early);
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
let from_modified_time = get_modification_time(&params.from.to_string_lossy());
let to_modified_time = get_modification_time(&params.to.to_string_lossy());
let mut output = format!(
"*** {0}\t{1}\n--- {2}\t{3}\n",
params.from.to_string_lossy(),
from_modified_time,
params.to.to_string_lossy(),
to_modified_time
)
.into_bytes();
let diff_results = make_diff(expected, actual, params.context_count, params.brief);
if diff_results.is_empty() {
return Vec::new();
}
if stop_early {
if params.brief {
return output;
}
for result in diff_results {
Expand Down Expand Up @@ -337,19 +336,19 @@ pub fn diff(
match line {
DiffLine::Context(e) => {
write!(output, " ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Change(e) => {
write!(output, "! ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Add(e) => {
write!(output, "- ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
Expand All @@ -367,19 +366,19 @@ pub fn diff(
match line {
DiffLine::Context(e) => {
write!(output, " ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Change(e) => {
write!(output, "! ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Add(e) => {
write!(output, "+ ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
Expand Down Expand Up @@ -449,13 +448,13 @@ mod tests {
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&format!("{target}/aalef"),
&bet,
&format!("{target}/alef"),
2,
false,
false,
8,
&Params {
from: (&format!("{target}/aalef")).into(),
to: (&format!("{target}/alef")).into(),
context_count: 2,
..Default::default()
},
);
File::create(&format!("{target}/ab.diff"))
.unwrap()
Expand Down Expand Up @@ -531,13 +530,13 @@ mod tests {
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&format!("{target}/aalef_"),
&bet,
&format!("{target}/alef_"),
2,
false,
false,
8,
&Params {
from: (&format!("{target}/aalef_")).into(),
to: (&format!("{target}/alef_")).into(),
context_count: 2,
..Default::default()
},
);
File::create(&format!("{target}/ab_.diff"))
.unwrap()
Expand Down Expand Up @@ -616,13 +615,13 @@ mod tests {
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&format!("{target}/aalefx"),
&bet,
&format!("{target}/alefx"),
2,
false,
false,
8,
&Params {
from: (&format!("{target}/aalefx")).into(),
to: (&format!("{target}/alefx")).into(),
context_count: 2,
..Default::default()
},
);
File::create(&format!("{target}/abx.diff"))
.unwrap()
Expand Down Expand Up @@ -704,13 +703,13 @@ mod tests {
// We want it to turn the alef into bet.
let diff = diff(
&alef,
&format!("{target}/aalefr"),
&bet,
&format!("{target}/alefr"),
2,
false,
false,
8,
&Params {
from: (&format!("{target}/aalefr")).into(),
to: (&format!("{target}/alefr")).into(),
context_count: 2,
..Default::default()
},
);
File::create(&format!("{target}/abr.diff"))
.unwrap()
Expand Down Expand Up @@ -756,17 +755,15 @@ mod tests {
let to_filename = &format!("{target}/bar");
let _ = File::create(to_filename).unwrap();
let to = ["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,
false,
8,
&Params {
from: from_filename.into(),
to: to_filename.into(),
..Default::default()
},
);

let diff_full_text = str::from_utf8(&diff_full).unwrap();
Expand All @@ -792,42 +789,46 @@ mod tests {

let diff_brief = diff(
from.as_bytes(),
from_filename,
to.as_bytes(),
to_filename,
context_size,
true,
false,
8,
&Params {
from: from_filename.into(),
to: to_filename.into(),
brief: true,
..Default::default()
},
);

let diff_brief_text = str::from_utf8(&diff_brief).unwrap();
let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [+-]\d{4}").unwrap();
let diff_brief = re.replace_all(diff_brief_text, "");
let expected_brief = ["*** target/context-diff/foo\t", "--- target/context-diff/bar\t", ""].join("\n");
let expected_brief = [
"*** target/context-diff/foo\t",
"--- target/context-diff/bar\t",
"",
]
.join("\n");
assert_eq!(diff_brief, expected_brief);

let nodiff_full = diff(
from.as_bytes(),
from_filename,
from.as_bytes(),
to_filename,
context_size,
false,
false,
8,
&Params {
from: from_filename.into(),
to: to_filename.into(),
..Default::default()
},
);
assert!(nodiff_full.is_empty());

let nodiff_brief = diff(
from.as_bytes(),
from_filename,
from.as_bytes(),
to_filename,
context_size,
true,
false,
8,
&Params {
from: from_filename.into(),
to: to_filename.into(),
brief: true,
..Default::default()
},
);
assert!(nodiff_brief.is_empty());
}
Expand Down
Loading

0 comments on commit 88a7568

Please sign in to comment.