diff options
author | Santiago M. Mola <santi@mola.io> | 2016-11-28 09:57:38 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-28 09:57:38 +0100 |
commit | 68893edf9ddc3de181431f1552e3b773cb66f080 (patch) | |
tree | f45e5fa18e10168a278b7d0ed7dea984ff9969f7 /plumbing/transport/http | |
parent | f9adb3565b36ba1573102f954d0ee916009efac2 (diff) | |
download | go-git-68893edf9ddc3de181431f1552e3b773cb66f080.tar.gz |
remove old types from transport and use packp (#142)
* protocol: move UploadPackRequest to protocol.
* UploadPackRequest is now defined as an embedding of UploadRequest and
UploadHaves.
* Move http encoding specific code from UploadPackRequest to transport/http.
* rename UlReq to UploadRequest
* packp: move AdvRefs Encoder/Decoder to Encode/Decode methods.
* packp: move UploadRequest Encoder/Decoder to Encode/Decode methods.
* packp: Remove transport.UploadPackInfo in favor of packp. AdvRefs.
Diffstat (limited to 'plumbing/transport/http')
-rw-r--r-- | plumbing/transport/http/fetch_pack.go | 49 | ||||
-rw-r--r-- | plumbing/transport/http/fetch_pack_test.go | 21 | ||||
-rw-r--r-- | plumbing/transport/http/send_pack.go | 6 |
3 files changed, 65 insertions, 11 deletions
diff --git a/plumbing/transport/http/fetch_pack.go b/plumbing/transport/http/fetch_pack.go index 29e385b..40c3a7f 100644 --- a/plumbing/transport/http/fetch_pack.go +++ b/plumbing/transport/http/fetch_pack.go @@ -31,8 +31,7 @@ func newFetchPackSession(c *http.Client, } } -func (s *fetchPackSession) AdvertisedReferences() (*transport.UploadPackInfo, - error) { +func (s *fetchPackSession) AdvertisedReferences() (*packp.AdvRefs, error) { if s.advRefsRun { return nil, transport.ErrAdvertistedReferencesAlreadyCalled } @@ -62,19 +61,19 @@ func (s *fetchPackSession) AdvertisedReferences() (*transport.UploadPackInfo, return nil, transport.ErrAuthorizationRequired } - i := transport.NewUploadPackInfo() - if err := i.Decode(res.Body); err != nil { - if err == packp.ErrEmpty { + ar := packp.NewAdvRefs() + if err := ar.Decode(res.Body); err != nil { + if err == packp.ErrEmptyAdvRefs { err = transport.ErrEmptyRemoteRepository } return nil, err } - return i, nil + return ar, nil } -func (s *fetchPackSession) FetchPack(r *transport.UploadPackRequest) (io.ReadCloser, error) { +func (s *fetchPackSession) FetchPack(r *packp.UploadPackRequest) (io.ReadCloser, error) { if r.IsEmpty() { return nil, transport.ErrEmptyUploadPackRequest } @@ -84,7 +83,12 @@ func (s *fetchPackSession) FetchPack(r *transport.UploadPackRequest) (io.ReadClo s.endpoint.String(), transport.UploadPackServiceName, ) - res, err := s.doRequest("POST", url, r.Reader()) + content, err := uploadPackRequestToReader(r) + if err != nil { + return nil, err + } + + res, err := s.doRequest("POST", url, content) if err != nil { return nil, err } @@ -161,3 +165,32 @@ func (s *fetchPackSession) applyHeadersToRequest(req *http.Request, content *str req.Header.Add("Content-Length", string(content.Len())) } } + +func uploadPackRequestToReader(r *packp.UploadPackRequest) (*strings.Reader, error) { + var buf bytes.Buffer + e := pktline.NewEncoder(&buf) + + for _, want := range r.Wants { + _ = e.Encodef("want %s\n", want) + } + + for _, have := range r.Haves { + _ = e.Encodef("have %s\n", have) + } + + if r.Depth != nil { + depth, ok := r.Depth.(packp.DepthCommits) + if !ok { + return nil, fmt.Errorf("only commit depth is supported") + } + + if depth != 0 { + _ = e.Encodef("deepen %d\n", depth) + } + } + + _ = e.Flush() + _ = e.EncodeString("done\n") + + return strings.NewReader(buf.String()), nil +} diff --git a/plumbing/transport/http/fetch_pack_test.go b/plumbing/transport/http/fetch_pack_test.go index c7666c8..7471208 100644 --- a/plumbing/transport/http/fetch_pack_test.go +++ b/plumbing/transport/http/fetch_pack_test.go @@ -1,6 +1,10 @@ package http import ( + "io/ioutil" + + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp" "gopkg.in/src-d/go-git.v4/plumbing/transport" "gopkg.in/src-d/go-git.v4/plumbing/transport/test" @@ -36,3 +40,20 @@ func (s *FetchPackSuite) TestInfoNotExists(c *C) { c.Assert(err, Equals, transport.ErrAuthorizationRequired) c.Assert(info, IsNil) } + +func (s *FetchPackSuite) TestuploadPackRequestToReader(c *C) { + r := packp.NewUploadPackRequest() + r.Want(plumbing.NewHash("d82f291cde9987322c8a0c81a325e1ba6159684c")) + r.Want(plumbing.NewHash("2b41ef280fdb67a9b250678686a0c3e03b0a9989")) + r.Have(plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) + + sr, err := uploadPackRequestToReader(r) + c.Assert(err, IsNil) + b, _ := ioutil.ReadAll(sr) + c.Assert(string(b), Equals, + "0032want d82f291cde9987322c8a0c81a325e1ba6159684c\n"+ + "0032want 2b41ef280fdb67a9b250678686a0c3e03b0a9989\n"+ + "0032have 6ecf0ef2c2dffb796033e5a02219af86ec6584e5\n0000"+ + "0009done\n", + ) +} diff --git a/plumbing/transport/http/send_pack.go b/plumbing/transport/http/send_pack.go index 39be95c..5e3b2bb 100644 --- a/plumbing/transport/http/send_pack.go +++ b/plumbing/transport/http/send_pack.go @@ -5,12 +5,13 @@ import ( "io" "net/http" + "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp" "gopkg.in/src-d/go-git.v4/plumbing/transport" ) var errSendPackNotSupported = errors.New("send-pack not supported yet") -type sendPackSession struct{ +type sendPackSession struct { *session } @@ -18,8 +19,7 @@ func newSendPackSession(c *http.Client, ep transport.Endpoint) transport.SendPac return &sendPackSession{&session{}} } -func (s *sendPackSession) AdvertisedReferences() (*transport.UploadPackInfo, - error) { +func (s *sendPackSession) AdvertisedReferences() (*packp.AdvRefs, error) { return nil, errSendPackNotSupported } |