]> source.dussan.org Git - gitea.git/commitdiff
Fix incorrect default branch when adopt a repository (#30912)
authoryp05327 <576951401@qq.com>
Thu, 9 May 2024 08:44:26 +0000 (17:44 +0900)
committerGitHub <noreply@github.com>
Thu, 9 May 2024 08:44:26 +0000 (08:44 +0000)
Fix #30521

we should sync branches first, then detect default branch, or
`git_model.FindBranchNames` will always return empty list, and the
detection will be wrong.

services/repository/adopt.go

index f4d0da67a5069e4de06e7d9f08ab92f33bcf8644..3d6fe71a091910bab1827bf653ff993b8bc62c91 100644 (file)
@@ -36,10 +36,6 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts CreateR
                }
        }
 
-       if len(opts.DefaultBranch) == 0 {
-               opts.DefaultBranch = setting.Repository.DefaultBranch
-       }
-
        repo := &repo_model.Repository{
                OwnerID:                         u.ID,
                Owner:                           u,
@@ -81,7 +77,7 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts CreateR
                }
 
                if err := adoptRepository(ctx, repoPath, repo, opts.DefaultBranch); err != nil {
-                       return fmt.Errorf("createDelegateHooks: %w", err)
+                       return fmt.Errorf("adoptRepository: %w", err)
                }
 
                if err := repo_module.CheckDaemonExportOK(ctx, repo); err != nil {
@@ -143,6 +139,21 @@ func adoptRepository(ctx context.Context, repoPath string, repo *repo_model.Repo
                }
        }
 
+       // Don't bother looking this repo in the context it won't be there
+       gitRepo, err := gitrepo.OpenRepository(ctx, repo)
+       if err != nil {
+               return fmt.Errorf("openRepository: %w", err)
+       }
+       defer gitRepo.Close()
+
+       if _, err = repo_module.SyncRepoBranchesWithRepo(ctx, repo, gitRepo, 0); err != nil {
+               return fmt.Errorf("SyncRepoBranchesWithRepo: %w", err)
+       }
+
+       if err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
+               return fmt.Errorf("SyncReleasesWithTags: %w", err)
+       }
+
        branches, _ := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{
                RepoID:          repo.ID,
                ListOptions:     db.ListOptionsAll,
@@ -183,26 +194,10 @@ func adoptRepository(ctx context.Context, repoPath string, repo *repo_model.Repo
                        return fmt.Errorf("setDefaultBranch: %w", err)
                }
        }
-
        if err = repo_module.UpdateRepository(ctx, repo, false); err != nil {
                return fmt.Errorf("updateRepository: %w", err)
        }
 
-       // Don't bother looking this repo in the context it won't be there
-       gitRepo, err := gitrepo.OpenRepository(ctx, repo)
-       if err != nil {
-               return fmt.Errorf("openRepository: %w", err)
-       }
-       defer gitRepo.Close()
-
-       if _, err = repo_module.SyncRepoBranchesWithRepo(ctx, repo, gitRepo, 0); err != nil {
-               return fmt.Errorf("SyncRepoBranches: %w", err)
-       }
-
-       if err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
-               return fmt.Errorf("SyncReleasesWithTags: %w", err)
-       }
-
        return nil
 }