/
sync.go
558 lines (478 loc) · 12.3 KB
/
sync.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright © 2019 Alibaba Co. Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/alibaba/git-repo-go/cap"
"github.com/alibaba/git-repo-go/config"
"github.com/alibaba/git-repo-go/project"
"github.com/alibaba/git-repo-go/workspace"
log "github.com/jiangxin/multi-log"
"github.com/spf13/cobra"
)
const (
// syncDefaultJobs is the default value of --jobs
syncDefaultJobs = 4
)
type syncCommand struct {
WorkSpaceCommand
cmd *cobra.Command
FetchOptions project.FetchOptions
O struct {
ForceBroken bool
ForceSync bool
LocalOnly bool
NetworkOnly bool
DetachHead bool
CurrentBranchOnly bool
CheckPublished bool
Jobs int
ManifestName string
NoCache bool
NoCloneBundle bool
ManifestServerUsername string
ManifestServerPassword string
FetchSubmodules bool
NoTags bool
OptimizedFetch bool
Prune bool
SmartSync bool
SmartTag string
}
}
func (v *syncCommand) Command() *cobra.Command {
if v.cmd != nil {
return v.cmd
}
v.cmd = &cobra.Command{
Use: "sync",
Short: "Update working tree to the latest revision",
RunE: func(cmd *cobra.Command, args []string) error {
return v.Execute(args)
},
}
v.cmd.Flags().BoolVarP(&v.O.ForceBroken,
"force-broken",
"f",
false,
"continue sync even if a project fails to sync")
v.cmd.Flags().BoolVar(&v.O.ForceSync,
"force-sync",
false,
"overwrite an existing git directory if it needs to "+
"point to a different object directory. WARNING: this "+
"may cause loss of data")
v.cmd.Flags().BoolVarP(&v.O.LocalOnly,
"local-only",
"l",
false,
"only update working tree, don't fetch")
v.cmd.Flags().BoolVarP(&v.O.NetworkOnly,
"network-only",
"n",
false,
"fetch only, don't update working tree")
v.cmd.Flags().BoolVarP(&v.O.DetachHead,
"detach",
"d",
false,
"detach projects back to manifest revision")
v.cmd.Flags().BoolVarP(&v.O.CurrentBranchOnly,
"current-branch",
"c",
false,
"fetch only current branch from server")
v.cmd.Flags().BoolVar(&v.O.CheckPublished,
"check-published",
false,
"do not sync project which is published but not merged")
v.cmd.Flags().IntVarP(&v.O.Jobs,
"jobs",
"j",
v.manifestsDefaultJobs(),
fmt.Sprintf("projects to fetch simultaneously"))
v.cmd.Flags().StringVarP(&v.O.ManifestName,
"manifest-name",
"m",
"",
"temporary manifest to use for this sync")
v.cmd.Flags().BoolVar(&v.O.NoCache,
"no-cache",
false,
"Ignore ssh-info cache, and recheck ssh-info API")
v.cmd.Flags().BoolVar(&v.O.NoCloneBundle,
"no-clone-bundle",
false,
"disable use of /clone.bundle on HTTP/HTTPS")
v.cmd.Flags().StringVarP(&v.O.ManifestServerUsername,
"manifest-server-username",
"u",
"",
"username to authenticate with the manifest server")
v.cmd.Flags().StringVarP(&v.O.ManifestServerPassword,
"manifest-server-password",
"p",
"",
"password to authenticate with the manifest server")
v.cmd.Flags().BoolVar(&v.O.FetchSubmodules,
"fetch-submodules",
false,
"fetch submodules from server")
v.cmd.Flags().BoolVar(&v.O.NoTags,
"no-tags",
false,
"don't fetch tags")
v.cmd.Flags().BoolVar(&v.O.OptimizedFetch,
"optimized-fetch",
false,
"only fetch projects fixed to sha1 if revision does not exist locally")
v.cmd.Flags().BoolVar(&v.O.Prune,
"prune",
false,
"delete refs that no longer exist on the remote")
v.cmd.Flags().BoolVar(&v.O.SmartSync,
"smart-sync",
false,
"smart sync using manifest from the latest known good build")
v.cmd.Flags().StringVarP(&v.O.SmartTag,
"smart-tag",
"t",
"",
"smart sync using manifest from a known tag")
return v.cmd
}
// Value of manifestsDefaultJobs() is used as command arg's default value.
// Do not fail if run git-repo command out of a workspace.
func (v *syncCommand) manifestsDefaultJobs() int {
var nJobs int
rws, _ := workspace.NewRepoWorkSpace("")
if rws != nil &&
rws.Manifest != nil &&
rws.Manifest.Default != nil &&
rws.Manifest.Default.SyncJ > 0 {
nJobs = rws.Manifest.Default.SyncJ
}
if nJobs <= 0 {
nJobs = syncDefaultJobs
}
return nJobs
}
func (v *syncCommand) maxSyncJobs() int {
var (
nJobs int = config.MaxJobs
)
noFile, err := cap.GetRlimitNoFile()
if err == nil {
nJobs = min(int((noFile-5)/3), config.MaxJobs)
}
if nJobs <= 0 {
nJobs = 1
}
return nJobs
}
func (v syncCommand) CallManifestServerRPC() {
// TODO: implement `_SmartSyncSetup`
log.Panic("not implement CallManifestServerRPC")
}
func (v *syncCommand) updateManifestProject() error {
var err error
ws := v.RepoWorkSpace()
mp := ws.ManifestProject
s := mp.ReadSettings()
track := mp.TrackBranch("")
if track == "" {
log.Notef("manifest project is not updated, for there is no tracking branch")
return nil
}
if !v.O.LocalOnly {
// Fetch repositories
fetchOptions := project.FetchOptions{
RepoSettings: *s,
CurrentBranchOnly: v.O.CurrentBranchOnly,
NoTags: v.O.NoTags,
OptimizedFetch: v.O.OptimizedFetch,
Quiet: config.GetQuiet(),
}
err = mp.SyncNetworkHalf(&fetchOptions)
if err != nil {
return err
}
}
// Get current manifest version
oldrev, _ := mp.ResolveRevision("HEAD")
// No update found in manifest project
newrev, _ := mp.ResolveRemoteTracking(track)
if oldrev == newrev {
return nil
}
// Has commit not yet checkout?
revlist, err := mp.Revlist(newrev, "--not", oldrev)
if err != nil {
return err
}
if len(revlist) == 0 {
return nil
}
// Checkout
checkoutOptions := project.CheckoutOptions{
RepoSettings: *s,
Quiet: config.GetQuiet(),
}
err = mp.SyncLocalHalf(&checkoutOptions)
if err != nil {
return err
}
// Reload Manifest
v.ReloadRepoWorkSpace()
// Load different manifest file
if v.O.ManifestName != "" {
v.RepoWorkSpace().Override(v.O.ManifestName)
}
return nil
}
func (v syncCommand) NetworkHalf(allProjects []*project.Project) error {
var (
err error
errs []error
)
jobs := v.O.Jobs
if jobs < 1 {
jobs = 1
}
// TODO 1. Record fetch time, save time and project name to JSON
// TODO 2. Sort projects by its fetch time (reverse order).
projectsByName := project.IndexByName(allProjects)
jobTasks := make(chan string, jobs)
jobResults := make(chan error, jobs)
worker := func(i int) {
var (
err error
name string
projects []*project.Project
p *project.Project
)
log.Debugf("start NetworkHalf worker #%d", i)
for name = range jobTasks {
projects = projectsByName[name]
for _, p = range projects {
log.Debugf("worker #%d: sync %s", i, p.Name)
err = p.SyncNetworkHalf(&v.FetchOptions)
jobResults <- err
}
}
}
for i := 0; i < jobs; i++ {
go worker(i)
}
go func() {
for name := range projectsByName {
jobTasks <- name
}
close(jobTasks)
}()
for i := 0; i < len(projectsByName); i++ {
err = <-jobResults
if err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
return nil
}
errMsg := ""
for _, err = range errs {
errMsg += err.Error() + "\n"
}
return errors.New(errMsg)
}
func (v syncCommand) LocalHalf(allProjects []*project.Project) error {
var (
err error
errs []error
wg sync.WaitGroup
)
jobs := v.O.Jobs
if jobs < 1 {
jobs = 1
}
jobTasks := make(chan *project.Tree, jobs)
checkoutOptions := project.CheckoutOptions{
Quiet: config.GetQuiet(),
DetachHead: v.O.DetachHead,
}
wg.Add(len(allProjects))
worker := func(i int) {
var (
err error
tree *project.Tree
p *project.Project
)
log.Debugf("start LocalHalf worker #%d", i)
for tree = range jobTasks {
p = tree.Project
if p != nil {
log.Debugf("worker #%d: checkout %s", i, p.Name)
err = p.SyncLocalHalf(&checkoutOptions)
if err != nil {
errs = append(errs, err)
}
}
go func(tree project.Tree) {
for _, t := range tree.Trees {
jobTasks <- t
}
}(*tree)
// if p is nil, it's root tree
if p != nil {
log.Debugf("worker #%d: done %s", i, p.Name)
wg.Done()
}
}
}
for i := 0; i < jobs; i++ {
go worker(i)
}
tree := project.ProjectsTree(allProjects)
jobTasks <- tree
wg.Wait()
close(jobTasks)
if len(errs) == 0 {
return nil
}
errMsg := ""
for _, err = range errs {
errMsg += err.Error() + "\n"
}
return errors.New(errMsg)
}
func (v syncCommand) Execute(args []string) error {
var (
err error
)
rws := v.RepoWorkSpace()
if v.O.Jobs > 0 {
v.O.Jobs = min(v.O.Jobs, v.maxSyncJobs())
} else {
v.O.Jobs = 1
}
if v.O.NetworkOnly && v.O.DetachHead {
return newUserError("cannot combine -n and -d")
}
if v.O.NetworkOnly && v.O.LocalOnly {
return newUserError("cannot combine -n and -l")
}
if v.O.ManifestName != "" && v.O.SmartSync {
return newUserError("cannot combine -m and -s")
}
if v.O.ManifestName != "" && v.O.SmartTag != "" {
return newUserError("cannot combine -m and -t")
}
if v.O.ManifestServerUsername != "" || v.O.ManifestServerPassword != "" {
if !(v.O.SmartSync || v.O.SmartTag != "") {
return newUserError("-u and -p may only be combined with -s or -t")
}
if v.O.ManifestServerUsername == "" || v.O.ManifestServerPassword == "" {
return newUserError("both -u and -p must be given")
}
}
if v.O.ManifestName != "" {
rws.Override(v.O.ManifestName)
}
v.FetchOptions = project.FetchOptions{
RepoSettings: *(rws.Settings()),
Quiet: config.GetQuiet(),
CloneBundle: !v.O.NoCloneBundle,
CurrentBranchOnly: v.O.CurrentBranchOnly,
ForceSync: v.O.ForceSync,
NoTags: v.O.NoTags,
OptimizedFetch: v.O.OptimizedFetch,
Prune: v.O.Prune,
}
smartSyncManifestName := "smart_sync_override.xml"
smartSyncManifestPath := filepath.Join(rws.ManifestProject.WorkDir, smartSyncManifestName)
if v.O.SmartSync || v.O.SmartTag != "" {
v.CallManifestServerRPC()
} else {
if _, err = os.Stat(smartSyncManifestPath); err == nil {
err = os.Remove(smartSyncManifestPath)
if err != nil {
log.Fatalf("failed to remove existing smart sync override manifest: %s", smartSyncManifestPath)
}
}
}
err = v.updateManifestProject()
if err != nil {
return err
}
// Use reloaded WorkSpace after calling `updateManifestProject()`.
rws = v.RepoWorkSpace()
allProjects, err := rws.GetProjects(&workspace.GetProjectsOptions{
Groups: rws.Settings().Groups,
MissingOK: true,
SubmodulesOK: v.O.FetchSubmodules,
}, args...)
if !v.O.LocalOnly {
err = v.NetworkHalf(allProjects)
if err != nil {
return err
}
}
if v.O.NetworkOnly ||
rws.ManifestProject.MirrorEnabled() ||
rws.ManifestProject.ArchiveEnabled() {
return nil
}
// Call ssh_info API to detect types of remote servers
err = rws.LoadRemotes(v.O.NoCache)
if err != nil {
log.Notef("fail to check remote server, you may need to install gerrit hooks by hands")
log.Error(err)
}
// Remove obsolete projects
remains, err := rws.UpdateProjectList(v.O.FetchSubmodules)
if err != nil {
log.Fatal(err)
}
err = v.LocalHalf(allProjects)
if err != nil {
return err
}
// If there's a notice that's supposed to print at the end of the sync,
// print it now...
if rws.Manifest != nil && rws.Manifest.Notice != "" {
log.Note(rws.Manifest.Notice)
}
// Warn user there are obsolete projects not removed.
if len(remains) > 0 {
log.Error("The following obsolete projects are still in your workspace, please check and remove them:\n")
for _, p := range remains {
fmt.Fprintf(os.Stderr, " * %s\n", p)
}
return fmt.Errorf("%d obsolete projects in your workdir need to be removed", len(remains))
}
return nil
}
var syncCmd = syncCommand{
WorkSpaceCommand: WorkSpaceCommand{
MirrorOK: true,
SingleOK: false,
},
}
func init() {
rootCmd.AddCommand(syncCmd.Command())
}