Skip to content

Commit

Permalink
Remote: new ListContext function (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiujuan95 committed Apr 21, 2021
1 parent 9618dbb commit 67d3490
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
20 changes: 19 additions & 1 deletion remote.go
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"time"

"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5/config"
Expand Down Expand Up @@ -1030,15 +1031,32 @@ func (r *Remote) buildFetchedTags(refs memory.ReferenceStorage) (updated bool, e
}

// List the references on the remote repository.
// The provided Context must be non-nil. If the context expires before the
// operation is complete, an error is returned. The context only affects to the
// transport operations.
func (r *Remote) ListContext(ctx context.Context, o *ListOptions) (rfs []*plumbing.Reference, err error) {
refs, err := r.list(ctx, o)
if err != nil {
return refs, err
}
return refs, nil
}

func (r *Remote) List(o *ListOptions) (rfs []*plumbing.Reference, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 600*time.Millisecond)
defer cancel()
return r.ListContext(ctx, o)
}

func (r *Remote) list(ctx context.Context, o *ListOptions) (rfs []*plumbing.Reference, err error) {
s, err := newUploadPackSession(r.c.URLs[0], o.Auth, o.InsecureSkipTLS, o.CABundle)
if err != nil {
return nil, err
}

defer ioutil.CheckClose(s, &err)

ar, err := s.AdvertisedReferencesContext(context.TODO())
ar, err := s.AdvertisedReferencesContext(ctx)
if err != nil {
return nil, err
}
Expand Down
11 changes: 11 additions & 0 deletions remote_test.go
Expand Up @@ -944,6 +944,17 @@ func (s *RemoteSuite) TestList(c *C) {
}
}

func (s *RemoteSuite) TestListTimeout(c *C) {
remote := NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: DefaultRemoteName,
URLs: []string{"https://deelay.me/60000/https://httpstat.us/503"},
})

_, err := remote.List(&ListOptions{})

c.Assert(err, NotNil)
}

func (s *RemoteSuite) TestUpdateShallows(c *C) {
hashes := []plumbing.Hash{
plumbing.NewHash("0000000000000000000000000000000000000001"),
Expand Down

0 comments on commit 67d3490

Please sign in to comment.