Skip to content

Commit c7b3cdf

Browse files
authored
Use gitrepo's push function (#36245)
extract from #36186
1 parent 83527d3 commit c7b3cdf

File tree

3 files changed

+16
-30
lines changed

3 files changed

+16
-30
lines changed

modules/git/repo.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error {
186186
// PushOptions options when push to remote
187187
type PushOptions struct {
188188
Remote string
189+
LocalRefName string
189190
Branch string
190191
Force bool
191192
ForceWithLease string
@@ -207,7 +208,13 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error {
207208
}
208209
remoteBranchArgs := []string{opts.Remote}
209210
if len(opts.Branch) > 0 {
210-
remoteBranchArgs = append(remoteBranchArgs, opts.Branch)
211+
var refspec string
212+
if opts.LocalRefName != "" {
213+
refspec = fmt.Sprintf("%s:%s", opts.LocalRefName, opts.Branch)
214+
} else {
215+
refspec = opts.Branch
216+
}
217+
remoteBranchArgs = append(remoteBranchArgs, refspec)
211218
}
212219
cmd.AddDashesAndList(remoteBranchArgs...)
213220

services/repository/generate.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
git_model "code.gitea.io/gitea/models/git"
2222
repo_model "code.gitea.io/gitea/models/repo"
2323
"code.gitea.io/gitea/modules/git"
24-
"code.gitea.io/gitea/modules/git/gitcmd"
2524
"code.gitea.io/gitea/modules/gitrepo"
2625
"code.gitea.io/gitea/modules/glob"
2726
"code.gitea.io/gitea/modules/log"
@@ -216,19 +215,6 @@ func processGiteaTemplateFile(ctx context.Context, tmpDir string, templateRepo,
216215
}
217216

218217
func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository, tmpDir string) error {
219-
commitTimeStr := time.Now().Format(time.RFC3339)
220-
authorSig := repo.Owner.NewGitSig()
221-
222-
// Because this may call hooks we should pass in the environment
223-
env := append(os.Environ(),
224-
"GIT_AUTHOR_NAME="+authorSig.Name,
225-
"GIT_AUTHOR_EMAIL="+authorSig.Email,
226-
"GIT_AUTHOR_DATE="+commitTimeStr,
227-
"GIT_COMMITTER_NAME="+authorSig.Name,
228-
"GIT_COMMITTER_EMAIL="+authorSig.Email,
229-
"GIT_COMMITTER_DATE="+commitTimeStr,
230-
)
231-
232218
// Clone to temporary path and do the init commit.
233219
if err := gitrepo.CloneRepoToLocal(ctx, templateRepo, tmpDir, git.CloneRepoOptions{
234220
Depth: 1,
@@ -264,15 +250,6 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
264250
return err
265251
}
266252

267-
if stdout, _, err := gitcmd.NewCommand("remote", "add", "origin").
268-
AddDynamicArguments(repo.RepoPath()).
269-
WithDir(tmpDir).
270-
WithEnv(env).
271-
RunStdString(ctx); err != nil {
272-
log.Error("Unable to add %v as remote origin to temporary repo to %s: stdout %s\nError: %v", repo, tmpDir, stdout, err)
273-
return fmt.Errorf("git remote add: %w", err)
274-
}
275-
276253
if err = git.AddTemplateSubmoduleIndexes(ctx, tmpDir, submodules); err != nil {
277254
return fmt.Errorf("failed to add submodules: %v", err)
278255
}

services/repository/init.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111

1212
repo_model "code.gitea.io/gitea/models/repo"
1313
user_model "code.gitea.io/gitea/models/user"
14+
"code.gitea.io/gitea/modules/git"
1415
"code.gitea.io/gitea/modules/git/gitcmd"
16+
"code.gitea.io/gitea/modules/gitrepo"
1517
"code.gitea.io/gitea/modules/log"
1618
repo_module "code.gitea.io/gitea/modules/repository"
1719
"code.gitea.io/gitea/modules/setting"
@@ -71,12 +73,12 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi
7173
defaultBranch = setting.Repository.DefaultBranch
7274
}
7375

74-
if stdout, _, err := gitcmd.NewCommand("push", "origin").
75-
AddDynamicArguments("HEAD:" + defaultBranch).
76-
WithDir(tmpPath).
77-
WithEnv(repo_module.InternalPushingEnvironment(u, repo)).
78-
RunStdString(ctx); err != nil {
79-
log.Error("Failed to push back to HEAD: Stdout: %s\nError: %v", stdout, err)
76+
if err := gitrepo.PushFromLocal(ctx, tmpPath, repo, git.PushOptions{
77+
LocalRefName: "HEAD",
78+
Branch: defaultBranch,
79+
Env: repo_module.InternalPushingEnvironment(u, repo),
80+
}); err != nil {
81+
log.Error("Failed to push back to HEAD Error: %v", err)
8082
return fmt.Errorf("git push: %w", err)
8183
}
8284

0 commit comments

Comments
 (0)