9
9
"bytes"
10
10
"context"
11
11
"fmt"
12
- "os"
13
- "path"
14
12
"strings"
15
13
"time"
16
14
@@ -177,18 +175,6 @@ func checkForInvalidation(requests models.PullRequestList, repoID int64, doer *m
177
175
return nil
178
176
}
179
177
180
- func addHeadRepoTasks (prs []* models.PullRequest ) {
181
- for _ , pr := range prs {
182
- log .Trace ("addHeadRepoTasks[%d]: composing new test task" , pr .ID )
183
- if err := PushToBaseRepo (pr ); err != nil {
184
- log .Error ("PushToBaseRepo: %v" , err )
185
- continue
186
- }
187
-
188
- AddToTaskQueue (pr )
189
- }
190
- }
191
-
192
178
// AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch,
193
179
// and generate new patch for testing as needed.
194
180
func AddTestPullRequestTask (doer * models.User , repoID int64 , branch string , isSync bool , oldCommitID , newCommitID string ) {
@@ -245,7 +231,15 @@ func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSy
245
231
}
246
232
}
247
233
248
- addHeadRepoTasks (prs )
234
+ for _ , pr := range prs {
235
+ log .Trace ("Updating PR[%d]: composing new test task" , pr .ID )
236
+ if err := PushToBaseRepo (pr ); err != nil {
237
+ log .Error ("PushToBaseRepo: %v" , err )
238
+ continue
239
+ }
240
+
241
+ AddToTaskQueue (pr )
242
+ }
249
243
250
244
log .Trace ("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests" , repoID , branch )
251
245
prs , err = models .GetUnmergedPullRequestsByBaseInfo (repoID , branch )
@@ -345,54 +339,17 @@ func checkIfPRContentChanged(pr *models.PullRequest, oldCommitID, newCommitID st
345
339
func PushToBaseRepo (pr * models.PullRequest ) (err error ) {
346
340
log .Trace ("PushToBaseRepo[%d]: pushing commits to base repo '%s'" , pr .BaseRepoID , pr .GetGitRefName ())
347
341
348
- // Clone base repo.
349
- tmpBasePath , err := models .CreateTemporaryPath ("pull" )
350
- if err != nil {
351
- log .Error ("CreateTemporaryPath: %v" , err )
352
- return err
353
- }
354
- defer func () {
355
- err := models .RemoveTemporaryPath (tmpBasePath )
356
- if err != nil {
357
- log .Error ("Error whilst removing temporary path: %s Error: %v" , tmpBasePath , err )
358
- }
359
- }()
360
-
361
342
if err := pr .LoadHeadRepo (); err != nil {
362
343
log .Error ("Unable to load head repository for PR[%d] Error: %v" , pr .ID , err )
363
344
return err
364
345
}
365
346
headRepoPath := pr .HeadRepo .RepoPath ()
366
347
367
- if err := git .Clone (headRepoPath , tmpBasePath , git.CloneRepoOptions {
368
- Bare : true ,
369
- Shared : true ,
370
- Branch : pr .HeadBranch ,
371
- Quiet : true ,
372
- }); err != nil {
373
- log .Error ("git clone tmpBasePath: %v" , err )
374
- return err
375
- }
376
- gitRepo , err := git .OpenRepository (tmpBasePath )
377
- if err != nil {
378
- return fmt .Errorf ("OpenRepository: %v" , err )
379
- }
380
-
381
348
if err := pr .LoadBaseRepo (); err != nil {
382
349
log .Error ("Unable to load base repository for PR[%d] Error: %v" , pr .ID , err )
383
350
return err
384
351
}
385
- if err := gitRepo .AddRemote ("base" , pr .BaseRepo .RepoPath (), false ); err != nil {
386
- return fmt .Errorf ("tmpGitRepo.AddRemote: %v" , err )
387
- }
388
- defer gitRepo .Close ()
389
-
390
- headFile := pr .GetGitRefName ()
391
-
392
- // Remove head in case there is a conflict.
393
- file := path .Join (pr .BaseRepo .RepoPath (), headFile )
394
-
395
- _ = os .Remove (file )
352
+ baseRepoPath := pr .BaseRepo .RepoPath ()
396
353
397
354
if err = pr .LoadIssue (); err != nil {
398
355
return fmt .Errorf ("unable to load issue %d for pr %d: %v" , pr .IssueID , pr .ID , err )
@@ -401,24 +358,26 @@ func PushToBaseRepo(pr *models.PullRequest) (err error) {
401
358
return fmt .Errorf ("unable to load poster %d for pr %d: %v" , pr .Issue .PosterID , pr .ID , err )
402
359
}
403
360
404
- if err = git .Push (tmpBasePath , git.PushOptions {
405
- Remote : "base" ,
406
- Branch : fmt .Sprintf ("%s:%s" , pr .HeadBranch , headFile ),
361
+ gitRefName := pr .GetGitRefName ()
362
+
363
+ if err := git .Push (headRepoPath , git.PushOptions {
364
+ Remote : baseRepoPath ,
365
+ Branch : pr .HeadBranch + ":" + gitRefName ,
407
366
Force : true ,
408
367
// Use InternalPushingEnvironment here because we know that pre-receive and post-receive do not run on a refs/pulls/...
409
368
Env : models .InternalPushingEnvironment (pr .Issue .Poster , pr .BaseRepo ),
410
369
}); err != nil {
411
370
if git .IsErrPushOutOfDate (err ) {
412
371
// This should not happen as we're using force!
413
- 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 )
372
+ 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 )
414
373
return err
415
374
} else if git .IsErrPushRejected (err ) {
416
375
rejectErr := err .(* git.ErrPushRejected )
417
- log .Info ("Unable to push PR head for %s#%d (%-v:%s) due to rejection:\n Stdout: %s\n Stderr: %s\n Error: %v" , pr .BaseRepo .FullName (), pr .Index , pr .BaseRepo , headFile , rejectErr .StdOut , rejectErr .StdErr , rejectErr .Err )
376
+ log .Info ("Unable to push PR head for %s#%d (%-v:%s) due to rejection:\n Stdout: %s\n Stderr: %s\n Error: %v" , pr .BaseRepo .FullName (), pr .Index , pr .BaseRepo , gitRefName , rejectErr .StdOut , rejectErr .StdErr , rejectErr .Err )
418
377
return err
419
378
}
420
- 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 )
421
- return fmt .Errorf ("Push: %s:%s %s:%s %v" , pr .HeadRepo .FullName (), pr .HeadBranch , pr .BaseRepo .FullName (), headFile , err )
379
+ 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 )
380
+ return fmt .Errorf ("Push: %s:%s %s:%s %v" , pr .HeadRepo .FullName (), pr .HeadBranch , pr .BaseRepo .FullName (), gitRefName , err )
422
381
}
423
382
424
383
return nil
0 commit comments