## 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>
This commit is contained in:
@@ -4,9 +4,18 @@
|
|||||||
package gitrepo
|
package gitrepo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||||
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockRepository struct {
|
type mockRepository struct {
|
||||||
@@ -17,6 +26,61 @@ func (r *mockRepository) RelativePath() string {
|
|||||||
return r.path
|
return r.path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func commitRootTree(t *testing.T, repoDir, fileName, content, message string) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
require.NoError(t, gitcmd.NewCommand("read-tree", "--empty").WithDir(repoDir).Run(t.Context()))
|
||||||
|
|
||||||
|
stdout, _, err := gitcmd.NewCommand("hash-object", "-w", "--stdin").
|
||||||
|
WithDir(repoDir).
|
||||||
|
WithStdinBytes([]byte(content)).
|
||||||
|
RunStdString(t.Context())
|
||||||
|
require.NoError(t, err)
|
||||||
|
blobSHA := strings.TrimSpace(stdout)
|
||||||
|
|
||||||
|
_, _, err = gitcmd.NewCommand("update-index", "--add", "--replace", "--cacheinfo").
|
||||||
|
AddDynamicArguments("100644", blobSHA, fileName).
|
||||||
|
WithDir(repoDir).
|
||||||
|
RunStdString(t.Context())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
stdout, _, err = gitcmd.NewCommand("write-tree").WithDir(repoDir).RunStdString(t.Context())
|
||||||
|
require.NoError(t, err)
|
||||||
|
treeSHA := strings.TrimSpace(stdout)
|
||||||
|
|
||||||
|
commitTimeStr := time.Now().Format(time.RFC3339)
|
||||||
|
env := append(os.Environ(),
|
||||||
|
"GIT_AUTHOR_NAME=Test",
|
||||||
|
"GIT_AUTHOR_EMAIL=test@example.com",
|
||||||
|
"GIT_AUTHOR_DATE="+commitTimeStr,
|
||||||
|
"GIT_COMMITTER_NAME=Test",
|
||||||
|
"GIT_COMMITTER_EMAIL=test@example.com",
|
||||||
|
"GIT_COMMITTER_DATE="+commitTimeStr,
|
||||||
|
)
|
||||||
|
|
||||||
|
messageBytes := bytes.NewBufferString(message + "\n")
|
||||||
|
stdout, _, err = gitcmd.NewCommand("commit-tree").AddDynamicArguments(treeSHA).
|
||||||
|
WithEnv(env).
|
||||||
|
WithDir(repoDir).
|
||||||
|
WithStdinBytes(messageBytes.Bytes()).
|
||||||
|
RunStdString(t.Context())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
return strings.TrimSpace(stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMergeBaseNoCommonHistory(t *testing.T) {
|
||||||
|
repoDir := filepath.Join(t.TempDir(), "repo.git")
|
||||||
|
require.NoError(t, gitcmd.NewCommand("init").AddDynamicArguments(repoDir).Run(t.Context()))
|
||||||
|
|
||||||
|
baseCommit := commitRootTree(t, repoDir, "base.txt", "base", "base")
|
||||||
|
headCommit := commitRootTree(t, repoDir, "head.txt", "head", "head")
|
||||||
|
|
||||||
|
mergeBase, err := MergeBase(t.Context(), &mockRepository{path: repoDir}, baseCommit, headCommit)
|
||||||
|
assert.Empty(t, mergeBase)
|
||||||
|
assert.ErrorIs(t, err, util.ErrNotExist)
|
||||||
|
}
|
||||||
|
|
||||||
func TestRepoGetDivergingCommits(t *testing.T) {
|
func TestRepoGetDivergingCommits(t *testing.T) {
|
||||||
repo := &mockRepository{path: "repo1_bare"}
|
repo := &mockRepository{path: "repo1_bare"}
|
||||||
do, err := GetDivergingCommits(t.Context(), repo, "master", "branch2")
|
do, err := GetDivergingCommits(t.Context(), repo, "master", "branch2")
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/git/gitcmd"
|
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||||
|
"code.gitea.io/gitea/modules/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MergeBase checks and returns merge base of two commits.
|
// MergeBase checks and returns merge base of two commits.
|
||||||
@@ -16,6 +17,9 @@ func MergeBase(ctx context.Context, repo Repository, baseCommitID, headCommitID
|
|||||||
mergeBase, _, err := RunCmdString(ctx, repo, gitcmd.NewCommand("merge-base").
|
mergeBase, _, err := RunCmdString(ctx, repo, gitcmd.NewCommand("merge-base").
|
||||||
AddDashesAndList(baseCommitID, headCommitID))
|
AddDashesAndList(baseCommitID, headCommitID))
|
||||||
if err != nil {
|
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 "", fmt.Errorf("get merge-base of %s and %s failed: %w", baseCommitID, headCommitID, err)
|
||||||
}
|
}
|
||||||
return strings.TrimSpace(mergeBase), nil
|
return strings.TrimSpace(mergeBase), nil
|
||||||
|
|||||||
@@ -1781,6 +1781,7 @@
|
|||||||
"repo.pulls.review_only_possible_for_full_diff": "Review is only possible when viewing the full diff",
|
"repo.pulls.review_only_possible_for_full_diff": "Review is only possible when viewing the full diff",
|
||||||
"repo.pulls.filter_changes_by_commit": "Filter by commit",
|
"repo.pulls.filter_changes_by_commit": "Filter by commit",
|
||||||
"repo.pulls.nothing_to_compare": "These branches are equal. There is no need to create a pull request.",
|
"repo.pulls.nothing_to_compare": "These branches are equal. There is no need to create a pull request.",
|
||||||
|
"repo.pulls.no_common_history": "These branches do not share a common merge base. Select a different base or compare branch.",
|
||||||
"repo.pulls.nothing_to_compare_have_tag": "The selected branches/tags are equal.",
|
"repo.pulls.nothing_to_compare_have_tag": "The selected branches/tags are equal.",
|
||||||
"repo.pulls.nothing_to_compare_and_allow_empty_pr": "These branches are equal. This PR will be empty.",
|
"repo.pulls.nothing_to_compare_and_allow_empty_pr": "These branches are equal. This PR will be empty.",
|
||||||
"repo.pulls.has_pull_request": "A pull request between these branches already exists: <a href=\"%[1]s\">%[2]s#%[3]d</a>",
|
"repo.pulls.has_pull_request": "A pull request between these branches already exists: <a href=\"%[1]s\">%[2]s#%[3]d</a>",
|
||||||
|
|||||||
+21
-13
@@ -413,6 +413,10 @@ func ParseCompareInfo(ctx *context.Context) *git_service.CompareInfo {
|
|||||||
|
|
||||||
compareInfo, err := git_service.GetCompareInfo(ctx, baseRepo, headRepo, headGitRepo, baseRef, headRef, compareReq.DirectComparison(), fileOnly)
|
compareInfo, err := git_service.GetCompareInfo(ctx, baseRepo, headRepo, headGitRepo, baseRef, headRef, compareReq.DirectComparison(), fileOnly)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, util.ErrNotExist) {
|
||||||
|
ctx.Data["IsNoMergeBase"] = true
|
||||||
|
return compareInfo
|
||||||
|
}
|
||||||
ctx.ServerError("GetCompareInfo", err)
|
ctx.ServerError("GetCompareInfo", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -603,9 +607,18 @@ func CompareDiff(ctx *context.Context) {
|
|||||||
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
|
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
|
||||||
ctx.Data["CompareInfo"] = ci
|
ctx.Data["CompareInfo"] = ci
|
||||||
|
|
||||||
nothingToCompare := PrepareCompareDiff(ctx, ci, gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
|
var nothingToCompare bool
|
||||||
if ctx.Written() {
|
noMergeBase := ctx.Data["IsNoMergeBase"] == true
|
||||||
return
|
if noMergeBase {
|
||||||
|
ctx.Flash.Error(ctx.Tr("repo.pulls.no_common_history"), true)
|
||||||
|
ctx.Data["PageIsComparePull"] = false
|
||||||
|
ctx.Data["CommitCount"] = 0
|
||||||
|
nothingToCompare = true
|
||||||
|
} else {
|
||||||
|
nothingToCompare = PrepareCompareDiff(ctx, ci, gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
|
||||||
|
if ctx.Written() {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
baseTags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
|
baseTags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
|
||||||
@@ -621,16 +634,13 @@ func CompareDiff(ctx *context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
headBranches, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{
|
headBranches, headTags, err := getBranchesAndTagsForRepo(ctx, ci.HeadRepo)
|
||||||
RepoID: ci.HeadRepo.ID,
|
|
||||||
ListOptions: db.ListOptionsAll,
|
|
||||||
IsDeletedBranch: optional.Some(false),
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("GetBranches", err)
|
ctx.ServerError("GetBranchesAndTagsForRepo", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Data["HeadBranches"] = headBranches
|
ctx.Data["HeadBranches"] = headBranches
|
||||||
|
ctx.Data["HeadTags"] = headTags
|
||||||
|
|
||||||
// For compare repo branches
|
// For compare repo branches
|
||||||
PrepareBranchList(ctx)
|
PrepareBranchList(ctx)
|
||||||
@@ -638,12 +648,10 @@ func CompareDiff(ctx *context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
headTags, err := repo_model.GetTagNamesByRepoID(ctx, ci.HeadRepo.ID)
|
if noMergeBase {
|
||||||
if err != nil {
|
ctx.HTML(http.StatusOK, tplCompare)
|
||||||
ctx.ServerError("GetTagNamesByRepoID", err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Data["HeadTags"] = headTags
|
|
||||||
|
|
||||||
if ctx.Data["PageIsComparePull"] == true {
|
if ctx.Data["PageIsComparePull"] == true {
|
||||||
pr, err := issues_model.GetUnmergedPullRequest(ctx, ci.HeadRepo.ID, ctx.Repo.Repository.ID, ci.HeadRef.ShortName(), ci.BaseRef.ShortName(), issues_model.PullRequestFlowGithub)
|
pr, err := issues_model.GetUnmergedPullRequest(ctx, ci.HeadRepo.ID, ctx.Repo.Repository.ID, ci.HeadRef.ShortName(), ci.BaseRef.ShortName(), issues_model.PullRequestFlowGithub)
|
||||||
|
|||||||
@@ -1366,6 +1366,10 @@ func CompareAndPullRequestPost(ctx *context.Context) {
|
|||||||
if ctx.Written() {
|
if ctx.Written() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if ctx.Data["IsNoMergeBase"] == true {
|
||||||
|
ctx.JSONError(ctx.Tr("repo.pulls.no_common_history"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
validateRet := ValidateRepoMetasForNewIssue(ctx, *form, true)
|
validateRet := ValidateRepoMetasForNewIssue(ctx, *form, true)
|
||||||
if ctx.Written() {
|
if ctx.Written() {
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ func GetCompareInfo(ctx context.Context, baseRepo, headRepo *repo_model.Reposito
|
|||||||
if !directComparison {
|
if !directComparison {
|
||||||
compareInfo.MergeBase, err = gitrepo.MergeBase(ctx, headRepo, compareInfo.BaseCommitID, compareInfo.HeadCommitID)
|
compareInfo.MergeBase, err = gitrepo.MergeBase(ctx, headRepo, compareInfo.BaseCommitID, compareInfo.HeadCommitID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("MergeBase: %w", err)
|
return compareInfo, fmt.Errorf("MergeBase: %w", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
compareInfo.MergeBase = compareInfo.BaseCommitID
|
compareInfo.MergeBase = compareInfo.BaseCommitID
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
{{ctx.Locale.Tr "action.compare_commits_general"}}
|
{{ctx.Locale.Tr "action.compare_commits_general"}}
|
||||||
{{end}}
|
{{end}}
|
||||||
</h2>
|
</h2>
|
||||||
|
{{template "base/alert" .}}
|
||||||
{{$BaseCompareName := $.Repository.FullName -}}
|
{{$BaseCompareName := $.Repository.FullName -}}
|
||||||
{{$HeadCompareName := $.HeadRepo.FullName -}}
|
{{$HeadCompareName := $.HeadRepo.FullName -}}
|
||||||
{{$OwnForkCompareName := "" -}}
|
{{$OwnForkCompareName := "" -}}
|
||||||
|
|||||||
@@ -4,19 +4,25 @@
|
|||||||
package integration
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
"code.gitea.io/gitea/models/unittest"
|
"code.gitea.io/gitea/models/unittest"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||||
"code.gitea.io/gitea/modules/test"
|
"code.gitea.io/gitea/modules/test"
|
||||||
repo_service "code.gitea.io/gitea/services/repository"
|
repo_service "code.gitea.io/gitea/services/repository"
|
||||||
"code.gitea.io/gitea/tests"
|
"code.gitea.io/gitea/tests"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCompareTag(t *testing.T) {
|
func TestCompareTag(t *testing.T) {
|
||||||
@@ -124,6 +130,76 @@ func TestCompareBranches(t *testing.T) {
|
|||||||
inspectCompare(t, htmlDoc, diffCount, diffChanges)
|
inspectCompare(t, htmlDoc, diffCount, diffChanges)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createUnrelatedBranch(t *testing.T, repo *repo_model.Repository, user *user_model.User, branchName string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
repoPath := repo_model.RepoPath(user.Name, repo.Name)
|
||||||
|
require.NoError(t, gitcmd.NewCommand("read-tree", "--empty").WithDir(repoPath).Run(t.Context()))
|
||||||
|
|
||||||
|
stdout, _, err := gitcmd.NewCommand("hash-object", "-w", "--stdin").
|
||||||
|
WithDir(repoPath).
|
||||||
|
WithStdinBytes([]byte("Unrelated File")).
|
||||||
|
RunStdString(t.Context())
|
||||||
|
require.NoError(t, err)
|
||||||
|
blobSHA := strings.TrimSpace(stdout)
|
||||||
|
|
||||||
|
_, _, err = gitcmd.NewCommand("update-index", "--add", "--replace", "--cacheinfo").
|
||||||
|
AddDynamicArguments("100644", blobSHA, "unrelated.txt").
|
||||||
|
WithDir(repoPath).
|
||||||
|
RunStdString(t.Context())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
stdout, _, err = gitcmd.NewCommand("write-tree").WithDir(repoPath).RunStdString(t.Context())
|
||||||
|
require.NoError(t, err)
|
||||||
|
treeSHA := strings.TrimSpace(stdout)
|
||||||
|
|
||||||
|
commitTimeStr := time.Now().Format(time.RFC3339)
|
||||||
|
doerSig := user.NewGitSig()
|
||||||
|
env := append(os.Environ(),
|
||||||
|
"GIT_AUTHOR_NAME="+doerSig.Name,
|
||||||
|
"GIT_AUTHOR_EMAIL="+doerSig.Email,
|
||||||
|
"GIT_AUTHOR_DATE="+commitTimeStr,
|
||||||
|
"GIT_COMMITTER_NAME="+doerSig.Name,
|
||||||
|
"GIT_COMMITTER_EMAIL="+doerSig.Email,
|
||||||
|
"GIT_COMMITTER_DATE="+commitTimeStr,
|
||||||
|
)
|
||||||
|
|
||||||
|
messageBytes := bytes.NewBufferString("Unrelated\n")
|
||||||
|
stdout, _, err = gitcmd.NewCommand("commit-tree").AddDynamicArguments(treeSHA).
|
||||||
|
WithEnv(env).
|
||||||
|
WithDir(repoPath).
|
||||||
|
WithStdinBytes(messageBytes.Bytes()).
|
||||||
|
RunStdString(t.Context())
|
||||||
|
require.NoError(t, err)
|
||||||
|
commitSHA := strings.TrimSpace(stdout)
|
||||||
|
|
||||||
|
_, _, err = gitcmd.NewCommand("branch").AddDynamicArguments(branchName, commitSHA).
|
||||||
|
WithDir(repoPath).
|
||||||
|
RunStdString(t.Context())
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCompareBranchesNoCommonMergeBase(t *testing.T) {
|
||||||
|
defer tests.PrepareTestEnv(t)()
|
||||||
|
|
||||||
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"})
|
||||||
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user2.ID, Name: "repo1"})
|
||||||
|
createUnrelatedBranch(t, repo1, user2, "unrelated-history")
|
||||||
|
|
||||||
|
session := loginUser(t, "user2")
|
||||||
|
req := NewRequest(t, "GET", "/user2/repo1/compare/master...unrelated-history")
|
||||||
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||||
|
body := resp.Body.String()
|
||||||
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||||
|
|
||||||
|
selection := htmlDoc.doc.Find(".ui.dropdown.select-branch")
|
||||||
|
assert.Lenf(t, selection.Nodes, 2, "The template has changed")
|
||||||
|
assert.Contains(t, body, "These branches do not share a common merge base")
|
||||||
|
assert.Equal(t, 1, htmlDoc.doc.Find(`a.item[href="/user2/repo1/compare/master...unrelated-history"]`).Length())
|
||||||
|
assert.Equal(t, 1, htmlDoc.doc.Find(`a.item[href="/user2/repo1/compare/master...master"]`).Length())
|
||||||
|
assert.Equal(t, 0, htmlDoc.doc.Find(".pullrequest-form").Length())
|
||||||
|
}
|
||||||
|
|
||||||
func TestCompareCodeExpand(t *testing.T) {
|
func TestCompareCodeExpand(t *testing.T) {
|
||||||
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
||||||
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||||
|
|||||||
Reference in New Issue
Block a user