Skip to content

Commit 648eb3f

Browse files
committed
Stop cloning unnecessarily on PR update
Fix go-gitea#12740 Signed-off-by: Andrew Thornton <[email protected]>
1 parent c950ea1 commit 648eb3f

File tree

1 file changed

+17
-53
lines changed

1 file changed

+17
-53
lines changed

services/pull/pull.go

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,6 @@ func checkForInvalidation(requests models.PullRequestList, repoID int64, doer *m
215215
return nil
216216
}
217217

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-
230218
// AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
231219
// and generate new patch for testing as needed.
232220
func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSync bool, oldCommitID, newCommitID string) {
@@ -283,8 +271,14 @@ func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSy
283271
}
284272
}
285273

286-
addHeadRepoTasks(prs)
287274
for _, pr := range prs {
275+
log.Trace("Updating PR[%d]: composing new test task", pr.ID)
276+
if err := PushToBaseRepo(pr); err != nil {
277+
log.Error("PushToBaseRepo: %v", err)
278+
continue
279+
}
280+
281+
AddToTaskQueue(pr)
288282
comment, err := models.CreatePushPullComment(doer, pr, oldCommitID, newCommitID)
289283
if err == nil && comment != nil {
290284
notification.NotifyPullRequestPushCommits(doer, pr, comment)
@@ -389,46 +383,23 @@ func checkIfPRContentChanged(pr *models.PullRequest, oldCommitID, newCommitID st
389383
func PushToBaseRepo(pr *models.PullRequest) (err error) {
390384
log.Trace("PushToBaseRepo[%d]: pushing commits to base repo '%s'", pr.BaseRepoID, pr.GetGitRefName())
391385

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-
405386
if err := pr.LoadHeadRepo(); err != nil {
406387
log.Error("Unable to load head repository for PR[%d] Error: %v", pr.ID, err)
407388
return err
408389
}
409390
headRepoPath := pr.HeadRepo.RepoPath()
410391

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-
426392
if err := pr.LoadBaseRepo(); err != nil {
427393
log.Error("Unable to load base repository for PR[%d] Error: %v", pr.ID, err)
428394
return err
429395
}
430-
if err := gitRepo.AddRemote("base", pr.BaseRepo.RepoPath(), false); err != nil {
431-
return fmt.Errorf("tmpGitRepo.AddRemote: %v", err)
396+
baseRepoPath := pr.BaseRepo.RepoPath()
397+
398+
if err = pr.LoadIssue(); err != nil {
399+
return fmt.Errorf("unable to load issue %d for pr %d: %v", pr.IssueID, pr.ID, err)
400+
}
401+
if err = pr.Issue.LoadPoster(); err != nil {
402+
return fmt.Errorf("unable to load poster %d for pr %d: %v", pr.Issue.PosterID, pr.ID, err)
432403
}
433404

434405
headFile := pr.GetGitRefName()
@@ -438,16 +409,9 @@ func PushToBaseRepo(pr *models.PullRequest) (err error) {
438409

439410
_ = util.Remove(file)
440411

441-
if err = pr.LoadIssue(); err != nil {
442-
return fmt.Errorf("unable to load issue %d for pr %d: %v", pr.IssueID, pr.ID, err)
443-
}
444-
if err = pr.Issue.LoadPoster(); err != nil {
445-
return fmt.Errorf("unable to load poster %d for pr %d: %v", pr.Issue.PosterID, pr.ID, err)
446-
}
447-
448-
if err = git.Push(tmpBasePath, git.PushOptions{
449-
Remote: "base",
450-
Branch: fmt.Sprintf("%s:%s", pr.HeadBranch, headFile),
412+
if err := git.Push(headRepoPath, git.PushOptions{
413+
Remote: baseRepoPath,
414+
Branch: pr.HeadBranch + ":" + headFile,
451415
Force: true,
452416
// Use InternalPushingEnvironment here because we know that pre-receive and post-receive do not run on a refs/pulls/...
453417
Env: models.InternalPushingEnvironment(pr.Issue.Poster, pr.BaseRepo),

0 commit comments

Comments
 (0)