From 08da9bb09f6046c7344f537fc4a819ed03d23f55 Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Wed, 13 May 2020 11:58:48 +0200 Subject: plumbing: object, Commit.Patch support to as nil --- plumbing/object/commit.go | 9 ++++++--- plumbing/object/commit_test.go | 11 ++++++++++- plumbing/object/tree_test.go | 11 +++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go index b27f20b..113cb29 100644 --- a/plumbing/object/commit.go +++ b/plumbing/object/commit.go @@ -87,9 +87,12 @@ func (c *Commit) PatchContext(ctx context.Context, to *Commit) (*Patch, error) { return nil, err } - toTree, err := to.Tree() - if err != nil { - return nil, err + var toTree *Tree + if to != nil { + toTree, err = to.Tree() + if err != nil { + return nil, err + } } return fromTree.PatchContext(ctx, toTree) diff --git a/plumbing/object/commit_test.go b/plumbing/object/commit_test.go index 28a7a81..e260a7f 100644 --- a/plumbing/object/commit_test.go +++ b/plumbing/object/commit_test.go @@ -12,8 +12,8 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/cache" - . "gopkg.in/check.v1" "github.com/go-git/go-git/v5/storage/filesystem" + . "gopkg.in/check.v1" ) type SuiteCommit struct { @@ -188,6 +188,15 @@ Binary files /dev/null and b/binary.jpg differ c.Assert(buf.String(), Equals, patch.String()) } +func (s *SuiteCommit) TestPatchContext_ToNil(c *C) { + from := s.commit(c, plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294")) + + patch, err := from.PatchContext(context.Background(), nil) + c.Assert(err, IsNil) + + c.Assert(len(patch.String()), Equals, 242679) +} + func (s *SuiteCommit) TestCommitEncodeDecodeIdempotent(c *C) { ts, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05-07:00") c.Assert(err, IsNil) diff --git a/plumbing/object/tree_test.go b/plumbing/object/tree_test.go index 474bb6a..d9dad47 100644 --- a/plumbing/object/tree_test.go +++ b/plumbing/object/tree_test.go @@ -377,6 +377,17 @@ func (s *TreeSuite) TestTreeWalkerNextNonRecursive(c *C) { c.Assert(count, Equals, 8) } +func (s *TreeSuite) TestPatchContext_ToNil(c *C) { + commit := s.commit(c, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) + tree, err := commit.Tree() + c.Assert(err, IsNil) + + patch, err := tree.PatchContext(context.Background(), nil) + c.Assert(err, IsNil) + + c.Assert(len(patch.String()), Equals, 242971) +} + func (s *TreeSuite) TestTreeWalkerNextSubmodule(c *C) { dotgit := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().DotGit() st := filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault()) -- cgit