Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit a955b6f

Browse files
authored
Merge pull request #677 from krylovsk/bugffix/stats-on-empty-patch
object: patch, fix stats for submodules (fixes #654)
2 parents eb74b0e + 8993246 commit a955b6f

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

plumbing/format/diff/unified_encoder.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package diff
22

33
import (
44
"bytes"
5-
"errors"
65
"fmt"
76
"io"
87
"strings"
@@ -45,8 +44,6 @@ const (
4544
DefaultContextLines = 3
4645
)
4746

48-
var ErrBothFilesEmpty = errors.New("both files are empty")
49-
5047
// UnifiedEncoder encodes an unified diff into the provided Writer.
5148
// There are some unsupported features:
5249
// - Similarity index for renames
@@ -106,7 +103,7 @@ func (e *UnifiedEncoder) printMessage(message string) {
106103
func (e *UnifiedEncoder) header(from, to File, isBinary bool) error {
107104
switch {
108105
case from == nil && to == nil:
109-
return ErrBothFilesEmpty
106+
return nil
110107
case from != nil && to != nil:
111108
hashEquals := from.Hash() == to.Hash()
112109

plumbing/format/diff/unified_encoder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (s *UnifiedEncoderTestSuite) TestBothFilesEmpty(c *C) {
2020
buffer := bytes.NewBuffer(nil)
2121
e := NewUnifiedEncoder(buffer, 1)
2222
err := e.Encode(testPatch{filePatches: []testFilePatch{{}}})
23-
c.Assert(err, Equals, ErrBothFilesEmpty)
23+
c.Assert(err, IsNil)
2424
}
2525

2626
func (s *UnifiedEncoderTestSuite) TestBinaryFile(c *C) {

plumbing/object/patch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ func getFileStatsFromFilePatches(filePatches []fdiff.FilePatch) FileStats {
271271
var fileStats FileStats
272272

273273
for _, fp := range filePatches {
274+
// ignore empty patches (binary files, submodule refs updates)
275+
if len(fp.Chunks()) == 0 {
276+
continue
277+
}
278+
274279
cs := FileStat{}
275280
from, to := fp.Files()
276281
if from == nil {

plumbing/object/patch_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package object
2+
3+
import (
4+
. "gopkg.in/check.v1"
5+
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
6+
"gopkg.in/src-d/go-git.v4/plumbing"
7+
"gopkg.in/src-d/go-git.v4/storage/filesystem"
8+
)
9+
10+
type PatchSuite struct {
11+
BaseObjectsSuite
12+
}
13+
14+
var _ = Suite(&PatchSuite{})
15+
16+
func (s *PatchSuite) TestStatsWithSubmodules(c *C) {
17+
storer, err := filesystem.NewStorage(
18+
fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().DotGit())
19+
20+
commit, err := GetCommit(storer, plumbing.NewHash("b685400c1f9316f350965a5993d350bc746b0bf4"))
21+
22+
tree, err := commit.Tree()
23+
c.Assert(err, IsNil)
24+
25+
e, err := tree.entry("basic")
26+
c.Assert(err, IsNil)
27+
28+
ch := &Change{
29+
From: ChangeEntry{
30+
Name: "basic",
31+
Tree: tree,
32+
TreeEntry: *e,
33+
},
34+
To: ChangeEntry{
35+
Name: "basic",
36+
Tree: tree,
37+
TreeEntry: *e,
38+
},
39+
}
40+
41+
p, err := getPatch("", ch)
42+
c.Assert(err, IsNil)
43+
c.Assert(p, NotNil)
44+
}

0 commit comments

Comments
 (0)