diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 41ad365ce1..d216403b53 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -231,13 +231,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { if infoPath == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { - infoPath, isDiff := strings.CutSuffix(infoPath, ".diff") - ctx.Data["ComparingDiff"] = isDiff - if !isDiff { - var isPatch bool - infoPath, isPatch = strings.CutSuffix(infoPath, ".patch") - ctx.Data["ComparingPatch"] = isPatch - } infos = strings.SplitN(infoPath, "...", 2) if len(infos) != 2 { if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 { @@ -247,6 +240,14 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { infos = []string{baseRepo.DefaultBranch, infoPath} } } + var isDiff bool + infos[1], isDiff = strings.CutSuffix(infos[1], ".diff") + ctx.Data["ComparingDiff"] = isDiff + if !isDiff { + var isPatch bool + infos[1], isPatch = strings.CutSuffix(infos[1], ".patch") + ctx.Data["ComparingPatch"] = isPatch + } } ctx.Data["BaseName"] = baseRepo.OwnerName diff --git a/tests/gitea-repositories-meta/user2/repo16.git/refs/heads/diff b/tests/gitea-repositories-meta/user2/repo16.git/refs/heads/diff new file mode 100644 index 0000000000..4750a76432 --- /dev/null +++ b/tests/gitea-repositories-meta/user2/repo16.git/refs/heads/diff @@ -0,0 +1 @@ +f27c2b2b03dcab38beaf89b0ab4ff61f6de63441 diff --git a/tests/gitea-repositories-meta/user2/repo16.git/refs/heads/patch b/tests/gitea-repositories-meta/user2/repo16.git/refs/heads/patch new file mode 100644 index 0000000000..4750a76432 --- /dev/null +++ b/tests/gitea-repositories-meta/user2/repo16.git/refs/heads/patch @@ -0,0 +1 @@ +f27c2b2b03dcab38beaf89b0ab4ff61f6de63441 diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 396c1c22ad..070f7e1b20 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -239,6 +239,16 @@ func TestCompareBranches(t *testing.T) { diffChanges = []string{"test.txt"} inspectCompare(t, htmlDoc, diffCount, diffChanges) + + // Branches with name 'diff' or 'patch' + req = NewRequest(t, "GET", "/user2/repo16/compare/master..diff") + session.MakeRequest(t, req, http.StatusOK) + req = NewRequest(t, "GET", "/user2/repo16/compare/master..diff.patch") + session.MakeRequest(t, req, http.StatusOK) + req = NewRequest(t, "GET", "/user2/repo16/compare/master..patch") + session.MakeRequest(t, req, http.StatusOK) + req = NewRequest(t, "GET", "/user2/repo16/compare/master..patch.diff") + session.MakeRequest(t, req, http.StatusOK) } func TestCompareWithPRsDisabled(t *testing.T) {