Skip to content

Commit c98fccf

Browse files
committed
support tag and branch names with ends with .diff and .patch
1 parent 460fe62 commit c98fccf

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

routers/web/repo/compare.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,28 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
231231

232232
var infos []string
233233

234-
// Handle possible suffixes: .diff or .patch
235-
if strings.HasSuffix(infoPath, ".diff") {
236-
ci.RawDiffType = git.RawDiffNormal
237-
infoPath = strings.TrimSuffix(infoPath, ".diff")
238-
} else if strings.HasSuffix(infoPath, ".patch") {
239-
ci.RawDiffType = git.RawDiffPatch
240-
infoPath = strings.TrimSuffix(infoPath, ".patch")
241-
}
242-
243234
if infoPath == "" {
244235
infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch}
245236
} else {
237+
// check if head is a branch or tag on ly infoPath ends with .diff or .patch
238+
if strings.HasSuffix(infoPath, ".diff") || strings.HasSuffix(infoPath, ".patch") {
239+
infos = strings.SplitN(infoPath, "...", 2)
240+
if len(infos) != 2 {
241+
infos = strings.SplitN(infoPath, "..", 2) // match github behavior
242+
}
243+
ref2IsBranch := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, infos[1])
244+
ref2IsTag := gitrepo.IsTagExist(ctx, ctx.Repo.Repository, infos[1])
245+
if !ref2IsBranch && !ref2IsTag {
246+
if strings.HasSuffix(infoPath, ".diff") {
247+
ci.RawDiffType = git.RawDiffNormal
248+
infoPath = strings.TrimSuffix(infoPath, ".diff")
249+
} else if strings.HasSuffix(infoPath, ".patch") {
250+
ci.RawDiffType = git.RawDiffPatch
251+
infoPath = strings.TrimSuffix(infoPath, ".patch")
252+
}
253+
}
254+
}
255+
246256
infos = strings.SplitN(infoPath, "...", 2)
247257
if len(infos) != 2 {
248258
if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 {

0 commit comments

Comments
 (0)