@@ -303,20 +303,17 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer
303303		pr .Status  =  issues_model .PullRequestStatusMergeable 
304304	}
305305
306- 	// Update Commit Divergence 
307- 	divergence , err  :=  GetDiverging (ctx , pr )
308- 	if  err  !=  nil  {
306+ 	if  err  :=  pr .LoadBaseRepo (ctx ); err  !=  nil  {
309307		return  err 
310308	}
311- 	pr .CommitsAhead  =  divergence .Ahead 
312- 	pr .CommitsBehind  =  divergence .Behind 
313309
314- 	// add first push codes comment  
315- 	baseGitRepo , err  :=  gitrepo . OpenRepository (ctx , pr .BaseRepo )
310+ 	// update commits ahead and behind  
311+ 	divergence , err  :=  git . GetDivergingCommits (ctx , pr .BaseRepo . RepoPath (),  pr . BaseBranch ,  pr . GetGitHeadRefName () )
316312	if  err  !=  nil  {
317313		return  err 
318314	}
319- 	defer  baseGitRepo .Close ()
315+ 	pr .CommitsAhead  =  divergence .Ahead 
316+ 	pr .CommitsBehind  =  divergence .Behind 
320317
321318	return  db .WithTx (ctx , func (ctx  context.Context ) error  {
322319		if  err  :=  pr .UpdateColsIfNotMerged (ctx , "merge_base" , "status" , "conflicted_files" , "changed_protected_files" , "base_branch" , "commits_ahead" , "commits_behind" ); err  !=  nil  {
@@ -448,9 +445,10 @@ func AddTestPullRequestTask(opts TestPullRequestOptions) {
448445						if  err  :=  issues_model .MarkReviewsAsNotStale (ctx , pr .IssueID , opts .NewCommitID ); err  !=  nil  {
449446							log .Error ("MarkReviewsAsNotStale: %v" , err )
450447						}
451- 						divergence , err  :=  GetDiverging (ctx , pr )
448+ 
449+ 						divergence , err  :=  git .GetDivergingCommits (ctx , pr .BaseRepo .RepoPath (), pr .BaseBranch , pr .GetGitHeadRefName ())
452450						if  err  !=  nil  {
453- 							log .Error ("GetDiverging : %v" , err )
451+ 							log .Error ("GetDivergingCommits : %v" , err )
454452						} else  {
455453							err  =  pr .UpdateCommitDivergence (ctx , divergence .Ahead , divergence .Behind )
456454							if  err  !=  nil  {
@@ -480,8 +478,13 @@ func AddTestPullRequestTask(opts TestPullRequestOptions) {
480478			log .Error ("Find pull requests [base_repo_id: %d, base_branch: %s]: %v" , opts .RepoID , opts .Branch , err )
481479			return 
482480		}
481+ 		baseRepo , err  :=  repo_model .GetRepositoryByID (ctx , opts .RepoID )
482+ 		if  err  !=  nil  {
483+ 			log .Error ("GetRepositoryByID: %v" , err )
484+ 			return 
485+ 		}
483486		for  _ , pr  :=  range  prs  {
484- 			divergence , err  :=  GetDiverging (ctx , pr )
487+ 			divergence , err  :=  git . GetDivergingCommits (ctx , baseRepo . RepoPath (),  pr . BaseBranch ,  pr . GetGitHeadRefName () )
485488			if  err  !=  nil  {
486489				if  git_model .IsErrBranchNotExist (err ) &&  ! gitrepo .IsBranchExist (ctx , pr .HeadRepo , pr .HeadBranch ) {
487490					log .Warn ("Cannot test PR %s/%d: head_branch %s no longer exists" , pr .BaseRepo .Name , pr .IssueID , pr .HeadBranch )
@@ -767,9 +770,9 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ
767770	if  pr .Flow  ==  issues_model .PullRequestFlowGithub  {
768771		headCommit , err  =  gitRepo .GetBranchCommit (pr .HeadBranch )
769772	} else  {
770- 		headCommitID , err  :=  gitRepo .GetRefCommitID (pr .GetGitHeadRefName ())
771- 		if  err  !=  nil  {
772- 			log .Error ("Unable to get head commit: %s Error: %v" , pr .GetGitHeadRefName (), err )
773+ 		headCommitID , err1  :=  gitRepo .GetRefCommitID (pr .GetGitHeadRefName ())
774+ 		if  err1  !=  nil  {
775+ 			log .Error ("Unable to get head commit: %s Error: %v" , pr .GetGitHeadRefName (), err1 )
773776			return  "" 
774777		}
775778		headCommit , err  =  gitRepo .GetCommit (headCommitID )
0 commit comments