Skip to content

Commit 342e49e

Browse files
zeripathlunny
andauthored
Stop cloning unnecessarily on PR update (#12839)
* Stop cloning unnecessarily on PR update Fix #12740 Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove unnecessary delete Signed-off-by: Andrew Thornton <art27@cantab.net> * fix lint Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent 08a905f commit 342e49e

File tree

1 file changed

+17
-60
lines changed

1 file changed

+17
-60
lines changed

services/pull/pull.go

+17-60
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"context"
1111
"encoding/json"
1212
"fmt"
13-
"path/filepath"
1413
"strings"
1514
"time"
1615

@@ -20,7 +19,6 @@ import (
2019
"code.gitea.io/gitea/modules/log"
2120
"code.gitea.io/gitea/modules/notification"
2221
"code.gitea.io/gitea/modules/setting"
23-
"code.gitea.io/gitea/modules/util"
2422
issue_service "code.gitea.io/gitea/services/issue"
2523

2624
"github.com/unknwon/com"
@@ -215,18 +213,6 @@ func checkForInvalidation(requests models.PullRequestList, repoID int64, doer *m
215213
return nil
216214
}
217215

218-
func addHeadRepoTasks(prs []*models.PullRequest) {
219-
for _, pr := range prs {
220-
log.Trace("addHeadRepoTasks[%d]: composing new test task", pr.ID)
221-
if err := PushToBaseRepo(pr); err != nil {
222-
log.Error("PushToBaseRepo: %v", err)
223-
continue
224-
}
225-
226-
AddToTaskQueue(pr)
227-
}
228-
}
229-
230216
// AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
231217
// and generate new patch for testing as needed.
232218
func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSync bool, oldCommitID, newCommitID string) {
@@ -283,8 +269,14 @@ func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSy
283269
}
284270
}
285271

