Skip to content

Commit e94723f

Browse files
authored
Fix incorrect default branch when adopt a repository (#30912)
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.
1 parent ed0fc27 commit e94723f

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

services/repository/adopt.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts CreateR
3636
}
3737
}
3838

39-
if len(opts.DefaultBranch) == 0 {
40-
opts.DefaultBranch = setting.Repository.DefaultBranch
41-
}
42-
4339
repo := &repo_model.Repository{
4440
OwnerID: u.ID,
4541
Owner: u,
@@ -81,7 +77,7 @@ func AdoptRepository(ctx context.Context, doer, u *user_model.User, opts CreateR
8177
}
8278

8379
if err := adoptRepository(ctx, repoPath, repo, opts.DefaultBranch); err != nil {
84-
return fmt.Errorf("createDelegateHooks: %w", err)
80+
return fmt.Errorf("adoptRepository: %w", err)
8581
}
8682

8783
if err := repo_module.CheckDaemonExportOK(ctx, repo); err != nil {
@@ -143,6 +139,21 @@ func adoptRepository(ctx context.Context, repoPath string, repo *repo_model.Repo
143139
}
144140
}
145141

142+
// Don't bother looking this repo in the context it won't be there
143+
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
144+
if err != nil {
145+
return fmt.Errorf("openRepository: %w", err)
146+
}
147+
defer gitRepo.Close()
148+
149+
if _, err = repo_module.SyncRepoBranchesWithRepo(ctx, repo, gitRepo, 0); err != nil {
150+
return fmt.Errorf("SyncRepoBranchesWithRepo: %w", err)
151+
}
152+
153+
if err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
154+
return fmt.Errorf("SyncReleasesWithTags: %w", err)
155+
}
156+
146157
branches, _ := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{
147158
RepoID: repo.ID,
148159
ListOptions: db.ListOptionsAll,
@@ -183,26 +194,10 @@ func adoptRepository(ctx context.Context, repoPath string, repo *repo_model.Repo
183194
return fmt.Errorf("setDefaultBranch: %w", err)
184195
}
185196
}
186-
187197
if err = repo_module.UpdateRepository(ctx, repo, false); err != nil {
188198
return fmt.Errorf("updateRepository: %w", err)
189199
}
190200

191-
// Don't bother looking this repo in the context it won't be there
192-
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
193-
if err != nil {
194-
return fmt.Errorf("openRepository: %w", err)
195-
}
196-
defer gitRepo.Close()
197-
198-
if _, err = repo_module.SyncRepoBranchesWithRepo(ctx, repo, gitRepo, 0); err != nil {
199-
return fmt.Errorf("SyncRepoBranches: %w", err)
200-
}
201-
202-
if err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
203-
return fmt.Errorf("SyncReleasesWithTags: %w", err)
204-
}
205-
206201
return nil
207202
}
208203

0 commit comments

Comments
 (0)