1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
package http
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
"github.com/go-git/go-git/v5/plumbing/protocol/packp/sideband"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/utils/ioutil"
)
type rpSession struct {
*session
}
func newReceivePackSession(c *client, ep *transport.Endpoint, auth transport.AuthMethod) (transport.ReceivePackSession, error) {
s, err := newSession(c, ep, auth)
return &rpSession{s}, err
}
func (s *rpSession) AdvertisedReferences() (*packp.AdvRefs, error) {
return advertisedReferences(context.TODO(), s.session, transport.ReceivePackServiceName)
}
func (s *rpSession) AdvertisedReferencesContext(ctx context.Context) (*packp.AdvRefs, error) {
return advertisedReferences(ctx, s.session, transport.ReceivePackServiceName)
}
func (s *rpSession) ReceivePack(ctx context.Context, req *packp.ReferenceUpdateRequest) (
*packp.ReportStatus, error) {
url := fmt.Sprintf(
"%s/%s",
s.endpoint.String(), transport.ReceivePackServiceName,
)
buf := bytes.NewBuffer(nil)
if err := req.Encode(buf); err != nil {
return nil, err
}
res, err := s.doRequest(ctx, http.MethodPost, url, buf)
if err != nil {
return nil, err
}
r, err := ioutil.NonEmptyReader(res.Body)
if err == ioutil.ErrEmptyReader {
return nil, nil
}
if err != nil {
return nil, err
}
var d *sideband.Demuxer
if req.Capabilities.Supports(capability.Sideband64k) {
d = sideband.NewDemuxer(sideband.Sideband64k, r)
} else if req.Capabilities.Supports(capability.Sideband) {
d = sideband.NewDemuxer(sideband.Sideband, r)
}
if d != nil {
d.Progress = req.Progress
r = d
}
rc := ioutil.NewReadCloser(r, res.Body)
report := packp.NewReportStatus()
if err := report.Decode(rc); err != nil {
return nil, err
}
return report, report.Error()
}
func (s *rpSession) doRequest(
ctx context.Context, method, url string, content *bytes.Buffer,
) (*http.Response, error) {
var body io.Reader
if content != nil {
body = content
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, plumbing.NewPermanentError(err)
}
applyHeadersToRequest(req, content, s.endpoint.Host, transport.ReceivePackServiceName)
s.ApplyAuthToRequest(req)
res, err := s.client.Do(req.WithContext(ctx))
if err != nil {
return nil, plumbing.NewUnexpectedError(err)
}
if err := NewErr(res); err != nil {
_ = res.Body.Close()
return nil, err
}
return res, nil
}
|