Skip to content

Commit

Permalink
Fix loadOneBranch panic (go-gitea#29938)
Browse files Browse the repository at this point in the history
Try to fix go-gitea#29936

Far from ideal, but still better than panic.
  • Loading branch information
wxiaoguang authored Mar 20, 2024
1 parent bc55a80 commit 2115147
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions services/repository/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,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 {
Expand All @@ -180,6 +177,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)
Expand Down

0 comments on commit 2115147

Please sign in to comment.