Skip to content

Commit b4a6c6f

Browse files
authored
Fix loadOneBranch panic (#29938) (#29939)
Backport #29938 Try to fix #29936 Far from ideal, but still better than panic.
1 parent 3fd15ae commit b4a6c6f

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

modules/git/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ type DivergeObject struct {
248248
// GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch
249249
func GetDivergingCommits(ctx context.Context, repoPath, baseBranch, targetBranch string) (do DivergeObject, err error) {
250250
cmd := NewCommand(ctx, "rev-list", "--count", "--left-right").
251-
AddDynamicArguments(baseBranch + "..." + targetBranch)
251+
AddDynamicArguments(baseBranch + "..." + targetBranch).AddArguments("--")
252252
stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath})
253253
if err != nil {
254254
return do, err

services/repository/branch.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g
128128
p := protectedBranches.GetFirstMatched(branchName)
129129
isProtected := p != nil
130130

131-
divergence := &git.DivergeObject{
132-
Ahead: -1,
133-
Behind: -1,
134-
}
131+
var divergence *git.DivergeObject
135132

136133
// it's not default branch
137134
if repo.DefaultBranch != dbBranch.Name && !dbBranch.IsDeleted {
@@ -142,6 +139,11 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g
142139
}
143140
}
144141

142+
if divergence == nil {
143+
// tolerate error that we can't get divergence
144+
divergence = &git.DivergeObject{Ahead: -1, Behind: -1}
145+
}
146+
145147
pr, err := issues_model.GetLatestPullRequestByHeadInfo(repo.ID, branchName)
146148
if err != nil {
147149
return nil, fmt.Errorf("GetLatestPullRequestByHeadInfo: %v", err)

0 commit comments

Comments
 (0)