From c7b3cdf7b184f6baed50849eeaaf5d0cc79954a3 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 28 Dec 2025 03:24:28 -0800 Subject: [PATCH] Use gitrepo's push function (#36245) extract from #36186 --- modules/git/repo.go | 9 ++++++++- services/repository/generate.go | 23 ----------------------- services/repository/init.go | 14 ++++++++------ 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/modules/git/repo.go b/modules/git/repo.go index 579accf92e..bea599e22e 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -186,6 +186,7 @@ func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error { // PushOptions options when push to remote type PushOptions struct { Remote string + LocalRefName string Branch string Force bool ForceWithLease string @@ -207,7 +208,13 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error { } remoteBranchArgs := []string{opts.Remote} if len(opts.Branch) > 0 { - remoteBranchArgs = append(remoteBranchArgs, opts.Branch) + var refspec string + if opts.LocalRefName != "" { + refspec = fmt.Sprintf("%s:%s", opts.LocalRefName, opts.Branch) + } else { + refspec = opts.Branch + } + remoteBranchArgs = append(remoteBranchArgs, refspec) } cmd.AddDashesAndList(remoteBranchArgs...) diff --git a/services/repository/generate.go b/services/repository/generate.go index b2913cd110..bc37bc7bfe 100644 --- a/services/repository/generate.go +++ b/services/repository/generate.go @@ -21,7 +21,6 @@ import ( git_model "code.gitea.io/gitea/models/git" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/git/gitcmd" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/glob" "code.gitea.io/gitea/modules/log" @@ -216,19 +215,6 @@ func processGiteaTemplateFile(ctx context.Context, tmpDir string, templateRepo, } func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository, tmpDir string) error { - commitTimeStr := time.Now().Format(time.RFC3339) - authorSig := repo.Owner.NewGitSig() - - // Because this may call hooks we should pass in the environment - env := append(os.Environ(), - "GIT_AUTHOR_NAME="+authorSig.Name, - "GIT_AUTHOR_EMAIL="+authorSig.Email, - "GIT_AUTHOR_DATE="+commitTimeStr, - "GIT_COMMITTER_NAME="+authorSig.Name, - "GIT_COMMITTER_EMAIL="+authorSig.Email, - "GIT_COMMITTER_DATE="+commitTimeStr, - ) - // Clone to temporary path and do the init commit. if err := gitrepo.CloneRepoToLocal(ctx, templateRepo, tmpDir, git.CloneRepoOptions{ Depth: 1, @@ -264,15 +250,6 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r return err } - if stdout, _, err := gitcmd.NewCommand("remote", "add", "origin"). - AddDynamicArguments(repo.RepoPath()). - WithDir(tmpDir). - WithEnv(env). - RunStdString(ctx); err != nil { - log.Error("Unable to add %v as remote origin to temporary repo to %s: stdout %s\nError: %v", repo, tmpDir, stdout, err) - return fmt.Errorf("git remote add: %w", err) - } - if err = git.AddTemplateSubmoduleIndexes(ctx, tmpDir, submodules); err != nil { return fmt.Errorf("failed to add submodules: %v", err) } diff --git a/services/repository/init.go b/services/repository/init.go index 51cc113d63..6aeb5ec644 100644 --- a/services/repository/init.go +++ b/services/repository/init.go @@ -11,7 +11,9 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git/gitcmd" + "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" @@ -71,12 +73,12 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi defaultBranch = setting.Repository.DefaultBranch } - if stdout, _, err := gitcmd.NewCommand("push", "origin"). - AddDynamicArguments("HEAD:" + defaultBranch). - WithDir(tmpPath). - WithEnv(repo_module.InternalPushingEnvironment(u, repo)). - RunStdString(ctx); err != nil { - log.Error("Failed to push back to HEAD: Stdout: %s\nError: %v", stdout, err) + if err := gitrepo.PushFromLocal(ctx, tmpPath, repo, git.PushOptions{ + LocalRefName: "HEAD", + Branch: defaultBranch, + Env: repo_module.InternalPushingEnvironment(u, repo), + }); err != nil { + log.Error("Failed to push back to HEAD Error: %v", err) return fmt.Errorf("git push: %w", err) }