From 2d29e4a971a69dfa79946e67cbbd04b83a9e71a9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 14 Dec 2021 10:30:21 +0800 Subject: [PATCH 1/3] Support open compare page directly --- routers/web/repo/compare.go | 30 +++++++++++++++++------------- routers/web/web.go | 1 + 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 8d08fec8fdb8..2f23ff846833 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -215,21 +215,25 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { ) infoPath = ctx.Params("*") - infos := strings.SplitN(infoPath, "...", 2) - - if len(infos) != 2 { - infos = []string{baseRepo.DefaultBranch, infoPath} - if strings.Contains(infoPath, "..") { - infos = strings.SplitN(infoPath, "..", 2) - ci.DirectComparison = true - ctx.Data["PageIsComparePull"] = false + var infos []string + if infoPath == "" { + infos = []string{ctx.Repo.Repository.DefaultBranch, ctx.Repo.Repository.DefaultBranch} + } else { + infos = strings.SplitN(infoPath, "...", 2) + if len(infos) != 2 { + infos = []string{baseRepo.DefaultBranch, infoPath} + if strings.Contains(infoPath, "..") { + infos = strings.SplitN(infoPath, "..", 2) + ci.DirectComparison = true + ctx.Data["PageIsComparePull"] = false + } } - } - if len(infos) != 2 { - log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos) - ctx.NotFound("CompareAndPullRequest", nil) - return nil + if len(infos) != 2 { + log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos) + ctx.NotFound("CompareAndPullRequest", nil) + return nil + } } ctx.Data["BaseName"] = baseRepo.OwnerName diff --git a/routers/web/web.go b/routers/web/web.go index 0d4d3bd90f4f..8e87023387a6 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -702,6 +702,7 @@ func RegisterRoutes(m *web.Route) { m.Group("/milestone", func() { m.Get("/{id}", repo.MilestoneIssuesAndPulls) }, reqRepoIssuesOrPullsReader, context.RepoRef()) + m.Get("/compare", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists, ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff) m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists). Get(ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff). Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(forms.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost) From f75b87dd29ac3b9e8563a8ed5a45a517b46d49b6 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 14 Dec 2021 10:39:06 +0800 Subject: [PATCH 2/3] simple code --- routers/web/repo/compare.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 2f23ff846833..fd5050792889 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -217,7 +217,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { infoPath = ctx.Params("*") var infos []string if infoPath == "" { - infos = []string{ctx.Repo.Repository.DefaultBranch, ctx.Repo.Repository.DefaultBranch} + infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { infos = strings.SplitN(infoPath, "...", 2) if len(infos) != 2 { From 36c8ce7eeb9702a9c2f532b1424f124b3c04b32d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 16 Dec 2021 20:49:57 +0800 Subject: [PATCH 3/3] Some improvements --- routers/web/repo/compare.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index fd5050792889..f6eb2ef8ebe7 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -221,19 +221,13 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { } else { infos = strings.SplitN(infoPath, "...", 2) if len(infos) != 2 { - infos = []string{baseRepo.DefaultBranch, infoPath} - if strings.Contains(infoPath, "..") { - infos = strings.SplitN(infoPath, "..", 2) + if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 { ci.DirectComparison = true ctx.Data["PageIsComparePull"] = false + } else { + infos = []string{baseRepo.DefaultBranch, infoPath} } } - - if len(infos) != 2 { - log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos) - ctx.NotFound("CompareAndPullRequest", nil) - return nil - } } ctx.Data["BaseName"] = baseRepo.OwnerName