mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-20 12:01:07 +00:00
## Summary - handle compare requests where base and head refs have no common merge base without returning 500 - keep the compare branch selectors usable and show a clear warning message - add regression coverage for unrelated-history compare selection and merge-base error detection Fixes #37469 Manuel Backport of: https://github.com/go-gitea/gitea/pull/37470 --------- Co-authored-by: Codex <codex@openai.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
27 lines
828 B
Go
27 lines
828 B
Go
// Copyright 2025 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package gitrepo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/modules/git/gitcmd"
|
|
"code.gitea.io/gitea/modules/util"
|
|
)
|
|
|
|
// MergeBase checks and returns merge base of two commits.
|
|
func MergeBase(ctx context.Context, repo Repository, baseCommitID, headCommitID string) (string, error) {
|
|
mergeBase, _, err := RunCmdString(ctx, repo, gitcmd.NewCommand("merge-base").
|
|
AddDashesAndList(baseCommitID, headCommitID))
|
|
if err != nil {
|
|
if gitcmd.IsErrorExitCode(err, 1) {
|
|
return "", util.NewNotExistErrorf("get merge-base of %s and %s failed", baseCommitID, headCommitID)
|
|
}
|
|
return "", fmt.Errorf("get merge-base of %s and %s failed: %w", baseCommitID, headCommitID, err)
|
|
}
|
|
return strings.TrimSpace(mergeBase), nil
|
|
}
|