mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-08 22:13:26 +09:00
fix(pull): name the head repository in default compare links (#39075)
The "New Pull Request" buttons and the `/pulls/new/{branch}` redirect
build their compare link as `{owner}:{branch}`. If a fork and its parent
share an owner, through ALLOW_FORK_INTO_SAME_OWNER, or after a transfer,
that head resolves back to the base repo, so the link compares the base
against itself and 404s on a branch that only exists in the fork.
Switching to `{owner}/{repo}:{branch}` names the head repo
unambiguously, and it's what the compare page's own links already use.
Also clears the 404 in #37649; the archived-parent half of that report
is separate.
This commit is contained in:
@@ -1308,7 +1308,7 @@ func PullsNewRedirect(ctx *context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
redirectRepo = repo.BaseRepo
|
redirectRepo = repo.BaseRepo
|
||||||
branch = fmt.Sprintf("%s:%s", repo.OwnerName, branch)
|
branch = context.CompareHeadRef(repo, branch)
|
||||||
}
|
}
|
||||||
ctx.Redirect(fmt.Sprintf("%s/compare/%s...%s?expand=1", redirectRepo.Link(), util.PathEscapeSegments(redirectRepo.DefaultBranch), util.PathEscapeSegments(branch)))
|
ctx.Redirect(fmt.Sprintf("%s/compare/%s...%s?expand=1", redirectRepo.Link(), util.PathEscapeSegments(redirectRepo.DefaultBranch), util.PathEscapeSegments(branch)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,11 +64,15 @@ func (prc *PullRequestContext) CanCreateNewPull() bool {
|
|||||||
return can
|
return can
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CompareHeadRef formats the head side of a compare link, "owner/repo:branch" is only needed when a fork can share its base repo's owner
|
||||||
|
func CompareHeadRef(headRepo *repo_model.Repository, headBranch string) string {
|
||||||
|
return util.Iif(setting.Repository.AllowForkIntoSameOwner, headRepo.FullName(), headRepo.OwnerName) + ":" + headBranch
|
||||||
|
}
|
||||||
|
|
||||||
func (prc *PullRequestContext) MakeDefaultCompareLink(headBranch string) string {
|
func (prc *PullRequestContext) MakeDefaultCompareLink(headBranch string) string {
|
||||||
return prc.baseRepo.Link() + "/compare/" +
|
return prc.baseRepo.Link() + "/compare/" +
|
||||||
util.PathEscapeSegments(prc.DefaultTargetBranch()) + "..." +
|
util.PathEscapeSegments(prc.DefaultTargetBranch()) + "..." +
|
||||||
util.Iif(prc.SameRepo(), "", util.PathEscapeSegments(prc.headRepo.OwnerName)+":") +
|
util.PathEscapeSegments(util.Iif(prc.SameRepo(), headBranch, CompareHeadRef(prc.headRepo, headBranch)))
|
||||||
util.PathEscapeSegments(headBranch)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (prc *PullRequestContext) DefaultTargetBranch() string {
|
func (prc *PullRequestContext) DefaultTargetBranch() string {
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
repo_model "gitea.dev/models/repo"
|
||||||
|
"gitea.dev/modules/setting"
|
||||||
|
"gitea.dev/modules/test"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCompareHeadRef(t *testing.T) {
|
||||||
|
defer test.MockVariableValue(&setting.Repository.AllowForkIntoSameOwner)()
|
||||||
|
headRepo := &repo_model.Repository{OwnerName: "user", Name: "fork"}
|
||||||
|
|
||||||
|
setting.Repository.AllowForkIntoSameOwner = false
|
||||||
|
assert.Equal(t, "user:my-branch", CompareHeadRef(headRepo, "my-branch"))
|
||||||
|
|
||||||
|
setting.Repository.AllowForkIntoSameOwner = true
|
||||||
|
assert.Equal(t, "user/fork:my-branch", CompareHeadRef(headRepo, "my-branch"))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user