Skip to content

Commit 2973cfb

Browse files
sourcefrogvrmiguel
authored andcommitted
Handle more ambiguous binary file names
1 parent 0fea54f commit 2973cfb

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/parser.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use nom::{
1010
combinator::{map, map_opt, not, opt},
1111
error::context,
1212
multi::{many0, many1},
13-
sequence::{delimited, pair, preceded, terminated, tuple},
13+
sequence::{delimited, preceded, terminated, tuple},
1414
};
1515

1616
use crate::ast::*;
@@ -119,17 +119,19 @@ fn patch(input: Input<'_>) -> IResult<Input<'_>, Patch> {
119119

120120
/// Recognize a "binary files XX and YY differ" line as an empty patch.
121121
fn binary_files_differ(input: Input<'_>) -> IResult<Input<'_>, Patch> {
122-
// let (input, _) = context("Binary file line", tag("Binary files "))(input)?;
123122
// The names aren't quoted so this seems to require lookahead and then
124123
// parsing the identified string.
125124
let (input, (old, new)) = context(
126125
"Binary file line",
127126
delimited(
128127
tag("Binary files "),
129-
map_opt(take_until(" differ"), |names: Input<'_>| {
130-
names.split_once(" and ")
128+
map_opt(take_until("\n"), |names: Input<'_>| {
129+
names
130+
.trim_end()
131+
.strip_suffix(" differ")
132+
.and_then(|s| s.split_once(" and "))
131133
}),
132-
pair(tag(" differ"), line_ending),
134+
line_ending,
133135
),
134136
)(input)?;
135137
dbg!(&old, &new);

tests/parse_patch.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,21 @@ fn binary_diff_with_crlf() {
298298
assert_eq!(patch.hunks, []);
299299
}
300300

301+
#[test]
302+
fn binary_diff_with_ambiguous_names() {
303+
// The main goal here is that this doesn't crash or error:
304+
// the format with quotes means for some names there is no
305+
// unambiguous meaning.
306+
let sample = "Binary files a differ program and a patcher binary and a wiggler differ\r\n";
307+
308+
let patch = Patch::from_single(sample).unwrap();
309+
assert_eq!(patch.old.path, "a differ program");
310+
assert_eq!(patch.old.meta, None);
311+
assert_eq!(patch.new.path, "a patcher binary and a wiggler");
312+
assert_eq!(patch.new.meta, None);
313+
assert_eq!(patch.hunks, []);
314+
}
315+
301316
#[test]
302317
fn binary_diff_with_spaces_in_name() {
303318
let sample = "Binary files an old binary and a new binary differ\n";

0 commit comments

Comments
 (0)