@@ -342,6 +342,7 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
342
342
apiRepo := ToRepo (ctx , baseRepo , baseRepoPerm )
343
343
baseBranchCache := make (map [string ]* git_model.Branch )
344
344
apiPullRequests := make ([]* api.PullRequest , 0 , len (prs ))
345
+ headGitRepoCache := make (map [int64 ]* git.Repository , len (prs ))
345
346
for _ , pr := range prs {
346
347
apiIssue := ToAPIIssue (ctx , doer , pr .Issue )
347
348
@@ -431,12 +432,10 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
431
432
apiPullRequest .Head .Name = ""
432
433
}
433
434
434
- var headGitRepo * git.Repository
435
435
if pr .HeadRepo != nil && pr .Flow == issues_model .PullRequestFlowGithub {
436
436
if pr .HeadRepoID == pr .BaseRepoID {
437
437
apiPullRequest .Head .RepoID = pr .HeadRepo .ID
438
438
apiPullRequest .Head .Repository = apiRepo
439
- headGitRepo = gitRepo
440
439
} else {
441
440
p , err := access_model .GetUserRepoPermission (ctx , pr .HeadRepo , doer )
442
441
if err != nil {
@@ -446,22 +445,30 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
446
445
447
446
apiPullRequest .Head .RepoID = pr .HeadRepo .ID
448
447
apiPullRequest .Head .Repository = ToRepo (ctx , pr .HeadRepo , p )
449
-
450
- headGitRepo , err = gitrepo .OpenRepository (ctx , pr .HeadRepo )
451
- if err != nil {
452
- log .Error ("OpenRepository[%s]: %v" , pr .HeadRepo .RepoPath (), err )
453
- return nil , err
454
- }
455
- defer headGitRepo .Close ()
456
448
}
457
449
458
- exist , err := git_model .IsBranchExist (ctx , pr .HeadRepoID , pr .HeadBranch )
459
- if err != nil {
450
+ headBranch , err := git_model .GetBranch (ctx , pr .HeadRepoID , pr .HeadBranch )
451
+ if err != nil && ! git_model . IsErrBranchNotExist ( err ) {
460
452
log .Error ("GetBranch[%s]: %v" , pr .HeadBranch , err )
461
453
return nil , err
462
454
}
463
455
464
- if ! exist {
456
+ if headBranch == nil || headBranch .IsDeleted {
457
+ var headGitRepo * git.Repository
458
+ if pr .HeadRepoID == pr .BaseRepoID {
459
+ headGitRepo = gitRepo
460
+ } else {
461
+ headGitRepo , ok = headGitRepoCache [pr .HeadRepoID ]
462
+ if ! ok {
463
+ headGitRepo , err = gitrepo .OpenRepository (ctx , pr .HeadRepo )
464
+ if err != nil {
465
+ log .Error ("OpenRepository[%s]: %v" , pr .HeadRepo .RepoPath (), err )
466
+ return nil , err
467
+ }
468
+ defer headGitRepo .Close ()
469
+ headGitRepoCache [pr .HeadRepoID ] = headGitRepo
470
+ }
471
+ }
465
472
headCommitID , err := headGitRepo .GetRefCommitID (apiPullRequest .Head .Ref )
466
473
if err != nil && ! git .IsErrNotExist (err ) {
467
474
log .Error ("GetCommit[%s]: %v" , pr .HeadBranch , err )
@@ -471,15 +478,8 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
471
478
apiPullRequest .Head .Sha = headCommitID
472
479
}
473
480
} else {
474
- commit , err := headGitRepo .GetBranchCommit (pr .HeadBranch )
475
- if err != nil && ! git .IsErrNotExist (err ) {
476
- log .Error ("GetCommit[%s]: %v" , pr .HeadBranch , err )
477
- return nil , err
478
- }
479
- if err == nil {
480
- apiPullRequest .Head .Ref = pr .HeadBranch
481
- apiPullRequest .Head .Sha = commit .ID .String ()
482
- }
481
+ apiPullRequest .Head .Ref = pr .HeadBranch
482
+ apiPullRequest .Head .Sha = headBranch .CommitID
483
483
}
484
484
}
485
485
0 commit comments