From e9fac73bcfd651aa55a7382b43803cb3deb19db9 Mon Sep 17 00:00:00 2001 From: badhezi Date: Thu, 29 May 2025 16:04:16 +0300 Subject: [PATCH] cover all head ref format cases --- routers/web/repo/compare.go | 49 ++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 351236ac29..4f0f6341c1 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -234,25 +234,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { if infoPath == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { - // check if head is a branch or tag only if 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 { @@ -273,7 +254,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { if len(headInfos) == 1 { isSameRepo = true ci.HeadUser = ctx.Repo.Owner - ci.HeadBranch = headInfos[0] + ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[0]) } else if len(headInfos) == 2 { headInfosSplit := strings.Split(headInfos[0], "/") if len(headInfosSplit) == 1 { @@ -286,7 +267,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { } return nil } - ci.HeadBranch = headInfos[1] + ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1]) isSameRepo = ci.HeadUser.ID == ctx.Repo.Owner.ID if isSameRepo { ci.HeadRepo = baseRepo @@ -309,7 +290,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { } return nil } - ci.HeadBranch = headInfos[1] + ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1]) ci.HeadUser = ci.HeadRepo.Owner isSameRepo = ci.HeadRepo.ID == ctx.Repo.Repository.ID } @@ -317,6 +298,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { ctx.NotFound(nil) return nil } + ctx.Data["HeadUser"] = ci.HeadUser ctx.Data["HeadBranch"] = ci.HeadBranch ctx.Repo.PullRequest.SameRepo = isSameRepo @@ -1016,3 +998,26 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu } return diffLines, nil } + +func parseRefForRawDiff(ctx *context.Context, ci *common.CompareInfo, ref string) string { + if strings.HasSuffix(ref, ".diff") || strings.HasSuffix(ref, ".patch") { + var headRepo *repo_model.Repository + if ci.HeadRepo != nil { + headRepo = ci.HeadRepo + } else { + headRepo = ctx.Repo.Repository + } + ref2IsBranch := gitrepo.IsBranchExist(ctx, headRepo, ref) + ref2IsTag := gitrepo.IsTagExist(ctx, headRepo, ref) + if !ref2IsBranch && !ref2IsTag { + if strings.HasSuffix(ref, ".diff") { + ci.RawDiffType = git.RawDiffNormal + ref = strings.TrimSuffix(ref, ".diff") + } else if strings.HasSuffix(ref, ".patch") { + ci.RawDiffType = git.RawDiffPatch + ref = strings.TrimSuffix(ref, ".patch") + } + } + } + return ref +}