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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
package git
import (
"errors"
"fmt"
"io"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/format/packfile"
"gopkg.in/src-d/go-git.v4/plumbing/format/packp"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/client"
)
var NoErrAlreadyUpToDate = errors.New("already up-to-date")
// Remote represents a connection to a remote repository
type Remote struct {
c *config.RemoteConfig
s Storer
// cache fields, there during the connection is open
endpoint transport.Endpoint
client transport.Client
fetchSession transport.FetchPackSession
upInfo *transport.UploadPackInfo
}
func newRemote(s Storer, c *config.RemoteConfig) *Remote {
return &Remote{s: s, c: c}
}
// Config return the config
func (r *Remote) Config() *config.RemoteConfig {
return r.c
}
// Connect with the endpoint
func (r *Remote) Connect() error {
if err := r.initClient(); err != nil {
return err
}
var err error
r.fetchSession, err = r.client.NewFetchPackSession(r.endpoint)
if err != nil {
return nil
}
return r.retrieveUpInfo()
}
func (r *Remote) initClient() error {
var err error
r.endpoint, err = transport.NewEndpoint(r.c.URL)
if err != nil {
return err
}
if r.client != nil {
return nil
}
r.client, err = client.NewClient(r.endpoint)
if err != nil {
return err
}
return nil
}
func (r *Remote) retrieveUpInfo() error {
var err error
r.upInfo, err = r.fetchSession.AdvertisedReferences()
return err
}
// Info returns the git-upload-pack info
func (r *Remote) Info() *transport.UploadPackInfo {
return r.upInfo
}
// Capabilities returns the remote capabilities
func (r *Remote) Capabilities() *packp.Capabilities {
return r.upInfo.Capabilities
}
// Fetch returns a reader using the request
func (r *Remote) Fetch(o *FetchOptions) (err error) {
if err := o.Validate(); err != nil {
return err
}
if len(o.RefSpecs) == 0 {
o.RefSpecs = r.c.Fetch
}
refs, err := r.getWantedReferences(o.RefSpecs)
if err != nil {
return err
}
if len(refs) == 0 {
return NoErrAlreadyUpToDate
}
req, err := r.buildRequest(r.s, o, refs)
if err != nil {
return err
}
reader, err := r.fetchSession.FetchPack(req)
if err != nil {
return err
}
defer checkClose(reader, &err)
if err := r.updateObjectStorage(reader); err != nil {
return err
}
return r.updateLocalReferenceStorage(o.RefSpecs, refs)
}
func (r *Remote) getWantedReferences(spec []config.RefSpec) ([]*plumbing.Reference, error) {
var refs []*plumbing.Reference
iter, err := r.Refs()
if err != nil {
return refs, err
}
wantTags := true
for _, s := range spec {
if !s.IsWildcard() {
wantTags = false
break
}
}
return refs, iter.ForEach(func(ref *plumbing.Reference) error {
if ref.Type() != plumbing.HashReference {
return nil
}
if !config.MatchAny(spec, ref.Name()) {
if !ref.IsTag() || !wantTags {
return nil
}
}
_, err := r.s.Object(plumbing.CommitObject, ref.Hash())
if err == plumbing.ErrObjectNotFound {
refs = append(refs, ref)
return nil
}
return err
})
}
func (r *Remote) buildRequest(
s storer.ReferenceStorer, o *FetchOptions, refs []*plumbing.Reference,
) (*transport.UploadPackRequest, error) {
req := &transport.UploadPackRequest{}
req.Depth = o.Depth
for _, ref := range refs {
req.Want(ref.Hash())
}
i, err := s.IterReferences()
if err != nil {
return nil, err
}
err = i.ForEach(func(ref *plumbing.Reference) error {
if ref.Type() != plumbing.HashReference {
return nil
}
req.Have(ref.Hash())
return nil
})
return req, err
}
func (r *Remote) updateObjectStorage(reader io.Reader) error {
if sw, ok := r.s.(storer.PackfileWriter); ok {
w, err := sw.PackfileWriter()
if err != nil {
return err
}
defer w.Close()
_, err = io.Copy(w, reader)
return err
}
stream := packfile.NewScanner(reader)
d, err := packfile.NewDecoder(stream, r.s)
if err != nil {
return err
}
_, err = d.Decode()
return err
}
func (r *Remote) updateLocalReferenceStorage(specs []config.RefSpec, refs []*plumbing.Reference) error {
for _, spec := range specs {
for _, ref := range refs {
if !spec.Match(ref.Name()) {
continue
}
if ref.Type() != plumbing.HashReference {
continue
}
name := spec.Dst(ref.Name())
n := plumbing.NewHashReference(name, ref.Hash())
if err := r.s.SetReference(n); err != nil {
return err
}
}
}
return r.buildFetchedTags()
}
func (r *Remote) buildFetchedTags() error {
iter, err := r.Refs()
if err != nil {
return err
}
return iter.ForEach(func(ref *plumbing.Reference) error {
if !ref.IsTag() {
return nil
}
_, err := r.s.Object(plumbing.AnyObject, ref.Hash())
if err == plumbing.ErrObjectNotFound {
return nil
}
if err != nil {
return err
}
return r.s.SetReference(ref)
})
}
// Head returns the Reference of the HEAD
func (r *Remote) Head() *plumbing.Reference {
return r.upInfo.Head()
}
// Ref returns the Hash pointing the given refName
func (r *Remote) Ref(name plumbing.ReferenceName, resolved bool) (*plumbing.Reference, error) {
if resolved {
return storer.ResolveReference(r.upInfo.Refs, name)
}
return r.upInfo.Refs.Reference(name)
}
// Refs returns a map with all the References
func (r *Remote) Refs() (storer.ReferenceIter, error) {
return r.upInfo.Refs.IterReferences()
}
// Disconnect from the remote and save the config
func (r *Remote) Disconnect() error {
r.upInfo = nil
return r.fetchSession.Close()
}
func (r *Remote) String() string {
fetch := r.c.URL
push := r.c.URL
return fmt.Sprintf("%s\t%s (fetch)\n%[1]s\t%s (push)", r.c.Name, fetch, push)
}
|