Skip to content

Commit 1e7398a

Browse files
committed
diff: support renamed, updated binary file
This commit parses a diff with a renamed, updated binary file and adds a test case for it.
1 parent c3b8bb2 commit 1e7398a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

diff/diff_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,23 @@ func TestParseFileDiffHeaders(t *testing.T) {
216216
},
217217
},
218218
},
219+
{
220+
filename: "sample_file_extended_binary_rename.diff",
221+
wantDiff: &FileDiff{
222+
OrigName: "a/data/Font.png",
223+
OrigTime: nil,
224+
NewName: "b/data/Other.png",
225+
NewTime: nil,
226+
Extended: []string{
227+
"diff --git a/data/Font.png b/data/Other.png",
228+
"similarity index 51%",
229+
"rename from data/Font.png",
230+
"rename to data/Other.png",
231+
"index 17a971d..599f8dd 100644",
232+
"Binary files a/data/Font.png and b/data/Other.png differ",
233+
},
234+
},
235+
},
219236
}
220237
for _, test := range tests {
221238
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))

diff/parse.go

+5
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,11 @@ func handleEmpty(fd *FileDiff) (wasEmpty bool) {
397397
fd.NewName = names[1]
398398
}
399399
return true
400+
case lineCount == 6 && strings.HasPrefix(fd.Extended[5], "Binary files ") && strings.HasPrefix(fd.Extended[2], "rename from ") && strings.HasPrefix(fd.Extended[3], "rename to "):
401+
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
402+
fd.OrigName = names[0]
403+
fd.NewName = names[1]
404+
return true
400405
case lineCount == 3 && strings.HasPrefix(fd.Extended[2], "Binary files ") || lineCount > 3 && strings.HasPrefix(fd.Extended[2], "GIT binary patch"):
401406
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
402407
fd.OrigName, err = strconv.Unquote(names[0])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
diff --git a/data/Font.png b/data/Other.png
2+
similarity index 51%
3+
rename from data/Font.png
4+
rename to data/Other.png
5+
index 17a971d..599f8dd 100644
6+
Binary files a/data/Font.png and b/data/Other.png differ

0 commit comments

Comments
 (0)