diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2020-04-27 12:29:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-27 12:29:07 +0200 |
commit | dcaccf7ad7fdb9e5563c8dc8a9b2fc1f3d871581 (patch) | |
tree | d79226ef7900375d1a096b3c10c29298b33b67d6 | |
parent | 2a623877d344e3a4e9f3260ffd55ed80aff1f592 (diff) | |
parent | 5d380d36ddd33b32a5d5ec1b4636605309564ddb (diff) | |
download | go-git-dcaccf7ad7fdb9e5563c8dc8a9b2fc1f3d871581.tar.gz |
Merge pull request #42 from mcuadros/renames-tree
plumbing: object, make renames diff default
-rw-r--r-- | plumbing/object/commit.go | 6 | ||||
-rw-r--r-- | plumbing/object/tree.go | 35 | ||||
-rw-r--r-- | plumbing/object/tree_test.go | 30 |
3 files changed, 55 insertions, 16 deletions
diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go index b37ff61..b27f20b 100644 --- a/plumbing/object/commit.go +++ b/plumbing/object/commit.go @@ -78,6 +78,9 @@ func (c *Commit) Tree() (*Tree, error) { // PatchContext returns the Patch between the actual commit and the provided one. // Error will be return if context expires. Provided context must be non-nil. +// +// NOTE: Since version 5.1.0 the renames are correctly handled, the settings +// used are the recommended options DefaultDiffTreeOptions. func (c *Commit) PatchContext(ctx context.Context, to *Commit) (*Patch, error) { fromTree, err := c.Tree() if err != nil { @@ -93,6 +96,9 @@ func (c *Commit) PatchContext(ctx context.Context, to *Commit) (*Patch, error) { } // Patch returns the Patch between the actual commit and the provided one. +// +// NOTE: Since version 5.1.0 the renames are correctly handled, the settings +// used are the recommended options DefaultDiffTreeOptions. func (c *Commit) Patch(to *Commit) (*Patch, error) { return c.PatchContext(context.Background(), to) } diff --git a/plumbing/object/tree.go b/plumbing/object/tree.go index d3c8c77..5e6378c 100644 --- a/plumbing/object/tree.go +++ b/plumbing/object/tree.go @@ -304,29 +304,34 @@ func (t *Tree) buildMap() { } // Diff returns a list of changes between this tree and the provided one -func (from *Tree) Diff(to *Tree) (Changes, error) { - return DiffTree(from, to) +func (t *Tree) Diff(to *Tree) (Changes, error) { + return t.DiffContext(context.Background(), to) } -// Diff returns a list of changes between this tree and the provided one -// Error will be returned if context expires -// Provided context must be non nil -func (from *Tree) DiffContext(ctx context.Context, to *Tree) (Changes, error) { - return DiffTreeContext(ctx, from, to) +// DiffContext returns a list of changes between this tree and the provided one +// Error will be returned if context expires. Provided context must be non nil. +// +// NOTE: Since version 5.1.0 the renames are correctly handled, the settings +// used are the recommended options DefaultDiffTreeOptions. +func (t *Tree) DiffContext(ctx context.Context, to *Tree) (Changes, error) { + return DiffTreeWithOptions(ctx, t, to, DefaultDiffTreeOptions) } // Patch returns a slice of Patch objects with all the changes between trees // in chunks. This representation can be used to create several diff outputs. -func (from *Tree) Patch(to *Tree) (*Patch, error) { - return from.PatchContext(context.Background(), to) +func (t *Tree) Patch(to *Tree) (*Patch, error) { + return t.PatchContext(context.Background(), to) } -// Patch returns a slice of Patch objects with all the changes between trees -// in chunks. This representation can be used to create several diff outputs. -// If context expires, an error will be returned -// Provided context must be non-nil -func (from *Tree) PatchContext(ctx context.Context, to *Tree) (*Patch, error) { - changes, err := DiffTreeContext(ctx, from, to) +// PatchContext returns a slice of Patch objects with all the changes between +// trees in chunks. This representation can be used to create several diff +// outputs. If context expires, an error will be returned. Provided context must +// be non-nil. +// +// NOTE: Since version 5.1.0 the renames are correctly handled, the settings +// used are the recommended options DefaultDiffTreeOptions. +func (t *Tree) PatchContext(ctx context.Context, to *Tree) (*Patch, error) { + changes, err := t.DiffContext(ctx, to) if err != nil { return nil, err } diff --git a/plumbing/object/tree_test.go b/plumbing/object/tree_test.go index 319caae..474bb6a 100644 --- a/plumbing/object/tree_test.go +++ b/plumbing/object/tree_test.go @@ -1,9 +1,11 @@ package object import ( + "context" "errors" "io" + fixtures "github.com/go-git/go-git-fixtures/v4" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/plumbing/filemode" @@ -11,7 +13,6 @@ import ( "github.com/go-git/go-git/v5/storage/filesystem" . "gopkg.in/check.v1" - "github.com/go-git/go-git-fixtures/v4" ) type TreeSuite struct { @@ -240,6 +241,33 @@ func (s *TreeSuite) TestTreeDecodeEncodeIdempotent(c *C) { } } +func (s *TreeSuite) TestTreeDiff(c *C) { + f := fixtures.ByURL("https://github.com/src-d/go-git.git").One() + storer := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault()) + commit, err := GetCommit(storer, plumbing.NewHash("89f8bda31d29767a6d6ba8f9d0dfb941d598e843")) + c.Assert(err, IsNil) + + tree, err := commit.Tree() + c.Assert(err, IsNil) + + parentCommit, err := commit.Parent(0) + c.Assert(err, IsNil) + + parentTree, err := parentCommit.Tree() + c.Assert(err, IsNil) + + ch, err := parentTree.Diff(tree) + c.Assert(err, IsNil) + + c.Assert(ch, HasLen, 3) + c.Assert(ch[0].From.Name, Equals, "examples/object_storage/main.go") + c.Assert(ch[0].To.Name, Equals, "examples/storage/main.go") + + ch, err = parentTree.DiffContext(context.Background(), tree) + c.Assert(err, IsNil) + c.Assert(ch, HasLen, 3) +} + func (s *TreeSuite) TestTreeIter(c *C) { encIter, err := s.Storer.IterEncodedObjects(plumbing.TreeObject) c.Assert(err, IsNil) |