diff options
author | Xiang Xiujuan <52686173+xiujuan95@users.noreply.github.com> | 2021-04-21 19:00:26 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-21 13:00:26 +0200 |
commit | 67d34902b0c41ee5d6d283f4c5b6c2ad7db123fd (patch) | |
tree | 813f845c2eecbc57a284b9babcebedf724326f7f | |
parent | 9618dbb80cfb6d862e531c4e1272d8280ce71e1d (diff) | |
download | go-git-67d34902b0c41ee5d6d283f4c5b6c2ad7db123fd.tar.gz |
Remote: new ListContext function (#278)
-rw-r--r-- | remote.go | 20 | ||||
-rw-r--r-- | remote_test.go | 11 |
2 files changed, 30 insertions, 1 deletions
@@ -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" @@ -1030,7 +1031,24 @@ 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 @@ -1038,7 +1056,7 @@ func (r *Remote) List(o *ListOptions) (rfs []*plumbing.Reference, err error) { defer ioutil.CheckClose(s, &err) - ar, err := s.AdvertisedReferencesContext(context.TODO()) + ar, err := s.AdvertisedReferencesContext(ctx) if err != nil { return nil, err } diff --git a/remote_test.go b/remote_test.go index 38f17ad..b39f5df 100644 --- a/remote_test.go +++ b/remote_test.go @@ -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"), |