/
push_cmd.go
514 lines (498 loc) · 14.9 KB
/
push_cmd.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2022 Wrangle Ltd
package wrgl
import (
"bytes"
"errors"
"fmt"
"sort"
"strings"
"github.com/cenkalti/backoff/v4"
"github.com/mitchellh/colorstring"
"github.com/spf13/cobra"
"github.com/wrgl/wrgl/cmd/wrgl/fetch"
"github.com/wrgl/wrgl/cmd/wrgl/utils"
apiclient "github.com/wrgl/wrgl/pkg/api/client"
"github.com/wrgl/wrgl/pkg/api/payload"
"github.com/wrgl/wrgl/pkg/conf"
conffs "github.com/wrgl/wrgl/pkg/conf/fs"
"github.com/wrgl/wrgl/pkg/credentials"
"github.com/wrgl/wrgl/pkg/objects"
"github.com/wrgl/wrgl/pkg/pbar"
"github.com/wrgl/wrgl/pkg/ref"
)
func newPushCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "push { REPOSITORY [REFSPEC...] | --all }",
Short: "Updates remote refs using local refs, sending objects necessary to complete the given refs.",
Example: utils.CombineExamples([]utils.Example{
{
Comment: "push changes of two branches to origin",
Line: "wrgl push origin refs/heads/main:main refs/heads/beta:beta",
},
{
Comment: "push to main branch (destination ref is assumed to be the same as source ref)",
Line: "wrgl push origin refs/heads/main:",
},
{
Comment: "push and set branch upstream",
Line: "wrgl push origin refs/heads/main:main --set-upstream",
},
{
Comment: "remove branch main",
Line: "wrgl push origin :refs/heads/main",
},
{
Comment: "force update branch (non-fast-forward)",
Line: "wrgl push origin +refs/heads/beta:beta",
},
{
Comment: "push to my-repo reading from remote.my-repo.push",
Line: "wrgl push my-repo",
},
{
Comment: "force update my-repo",
Line: "wrgl push my-repo --force",
},
{
Comment: "turn the remote into a mirror of local repository",
Line: "wrgl push my-mirror-repo --mirror",
},
}),
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
wrglDir := utils.MustWRGLDir(cmd)
s := conffs.NewStore(wrglDir, conffs.AggregateSource, "")
c, err := s.Open()
if err != nil {
return err
}
rd := utils.GetRepoDir(cmd)
defer rd.Close()
db, err := rd.OpenObjectsStore()
if err != nil {
return err
}
defer db.Close()
rs := rd.OpenRefStore()
setUpstream, err := cmd.Flags().GetBool("set-upstream")
if err != nil {
return err
}
all, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}
cs, err := credentials.NewStore()
if err != nil {
return err
}
logger := utils.GetLogger(cmd)
clients := utils.NewClientMap(cs, *logger)
if all {
names := []string{}
for name, branch := range c.Branch {
if branch.Remote == "" {
continue
}
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
branch := c.Branch[name]
colorstring.Fprintf(cmd.OutOrStdout(), "pushing [bold]%s[reset]\n", name)
if err := retryPushSingleRepo(
cmd, c, db, rs, clients, []string{branch.Remote, fmt.Sprintf("refs/heads/%s:%s", name, branch.Merge)}, false, wrglDir,
); err != nil {
if apiclient.IsHTTPError(err, 404, "Not Found") {
cmd.Println("Repository not found, skipping.")
} else {
return err
}
}
}
return nil
}
return retryPushSingleRepo(cmd, c, db, rs, clients, args, setUpstream, wrglDir)
},
}
cmd.Flags().BoolP("force", "f", false, "force update remote branch in certain conditions.")
cmd.Flags().BoolP("set-upstream", "u", false, strings.Join([]string{
"for every branch that is up to date or successfully pushed, add upstream",
"(tracking) reference, used by argument-less `wrgl pull`.",
}, " "))
cmd.Flags().Bool("mirror", false, strings.Join([]string{
"instead of naming each ref to push, specifies that all refs (which includes",
"but not limited to refs/heads/ and refs/remotes/) be mirrored to the remote",
"repository. Newly created local refs will be pushed to the remote end, locally",
"updated refs will be force updated on the remote end, and deleted refs will be",
"removed from the remote end. This is the default if the configuration option",
"remote.<remote>.mirror is set.",
}, " "))
cmd.Flags().Bool("all", false, "push all branches that have upstream configured.")
cmd.Flags().Bool("no-progress", false, "don't display progress bar")
return cmd
}
func setBranchUpstream(cmd *cobra.Command, wrglDir, remote string, refs []*conf.Refspec) error {
s := conffs.NewStore(wrglDir, conffs.LocalSource, "")
c, err := s.Open()
if err != nil {
return fmt.Errorf("error opening config store: %w", err)
}
if c.Branch == nil {
c.Branch = map[string]*conf.Branch{}
}
for _, ref := range refs {
if strings.HasPrefix(ref.Src(), "heads/") && strings.HasPrefix(ref.Dst(), "heads/") {
branch := strings.TrimPrefix(ref.Src(), "heads/")
merge := "refs/" + ref.Dst()
c.Branch[branch] = &conf.Branch{
Remote: remote,
Merge: merge,
}
// ensure that wrgl fetch pull merge
rem := c.Remote[remote]
sort.Sort(rem.Fetch)
globRS := conf.MustParseRefspec(fmt.Sprintf("+refs/heads/*:refs/remotes/%s/*", remote))
branchRS := conf.MustParseRefspec(
fmt.Sprintf("+%s:refs/remotes/%s/%s", merge, remote, strings.TrimPrefix(ref.Dst(), "heads/")),
)
if rem.Fetch.IndexOf(globRS) == -1 && rem.Fetch.IndexOf(branchRS) == -1 {
rem.Fetch = append(rem.Fetch, branchRS)
}
cmd.Printf("branch %q setup to track remote branch %q from %q\n", ref.Src()[6:], ref.Dst()[6:], remote)
}
}
if err := s.Save(c); err != nil {
return fmt.Errorf("error saving config: %w", err)
}
return nil
}
func getRepoToPush(c *conf.Config, args []string) (remote string, cr *conf.Remote, rem []string, err error) {
if len(args) > 0 {
if v, ok := c.Remote[args[0]]; ok {
return args[0], v, args[1:], nil
} else if v, ok := c.Remote["origin"]; ok {
return "origin", v, args, nil
} else {
return "", nil, nil, fmt.Errorf("unrecognized repository name %q", args[0])
}
} else if v, ok := c.Remote["origin"]; ok {
return "origin", v, args, nil
}
return "", nil, nil, fmt.Errorf("repository name not specified")
}
func getRefspecsToPush(cmd *cobra.Command, rs ref.Store, cr *conf.Remote, args []string, remoteRefs map[string][]byte, mirror bool) (refspecs []*conf.Refspec, err error) {
if mirror {
var localRefs map[string][]byte
localRefs, err = ref.ListAllRefs(rs)
if err != nil {
return
}
for ref, sum := range localRefs {
if v, ok := remoteRefs[ref]; !ok {
refspecs = append(refspecs, conf.MustParseRefspec(fmt.Sprintf("refs/%s:refs/%s", ref, ref)))
} else if !bytes.Equal(v, sum) {
refspecs = append(refspecs, conf.MustParseRefspec(fmt.Sprintf("+refs/%s:refs/%s", ref, ref)))
} else {
fetch.DisplayRefUpdate(cmd, '=', "[up to date]", "", ref, ref)
}
}
for ref := range remoteRefs {
if _, ok := localRefs[ref]; !ok {
refspecs = append(refspecs, conf.MustParseRefspec(fmt.Sprintf(":refs/%s", ref)))
}
}
sort.Slice(refspecs, func(i, j int) bool {
return refspecs[i].Dst() < refspecs[j].Dst()
})
} else if len(args) > 0 {
for _, s := range args {
rs, err := conf.ParseRefspec(s)
if err != nil {
return nil, err
}
if rs.Dst() == "" {
found := false
for _, obj := range cr.Push {
if obj.Src() == rs.Src() {
rs = obj
found = true
break
}
}
if !found {
rs, err = conf.NewRefspec(rs.Src(), rs.Src(), false, rs.Force)
if err != nil {
return nil, err
}
}
}
refspecs = append(refspecs, rs)
}
} else {
refspecs = cr.Push
}
if len(refspecs) == 0 {
return nil, fmt.Errorf("no refspec specified")
}
return
}
func interpretDestination(remoteRefs map[string][]byte, src, dst string) (string, error) {
if strings.HasPrefix(dst, "refs/") {
return strings.TrimPrefix(dst, "refs/"), nil
}
matchedRefs := []string{}
for ref := range remoteRefs {
if strings.HasSuffix(ref, "/"+dst) {
matchedRefs = append(matchedRefs, ref)
}
}
if len(matchedRefs) == 1 {
return matchedRefs[0], nil
} else if strings.HasPrefix(src, "refs/heads/") {
if strings.HasPrefix(dst, "heads/") {
return dst, nil
} else {
return "heads/" + dst, nil
}
} else if strings.HasPrefix(src, "refs/tags/") {
if strings.HasPrefix(dst, "tags/") {
return dst, nil
} else {
return "tags/" + dst, nil
}
}
return "", fmt.Errorf("ambiguous push destination %q", dst)
}
type receivePackUpdate struct {
Sum, OldSum []byte
Src, Dst, ErrMsg string
Force bool
}
func identifyUpdates(
cmd *cobra.Command, db objects.Store, rs ref.Store, refspecs []*conf.Refspec, remoteRefs map[string][]byte, force bool,
) (upToDateRefspecs []*conf.Refspec, updates []*receivePackUpdate, err error) {
for _, s := range refspecs {
src := s.Src()
dst := s.Dst()
var sum []byte
if src != "" {
_, sum, _, err = ref.InterpretCommitName(db, rs, src, false)
if err != nil {
err = fmt.Errorf("error interpreting %q: %v", src, err)
return
}
}
dst, err = interpretDestination(remoteRefs, src, dst)
if err != nil {
return
}
if v, ok := remoteRefs[dst]; ok {
if string(v) == string(sum) {
fetch.DisplayRefUpdate(cmd, '=', "[up to date]", "", src, dst)
upToDateRefspecs = append(upToDateRefspecs, s)
} else if sum == nil {
// delete ref
updates = append(updates, &receivePackUpdate{
OldSum: v,
Sum: sum,
Src: src,
Dst: dst,
})
} else if strings.HasPrefix(dst, "tags/") {
if force || s.Force {
updates = append(updates, &receivePackUpdate{
OldSum: v,
Sum: sum,
Src: src,
Dst: dst,
Force: true,
})
} else {
fetch.DisplayRefUpdate(cmd, '!', "[rejected]", "would clobber existing tag", src, dst)
}
} else if fastForward, err := ref.IsAncestorOf(db, v, sum); err != nil {
return nil, nil, err
} else if fastForward {
updates = append(updates, &receivePackUpdate{
OldSum: v,
Sum: sum,
Src: src,
Dst: dst,
})
} else if force || s.Force {
updates = append(updates, &receivePackUpdate{
OldSum: v,
Sum: sum,
Src: src,
Dst: dst,
Force: true,
})
} else {
fetch.DisplayRefUpdate(cmd, '!', "[rejected]", "non-fast-forward", src, dst)
}
} else if sum != nil {
updates = append(updates, &receivePackUpdate{
OldSum: nil,
Sum: sum,
Src: src,
Dst: dst,
})
}
}
return
}
func reportUpdateStatus(cmd *cobra.Command, updates []*receivePackUpdate) {
for _, u := range updates {
if u.ErrMsg == "" {
if u.Sum == nil {
fetch.DisplayRefUpdate(cmd, '-', "[deleted]", "", "", u.Dst)
} else if u.OldSum == nil {
var summary string
if strings.HasPrefix(u.Dst, "heads/") {
summary = "[new branch]"
} else if strings.HasPrefix(u.Dst, "tags/") {
summary = "[new tag]"
} else {
summary = "[new reference]"
}
fetch.DisplayRefUpdate(cmd, '*', summary, "", u.Src, u.Dst)
} else if u.Force {
fetch.DisplayRefUpdate(cmd, '+', fetch.Quickref(u.OldSum, u.Sum, false), "forced update", u.Src, u.Dst)
} else {
fetch.DisplayRefUpdate(cmd, ' ', fetch.Quickref(u.OldSum, u.Sum, true), "", u.Src, u.Dst)
}
} else {
fetch.DisplayRefUpdate(cmd, '!', "[remote rejected]", u.ErrMsg, u.Src, u.Dst)
}
}
}
// retryPushSingleRepo like pushSingleRepo but retry with exponential backoff
func retryPushSingleRepo(
cmd *cobra.Command, c *conf.Config, db objects.Store, rs ref.Store, clients *utils.ClientMap,
args []string, setUpstream bool, wrglDir string,
) error {
logger := utils.GetLogger(cmd)
ticker := backoff.NewTicker(backoff.NewExponentialBackOff())
defer ticker.Stop()
for range ticker.C {
if err := pushSingleRepo(cmd, c, db, rs, clients, args, setUpstream, wrglDir); err != nil {
var httpErr *apiclient.HTTPError
if errors.As(err, &httpErr) && httpErr.Code >= 500 {
logger.Error(err, "retrying push")
if err := clients.ResetCookies(); err != nil {
return err
}
continue
}
return err
}
break
}
return nil
}
func pushSingleRepo(
cmd *cobra.Command, c *conf.Config, db objects.Store, rs ref.Store, clients *utils.ClientMap,
args []string, setUpstream bool, wrglDir string,
) error {
mirror, err := cmd.Flags().GetBool("mirror")
if err != nil {
return err
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
remote, cr, args, err := getRepoToPush(c, args)
if err != nil {
return err
}
if cr.Mirror {
mirror = true
}
if mirror {
force = true
}
client, err := clients.GetClient(cmd, cr.URL)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
}
remoteRefs, err := clients.GetRefs(cmd, cr.URL)
if err != nil {
return fmt.Errorf("error getting remote refs: %w", err)
}
cmd.Printf("To %s\n", cr.URL)
refspecs, err := getRefspecsToPush(cmd, rs, cr, args, remoteRefs, mirror)
if err != nil {
return fmt.Errorf("error getting refspecs to push: %w", err)
}
upToDateRefspecs, updates, err := identifyUpdates(cmd, db, rs, refspecs, remoteRefs, force)
if err != nil {
return fmt.Errorf("error identifying updates: %w", err)
}
if len(updates) > 0 {
um := map[string]*payload.Update{}
for _, u := range updates {
um[u.Dst] = &payload.Update{
Sum: payload.BytesToHex(u.Sum),
OldSum: payload.BytesToHex(u.OldSum),
}
}
ses, err := apiclient.NewReceivePackSession(db, rs, client, um, remoteRefs, c.MaxPackFileSize())
if err != nil {
return utils.HandleHTTPError(cmd, clients.CredsStore, cr.URL, err)
}
if err := utils.WithProgressBar(cmd, false, func(cmd *cobra.Command, barContainer *pbar.Container) (err error) {
um, err = ses.Start(barContainer)
return err
}); err != nil {
return fmt.Errorf("error starting receive pack session: %w", err)
}
for _, u := range updates {
if v, ok := um[u.Dst]; ok {
u.ErrMsg = v.ErrMsg
} else {
u.ErrMsg = "remote failed to report status"
}
}
reportUpdateStatus(cmd, updates)
if username, reponame, ok := utils.IsWrglhubRemote(cr.URL); ok {
cmd.Printf("See latest data at:\n")
for _, u := range updates {
if u.ErrMsg != "" || u.Sum == nil || (!strings.HasPrefix(u.Dst, "heads/") && !strings.HasPrefix(u.Dst, "refs/heads/")) {
continue
}
branch := fetch.TrimRefPrefix(u.Dst)
cmd.Printf(" %srefs/heads/%s\n", utils.RepoWebURI(username, reponame), branch)
}
}
}
if setUpstream {
refs := []*conf.Refspec{}
for _, rs := range upToDateRefspecs {
ref, err := conf.NewRefspec(
strings.TrimPrefix(rs.Src(), "refs/"),
strings.TrimPrefix(rs.Dst(), "refs/"),
false, false,
)
if err != nil {
return fmt.Errorf("error creating up-to-date refspec: %w", err)
}
refs = append(refs, ref)
}
for _, u := range updates {
ref, err := conf.NewRefspec(
strings.TrimPrefix(u.Src, "refs/"),
strings.TrimPrefix(u.Dst, "refs/"),
false, false,
)
if err != nil {
return fmt.Errorf("error creating updated refspec: %w", err)
}
refs = append(refs, ref)
}
return setBranchUpstream(cmd, wrglDir, remote, refs)
}
return nil
}