Fix loadOneBranch panic (#29938) (#29939)

Backport #29938

Try to fix #29936

Far from ideal, but still better than panic.
pull/29946/head
wxiaoguang 8 months ago committed by GitHub
parent 3fd15aeff2
commit b4a6c6fd7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      modules/git/repo.go
  2. 10
      services/repository/branch.go

@ -248,7 +248,7 @@ type DivergeObject struct {
// GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch // 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) { func GetDivergingCommits(ctx context.Context, repoPath, baseBranch, targetBranch string) (do DivergeObject, err error) {
cmd := NewCommand(ctx, "rev-list", "--count", "--left-right"). cmd := NewCommand(ctx, "rev-list", "--count", "--left-right").
AddDynamicArguments(baseBranch + "..." + targetBranch) AddDynamicArguments(baseBranch + "..." + targetBranch).AddArguments("--")
stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath}) stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath})
if err != nil { if err != nil {
return do, err return do, err

@ -128,10 +128,7 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g
p := protectedBranches.GetFirstMatched(branchName) p := protectedBranches.GetFirstMatched(branchName)
isProtected := p != nil isProtected := p != nil
divergence := &git.DivergeObject{ var divergence *git.DivergeObject
Ahead: -1,
Behind: -1,
}
// it's not default branch // it's not default branch
if repo.DefaultBranch != dbBranch.Name && !dbBranch.IsDeleted { 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) pr, err := issues_model.GetLatestPullRequestByHeadInfo(repo.ID, branchName)
if err != nil { if err != nil {
return nil, fmt.Errorf("GetLatestPullRequestByHeadInfo: %v", err) return nil, fmt.Errorf("GetLatestPullRequestByHeadInfo: %v", err)

Loading…
Cancel
Save