Skip to content

Commit a04c5b2

Browse files
committed
support tag and branch names with ends with .diff and .patch
1 parent 5dae729 commit a04c5b2

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
@@ -233,18 +233,28 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
233233

234234
var infos []string
235235

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

0 commit comments

Comments
 (0)