Skip to content
Closed
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
42 changes: 40 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use nom::{
branch::alt,
bytes::complete::{is_not, tag, take_until},
character::complete::{char, digit1, line_ending, none_of, not_line_ending, one_of},
combinator::{map, not, opt},
combinator::{map, map_opt, not, opt},
error::context,
multi::{many0, many1},
sequence::{delimited, preceded, terminated, tuple},
};
Expand Down Expand Up @@ -95,6 +96,9 @@ fn multiple_patches(input: Input<'_>) -> IResult<Input<'_>, Vec<Patch>> {
}

fn patch(input: Input<'_>) -> IResult<Input<'_>, Patch> {
if let Ok(patch) = binary_files_differ(input) {
return Ok(patch);
}
let (input, files) = headers(input)?;
let (input, hunks) = chunks(input)?;
let (input, no_newline_indicator) = no_newline_indicator(input)?;
Expand All @@ -113,6 +117,40 @@ fn patch(input: Input<'_>) -> IResult<Input<'_>, Patch> {
))
}

/// Recognize a "binary files XX and YY differ" line as an empty patch.
fn binary_files_differ(input: Input<'_>) -> IResult<Input<'_>, Patch> {
// The names aren't quoted so this seems to require lookahead and then
// parsing the identified string.
let (input, (old, new)) = context(
"Binary file line",
delimited(
tag("Binary files "),
map_opt(take_until("\n"), |names: Input<'_>| {
names
.trim_end()
.strip_suffix(" differ")
.and_then(|s| s.split_once(" and "))
}),
line_ending,
),
)(input)?;
Ok((
input,
Patch {
old: File {
path: Cow::Borrowed(old),
meta: None,
},
new: File {
path: Cow::Borrowed(new),
meta: None,
},
hunks: Vec::new(),
end_newline: false,
},
))
}

// Header lines
fn headers(input: Input<'_>) -> IResult<Input<'_>, (File, File)> {
// Ignore any preamble lines in produced diffs
Expand Down Expand Up @@ -413,7 +451,7 @@ mod tests {
));

let sample2b = "\
--- lao
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiniest nit but i think the trailing space here was significant for the test.

--- lao
+++ tzu \n";
test_parser!(headers(sample2b) -> (
File {path: "lao".into(), meta: None},
Expand Down
83 changes: 83 additions & 0 deletions tests/parse_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,86 @@ fn test_parse_triple_plus_minus_hack() {

assert_eq!(format!("{}\n", patch), sample);
}

#[test]
fn binary_diff_with_crlf() {
let sample = "Binary files old.bin and new.bin differ\r\n";

let patch = Patch::from_single(sample).unwrap();
assert_eq!(patch.old.path, "old.bin");
assert_eq!(patch.old.meta, None);
assert_eq!(patch.new.path, "new.bin");
assert_eq!(patch.new.meta, None);
assert_eq!(patch.hunks, []);
}

#[test]
fn binary_diff_with_ambiguous_names() {
// The main goal here is that this doesn't crash or error:
// the format with quotes means for some names there is no
// unambiguous meaning.
let sample = "Binary files a differ program and a patcher binary and a wiggler differ\r\n";

let patch = Patch::from_single(sample).unwrap();
assert_eq!(patch.old.path, "a differ program");
assert_eq!(patch.old.meta, None);
assert_eq!(patch.new.path, "a patcher binary and a wiggler");
assert_eq!(patch.new.meta, None);
assert_eq!(patch.hunks, []);
}

#[test]
fn binary_diff_with_spaces_in_name() {
let sample = "Binary files an old binary and a new binary differ\n";

let patch = Patch::from_single(sample).unwrap();
assert_eq!(patch.old.path, "an old binary");
assert_eq!(patch.old.meta, None);
assert_eq!(patch.new.path, "a new binary");
assert_eq!(patch.new.meta, None);
assert_eq!(patch.hunks, []);
}

#[test]
fn single_binary_diff() {
let sample = "Binary files old.bin and new.bin differ\n";

let patch = Patch::from_single(sample).unwrap();
assert_eq!(patch.old.path, "old.bin");
assert_eq!(patch.old.meta, None);
assert_eq!(patch.new.path, "new.bin");
assert_eq!(patch.new.meta, None);
assert_eq!(patch.hunks, []);
}

#[test]
fn multiple_binary_diffs() {
let sample = "Binary files old.bin and new.bin differ
Binary files old1.bin and new1.bin differ
";
let patches = Patch::from_multiple(sample).unwrap();
assert_eq!(patches.len(), 2);
assert_eq!(patches[0].old.path, "old.bin");
assert_eq!(patches[1].old.path, "old1.bin");
}

#[test]
fn binary_diff_with_text_diff() {
let sample = "\
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
guido
Binary files old.bin and new.bin differ
";
let patches = Patch::from_multiple(sample).unwrap();
assert_eq!(patches.len(), 2);
assert_eq!(patches[0].old.path, "before.py");
assert_eq!(patches[1].old.path, "old.bin");
}