From 3274b844c69f99a34505e61037fc34fd67594aaf Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 14 May 2025 13:59:51 +0300 Subject: [PATCH] support tag and branch names with ends with .diff and .patch --- routers/web/repo/compare.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 8647033923..989691a572 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -231,18 +231,28 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { var infos []string - // Handle possible suffixes: .diff or .patch - if strings.HasSuffix(infoPath, ".diff") { - ci.RawDiffType = git.RawDiffNormal - infoPath = strings.TrimSuffix(infoPath, ".diff") - } else if strings.HasSuffix(infoPath, ".patch") { - ci.RawDiffType = git.RawDiffPatch - infoPath = strings.TrimSuffix(infoPath, ".patch") - } - if infoPath == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { + // check if head is a branch or tag on ly infoPath ends with .diff or .patch + if strings.HasSuffix(infoPath, ".diff") || strings.HasSuffix(infoPath, ".patch") { + infos = strings.SplitN(infoPath, "...", 2) + if len(infos) != 2 { + infos = strings.SplitN(infoPath, "..", 2) // match github behavior + } + ref2IsBranch := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, infos[1]) + ref2IsTag := gitrepo.IsTagExist(ctx, ctx.Repo.Repository, infos[1]) + if !ref2IsBranch && !ref2IsTag { + if strings.HasSuffix(infoPath, ".diff") { + ci.RawDiffType = git.RawDiffNormal + infoPath = strings.TrimSuffix(infoPath, ".diff") + } else if strings.HasSuffix(infoPath, ".patch") { + ci.RawDiffType = git.RawDiffPatch + infoPath = strings.TrimSuffix(infoPath, ".patch") + } + } + } + infos = strings.SplitN(infoPath, "...", 2) if len(infos) != 2 { if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 {