Skip to content
Permalink
Browse files
vcs: handle binary files in UnifiedDiffParser
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Oct 30, 2020
1 parent e583ed5 commit daa3c93e6a1ffa0674394edc1a37cb6e65c5fa14
Showing with 17 additions and 0 deletions.
  1. +6 −0 vcs/src/main/java/org/openjdk/skara/vcs/UnifiedDiffParser.java
  2. +11 −0 vcs/src/test/java/org/openjdk/skara/vcs/UnifiedDiffParserTests.java
@@ -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);
}
}

1 comment on commit daa3c93

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on daa3c93 Oct 30, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.