← 返回首页
Fixes #182 by parsing diffs with 'Binary files' by machadoit · Pull Request #183 · java-diff-utils/java-diff-utils · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .diff  (4) .java  (3) All 2 file types selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ public final class UnifiedDiffFile {
private String toTimestamp;
private String index;
private String newFileMode;
private String oldMode;
private String newMode;
private String deletedFileMode;
private String binaryAdded;
private String binaryDeleted;
private String binaryEdited;
private Patch<String> patch = new Patch<>();
private boolean noNewLineAtTheEndOfTheFile = false;
private Integer similarityIndex;
Expand Down Expand Up @@ -138,6 +143,46 @@ public void setDeletedFileMode(String deletedFileMode) {
this.deletedFileMode = deletedFileMode;
}

public String getOldMode() {
return oldMode;
}

public void setOldMode(String oldMode) {
this.oldMode = oldMode;
}

public String getNewMode() {
return newMode;
}

public void setNewMode(String newMode) {
this.newMode = newMode;
}

public String getBinaryAdded() {
return binaryAdded;
}

public void setBinaryAdded(String binaryAdded) {
this.binaryAdded = binaryAdded;
}

public String getBinaryDeleted() {
return binaryDeleted;
}

public void setBinaryDeleted(String binaryDeleted) {
this.binaryDeleted = binaryDeleted;
}

public String getBinaryEdited() {
return binaryEdited;
}

public void setBinaryEdited(String binaryEdited) {
this.binaryEdited = binaryEdited;
}

public boolean isNoNewLineAtTheEndOfTheFile() {
return noNewLineAtTheEndOfTheFile;
}
Expand Down
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public final class UnifiedDiffReader {
private final UnifiedDiffLine NEW_FILE_MODE = new UnifiedDiffLine(true, "^new\\sfile\\smode\\s(\\d+)", this::processNewFileMode);

private final UnifiedDiffLine DELETED_FILE_MODE = new UnifiedDiffLine(true, "^deleted\\sfile\\smode\\s(\\d+)", this::processDeletedFileMode);

private final UnifiedDiffLine OLD_MODE = new UnifiedDiffLine(true, "^old\\smode\\s(\\d+)", this::processOldMode);
private final UnifiedDiffLine NEW_MODE = new UnifiedDiffLine(true, "^new\\smode\\s(\\d+)", this::processNewMode);
private final UnifiedDiffLine BINARY_ADDED = new UnifiedDiffLine(true, "^Binary\\sfiles\\s/dev/null\\sand\\sb/(.+)\\sdiffer", this::processBinaryAdded);
private final UnifiedDiffLine BINARY_DELETED = new UnifiedDiffLine(true, "^Binary\\sfiles\\sa/(.+)\\sand\\s/dev/null\\sdiffer", this::processBinaryDeleted);
private final UnifiedDiffLine BINARY_EDITED = new UnifiedDiffLine(true, "^Binary\\sfiles\\sa/(.+)\\sand\\sb/(.+)\\sdiffer", this::processBinaryEdited);
private final UnifiedDiffLine CHUNK = new UnifiedDiffLine(false, UNIFIED_DIFF_CHUNK_REGEXP, this::processChunk);
private final UnifiedDiffLine LINE_NORMAL = new UnifiedDiffLine("^\\s", this::processNormalLine);
private final UnifiedDiffLine LINE_DEL = new UnifiedDiffLine("^-", this::processDelLine);
Expand Down Expand Up @@ -99,8 +103,10 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
if (validLine(line, DIFF_COMMAND, SIMILARITY_INDEX, INDEX,
FROM_FILE, TO_FILE,
RENAME_FROM, RENAME_TO,
NEW_FILE_MODE, DELETED_FILE_MODE,
CHUNK)) {
NEW_FILE_MODE, DELETED_FILE_MODE,
OLD_MODE, NEW_MODE,
BINARY_ADDED, BINARY_DELETED,
BINARY_EDITED, CHUNK)) {
break;
} else {
headerTxt += line + "\n";
Expand All @@ -116,7 +122,10 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
if (!processLine(line, DIFF_COMMAND, SIMILARITY_INDEX, INDEX,
FROM_FILE, TO_FILE,
RENAME_FROM, RENAME_TO,
NEW_FILE_MODE, DELETED_FILE_MODE)) {
NEW_FILE_MODE, DELETED_FILE_MODE,
OLD_MODE, NEW_MODE,
BINARY_ADDED , BINARY_DELETED,
BINARY_EDITED)) {
throw new UnifiedDiffParserException("expected file start line not found");
}
line = READER.readLine();
Expand Down Expand Up @@ -346,6 +355,26 @@ private void processDeletedFileMode(MatchResult match, String line) {
actualFile.setDeletedFileMode(match.group(1));
}

private void processOldMode(MatchResult match, String line) {
actualFile.setOldMode(match.group(1));
}

private void processNewMode(MatchResult match, String line) {
actualFile.setNewMode(match.group(1));
}

private void processBinaryAdded(MatchResult match, String line) {
actualFile.setBinaryAdded(match.group(1));
}

private void processBinaryDeleted(MatchResult match, String line) {
actualFile.setBinaryDeleted(match.group(1));
}

private void processBinaryEdited(MatchResult match, String line) {
actualFile.setBinaryEdited(match.group(1));
}

private String extractFileName(String _line) {
Matcher matcher = TIMESTAMP_REGEXP.matcher(_line);
String line = _line;
Expand Down
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,45 @@ public void testParseIssue141() throws IOException {
assertThat(file1.getFromFile()).isEqualTo("a.txt");
assertThat(file1.getToFile()).isEqualTo("a1.txt");
}

@Test
public void testParseIssue182_add() throws IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
UnifiedDiffReaderTest.class.getResourceAsStream("problem_diff_issue182_add.diff"));

UnifiedDiffFile file1 = diff.getFiles().get(0);

assertThat(file1.getBinaryAdded()).isEqualTo("some-image.png");
}

@Test
public void testParseIssue182_delete() throws IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
UnifiedDiffReaderTest.class.getResourceAsStream("problem_diff_issue182_delete.diff"));

UnifiedDiffFile file1 = diff.getFiles().get(0);

assertThat(file1.getBinaryDeleted()).isEqualTo("some-image.png");
}

@Test
public void testParseIssue182_edit() throws IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
UnifiedDiffReaderTest.class.getResourceAsStream("problem_diff_issue182_edit.diff"));

UnifiedDiffFile file1 = diff.getFiles().get(0);

assertThat(file1.getBinaryEdited()).isEqualTo("some-image.png");
}

@Test
public void testParseIssue182_mode() throws IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
UnifiedDiffReaderTest.class.getResourceAsStream("problem_diff_issue182_mode.diff"));

UnifiedDiffFile file1 = diff.getFiles().get(0);

assertThat(file1.getOldMode()).isEqualTo("100644");
assertThat(file1.getNewMode()).isEqualTo("100755");
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
diff --git a/some-image.png b/some-image.png
new file mode 100644
index 0000000..bc3b5b4
Binary files /dev/null and b/some-image.png differ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
diff --git a/some-image.png b/some-image.png
deleted file mode 100644
index 0e68078..0000000
Binary files a/some-image.png and /dev/null differ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
diff --git a/some-image.png b/some-image.png
index bc3b5b4..0e68078 100644
Binary files a/some-image.png and b/some-image.png differ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
diff --git a/some-image.png b/some-image.png
old mode 100644
new mode 100755
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.