-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
331 lines (280 loc) · 8.57 KB
/
main.go
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"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/plumbing/transport/client"
"github.com/go-git/go-git/v5/storage/memory"
)
func main() {
ctx := context.Background()
if err := mainImpl(ctx); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
type todoType interface{ Nothing() }
func mainImpl(ctx context.Context) error {
from := os.Args[1]
to := os.Args[2]
up, err := startUploadPack(from, nil)
if err != nil {
return fmt.Errorf("error starting upload-pack: %w", err)
}
defer checkClose(up, "upload-pack")
originAdv, err := up.GetAdvertisement(ctx)
if err != nil {
return fmt.Errorf("%s: error getting ref advertisement: %w", from, err)
}
originRefs, err := originAdv.AllReferences()
if err != nil {
return fmt.Errorf("%s: error reading advertised refs: %w", from, err)
}
//remoteRefs, err := ar.AllReferences()
//req := packp.NewUploadPackRequestFromCapabilities(ar.Capabilities)
//req, err := r.newUploadPackRequest(o, ar)
rp, err := startReceivePack(to, nil)
if err != nil {
return fmt.Errorf("%s: error starting receive-pack: %w", to, err)
}
defer checkClose(rp, "receive-pack")
destAdv, err := rp.GetAdvertisement(ctx)
if err != nil {
return fmt.Errorf("%s: error getting ref advertisement: %w", to, err)
}
destRefs, err := destAdv.AllReferences()
if err != nil {
return fmt.Errorf("%s: error reading advertised refs: %w", to, err)
}
f, updates, err := compareRefs(originRefs, destRefs)
if err != nil {
return fmt.Errorf("error comparing refs: %w", err)
}
fmt.Printf("%d haves, %d wants, %d updates ...\n", len(f.haves), len(f.wants), len(updates))
if len(updates) == 0 {
return nil
}
packReader, err := up.GetPack(ctx, originAdv, f)
if err != nil {
return fmt.Errorf("error getting pack data: %w", err)
}
//defer checkClose(packReader, "upload-pack data")
if os.Getenv("DEBUG_PACK") != "" && packReader != nil {
dbg, err := os.Create(os.Getenv("DEBUG_PACK"))
if err != nil {
return fmt.Errorf("debug.pack: %w", err)
}
defer checkClose(dbg, "debug.pack")
packReader = &rc{io.TeeReader(packReader, dbg), packReader}
}
if err := rp.SendPack(ctx, destAdv, updates, packReader); err != nil {
return fmt.Errorf("error sending pack data: %w", err)
}
fmt.Printf("Success! Mirrored from %s to %s\n", from, to)
return nil
}
type rc struct {
r io.Reader
c io.Closer
}
func (d *rc) Read(p []byte) (n int, err error) { return d.r.Read(p) }
func (d *rc) Close() error { return d.c.Close() }
func compareRefs(fromRefs, toRefs memory.ReferenceStorage) (fetchHashes, []*packp.Command, error) {
// haves = all oids in toRefs.
// wants = all oids in fromRefs that aren't in toRefs.
// updates = all differences between fromRefs and toRefs.
var haves, wants []plumbing.Hash
var updates []*packp.Command
toRefIter, err := toRefs.IterReferences()
if err != nil {
return fetchHashes{}, nil, fmt.Errorf("error creating ref iterator: %w", err)
}
knownOIDs := make(map[plumbing.Hash]struct{})
if err := toRefIter.ForEach(func(ref *plumbing.Reference) error {
if ref.Type() != plumbing.HashReference {
return nil
}
knownOIDs[ref.Hash()] = struct{}{}
if r, err := fromRefs.Reference(ref.Name()); err != nil {
updates = append(updates, &packp.Command{
Name: ref.Name(),
Old: ref.Hash(),
New: plumbing.ZeroHash,
})
} else if r.Hash() != ref.Hash() {
updates = append(updates, &packp.Command{
Name: ref.Name(),
Old: ref.Hash(),
New: r.Hash(),
})
}
return nil
}); err != nil {
return fetchHashes{}, nil, fmt.Errorf("error generating commands for new refs: %w", err)
}
haves = make([]plumbing.Hash, 0, len(knownOIDs))
for k := range knownOIDs {
haves = append(haves, k)
}
fromRefIter, err := fromRefs.IterReferences()
if err != nil {
return fetchHashes{}, nil, fmt.Errorf("error creating ref iterator: %w", err)
}
if err := fromRefIter.ForEach(func(ref *plumbing.Reference) error {
if ref.Type() != plumbing.HashReference {
return nil
}
if _, ok := knownOIDs[ref.Hash()]; !ok {
wants = append(wants, ref.Hash())
}
if _, err := toRefs.Reference(ref.Name()); err == plumbing.ErrReferenceNotFound {
updates = append(updates, &packp.Command{
Name: ref.Name(),
Old: plumbing.ZeroHash,
New: ref.Hash(),
})
}
return nil
}); err != nil {
return fetchHashes{}, nil, fmt.Errorf("error generating commands for new refs: %w", err)
}
return fetchHashes{
haves: haves,
wants: wants,
}, updates, nil
}
func startUploadPack(url string, auth transport.AuthMethod) (*uploadPackHelper, error) {
c, ep, err := newClient(url)
if err != nil {
return nil, fmt.Errorf("error creating client: %w", err)
}
s, err := c.NewUploadPackSession(ep, auth)
if err != nil {
return nil, fmt.Errorf("error creating session: %w", err)
}
return &uploadPackHelper{s, c, ep}, nil
}
func startReceivePack(url string, auth transport.AuthMethod) (*receivePackHelper, error) {
c, ep, err := newClient(url)
if err != nil {
return nil, fmt.Errorf("error creating client: %w", err)
}
s, err := c.NewReceivePackSession(ep, auth)
if err != nil {
return nil, fmt.Errorf("error creating session: %w", err)
}
return &receivePackHelper{s, c, ep}, nil
}
func newClient(url string) (transport.Transport, *transport.Endpoint, error) {
ep, err := transport.NewEndpoint(url)
if err != nil {
return nil, nil, fmt.Errorf("%s: %w", url, err)
}
c, err := client.NewClient(ep)
if err != nil {
return nil, nil, fmt.Errorf("error creating client for %s: %w", url, err)
}
return c, ep, nil
}
type uploadPackHelper struct {
s transport.UploadPackSession
c transport.Transport
ep *transport.Endpoint
}
func (h *uploadPackHelper) Close() error {
return h.s.Close()
}
func (h *uploadPackHelper) GetAdvertisement(ctx context.Context) (*packp.AdvRefs, error) {
return h.s.AdvertisedReferencesContext(ctx)
}
func (h *uploadPackHelper) GetPack(ctx context.Context, ar *packp.AdvRefs, f fetchHashes) (io.ReadCloser, error) {
req := packp.NewUploadPackRequestFromCapabilities(ar.Capabilities)
req.Wants = f.wants
req.Haves = f.haves
resp, err := h.s.UploadPack(ctx, req)
if err != nil {
if errors.Is(err, transport.ErrEmptyUploadPackRequest) {
return io.NopCloser(bytes.NewReader([]byte{
'P', 'A', 'C', 'K',
0, 0, 0, 2,
0, 0, 0, 0,
0x02, 0x9d, 0x08, 0x82,
0x3b, 0xd8, 0xa8, 0xea,
0xb5, 0x10, 0xad, 0x6a,
0xc7, 0x5c, 0x82, 0x3c,
0xfd, 0x3e, 0xd3, 0x1e,
})), nil
}
return nil, err
}
return handleSideband(resp, ar.Capabilities), nil
}
func handleSideband(r io.ReadCloser, caps *capability.List) io.ReadCloser {
if caps.Supports(capability.Sideband64k) {
d := sideband.NewDemuxer(sideband.Sideband64k, r)
d.Progress = os.Stderr
return &rc{d, r}
}
if caps.Supports(capability.Sideband) {
d := sideband.NewDemuxer(sideband.Sideband, r)
d.Progress = os.Stderr
return &rc{d, r}
}
return r
}
type fetchHashes struct {
haves, wants []plumbing.Hash
}
type receivePackHelper struct {
s transport.ReceivePackSession
c transport.Transport
ep *transport.Endpoint
}
func (h *receivePackHelper) Close() error {
return h.s.Close()
}
func (h *receivePackHelper) GetAdvertisement(ctx context.Context) (*packp.AdvRefs, error) {
return h.s.AdvertisedReferencesContext(ctx)
}
func (h *receivePackHelper) SendPack(ctx context.Context, ar *packp.AdvRefs, updates []*packp.Command, packData io.ReadCloser) error {
req := packp.NewReferenceUpdateRequestFromCapabilities(ar.Capabilities)
req.Progress = os.Stderr
// req.Progress = ...
// if ar.Capabilities.Supports(capability.Sideband64k) {
// _ = req.Capabilities.Set(capability.Sideband64k)
// } else if ar.Capabilities.Supports(capability.Sideband) {
// _ = req.Capabilities.Set(capability.Sideband)
// }
req.Commands = updates
req.Packfile = packData
st, err := h.s.ReceivePack(ctx, req)
if err != nil {
return fmt.Errorf("receive-pack: %w", err)
}
fmt.Printf("objects: %v\n", st.UnpackStatus)
okRefs := 0
for _, cs := range st.CommandStatuses {
if cs.Status == "ok" {
okRefs++
} else {
fmt.Printf("%s: %v\n", cs.ReferenceName, cs.Status)
}
}
if okRefs > 0 {
fmt.Printf("ok refs: %d\n", okRefs)
}
return nil
}
func checkClose(c io.Closer, label string) {
if err := c.Close(); err != nil {
fmt.Fprintf(os.Stderr, "%s: error closing: %v\n", label, err)
}
}