Skip to content

Commit 6f9eb4e

Browse files
authored
Merge pull request go-git#62 from mcuadros/commit-patch-nil
plumbing: object, Commit.Patch support to as nil
2 parents 568154c + 08da9bb commit 6f9eb4e

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

plumbing/object/commit.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ func (c *Commit) PatchContext(ctx context.Context, to *Commit) (*Patch, error) {
8787
return nil, err
8888
}
8989

90-
toTree, err := to.Tree()
91-
if err != nil {
92-
return nil, err
90+
var toTree *Tree
91+
if to != nil {
92+
toTree, err = to.Tree()
93+
if err != nil {
94+
return nil, err
95+
}
9396
}
9497

9598
return fromTree.PatchContext(ctx, toTree)

plumbing/object/commit_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/go-git/go-git/v5/plumbing"
1313
"github.com/go-git/go-git/v5/plumbing/cache"
1414

15-
. "gopkg.in/check.v1"
1615
"github.com/go-git/go-git/v5/storage/filesystem"
16+
. "gopkg.in/check.v1"
1717
)
1818

1919
type SuiteCommit struct {
@@ -188,6 +188,15 @@ Binary files /dev/null and b/binary.jpg differ
188188
c.Assert(buf.String(), Equals, patch.String())
189189
}
190190

191+
func (s *SuiteCommit) TestPatchContext_ToNil(c *C) {
192+
from := s.commit(c, plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294"))
193+
194+
patch, err := from.PatchContext(context.Background(), nil)
195+
c.Assert(err, IsNil)
196+
197+
c.Assert(len(patch.String()), Equals, 242679)
198+
}
199+
191200
func (s *SuiteCommit) TestCommitEncodeDecodeIdempotent(c *C) {
192201
ts, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05-07:00")
193202
c.Assert(err, IsNil)

plumbing/object/tree_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,17 @@ func (s *TreeSuite) TestTreeWalkerNextNonRecursive(c *C) {
377377
c.Assert(count, Equals, 8)
378378
}
379379

380+
func (s *TreeSuite) TestPatchContext_ToNil(c *C) {
381+
commit := s.commit(c, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
382+
tree, err := commit.Tree()
383+
c.Assert(err, IsNil)
384+
385+
patch, err := tree.PatchContext(context.Background(), nil)
386+
c.Assert(err, IsNil)
387+
388+
c.Assert(len(patch.String()), Equals, 242971)
389+
}
390+
380391
func (s *TreeSuite) TestTreeWalkerNextSubmodule(c *C) {
381392
dotgit := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().DotGit()
382393
st := filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault())

0 commit comments

Comments
 (0)