Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #567 from strib/avoid-revlist-when-deleting
Browse files Browse the repository at this point in the history
remote: avoid expensive revlist operation when only deleting refs
  • Loading branch information
mcuadros authored Aug 28, 2017
2 parents cb32722 + 467cb2a commit 7aa9d15
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
15 changes: 12 additions & 3 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ func (r *Remote) PushContext(ctx context.Context, o *PushOptions) error {
}

isDelete := false
allDelete := true
for _, rs := range o.RefSpecs {
if rs.IsDelete() {
isDelete = true
} else {
allDelete = false
}
if isDelete && !allDelete {
break
}
}
Expand Down Expand Up @@ -132,9 +137,13 @@ func (r *Remote) PushContext(ctx context.Context, o *PushOptions) error {
// we are aware.
haves = append(haves, stop...)

hashesToPush, err := revlist.Objects(r.s, objects, haves)
if err != nil {
return err
var hashesToPush []plumbing.Hash
// Avoid the expensive revlist operation if we're only doing deletes.
if !allDelete {
hashesToPush, err = revlist.Objects(r.s, objects, haves)
if err != nil {
return err
}
}

rs, err := pushHashes(ctx, s, r.s, req, hashesToPush)
Expand Down
36 changes: 36 additions & 0 deletions remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,42 @@ func (s *RemoteSuite) TestPushNewReference(c *C) {
})
}

func (s *RemoteSuite) TestPushNewReferenceAndDeleteInBatch(c *C) {
fs := fixtures.Basic().One().DotGit()
url := c.MkDir()
server, err := PlainClone(url, true, &CloneOptions{
URL: fs.Root(),
})

r, err := PlainClone(c.MkDir(), true, &CloneOptions{
URL: url,
})
c.Assert(err, IsNil)

remote, err := r.Remote(DefaultRemoteName)
c.Assert(err, IsNil)

ref, err := r.Reference(plumbing.ReferenceName("refs/heads/master"), true)
c.Assert(err, IsNil)

err = remote.Push(&PushOptions{RefSpecs: []config.RefSpec{
"refs/heads/master:refs/heads/branch2",
":refs/heads/branch",
}})
c.Assert(err, IsNil)

AssertReferences(c, server, map[string]string{
"refs/heads/branch2": ref.Hash().String(),
})

AssertReferences(c, r, map[string]string{
"refs/remotes/origin/branch2": ref.Hash().String(),
})

_, err = server.Storer.Reference(plumbing.ReferenceName("refs/heads/branch"))
c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
}

func (s *RemoteSuite) TestPushInvalidEndpoint(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URLs: []string{"http://\\"}})
err := r.Push(&PushOptions{RemoteName: "foo"})
Expand Down

0 comments on commit 7aa9d15

Please sign in to comment.