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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// Package ssh implements a ssh client for go-git.
package ssh
import (
"bufio"
"bytes"
"fmt"
"io"
"gopkg.in/src-d/go-git.v4/plumbing/format/pktline"
"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/utils/ioutil"
"golang.org/x/crypto/ssh"
)
type fetchPackSession struct {
*session
cmdRun bool
advRefsRun bool
done chan error
}
func newFetchPackSession(ep transport.Endpoint) (*fetchPackSession, error) {
s := &fetchPackSession{
session: &session{
endpoint: ep,
},
}
if err := s.connect(); err != nil {
return nil, err
}
return s, nil
}
func (s *fetchPackSession) AdvertisedReferences() (*transport.UploadPackInfo, error) {
if s.advRefsRun {
return nil, transport.ErrAdvertistedReferencesAlreadyCalled
}
defer func() { s.advRefsRun = true }()
if err := s.ensureRunCommand(); err != nil {
return nil, err
}
i := transport.NewUploadPackInfo()
if err := i.Decode(s.stdout); err != nil {
if err != packp.ErrEmpty {
return nil, err
}
_ = s.stdin.Close()
scan := bufio.NewScanner(s.stderr)
if !scan.Scan() {
return nil, transport.ErrEmptyRemoteRepository
}
if isRepoNotFoundError(string(scan.Bytes())) {
return nil, transport.ErrRepositoryNotFound
}
return nil, err
}
return i, nil
}
// FetchPack returns a packfile for a given upload request.
// Closing the returned reader will close the SSH session.
func (s *fetchPackSession) FetchPack(req *transport.UploadPackRequest) (
io.ReadCloser, error) {
if req.IsEmpty() {
return nil, transport.ErrEmptyUploadPackRequest
}
if !s.advRefsRun {
if _, err := s.AdvertisedReferences(); err != nil {
return nil, err
}
}
if err := fetchPack(s.stdin, s.stdout, req); err != nil {
return nil, err
}
fs := &fetchSession{
Reader: s.stdout,
session: s.session.session,
done: s.done,
}
r, err := ioutil.NonEmptyReader(fs)
if err == ioutil.ErrEmptyReader {
_ = fs.Close()
return nil, transport.ErrEmptyUploadPackRequest
}
return ioutil.NewReadCloser(r, fs), nil
}
func (s *fetchPackSession) ensureRunCommand() error {
if s.cmdRun {
return nil
}
s.cmdRun = true
s.done = s.runCommand(s.getCommand())
return nil
}
type fetchSession struct {
io.Reader
session *ssh.Session
done <-chan error
}
// Close closes the session and collects the output state of the remote
// SSH command.
//
// If both the remote command and the closing of the session completes
// susccessfully it returns nil.
//
// If the remote command completes unsuccessfully or is interrupted by a
// signal, it returns the corresponding *ExitError.
//
// Otherwise, if clossing the SSH session fails it returns the close
// error. Closing the session when the other has already close it is
// not cosidered an error.
func (f *fetchSession) Close() (err error) {
if err := <-f.done; err != nil {
return err
}
if err := f.session.Close(); err != nil && err != io.EOF {
return err
}
return nil
}
func (s *fetchPackSession) getCommand() string {
directory := s.endpoint.Path
directory = directory[1:]
return fmt.Sprintf("git-upload-pack '%s'", directory)
}
var (
nak = []byte("NAK")
eol = []byte("\n")
)
// FetchPack implements the git-fetch-pack protocol.
//
// TODO support multi_ack mode
// TODO support multi_ack_detailed mode
// TODO support acks for common objects
// TODO build a proper state machine for all these processing options
func fetchPack(w io.WriteCloser, r io.Reader,
req *transport.UploadPackRequest) error {
if err := sendUlReq(w, req); err != nil {
return fmt.Errorf("sending upload-req message: %s", err)
}
if err := sendHaves(w, req); err != nil {
return fmt.Errorf("sending haves message: %s", err)
}
if err := sendDone(w); err != nil {
return fmt.Errorf("sending done message: %s", err)
}
if err := w.Close(); err != nil {
return fmt.Errorf("closing input: %s", err)
}
if err := readNAK(r); err != nil {
return fmt.Errorf("reading NAK: %s", err)
}
return nil
}
func sendUlReq(w io.Writer, req *transport.UploadPackRequest) error {
ur := packp.NewUlReq()
ur.Wants = req.Wants
ur.Depth = packp.DepthCommits(req.Depth)
e := packp.NewUlReqEncoder(w)
return e.Encode(ur)
}
func sendHaves(w io.Writer, req *transport.UploadPackRequest) error {
e := pktline.NewEncoder(w)
for _, have := range req.Haves {
if err := e.Encodef("have %s\n", have); err != nil {
return fmt.Errorf("sending haves for %q: %s", have, err)
}
}
if len(req.Haves) != 0 {
if err := e.Flush(); err != nil {
return fmt.Errorf("sending flush-pkt after haves: %s", err)
}
}
return nil
}
func sendDone(w io.Writer) error {
e := pktline.NewEncoder(w)
return e.Encodef("done\n")
}
func readNAK(r io.Reader) error {
s := pktline.NewScanner(r)
if !s.Scan() {
return s.Err()
}
b := s.Bytes()
b = bytes.TrimSuffix(b, eol)
if !bytes.Equal(b, nak) {
return fmt.Errorf("expecting NAK, found %q instead", string(b))
}
return nil
}
|