Skip to content
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

vcs: handle binary files in UnifiedDiffParser #930

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -70,6 +70,10 @@ public static List<Hunk> parseSingleFileDiff(List<String> lines) {

var hunks = new ArrayList<Hunk>();
while (i < lines.size()) {
if (lines.get(i).startsWith("Binary files ") && lines.get(i).endsWith(" differ")) {
i++;
continue;
}
var words = lines.get(i).split("\\s");
if (!words[0].startsWith("@@")) {
throw new IllegalStateException("Unexpected diff line at index " + i + ": " + lines.get(i));
@@ -152,6 +156,8 @@ public static List<Hunk> parseSingleFileDiff(Range from, Range to, List<String>
sourceHasNewlineAtEndOfFile = false;
}
i++;
} else if (line.startsWith("Binary files") && line.endsWith("differ")) {
i++;
} else {
throw new IllegalStateException("Unexpected diff line: " + line);
}
@@ -421,4 +421,15 @@ public void noNewline() {
assertEquals(1, hunks.size());
assertFalse(hunks.get(0).target().hasNewlineAtEndOfFile());
}

@Test
public void binaryFile() {
var diff =
"diff --git a/file.bin b/file.bin\n" +
"new file mode 100644\n" +
"index 0000000000000000000000000000000000000000..2020dd2b626d1bcf60351a2be801548eb65c53cd\n" +
"Binary files /dev/null and b/file.bin differ";
var hunks = UnifiedDiffParser.parseSingleFileDiff(diff.split("\n"));
assertEquals(List.of(), hunks);
}
}