Skip to content

Commit 67d3490

Browse files
authored
Remote: new ListContext function (go-git#278)
1 parent 9618dbb commit 67d3490

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

remote.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"time"
89

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

10321033
// List the references on the remote repository.
1034+
// The provided Context must be non-nil. If the context expires before the
1035+
// operation is complete, an error is returned. The context only affects to the
1036+
// transport operations.
1037+
func (r *Remote) ListContext(ctx context.Context, o *ListOptions) (rfs []*plumbing.Reference, err error) {
1038+
refs, err := r.list(ctx, o)
1039+
if err != nil {
1040+
return refs, err
1041+
}
1042+
return refs, nil
1043+
}
1044+
10331045
func (r *Remote) List(o *ListOptions) (rfs []*plumbing.Reference, err error) {
1046+
ctx, cancel := context.WithTimeout(context.Background(), 600*time.Millisecond)
1047+
defer cancel()
1048+
return r.ListContext(ctx, o)
1049+
}
1050+
1051+
func (r *Remote) list(ctx context.Context, o *ListOptions) (rfs []*plumbing.Reference, err error) {
10341052
s, err := newUploadPackSession(r.c.URLs[0], o.Auth, o.InsecureSkipTLS, o.CABundle)
10351053
if err != nil {
10361054
return nil, err
10371055
}
10381056

10391057
defer ioutil.CheckClose(s, &err)
10401058

1041-
ar, err := s.AdvertisedReferencesContext(context.TODO())
1059+
ar, err := s.AdvertisedReferencesContext(ctx)
10421060
if err != nil {
10431061
return nil, err
10441062
}

remote_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,17 @@ func (s *RemoteSuite) TestList(c *C) {
944944
}
945945
}
946946

947+
func (s *RemoteSuite) TestListTimeout(c *C) {
948+
remote := NewRemote(memory.NewStorage(), &config.RemoteConfig{
949+
Name: DefaultRemoteName,
950+
URLs: []string{"https://deelay.me/60000/https://httpstat.us/503"},
951+
})
952+
953+
_, err := remote.List(&ListOptions{})
954+
955+
c.Assert(err, NotNil)
956+
}
957+
947958
func (s *RemoteSuite) TestUpdateShallows(c *C) {
948959
hashes := []plumbing.Hash{
949960
plumbing.NewHash("0000000000000000000000000000000000000001"),

0 commit comments

Comments
 (0)