286-
addHeadRepoTasks(prs)
287272
for _, pr := range prs {
273+
log.Trace("Updating PR[%d]: composing new test task", pr.ID)
274+
if err := PushToBaseRepo(pr); err != nil {
275+
log.Error("PushToBaseRepo: %v", err)
276+
continue
277+
}
278+
279+
AddToTaskQueue(pr)
288280
comment, err := models.CreatePushPullComment(doer, pr, oldCommitID, newCommitID)
289281
if err == nil && comment != nil {
290282
notification.NotifyPullRequestPushCommits(doer, pr, comment)
@@ -389,54 +381,17 @@ func checkIfPRContentChanged(pr *models.PullRequest, oldCommitID, newCommitID st
389381
func PushToBaseRepo(pr *models.PullRequest) (err error) {
390382
log.Trace("PushToBaseRepo[%d]: pushing commits to base repo '%s'", pr.BaseRepoID, pr.GetGitRefName())
391383

392-
// Clone base repo.
393-
tmpBasePath, err := models.CreateTemporaryPath("pull")
394-
if err != nil {
395-
log.Error("CreateTemporaryPath: %v", err)
396-
return err
397-
}
398-
defer func() {
399-
err := models.RemoveTemporaryPath(tmpBasePath)
400-
if err != nil {
401-
log.Error("Error whilst removing temporary path: %s Error: %v", tmpBasePath, err)
402-
}
403-
}()
404-
405384
if err := pr.LoadHeadRepo(); err != nil {
406385
log.Error("Unable to load head repository for PR[%d] Error: %v", pr.ID, err)
407386
return err
408387
}
409388
headRepoPath := pr.HeadRepo.RepoPath()
410389

411-
if err := git.Clone(headRepoPath, tmpBasePath, git.CloneRepoOptions{
412-
Bare: true,
413-
Shared: true,
414-
Branch: pr.HeadBranch,
415-
Quiet: true,
416-
}); err != nil {
417-
log.Error("git clone tmpBasePath: %v", err)
418-
return err
419-
}
420-
gitRepo, err := git.OpenRepository(tmpBasePath)
421-
if err != nil {
422-
return fmt.Errorf("OpenRepository: %v", err)
423-
}
424-
defer gitRepo.Close()
425-
426390
if err := pr.LoadBaseRepo(); err != nil {
427391
log.Error("Unable to load base repository for PR[%d] Error: %v", pr.ID, err)
428392
return err
429393
}
430-
if err := gitRepo.AddRemote("base", pr.BaseRepo.RepoPath(), false); err != nil {
431-
return fmt.Errorf("tmpGitRepo.AddRemote: %v", err)
432-
}
433-
434-
headFile := pr.GetGitRefName()
435-
436-
// Remove head in case there is a conflict.
437-
file := filepath.Join(pr.BaseRepo.RepoPath(), headFile)
438-
439-
_ = util.Remove(file)
394+
baseRepoPath := pr.BaseRepo.RepoPath()
440395

441396
if err = pr.LoadIssue(); err != nil {
442397
return fmt.Errorf("unable to load issue %d for pr %d: %v", pr.IssueID, pr.ID, err)
@@ -445,24 +400,26 @@ func PushToBaseRepo(pr *models.PullRequest) (err error) {
445400
return fmt.Errorf("unable to load poster %d for pr %d: %v", pr.Issue.PosterID, pr.ID, err)
446401
}
447402

448-
if err = git.Push(tmpBasePath, git.PushOptions{
449-
Remote: "base",
450-
Branch: fmt.Sprintf("%s:%s", pr.HeadBranch, headFile),
403+
gitRefName := pr.GetGitRefName()
404+
405+
if err := git.Push(headRepoPath, git.PushOptions{
406+
Remote: baseRepoPath,
407+
Branch: pr.HeadBranch + ":" + gitRefName,
451408
Force: true,
452409
// Use InternalPushingEnvironment here because we know that pre-receive and post-receive do not run on a refs/pulls/...
453410
Env: models.InternalPushingEnvironment(pr.Issue.Poster, pr.BaseRepo),
454411
}); err != nil {
455412
if git.IsErrPushOutOfDate(err) {
456413
// This should not happen as we're using force!
457-
log.Error("Unable to push PR head for %s#%d (%-v:%s) due to ErrPushOfDate: %v", pr.BaseRepo.FullName(), pr.Index, pr.BaseRepo, headFile, err)
414+
log.Error("Unable to push PR head for %s#%d (%-v:%s) due to ErrPushOfDate: %v", pr.BaseRepo.FullName(), pr.Index, pr.BaseRepo, gitRefName, err)
458415
return err
459416
} else if git.IsErrPushRejected(err) {
460417
rejectErr := err.(*git.ErrPushRejected)
461-
log.Info("Unable to push PR head for %s#%d (%-v:%s) due to rejection:\nStdout: %s\nStderr: %s\nError: %v", pr.BaseRepo.FullName(), pr.Index, pr.BaseRepo, headFile, rejectErr.StdOut, rejectErr.StdErr, rejectErr.Err)
418+
log.Info("Unable to push PR head for %s#%d (%-v:%s) due to rejection:\nStdout: %s\nStderr: %s\nError: %v", pr.BaseRepo.FullName(), pr.Index, pr.BaseRepo, gitRefName, rejectErr.StdOut, rejectErr.StdErr, rejectErr.Err)
462419
return err
463420
}
464-
log.Error("Unable to push PR head for %s#%d (%-v:%s) due to Error: %v", pr.BaseRepo.FullName(), pr.Index, pr.BaseRepo, headFile, err)
465-
return fmt.Errorf("Push: %s:%s %s:%s %v", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), headFile, err)
421+
log.Error("Unable to push PR head for %s#%d (%-v:%s) due to Error: %v", pr.BaseRepo.FullName(), pr.Index, pr.BaseRepo, gitRefName, err)
422+
return fmt.Errorf("Push: %s:%s %s:%s %v", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), gitRefName, err)
466423
}
467424

468425
return nil

0 commit comments

Comments
 (0)