improve the compare page (#36261)
- The compare page head title should be `compare` but not `new pull request`. - Use `UnstableGuessRefByShortName` instead of duplicated functions calls. - Direct-compare, tags, commits compare will not display `New Pull Request` button any more. The new screenshot <img width="1459" height="391" alt="image" src="https://github.com/user-attachments/assets/64e9b070-9c0b-41d1-b4b8-233b96270e1b" /> --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
+28
-44
@@ -9,31 +9,28 @@ import (
|
||||
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
type CompareRouterReq struct {
|
||||
BaseOriRef string
|
||||
BaseOriRef string
|
||||
BaseOriRefSuffix string
|
||||
|
||||
CompareSeparator string
|
||||
|
||||
HeadOwner string
|
||||
HeadRepoName string
|
||||
HeadOriRef string
|
||||
CaretTimes int // ^ times after base ref
|
||||
DotTimes int
|
||||
}
|
||||
|
||||
func (cr *CompareRouterReq) DirectComparison() bool {
|
||||
return cr.DotTimes == 2 || cr.DotTimes == 0
|
||||
// FIXME: the design of "DirectComparison" is wrong, it loses the information of `^`
|
||||
// To correctly handle the comparison, developers should use `ci.CompareSeparator` directly, all "DirectComparison" related code should be rewritten.
|
||||
return cr.CompareSeparator == ".."
|
||||
}
|
||||
|
||||
func parseBase(base string) (string, int) {
|
||||
parts := strings.SplitN(base, "^", 2)
|
||||
if len(parts) == 1 {
|
||||
return base, 0
|
||||
}
|
||||
return parts[0], len(parts[1]) + 1
|
||||
}
|
||||
|
||||
func parseHead(head string) (string, string, string) {
|
||||
func parseHead(head string) (headOwnerName, headRepoName, headRef string) {
|
||||
paths := strings.SplitN(head, ":", 2)
|
||||
if len(paths) == 1 {
|
||||
return "", "", paths[0]
|
||||
@@ -48,6 +45,7 @@ func parseHead(head string) (string, string, string) {
|
||||
// ParseCompareRouterParam Get compare information from the router parameter.
|
||||
// A full compare url is of the form:
|
||||
//
|
||||
// 0. /{:baseOwner}/{:baseRepoName}/compare
|
||||
// 1. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headBranch}
|
||||
// 2. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}:{:headBranch}
|
||||
// 3. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}/{:headRepoName}:{:headBranch}
|
||||
@@ -70,45 +68,31 @@ func parseHead(head string) (string, string, string) {
|
||||
// format: <base branch>...[<head repo>:]<head branch>
|
||||
// base<-head: master...head:feature
|
||||
// same repo: master...feature
|
||||
func ParseCompareRouterParam(routerParam string) (*CompareRouterReq, error) {
|
||||
func ParseCompareRouterParam(routerParam string) *CompareRouterReq {
|
||||
if routerParam == "" {
|
||||
return &CompareRouterReq{}, nil
|
||||
return &CompareRouterReq{}
|
||||
}
|
||||
|
||||
var basePart, headPart string
|
||||
dotTimes := 3
|
||||
parts := strings.Split(routerParam, "...")
|
||||
if len(parts) > 2 {
|
||||
return nil, util.NewInvalidArgumentErrorf("invalid compare router: %s", routerParam)
|
||||
}
|
||||
if len(parts) != 2 {
|
||||
parts = strings.Split(routerParam, "..")
|
||||
if len(parts) == 1 {
|
||||
sep := "..."
|
||||
basePart, headPart, ok := strings.Cut(routerParam, sep)
|
||||
if !ok {
|
||||
sep = ".."
|
||||
basePart, headPart, ok = strings.Cut(routerParam, sep)
|
||||
if !ok {
|
||||
headOwnerName, headRepoName, headRef := parseHead(routerParam)
|
||||
return &CompareRouterReq{
|
||||
HeadOriRef: headRef,
|
||||
HeadOwner: headOwnerName,
|
||||
HeadRepoName: headRepoName,
|
||||
DotTimes: dotTimes,
|
||||
}, nil
|
||||
} else if len(parts) > 2 {
|
||||
return nil, util.NewInvalidArgumentErrorf("invalid compare router: %s", routerParam)
|
||||
HeadOriRef: headRef,
|
||||
HeadOwner: headOwnerName,
|
||||
HeadRepoName: headRepoName,
|
||||
CompareSeparator: "...",
|
||||
}
|
||||
}
|
||||
dotTimes = 2
|
||||
}
|
||||
basePart, headPart = parts[0], parts[1]
|
||||
|
||||
baseRef, caretTimes := parseBase(basePart)
|
||||
headOwnerName, headRepoName, headRef := parseHead(headPart)
|
||||
|
||||
return &CompareRouterReq{
|
||||
BaseOriRef: baseRef,
|
||||
HeadOriRef: headRef,
|
||||
HeadOwner: headOwnerName,
|
||||
HeadRepoName: headRepoName,
|
||||
CaretTimes: caretTimes,
|
||||
DotTimes: dotTimes,
|
||||
}, nil
|
||||
ci := &CompareRouterReq{CompareSeparator: sep}
|
||||
ci.BaseOriRef, ci.BaseOriRefSuffix = git.ParseRefSuffix(basePart)
|
||||
ci.HeadOwner, ci.HeadRepoName, ci.HeadOriRef = parseHead(headPart)
|
||||
return ci
|
||||
}
|
||||
|
||||
// maxForkTraverseLevel defines the maximum levels to traverse when searching for the head repository.
|
||||
|
||||
@@ -6,146 +6,100 @@ package common
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCompareRouterReq(t *testing.T) {
|
||||
unittest.PrepareTestEnv(t)
|
||||
|
||||
kases := []struct {
|
||||
router string
|
||||
cases := []struct {
|
||||
input string
|
||||
CompareRouterReq *CompareRouterReq
|
||||
}{
|
||||
{
|
||||
router: "",
|
||||
input: "",
|
||||
CompareRouterReq: &CompareRouterReq{},
|
||||
},
|
||||
{
|
||||
input: "v1.0...v1.1",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
BaseOriRef: "",
|
||||
HeadOriRef: "",
|
||||
DotTimes: 0,
|
||||
BaseOriRef: "v1.0",
|
||||
CompareSeparator: "...",
|
||||
HeadOriRef: "v1.1",
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "main...develop",
|
||||
input: "main..develop",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
BaseOriRef: "main",
|
||||
HeadOriRef: "develop",
|
||||
DotTimes: 3,
|
||||
BaseOriRef: "main",
|
||||
CompareSeparator: "..",
|
||||
HeadOriRef: "develop",
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "main..develop",
|
||||
input: "main^...develop",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
BaseOriRef: "main",
|
||||
HeadOriRef: "develop",
|
||||
DotTimes: 2,
|
||||
BaseOriRef: "main",
|
||||
BaseOriRefSuffix: "^",
|
||||
CompareSeparator: "...",
|
||||
HeadOriRef: "develop",
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "main^...develop",
|
||||
input: "main^^^^^...develop",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
BaseOriRef: "main",
|
||||
HeadOriRef: "develop",
|
||||
CaretTimes: 1,
|
||||
DotTimes: 3,
|
||||
BaseOriRef: "main",
|
||||
BaseOriRefSuffix: "^^^^^",
|
||||
CompareSeparator: "...",
|
||||
HeadOriRef: "develop",
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "main^^^^^...develop",
|
||||
input: "develop",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
BaseOriRef: "main",
|
||||
HeadOriRef: "develop",
|
||||
CaretTimes: 5,
|
||||
DotTimes: 3,
|
||||
CompareSeparator: "...",
|
||||
HeadOriRef: "develop",
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "develop",
|
||||
input: "teabot:feature1",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
HeadOriRef: "develop",
|
||||
DotTimes: 3,
|
||||
CompareSeparator: "...",
|
||||
HeadOwner: "teabot",
|
||||
HeadOriRef: "feature1",
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "lunny/forked_repo:develop",
|
||||
input: "lunny/forked_repo:develop",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
HeadOwner: "lunny",
|
||||
HeadRepoName: "forked_repo",
|
||||
HeadOriRef: "develop",
|
||||
DotTimes: 3,
|
||||
CompareSeparator: "...",
|
||||
HeadOwner: "lunny",
|
||||
HeadRepoName: "forked_repo",
|
||||
HeadOriRef: "develop",
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "main...lunny/forked_repo:develop",
|
||||
input: "main...lunny/forked_repo:develop",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
BaseOriRef: "main",
|
||||
HeadOwner: "lunny",
|
||||
HeadRepoName: "forked_repo",
|
||||
HeadOriRef: "develop",
|
||||
DotTimes: 3,
|
||||
BaseOriRef: "main",
|
||||
CompareSeparator: "...",
|
||||
HeadOwner: "lunny",
|
||||
HeadRepoName: "forked_repo",
|
||||
HeadOriRef: "develop",
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "main...lunny/forked_repo:develop",
|
||||
input: "main^...lunny/forked_repo:develop",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
BaseOriRef: "main",
|
||||
HeadOwner: "lunny",
|
||||
HeadRepoName: "forked_repo",
|
||||
HeadOriRef: "develop",
|
||||
DotTimes: 3,
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "main^...lunny/forked_repo:develop",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
BaseOriRef: "main",
|
||||
HeadOwner: "lunny",
|
||||
HeadRepoName: "forked_repo",
|
||||
HeadOriRef: "develop",
|
||||
DotTimes: 3,
|
||||
CaretTimes: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "v1.0...v1.1",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
BaseOriRef: "v1.0",
|
||||
HeadOriRef: "v1.1",
|
||||
DotTimes: 3,
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "teabot-patch-1...v0.0.1",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
BaseOriRef: "teabot-patch-1",
|
||||
HeadOriRef: "v0.0.1",
|
||||
DotTimes: 3,
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "teabot:feature1",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
HeadOwner: "teabot",
|
||||
HeadOriRef: "feature1",
|
||||
DotTimes: 3,
|
||||
},
|
||||
},
|
||||
{
|
||||
router: "8eb19a5ae19abae15c0666d4ab98906139a7f439...283c030497b455ecfa759d4649f9f8b45158742e",
|
||||
CompareRouterReq: &CompareRouterReq{
|
||||
BaseOriRef: "8eb19a5ae19abae15c0666d4ab98906139a7f439",
|
||||
HeadOriRef: "283c030497b455ecfa759d4649f9f8b45158742e",
|
||||
DotTimes: 3,
|
||||
BaseOriRef: "main",
|
||||
BaseOriRefSuffix: "^",
|
||||
CompareSeparator: "...",
|
||||
HeadOwner: "lunny",
|
||||
HeadRepoName: "forked_repo",
|
||||
HeadOriRef: "develop",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, kase := range kases {
|
||||
t.Run(kase.router, func(t *testing.T) {
|
||||
r, err := ParseCompareRouterParam(kase.router)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, kase.CompareRouterReq, r)
|
||||
})
|
||||
for _, c := range cases {
|
||||
assert.Equal(t, c.CompareRouterReq, ParseCompareRouterParam(c.input), "input: %s", c.input)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user