0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-05-23 17:11:45 +02:00

fix(repo): /generate must sync the branch table for the new repo (#37693) (#37712)

Backport #37693
This commit is contained in:
wxiaoguang 2026-05-16 01:54:48 +08:00 committed by GitHub
parent 0e53c41694
commit 1d5163133b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 18 deletions

View File

@ -207,6 +207,9 @@ func processGiteaTemplateFile(ctx context.Context, tmpDir string, templateRepo,
}
func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository, tmpDir string) error {
// set default branch based on whether it's specified in the newly generated repo or not
repo.DefaultBranch = util.IfZero(repo.DefaultBranch, templateRepo.DefaultBranch)
// Clone to temporary path and do the init commit.
if err := gitrepo.CloneRepoToLocal(ctx, templateRepo, tmpDir, git.CloneRepoOptions{
Depth: 1,
@ -246,13 +249,7 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
return fmt.Errorf("failed to add submodules: %v", err)
}
// set default branch based on whether it's specified in the newly generated repo or not
defaultBranch := repo.DefaultBranch
if strings.TrimSpace(defaultBranch) == "" {
defaultBranch = templateRepo.DefaultBranch
}
return initRepoCommit(ctx, tmpDir, repo, repo.Owner, defaultBranch)
return initRepoCommit(ctx, tmpDir, repo, repo.Owner, repo.DefaultBranch)
}
// GenerateGitContent generates git content from a template repository
@ -266,17 +263,6 @@ func GenerateGitContent(ctx context.Context, templateRepo, generateRepo *repo_mo
if err = generateRepoCommit(ctx, generateRepo, templateRepo, generateRepo, tmpDir); err != nil {
return fmt.Errorf("generateRepoCommit: %w", err)
}
// re-fetch repo
if generateRepo, err = repo_model.GetRepositoryByID(ctx, generateRepo.ID); err != nil {
return fmt.Errorf("getRepositoryByID: %w", err)
}
// if there was no default branch supplied when generating the repo, use the default one from the template
if strings.TrimSpace(generateRepo.DefaultBranch) == "" {
generateRepo.DefaultBranch = templateRepo.DefaultBranch
}
if err = gitrepo.SetDefaultBranch(ctx, generateRepo, generateRepo.DefaultBranch); err != nil {
return fmt.Errorf("setDefaultBranch: %w", err)
}
@ -291,6 +277,9 @@ func GenerateGitContent(ctx context.Context, templateRepo, generateRepo *repo_mo
if err := git_model.CopyLFS(ctx, generateRepo, templateRepo); err != nil {
return fmt.Errorf("failed to copy LFS: %w", err)
}
if _, err := repo_module.SyncRepoBranches(ctx, generateRepo.ID, 0); err != nil {
return fmt.Errorf("SyncRepoBranches: %w", err)
}
return nil
}

View File

@ -671,6 +671,16 @@ func TestAPIGenerateRepo(t *testing.T) {
templateRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 44})
assertGeneratedRepoIsUsable := func(t *testing.T, ownerName string, repo *api.Repository) {
t.Helper()
assert.NotEmpty(t, repo.DefaultBranch)
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/branches/%s", ownerName, repo.Name, repo.DefaultBranch).AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
branch := DecodeJSON(t, resp, &api.Branch{})
assert.Equal(t, repo.DefaultBranch, branch.Name)
}
// user
repo := new(api.Repository)
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/generate", templateRepo.OwnerName, templateRepo.Name), &api.GenerateRepoOption{
@ -684,6 +694,7 @@ func TestAPIGenerateRepo(t *testing.T) {
DecodeJSON(t, resp, repo)
assert.Equal(t, "new-repo", repo.Name)
assertGeneratedRepoIsUsable(t, user.Name, repo)
// org
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/generate", templateRepo.OwnerName, templateRepo.Name), &api.GenerateRepoOption{
@ -697,6 +708,7 @@ func TestAPIGenerateRepo(t *testing.T) {
DecodeJSON(t, resp, repo)
assert.Equal(t, "new-repo", repo.Name)
assertGeneratedRepoIsUsable(t, "org3", repo)
}
func TestAPIRepoGetReviewers(t *testing.T) {