From 0710de9d0d5b6626c08af3430c0429f0b4df1cc7 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 20 Mar 2024 23:38:22 +0800 Subject: [PATCH] Fix loadOneBranch panic (#29938) Try to fix #29936 Far from ideal, but still better than panic. (cherry picked from commit 21151474e36eecc5b808963b924cd27ec34e0608) --- modules/git/repo.go | 2 +- services/repository/branch.go | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/modules/git/repo.go b/modules/git/repo.go index 60078f327..e8a7016d9 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -283,7 +283,7 @@ type DivergeObject struct { // GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch func GetDivergingCommits(ctx context.Context, repoPath, baseBranch, targetBranch string) (do DivergeObject, err error) { cmd := NewCommand(ctx, "rev-list", "--count", "--left-right"). - AddDynamicArguments(baseBranch + "..." + targetBranch) + AddDynamicArguments(baseBranch + "..." + targetBranch).AddArguments("--") stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath}) if err != nil { return do, err diff --git a/services/repository/branch.go b/services/repository/branch.go index b68355324..64d8eda4e 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -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)