From cf3145a4fea7ec34a6bdbf26a44e6f95b958c82c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 3 Jan 2025 12:41:20 -0800 Subject: [PATCH] Some improvements --- routers/api/v1/repo/compare.go | 3 +++ routers/api/v1/repo/pull.go | 2 ++ routers/common/compare.go | 4 ++++ routers/common/compare_test.go | 20 ++++++++++++++++++++ routers/web/repo/compare.go | 5 +---- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/routers/api/v1/repo/compare.go b/routers/api/v1/repo/compare.go index 33a04723a7..4154727aae 100644 --- a/routers/api/v1/repo/compare.go +++ b/routers/api/v1/repo/compare.go @@ -11,6 +11,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" @@ -69,6 +70,8 @@ func CompareDiff(ctx *context.APIContext) { ctx.NotFound("GetRepositoryByOwnerAndName") case errors.Is(err, util.ErrInvalidArgument): ctx.NotFound("ParseComparePathParams") + case git.IsErrNotExist(err): + ctx.NotFound("ParseComparePathParams") default: ctx.ServerError("GetRepositoryByOwnerAndName", err) } diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index a101526093..5a80c93c8e 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -417,6 +417,8 @@ func CreatePullRequest(ctx *context.APIContext) { ctx.NotFound("GetRepositoryByOwnerAndName") case errors.Is(err, util.ErrInvalidArgument): ctx.NotFound("ParseComparePathParams") + case git.IsErrNotExist(err): + ctx.NotFound("ParseComparePathParams") default: ctx.ServerError("GetRepositoryByOwnerAndName", err) } diff --git a/routers/common/compare.go b/routers/common/compare.go index e73bd077c9..98e2082b0c 100644 --- a/routers/common/compare.go +++ b/routers/common/compare.go @@ -30,6 +30,10 @@ func (cr *CompareRouter) DirectComparison() bool { return cr.DotTimes == 2 } +func (cr *CompareRouter) CompareDots() string { + return strings.Repeat(".", cr.DotTimes) +} + func parseBase(base string) (string, int) { parts := strings.SplitN(base, "^", 2) if len(parts) == 1 { diff --git a/routers/common/compare_test.go b/routers/common/compare_test.go index 2c7178be12..3469e424a3 100644 --- a/routers/common/compare_test.go +++ b/routers/common/compare_test.go @@ -414,6 +414,26 @@ func Test_ParseComparePathParams(t *testing.T) { IsHeadCommit: true, }, }, + { + repoName: "repo1", + hasClose: true, + router: "user12/repo10:master", + compareInfo: &CompareInfo{ + CompareRouter: &CompareRouter{ + BaseOriRef: repo11.DefaultBranch, + BaseFullRef: git.RefNameFromBranch(repo11.DefaultBranch), + HeadOwnerName: "user12", + HeadRepoName: "repo10", + HeadOriRef: "master", + HeadFullRef: git.RefNameFromBranch("master"), + DotTimes: 3, + }, + BaseRepo: repo1, + HeadUser: repo10.Owner, + HeadRepo: repo10, + HeadGitRepo: gitRepo10, + }, + }, } for _, kase := range kases { diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 9e93ada5eb..37d286f43f 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -554,11 +554,8 @@ func CompareDiff(ctx *context.Context) { } beforeCommitID := ctx.Data["BeforeCommitID"].(string) afterCommitID := ctx.Data["AfterCommitID"].(string) + separator := ci.CompareDots() - separator := "..." - if ci.DirectComparison() { - separator = ".." - } ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + separator + base.ShortSha(afterCommitID) ctx.Data["IsDiffCompare"] = true