Skip to content

Commit 75da837

Browse files
committed
git: remote, Add shallow commits instead of substituting. Fixes src-d#412
updateShallow substituted the previous shallow list with the one returned by the UploadPackResponse. If the repository had previous shallow commits these are deleted from the list. This change adds the new shallow hashes to the old ones. Signed-off-by: Javi Fontan <[email protected]>
1 parent 0db54e8 commit 75da837

File tree

3 files changed

+132
-2
lines changed

3 files changed

+132
-2
lines changed

remote.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -976,9 +976,24 @@ func pushHashes(
976976
}
977977

978978
func (r *Remote) updateShallow(o *FetchOptions, resp *packp.UploadPackResponse) error {
979-
if o.Depth == 0 {
979+
if o.Depth == 0 || len(resp.Shallows) == 0 {
980980
return nil
981981
}
982982

983-
return r.s.SetShallow(resp.Shallows)
983+
shallows, err := r.s.Shallow()
984+
if err != nil {
985+
return err
986+
}
987+
988+
outer:
989+
for _, s := range resp.Shallows {
990+
for _, oldS := range shallows {
991+
if s == oldS {
992+
continue outer
993+
}
994+
}
995+
shallows = append(shallows, s)
996+
}
997+
998+
return r.s.SetShallow(shallows)
984999
}

remote_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"gopkg.in/src-d/go-git.v4/config"
1111
"gopkg.in/src-d/go-git.v4/plumbing"
12+
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp"
1213
"gopkg.in/src-d/go-git.v4/plumbing/storer"
1314
"gopkg.in/src-d/go-git.v4/storage"
1415
"gopkg.in/src-d/go-git.v4/storage/filesystem"
@@ -741,3 +742,54 @@ func (s *RemoteSuite) TestList(c *C) {
741742
c.Assert(found, Equals, true)
742743
}
743744
}
745+
746+
func (s *RemoteSuite) TestUpdateShallows(c *C) {
747+
hashes := []plumbing.Hash{
748+
plumbing.NewHash("0000000000000000000000000000000000000001"),
749+
plumbing.NewHash("0000000000000000000000000000000000000002"),
750+
plumbing.NewHash("0000000000000000000000000000000000000003"),
751+
plumbing.NewHash("0000000000000000000000000000000000000004"),
752+
plumbing.NewHash("0000000000000000000000000000000000000005"),
753+
plumbing.NewHash("0000000000000000000000000000000000000006"),
754+
}
755+
756+
tests := []struct {
757+
hashes []plumbing.Hash
758+
result []plumbing.Hash
759+
}{
760+
// add to empty shallows
761+
{hashes[0:2], hashes[0:2]},
762+
// add new hashes
763+
{hashes[2:4], hashes[0:4]},
764+
// add some hashes already in shallow list
765+
{hashes[2:6], hashes[0:6]},
766+
// add all hashes
767+
{hashes[0:6], hashes[0:6]},
768+
// add empty list
769+
{nil, hashes[0:6]},
770+
}
771+
772+
remote := newRemote(memory.NewStorage(), &config.RemoteConfig{
773+
Name: DefaultRemoteName,
774+
})
775+
776+
shallows, err := remote.s.Shallow()
777+
c.Assert(err, IsNil)
778+
c.Assert(len(shallows), Equals, 0)
779+
780+
resp := new(packp.UploadPackResponse)
781+
o := &FetchOptions{
782+
Depth: 1,
783+
}
784+
785+
for _, t := range tests {
786+
resp.Shallows = t.hashes
787+
err = remote.updateShallow(o, resp)
788+
c.Assert(err, IsNil)
789+
790+
shallow, err := remote.s.Shallow()
791+
c.Assert(err, IsNil)
792+
c.Assert(len(shallow), Equals, len(t.result))
793+
c.Assert(shallow, DeepEquals, t.result)
794+
}
795+
}

repository_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -1615,3 +1615,66 @@ func executeOnPath(path, cmd string) error {
16151615

16161616
return c.Run()
16171617
}
1618+
1619+
func (s *RepositorySuite) TestBrokenMultipleShallowFetch(c *C) {
1620+
r, _ := Init(memory.NewStorage(), nil)
1621+
_, err := r.CreateRemote(&config.RemoteConfig{
1622+
Name: DefaultRemoteName,
1623+
URLs: []string{s.GetBasicLocalRepositoryURL()},
1624+
})
1625+
c.Assert(err, IsNil)
1626+
1627+
c.Assert(r.Fetch(&FetchOptions{
1628+
Depth: 2,
1629+
RefSpecs: []config.RefSpec{config.RefSpec("refs/heads/master:refs/heads/master")},
1630+
}), IsNil)
1631+
1632+
shallows, err := r.Storer.Shallow()
1633+
c.Assert(err, IsNil)
1634+
c.Assert(len(shallows), Equals, 1)
1635+
1636+
ref, err := r.Reference("refs/heads/master", true)
1637+
c.Assert(err, IsNil)
1638+
cobj, err := r.CommitObject(ref.Hash())
1639+
c.Assert(err, IsNil)
1640+
c.Assert(cobj, NotNil)
1641+
err = object.NewCommitPreorderIter(cobj, nil, nil).ForEach(func(c *object.Commit) error {
1642+
for _, ph := range c.ParentHashes {
1643+
for _, h := range shallows {
1644+
if ph == h {
1645+
return storer.ErrStop
1646+
}
1647+
}
1648+
}
1649+
1650+
return nil
1651+
})
1652+
c.Assert(err, IsNil)
1653+
1654+
c.Assert(r.Fetch(&FetchOptions{
1655+
Depth: 5,
1656+
RefSpecs: []config.RefSpec{config.RefSpec("refs/heads/*:refs/heads/*")},
1657+
}), IsNil)
1658+
1659+
shallows, err = r.Storer.Shallow()
1660+
c.Assert(err, IsNil)
1661+
c.Assert(len(shallows), Equals, 3)
1662+
1663+
ref, err = r.Reference("refs/heads/master", true)
1664+
c.Assert(err, IsNil)
1665+
cobj, err = r.CommitObject(ref.Hash())
1666+
c.Assert(err, IsNil)
1667+
c.Assert(cobj, NotNil)
1668+
err = object.NewCommitPreorderIter(cobj, nil, nil).ForEach(func(c *object.Commit) error {
1669+
for _, ph := range c.ParentHashes {
1670+
for _, h := range shallows {
1671+
if ph == h {
1672+
return storer.ErrStop
1673+
}
1674+
}
1675+
}
1676+
1677+
return nil
1678+
})
1679+
c.Assert(err, IsNil)
1680+
}

0 commit comments

Comments
 (0)