@@ -303,8 +303,8 @@ func runSync(m *models.Mirror) ([]*mirrorSyncResult, bool) {
303
303
return nil , false
304
304
}
305
305
306
- for i := range branches {
307
- cache .Remove (m .Repo .GetCommitsCountCacheKey (branches [ i ] .Name , true ))
306
+ for _ , branch := range branches {
307
+ cache .Remove (m .Repo .GetCommitsCountCacheKey (branch .Name , true ))
308
308
}
309
309
310
310
m .UpdatedUnix = timeutil .TimeStampNow ()
@@ -422,6 +422,10 @@ func syncMirror(repoID string) {
422
422
return
423
423
}
424
424
defer gitRepo .Close ()
425
+
426
+ if ok := checkAndUpdateEmptyRepository (m , gitRepo , results ); ! ok {
427
+ return
428
+ }
425
429
}
426
430
427
431
for _ , result := range results {
@@ -487,6 +491,67 @@ func syncMirror(repoID string) {
487
491
log .Trace ("SyncMirrors [repo: %-v]: Successfully updated" , m .Repo )
488
492
}
489
493
494
+ func checkAndUpdateEmptyRepository (m * models.Mirror , gitRepo * git.Repository , results []* mirrorSyncResult ) bool {
495
+ if ! m .Repo .IsEmpty {
496
+ return true
497
+ }
498
+
499
+ hasDefault := false
500
+ hasMaster := false
501
+ defaultBranchName := m .Repo .DefaultBranch
502
+ if len (defaultBranchName ) == 0 {
503
+ defaultBranchName = setting .Repository .DefaultBranch
504
+ }
505
+ firstName := ""
506
+ for _ , result := range results {
507
+ if strings .HasPrefix (result .refName , "refs/pull/" ) {
508
+ continue
509
+ }
510
+ tp , name := git .SplitRefName (result .refName )
511
+ if len (tp ) > 0 && tp != git .BranchPrefix {
512
+ continue
513
+ }
514
+ if len (firstName ) == 0 {
515
+ firstName = name
516
+ }
517
+
518
+ hasDefault = hasDefault || name == defaultBranchName
519
+ hasMaster = hasMaster || name == "master"
520
+ }
521
+
522
+ if len (firstName ) > 0 {
523
+ if hasDefault {
524
+ m .Repo .DefaultBranch = defaultBranchName
525
+ } else if hasMaster {
526
+ m .Repo .DefaultBranch = "master"
527
+ } else {
528
+ m .Repo .DefaultBranch = firstName
529
+ }
530
+ // Update the git repository default branch
531
+ if err := gitRepo .SetDefaultBranch (m .Repo .DefaultBranch ); err != nil {
532
+ if ! git .IsErrUnsupportedVersion (err ) {
533
+ log .Error ("Failed to update default branch of underlying git repository %-v. Error: %v" , m .Repo , err )
534
+ desc := fmt .Sprintf ("Failed to uupdate default branch of underlying git repository '%s': %v" , m .Repo .RepoPath (), err )
535
+ if err = models .CreateRepositoryNotice (desc ); err != nil {
536
+ log .Error ("CreateRepositoryNotice: %v" , err )
537
+ }
538
+ return false
539
+ }
540
+ }
541
+ m .Repo .IsEmpty = false
542
+ // Update the is empty and default_branch columns
543
+ if err := models .UpdateRepositoryCols (m .Repo , "default_branch" , "is_empty" ); err != nil {
544
+ log .Error ("Failed to update default branch of repository %-v. Error: %v" , m .Repo , err )
545
+ desc := fmt .Sprintf ("Failed to uupdate default branch of repository '%s': %v" , m .Repo .RepoPath (), err )
546
+ if err = models .CreateRepositoryNotice (desc ); err != nil {
547
+ log .Error ("CreateRepositoryNotice: %v" , err )
548
+ }
549
+ return false
550
+ }
551
+ }
552
+ return true
553
+ }
554
+
490
555
// InitSyncMirrors initializes a go routine to sync the mirrors
491
556
func InitSyncMirrors () {
492
557
go graceful .GetManager ().RunWithShutdownContext (SyncMirrors )
0 commit comments