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

Two bugs in GenerateGitContent, the function behind
`POST /api/v1/repos/{owner}/{template}/generate`:

1. The new repository's refs were not written `branch` DB table
2. The function re-fetched the new repo row from the database
    but reassigned its local pointer

---------

Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
agyss
2026-05-15 07:24:40 +00:00
committed by GitHub
co-authored by wxiaoguang
parent 5c887d68ca
commit 5b3575a8be
4 changed files with 41 additions and 52 deletions
+2 -7
View File
@@ -16,12 +16,11 @@ import (
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
asymkey_service "code.gitea.io/gitea/services/asymkey"
)
// initRepoCommit temporarily changes with work directory.
func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Repository, u *user_model.User, defaultBranch string) (err error) {
func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Repository, u *user_model.User) (err error) {
commitTimeStr := time.Now().Format(time.RFC3339)
sig := u.NewGitSig()
@@ -69,13 +68,9 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi
return fmt.Errorf("git commit: %w", err)
}
if len(defaultBranch) == 0 {
defaultBranch = setting.Repository.DefaultBranch
}
if err := gitrepo.PushFromLocal(ctx, tmpPath, repo, git.PushOptions{
LocalRefName: "HEAD",
Branch: defaultBranch,
Branch: repo.DefaultBranch,
Env: repo_module.InternalPushingEnvironment(u, repo),
}); err != nil {
log.Error("Failed to push back to HEAD Error: %v", err)