@@ -342,6 +342,7 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
342342 apiRepo := ToRepo (ctx , baseRepo , baseRepoPerm )
343343 baseBranchCache := make (map [string ]* git_model.Branch )
344344 apiPullRequests := make ([]* api.PullRequest , 0 , len (prs ))
345+ headGitRepoCache := make (map [int64 ]* git.Repository , len (prs ))
345346 for _ , pr := range prs {
346347 apiIssue := ToAPIIssue (ctx , doer , pr .Issue )
347348
@@ -431,12 +432,10 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
431432 apiPullRequest .Head .Name = ""
432433 }
433434
434- var headGitRepo * git.Repository
435435 if pr .HeadRepo != nil && pr .Flow == issues_model .PullRequestFlowGithub {
436436 if pr .HeadRepoID == pr .BaseRepoID {
437437 apiPullRequest .Head .RepoID = pr .HeadRepo .ID
438438 apiPullRequest .Head .Repository = apiRepo
439- headGitRepo = gitRepo
440439 } else {
441440 p , err := access_model .GetUserRepoPermission (ctx , pr .HeadRepo , doer )
442441 if err != nil {
@@ -446,22 +445,30 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
446445
447446 apiPullRequest .Head .RepoID = pr .HeadRepo .ID
448447 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 ()
456448 }
457449
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 ) {
460452 log .Error ("GetBranch[%s]: %v" , pr .HeadBranch , err )
461453 return nil , err
462454 }
463455
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+ }
465472 headCommitID , err := headGitRepo .GetRefCommitID (apiPullRequest .Head .Ref )
466473 if err != nil && ! git .IsErrNotExist (err ) {
467474 log .Error ("GetCommit[%s]: %v" , pr .HeadBranch , err )
@@ -471,15 +478,8 @@ func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs
471478 apiPullRequest .Head .Sha = headCommitID
472479 }
473480 } 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
483483 }
484484 }
485485
0 commit comments