diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-07-13 13:21:36 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-07-13 13:21:36 +0200 |
commit | c0de8847ecbb20c99e652a8db5edc883c0e2f9ba (patch) | |
tree | 1027789d4692c5da82cc1e87606a48de42796371 | |
parent | 6b3a6df29920d39b8308924b3b84178226b56224 (diff) | |
download | go-git-c0de8847ecbb20c99e652a8db5edc883c0e2f9ba.tar.gz |
remote: avoid duplicate haves
-rw-r--r-- | remote.go | 16 | ||||
-rw-r--r-- | remote_test.go | 19 |
2 files changed, 32 insertions, 3 deletions
@@ -356,20 +356,30 @@ func getHaves(localRefs storer.ReferenceStorer) ([]plumbing.Hash, error) { return nil, err } - var haves []plumbing.Hash + haves := map[plumbing.Hash]bool{} err = iter.ForEach(func(ref *plumbing.Reference) error { + if haves[ref.Hash()] == true { + return nil + } + if ref.Type() != plumbing.HashReference { return nil } - haves = append(haves, ref.Hash()) + haves[ref.Hash()] = true return nil }) + if err != nil { return nil, err } - return haves, nil + var result []plumbing.Hash + for h := range haves { + result = append(result, h) + } + + return result, nil } func getWants( diff --git a/remote_test.go b/remote_test.go index c12d5d5..501d06e 100644 --- a/remote_test.go +++ b/remote_test.go @@ -499,6 +499,25 @@ func (s *RemoteSuite) TestPushWrongRemoteName(c *C) { c.Assert(err, ErrorMatches, ".*remote names don't match.*") } +func (s *RemoteSuite) TestGetHaves(c *C) { + st := memory.NewStorage() + st.SetReference(plumbing.NewReferenceFromStrings( + "foo", "f7b877701fbf855b44c0a9e86f3fdce2c298b07f", + )) + + st.SetReference(plumbing.NewReferenceFromStrings( + "bar", "fe6cb94756faa81e5ed9240f9191b833db5f40ae", + )) + + st.SetReference(plumbing.NewReferenceFromStrings( + "qux", "f7b877701fbf855b44c0a9e86f3fdce2c298b07f", + )) + + l, err := getHaves(st) + c.Assert(err, IsNil) + c.Assert(l, HasLen, 2) +} + const bareConfig = `[core] repositoryformatversion = 0 filemode = true |