diff options
author | Santiago M. Mola <santi@mola.io> | 2016-12-14 23:12:44 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-12-14 23:12:44 +0100 |
commit | 0af572dd21c0aa79d13745b633ee24ba6c4d6cf1 (patch) | |
tree | 49e81e74e82d84fd88b2fc1e4b0dc7c7bfe9c40f /plumbing/revlist | |
parent | df0f38af83f972f026d7e14150f3d37b95f13484 (diff) | |
download | go-git-0af572dd21c0aa79d13745b633ee24ba6c4d6cf1.tar.gz |
move plumbing from top level package to plumbing (#183)
* plumbing: rename Object -> EncodedObject.
* plumbing/storer: rename ObjectStorer -> EncodedObjectStorer.
* move difftree to plumbing/difftree.
* move diff -> utils/diff
* make Object/Tag/Blob/Tree/Commit/File depend on storer.
* Object and its implementations now depend only on
storer.EncodedObjectStorer, not git.Repository.
* Tests are decoupled accordingly.
* move Object/Commit/File/Tag/Tree to plumbing/object.
* move Object/Commit/File/Tag/Tree to plumbing/object.
* move checkClose to utils/ioutil.
* move RevListObjects to plumbing/revlist.Objects.
* move DiffTree to plumbing/difftree package.
* rename files with plural nouns to singular
* plumbing/object: add GetBlob/GetCommit/GetTag/GetTree.
Diffstat (limited to 'plumbing/revlist')
-rw-r--r-- | plumbing/revlist/revlist.go | 128 | ||||
-rw-r--r-- | plumbing/revlist/revlist_test.go | 150 |
2 files changed, 278 insertions, 0 deletions
diff --git a/plumbing/revlist/revlist.go b/plumbing/revlist/revlist.go new file mode 100644 index 0000000..106e78b --- /dev/null +++ b/plumbing/revlist/revlist.go @@ -0,0 +1,128 @@ +// Package revlist implements functions to walk the objects referenced by a +// commit history. Roughly equivalent to git-rev-list command. +package revlist + +import ( + "io" + + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/object" + "gopkg.in/src-d/go-git.v4/plumbing/storer" +) + +// Objects applies a complementary set. It gets all the hashes from all +// the reachable objects from the given commits. Ignore param are object hashes +// that we want to ignore on the result. It is a list because is +// easier to interact with other porcelain elements, but internally it is +// converted to a map. All that objects must be accessible from the object +// storer. +func Objects( + s storer.EncodedObjectStorer, + commits []*object.Commit, + ignore []plumbing.Hash) ([]plumbing.Hash, error) { + + seen := hashListToSet(ignore) + result := make(map[plumbing.Hash]bool) + for _, c := range commits { + err := reachableObjects(s, c, seen, func(h plumbing.Hash) error { + if !seen[h] { + result[h] = true + seen[h] = true + } + + return nil + }) + + if err != nil { + return nil, err + } + } + + return hashSetToList(result), nil +} + +// reachableObjects returns, using the callback function, all the reachable +// objects from the specified commit. To avoid to iterate over seen commits, +// if a commit hash is into the 'seen' set, we will not iterate all his trees +// and blobs objects. +func reachableObjects( + s storer.EncodedObjectStorer, + commit *object.Commit, + seen map[plumbing.Hash]bool, + cb func(h plumbing.Hash) error) error { + + return iterateCommits(commit, func(commit *object.Commit) error { + if seen[commit.Hash] { + return nil + } + + if err := cb(commit.Hash); err != nil { + return err + } + + return iterateCommitTrees(s, commit, func(h plumbing.Hash) error { + return cb(h) + }) + }) +} + +// iterateCommits iterate all reachable commits from the given one +func iterateCommits(commit *object.Commit, cb func(c *object.Commit) error) error { + if err := cb(commit); err != nil { + return err + } + + return object.WalkCommitHistory(commit, func(c *object.Commit) error { + return cb(c) + }) +} + +// iterateCommitTrees iterate all reachable trees from the given commit +func iterateCommitTrees( + s storer.EncodedObjectStorer, + commit *object.Commit, + cb func(h plumbing.Hash) error) error { + + tree, err := commit.Tree() + if err != nil { + return err + } + if err := cb(tree.Hash); err != nil { + return err + } + + treeWalker := object.NewTreeWalker(tree, true) + + for { + _, e, err := treeWalker.Next() + if err == io.EOF { + break + } + if err != nil { + return err + } + if err := cb(e.Hash); err != nil { + return err + } + } + + return nil +} + +func hashSetToList(hashes map[plumbing.Hash]bool) []plumbing.Hash { + var result []plumbing.Hash + for key := range hashes { + result = append(result, key) + } + + return result +} + +func hashListToSet(hashes []plumbing.Hash) map[plumbing.Hash]bool { + result := make(map[plumbing.Hash]bool) + for _, h := range hashes { + result[h] = true + } + + return result +} diff --git a/plumbing/revlist/revlist_test.go b/plumbing/revlist/revlist_test.go new file mode 100644 index 0000000..60ae660 --- /dev/null +++ b/plumbing/revlist/revlist_test.go @@ -0,0 +1,150 @@ +package revlist + +import ( + "testing" + + "gopkg.in/src-d/go-git.v4/fixtures" + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/object" + "gopkg.in/src-d/go-git.v4/plumbing/storer" + "gopkg.in/src-d/go-git.v4/storage/filesystem" + + . "gopkg.in/check.v1" +) + +func Test(t *testing.T) { TestingT(t) } + +type RevListSuite struct { + fixtures.Suite + Storer storer.EncodedObjectStorer +} + +var _ = Suite(&RevListSuite{}) + +const ( + initialCommit = "b029517f6300c2da0f4b651b8642506cd6aaf45d" + secondCommit = "b8e471f58bcbca63b07bda20e428190409c2db47" + + someCommit = "918c48b83bd081e863dbe1b80f8998f058cd8294" + someCommitBranch = "e8d3ffab552895c19b9fcf7aa264d277cde33881" + someCommitOtherBranch = "6ecf0ef2c2dffb796033e5a02219af86ec6584e5" +) + +// Created using: git log --graph --oneline --all +// +// Basic fixture repository commits tree: +// +// * 6ecf0ef vendor stuff +// | * e8d3ffa some code in a branch +// |/ +// * 918c48b some code +// * af2d6a6 some json +// * 1669dce Merge branch 'master' +// |\ +// | * a5b8b09 Merge pull request #1 +// | |\ +// | | * b8e471f Creating changelog +// | |/ +// * | 35e8510 binary file +// |/ +// * b029517 Initial commit + +func (s *RevListSuite) SetUpTest(c *C) { + s.Suite.SetUpSuite(c) + sto, err := filesystem.NewStorage(fixtures.Basic().One().DotGit()) + c.Assert(err, IsNil) + s.Storer = sto +} + +func (s *RevListSuite) commit(c *C, h plumbing.Hash) *object.Commit { + commit, err := object.GetCommit(s.Storer, h) + c.Assert(err, IsNil) + return commit +} + +// --- +// | |\ +// | | * b8e471f Creating changelog +// | |/ +// * | 35e8510 binary file +// |/ +// * b029517 Initial commit +func (s *RevListSuite) TestRevListObjects(c *C) { + revList := map[string]bool{ + "b8e471f58bcbca63b07bda20e428190409c2db47": true, // second commit + "c2d30fa8ef288618f65f6eed6e168e0d514886f4": true, // init tree + "d3ff53e0564a9f87d8e84b6e28e5060e517008aa": true, // CHANGELOG + } + + initCommit := s.commit(c, plumbing.NewHash(initialCommit)) + secondCommit := s.commit(c, plumbing.NewHash(secondCommit)) + + localHist, err := Objects(s.Storer, []*object.Commit{initCommit}, nil) + c.Assert(err, IsNil) + + remoteHist, err := Objects(s.Storer, []*object.Commit{secondCommit}, localHist) + c.Assert(err, IsNil) + + for _, h := range remoteHist { + c.Assert(revList[h.String()], Equals, true) + } + c.Assert(len(remoteHist), Equals, len(revList)) +} + +func (s *RevListSuite) TestRevListObjectsReverse(c *C) { + initCommit := s.commit(c, plumbing.NewHash(initialCommit)) + secondCommit := s.commit(c, plumbing.NewHash(secondCommit)) + + localHist, err := Objects(s.Storer, []*object.Commit{secondCommit}, nil) + c.Assert(err, IsNil) + + remoteHist, err := Objects(s.Storer, []*object.Commit{initCommit}, localHist) + c.Assert(err, IsNil) + + c.Assert(len(remoteHist), Equals, 0) +} + +func (s *RevListSuite) TestRevListObjectsSameCommit(c *C) { + commit := s.commit(c, plumbing.NewHash(secondCommit)) + + localHist, err := Objects(s.Storer, []*object.Commit{commit}, nil) + c.Assert(err, IsNil) + + remoteHist, err := Objects(s.Storer, []*object.Commit{commit}, localHist) + c.Assert(err, IsNil) + + c.Assert(len(remoteHist), Equals, 0) +} + +// * 6ecf0ef vendor stuff +// | * e8d3ffa some code in a branch +// |/ +// * 918c48b some code +// ----- +func (s *RevListSuite) TestRevListObjectsNewBranch(c *C) { + someCommit := s.commit(c, plumbing.NewHash(someCommit)) + someCommitBranch := s.commit(c, plumbing.NewHash(someCommitBranch)) + someCommitOtherBranch := s.commit(c, plumbing.NewHash(someCommitOtherBranch)) + + localHist, err := Objects(s.Storer, []*object.Commit{someCommit}, nil) + c.Assert(err, IsNil) + + remoteHist, err := Objects( + s.Storer, []*object.Commit{someCommitBranch, someCommitOtherBranch}, localHist) + c.Assert(err, IsNil) + + revList := map[string]bool{ + "a8d315b2b1c615d43042c3a62402b8a54288cf5c": true, // init tree + "cf4aa3b38974fb7d81f367c0830f7d78d65ab86b": true, // vendor folder + "9dea2395f5403188298c1dabe8bdafe562c491e3": true, // foo.go + "e8d3ffab552895c19b9fcf7aa264d277cde33881": true, // branch commit + "dbd3641b371024f44d0e469a9c8f5457b0660de1": true, // init tree + "7e59600739c96546163833214c36459e324bad0a": true, // README + "6ecf0ef2c2dffb796033e5a02219af86ec6584e5": true, // otherBranch commit + } + + for _, h := range remoteHist { + c.Assert(revList[h.String()], Equals, true) + } + c.Assert(len(remoteHist), Equals, len(revList)) +} |