aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-03-20 22:31:32 +0800
committerGitHub <noreply@github.com>2024-03-20 22:31:32 +0800
commitb4a6c6fd7a4ed8e018d27fcdb5203fa04becdddb (patch)
treefb6895cecbf6650760d0456fbc3f55bb0e37b884
parent3fd15aeff2fc65479bffa5b9914e8a607c95f70e (diff)
downloadgitea-b4a6c6fd7a4ed8e018d27fcdb5203fa04becdddb.tar.gz
gitea-b4a6c6fd7a4ed8e018d27fcdb5203fa04becdddb.zip
Fix loadOneBranch panic (#29938) (#29939)
Backport #29938 Try to fix #29936 Far from ideal, but still better than panic.
-rw-r--r--modules/git/repo.go2
-rw-r--r--services/repository/branch.go10
2 files changed, 7 insertions, 5 deletions
diff --git a/modules/git/repo.go b/modules/git/repo.go
index 0c92c979a7..fe1613b666 100644
--- a/modules/git/repo.go
+++ b/modules/git/repo.go
@@ -248,7 +248,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 1ad96aceb2..f0416a1306 100644
--- a/services/repository/branch.go
+++ b/services/repository/branch.go
@@ -128,10 +128,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 {
@@ -142,6 +139,11 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g
}
}
+ if divergence == nil {
+ // tolerate error that we can't get divergence
+ divergence = &git.DivergeObject{Ahead: -1, Behind: -1}
+ }
+
pr, err := issues_model.GetLatestPullRequestByHeadInfo(repo.ID, branchName)
if err != nil {
return nil, fmt.Errorf("GetLatestPullRequestByHeadInfo: %v", err)