Skip to content

Commit 5039278

Browse files
committed
Fix support for modified binary files.
Add tests. Fixes #10.
1 parent d15cab2 commit 5039278

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

diff/diff_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,43 @@ func TestParseMultiFileDiffHeaders(t *testing.T) {
306306
},
307307
},
308308
},
309+
{
310+
filename: "sample_multi_file_binary.diff",
311+
wantDiffs: []*FileDiff{
312+
{
313+
OrigName: "a/README.md",
314+
OrigTime: nil,
315+
NewName: "b/README.md",
316+
NewTime: nil,
317+
Extended: []string{
318+
"diff --git a/README.md b/README.md",
319+
"index 7b73e04..36cde13 100644",
320+
},
321+
},
322+
{
323+
OrigName: "a/data/Font.png",
324+
OrigTime: nil,
325+
NewName: "b/data/Font.png",
326+
NewTime: nil,
327+
Extended: []string{
328+
"diff --git a/data/Font.png b/data/Font.png",
329+
"index 17a971d..599f8dd 100644",
330+
"Binary files a/data/Font.png and b/data/Font.png differ",
331+
},
332+
Empty: true,
333+
},
334+
{
335+
OrigName: "a/main.go",
336+
OrigTime: nil,
337+
NewName: "b/main.go",
338+
NewTime: nil,
339+
Extended: []string{
340+
"diff --git a/main.go b/main.go",
341+
"index 1aced1e..98a982e 100644",
342+
},
343+
},
344+
},
345+
},
309346
}
310347
for _, test := range tests {
311348
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
@@ -337,6 +374,7 @@ func TestParseFileDiffAndPrintFileDiff(t *testing.T) {
337374
{filename: "sample_file_extended_empty_new.diff"},
338375
{filename: "sample_file_extended_empty_deleted.diff"},
339376
{filename: "sample_file_extended_empty_rename.diff"},
377+
{filename: "sample_file_extended_empty_binary.diff"},
340378
{
341379
filename: "empty.diff",
342380
wantParseErr: &ParseError{0, 0, ErrExtendedHeadersEOF},
@@ -377,6 +415,7 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
377415
{filename: "sample_multi_file_new.diff", wantFileDiffs: 3},
378416
{filename: "sample_multi_file_deleted.diff", wantFileDiffs: 3},
379417
{filename: "sample_multi_file_rename.diff", wantFileDiffs: 3},
418+
{filename: "sample_multi_file_binary.diff", wantFileDiffs: 3},
380419
{filename: "long_line_multi.diff", wantFileDiffs: 3},
381420
{filename: "empty.diff", wantFileDiffs: 0},
382421
{filename: "empty_multi.diff", wantFileDiffs: 2},

diff/parse.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,13 @@ func (r *FileDiffReader) ReadAllHeaders() (*FileDiff, error) {
191191
fd.NewName = "/dev/null"
192192
fd.Empty = true
193193
return fd, nil
194-
} else if len(fd.Extended) == 4 && strings.HasPrefix(fd.Extended[2], "rename from ") && strings.HasPrefix(fd.Extended[3], "rename to ") {
194+
} else if len(fd.Extended) == 4 && strings.HasPrefix(fd.Extended[2], "rename from ") && strings.HasPrefix(fd.Extended[3], "rename to ") && strings.HasPrefix(fd.Extended[0], "diff --git ") {
195+
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
196+
fd.OrigName = names[0]
197+
fd.NewName = names[1]
198+
fd.Empty = true
199+
return fd, nil
200+
} else if len(fd.Extended) == 3 && strings.HasPrefix(fd.Extended[2], "Binary files ") && strings.HasPrefix(fd.Extended[0], "diff --git ") {
195201
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
196202
fd.OrigName = names[0]
197203
fd.NewName = names[1]
@@ -211,7 +217,12 @@ func (r *FileDiffReader) ReadAllHeaders() (*FileDiff, error) {
211217
fd.OrigName = names[0]
212218
fd.NewName = "/dev/null"
213219
fd.Empty = true
214-
} else if len(fd.Extended) == 4 && strings.HasPrefix(fd.Extended[2], "rename from ") && strings.HasPrefix(fd.Extended[3], "rename to ") {
220+
} else if len(fd.Extended) == 4 && strings.HasPrefix(fd.Extended[2], "rename from ") && strings.HasPrefix(fd.Extended[3], "rename to ") && strings.HasPrefix(fd.Extended[0], "diff --git ") {
221+
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
222+
fd.OrigName = names[0]
223+
fd.NewName = names[1]
224+
fd.Empty = true
225+
} else if len(fd.Extended) == 3 && strings.HasPrefix(fd.Extended[2], "Binary files ") && strings.HasPrefix(fd.Extended[0], "diff --git ") {
215226
names := strings.SplitN(fd.Extended[0][len("diff --git "):], " ", 2)
216227
fd.OrigName = names[0]
217228
fd.NewName = names[1]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
diff --git a/data/Font.png b/data/Font.png
2+
index 17a971d..599f8dd 100644
3+
Binary files a/data/Font.png and b/data/Font.png differ
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
diff --git a/README.md b/README.md
2+
index 7b73e04..36cde13 100644
3+
--- a/README.md
4+
+++ b/README.md
5+
@@ -1,6 +1,8 @@
6+
Conception-go [![Build Status](https://travis-ci.org/shurcooL/Conception-go.svg?branch=master)](https://travis-ci.org/shurcooL/Conception-go)
7+
=============
8+
9+
+This is a change.
10+
+
11+
This is a work in progress Go implementation of [Conception](https://github.com/shurcooL/Conception#demonstration).
12+
13+
Conception is an experimental project. It's a platform for researching software development tools and techniques. It is driven by a set of guiding principles. Conception-go targets Go development.
14+
diff --git a/data/Font.png b/data/Font.png
15+
index 17a971d..599f8dd 100644
16+
Binary files a/data/Font.png and b/data/Font.png differ
17+
diff --git a/main.go b/main.go
18+
index 1aced1e..98a982e 100644
19+
--- a/main.go
20+
+++ b/main.go
21+
@@ -6710,6 +6710,8 @@ func init() {
22+
}
23+
24+
func main() {
25+
+ // Another plain text change.
26+
+
27+
//defer profile.Start(profile.CPUProfile).Stop()
28+
//defer profile.Start(profile.MemProfile).Stop()
29+

0 commit comments

Comments
 (0)