Fix loadOneBranch panic (#29938)

Try to fix #29936

Far from ideal, but still better than panic.

(cherry picked from commit 21151474e36eecc5b808963b924cd27ec34e0608)
This commit is contained in:
wxiaoguang 2024-03-20 23:38:22 +08:00 committed by Earl Warren
parent 165182a92d
commit 0710de9d0d
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
2 changed files with 7 additions and 5 deletions

View file

@ -127,10 +127,7 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g
p := protectedBranches.GetFirstMatched(branchName)
isProtected := p != nil
divergence := &git.DivergeObject{
Ahead: -1,
Behind: -1,
}
var divergence *git.DivergeObject
// it's not default branch
if repo.DefaultBranch != dbBranch.Name && !dbBranch.IsDeleted {
@ -141,6 +138,11 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g
}
}
if divergence == nil {
// tolerate the error that we cannot get divergence
divergence = &git.DivergeObject{Ahead: -1, Behind: -1}
}
pr, err := issues_model.GetLatestPullRequestByHeadInfo(ctx, repo.ID, branchName)
if err != nil {
return nil, fmt.Errorf("GetLatestPullRequestByHeadInfo: %v", err)