Skip to content

Commit 88a7568

Browse files
Merge branch 'main' into context-diff-modification-time
2 parents 80c9944 + b135b6f commit 88a7568

File tree

10 files changed

+225
-237
lines changed

10 files changed

+225
-237
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

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

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

1010
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.
1111

fuzz/fuzz_targets/fuzz_ed.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
extern crate libfuzzer_sys;
44
use diffutilslib::ed_diff;
55
use diffutilslib::ed_diff::DiffError;
6+
use diffutilslib::params::Params;
67
use std::fs::{self, File};
78
use std::io::Write;
89
use std::process::Command;
910

1011
fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Result<Vec<u8>, DiffError> {
11-
let mut output = ed_diff::diff(expected, actual, false, false, 8)?;
12+
let mut output = ed_diff::diff(expected, actual, &Params::default())?;
1213
writeln!(&mut output, "w {filename}").unwrap();
1314
Ok(output)
1415
}

fuzz/fuzz_targets/fuzz_normal.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#[macro_use]
33
extern crate libfuzzer_sys;
44
use diffutilslib::normal_diff;
5+
use diffutilslib::params::Params;
56

67
use std::fs::{self, File};
78
use std::io::Write;
@@ -21,7 +22,7 @@ fuzz_target!(|x: (Vec<u8>, Vec<u8>)| {
2122
} else {
2223
return
2324
}*/
24-
let diff = normal_diff::diff(&from, &to, false, false, 8);
25+
let diff = normal_diff::diff(&from, &to, &Params::default());
2526
File::create("target/fuzz.file.original")
2627
.unwrap()
2728
.write_all(&from)

fuzz/fuzz_targets/fuzz_patch.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![no_main]
22
#[macro_use]
33
extern crate libfuzzer_sys;
4+
use diffutilslib::params::Params;
45
use diffutilslib::unified_diff;
56
use std::fs::{self, File};
67
use std::io::Write;
@@ -22,13 +23,13 @@ fuzz_target!(|x: (Vec<u8>, Vec<u8>, u8)| {
2223
}*/
2324
let diff = unified_diff::diff(
2425
&from,
25-
"a/fuzz.file",
2626
&to,
27-
"target/fuzz.file",
28-
context as usize,
29-
false,
30-
false,
31-
8,
27+
&Params {
28+
from: "a/fuzz.file".into(),
29+
to: "target/fuzz.file".into(),
30+
context_count: context as usize,
31+
..Default::default()
32+
}
3233
);
3334
File::create("target/fuzz.file.original")
3435
.unwrap()

src/context_diff.rs

Lines changed: 72 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use std::collections::VecDeque;
77
use std::io::Write;
88

9+
use crate::params::Params;
910
use crate::utils::do_write_line;
1011

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

283284
#[must_use]
284285
#[allow(clippy::too_many_arguments)]
285-
pub fn diff(
286-
expected: &[u8],
287-
expected_filename: &str,
288-
actual: &[u8],
289-
actual_filename: &str,
290-
context_size: usize,
291-
stop_early: bool,
292-
expand_tabs: bool,
293-
tabsize: usize,
294-
) -> Vec<u8> {
295-
let expected_file_modified_time = get_modification_time(expected_filename);
296-
let actual_file_modified_time = get_modification_time(actual_filename);
297-
let mut output = format!("*** {expected_filename}\t{expected_file_modified_time}\n--- {actual_filename}\t{actual_file_modified_time}\n").into_bytes();
298-
let diff_results = make_diff(expected, actual, context_size, stop_early);
286+
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
287+
let from_modified_time = get_modification_time(&params.from.to_string_lossy());
288+
let to_modified_time = get_modification_time(&params.to.to_string_lossy());
289+
let mut output = format!(
290+
"*** {0}\t{1}\n--- {2}\t{3}\n",
291+
params.from.to_string_lossy(),
292+
from_modified_time,
293+
params.to.to_string_lossy(),
294+
to_modified_time
295+
)
296+
.into_bytes();
297+
let diff_results = make_diff(expected, actual, params.context_count, params.brief);
299298
if diff_results.is_empty() {
300299
return Vec::new();
301300
}
302-
if stop_early {
301+
if params.brief {
303302
return output;
304303
}
305304
for result in diff_results {
@@ -337,19 +336,19 @@ pub fn diff(
337336
match line {
338337
DiffLine::Context(e) => {
339338
write!(output, " ").expect("write to Vec is infallible");
340-
do_write_line(&mut output, &e, expand_tabs, tabsize)
339+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
341340
.expect("write to Vec is infallible");
342341
writeln!(output).unwrap();
343342
}
344343
DiffLine::Change(e) => {
345344
write!(output, "! ").expect("write to Vec is infallible");
346-
do_write_line(&mut output, &e, expand_tabs, tabsize)
345+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
347346
.expect("write to Vec is infallible");
348347
writeln!(output).unwrap();
349348
}
350349
DiffLine::Add(e) => {
351350
write!(output, "- ").expect("write to Vec is infallible");
352-
do_write_line(&mut output, &e, expand_tabs, tabsize)
351+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
353352
.expect("write to Vec is infallible");
354353
writeln!(output).unwrap();
355354
}
@@ -367,19 +366,19 @@ pub fn diff(
367366
match line {
368367
DiffLine::Context(e) => {
369368
write!(output, " ").expect("write to Vec is infallible");
370-
do_write_line(&mut output, &e, expand_tabs, tabsize)
369+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
371370
.expect("write to Vec is infallible");
372371
writeln!(output).unwrap();
373372
}
374373
DiffLine::Change(e) => {
375374
write!(output, "! ").expect("write to Vec is infallible");
376-
do_write_line(&mut output, &e, expand_tabs, tabsize)
375+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
377376
.expect("write to Vec is infallible");
378377
writeln!(output).unwrap();
379378
}
380379
DiffLine::Add(e) => {
381380
write!(output, "+ ").expect("write to Vec is infallible");
382-
do_write_line(&mut output, &e, expand_tabs, tabsize)
381+
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
383382
.expect("write to Vec is infallible");
384383
writeln!(output).unwrap();
385384
}
@@ -449,13 +448,13 @@ mod tests {
449448
// We want it to turn the alef into bet.
450449
let diff = diff(
451450
&alef,
452-
&format!("{target}/aalef"),
453451
&bet,
454-
&format!("{target}/alef"),
455-
2,
456-
false,
457-
false,
458-
8,
452+
&Params {
453+
from: (&format!("{target}/aalef")).into(),
454+
to: (&format!("{target}/alef")).into(),
455+
context_count: 2,
456+
..Default::default()
457+
},
459458
);
460459
File::create(&format!("{target}/ab.diff"))
461460
.unwrap()
@@ -531,13 +530,13 @@ mod tests {
531530
// We want it to turn the alef into bet.
532531
let diff = diff(
533532
&alef,
534-
&format!("{target}/aalef_"),
535533
&bet,
536-
&format!("{target}/alef_"),
537-
2,
538-
false,
539-
false,
540-
8,
534+
&Params {
535+
from: (&format!("{target}/aalef_")).into(),
536+
to: (&format!("{target}/alef_")).into(),
537+
context_count: 2,
538+
..Default::default()
539+
},
541540
);
542541
File::create(&format!("{target}/ab_.diff"))
543542
.unwrap()
@@ -616,13 +615,13 @@ mod tests {
616615
// We want it to turn the alef into bet.
617616
let diff = diff(
618617
&alef,
619-
&format!("{target}/aalefx"),
620618
&bet,
621-
&format!("{target}/alefx"),
622-
2,
623-
false,
624-
false,
625-
8,
619+
&Params {
620+
from: (&format!("{target}/aalefx")).into(),
621+
to: (&format!("{target}/alefx")).into(),
622+
context_count: 2,
623+
..Default::default()
624+
},
626625
);
627626
File::create(&format!("{target}/abx.diff"))
628627
.unwrap()
@@ -704,13 +703,13 @@ mod tests {
704703
// We want it to turn the alef into bet.
705704
let diff = diff(
706705
&alef,
707-
&format!("{target}/aalefr"),
708706
&bet,
709-
&format!("{target}/alefr"),
710-
2,
711-
false,
712-
false,
713-
8,
707+
&Params {
708+
from: (&format!("{target}/aalefr")).into(),
709+
to: (&format!("{target}/alefr")).into(),
710+
context_count: 2,
711+
..Default::default()
712+
},
714713
);
715714
File::create(&format!("{target}/abr.diff"))
716715
.unwrap()
@@ -756,17 +755,15 @@ mod tests {
756755
let to_filename = &format!("{target}/bar");
757756
let _ = File::create(to_filename).unwrap();
758757
let to = ["a", "d", "c", ""].join("\n");
759-
let context_size: usize = 3;
760758

761759
let diff_full = diff(
762760
from.as_bytes(),
763-
from_filename,
764761
to.as_bytes(),
765-
to_filename,
766-
context_size,
767-
false,
768-
false,
769-
8,
762+
&Params {
763+
from: from_filename.into(),
764+
to: to_filename.into(),
765+
..Default::default()
766+
},
770767
);
771768

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

793790
let diff_brief = diff(
794791
from.as_bytes(),
795-
from_filename,
796792
to.as_bytes(),
797-
to_filename,
798-
context_size,
799-
true,
800-
false,
801-
8,
793+
&Params {
794+
from: from_filename.into(),
795+
to: to_filename.into(),
796+
brief: true,
797+
..Default::default()
798+
},
802799
);
803800

804801
let diff_brief_text = str::from_utf8(&diff_brief).unwrap();
805802
let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [+-]\d{4}").unwrap();
806803
let diff_brief = re.replace_all(diff_brief_text, "");
807-
let expected_brief = ["*** target/context-diff/foo\t", "--- target/context-diff/bar\t", ""].join("\n");
804+
let expected_brief = [
805+
"*** target/context-diff/foo\t",
806+
"--- target/context-diff/bar\t",
807+
"",
808+
]
809+
.join("\n");
808810
assert_eq!(diff_brief, expected_brief);
809811

810812
let nodiff_full = diff(
811813
from.as_bytes(),
812-
from_filename,
813814
from.as_bytes(),
814-
to_filename,
815-
context_size,
816-
false,
817-
false,
818-
8,
815+
&Params {
816+
from: from_filename.into(),
817+
to: to_filename.into(),
818+
..Default::default()
819+
},
819820
);
820821
assert!(nodiff_full.is_empty());
821822

822823
let nodiff_brief = diff(
823824
from.as_bytes(),
824-
from_filename,
825825
from.as_bytes(),
826-
to_filename,
827-
context_size,
828-
true,
829-
false,
830-
8,
826+
&Params {
827+
from: from_filename.into(),
828+
to: to_filename.into(),
829+
brief: true,
830+
..Default::default()
831+
},
831832
);
832833
assert!(nodiff_brief.is_empty());
833834
}

0 commit comments

Comments
 (0)