Skip to content

Commit e9fac73

Browse files
committed
cover all head ref format cases
1 parent c80f0b9 commit e9fac73

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

routers/web/repo/compare.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -234,25 +234,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
234234
if infoPath == "" {
235235
infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch}
236236
} else {
237-
// check if head is a branch or tag only if 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-
256237
infos = strings.SplitN(infoPath, "...", 2)
257238
if len(infos) != 2 {
258239
if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 {
@@ -273,7 +254,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
273254
if len(headInfos) == 1 {
274255
isSameRepo = true
275256
ci.HeadUser = ctx.Repo.Owner
276-
ci.HeadBranch = headInfos[0]
257+
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[0])
277258
} else if len(headInfos) == 2 {
278259
headInfosSplit := strings.Split(headInfos[0], "/")
279260
if len(headInfosSplit) == 1 {
@@ -286,7 +267,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
286267
}
287268
return nil
288269
}
289-
ci.HeadBranch = headInfos[1]
270+
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1])
290271
isSameRepo = ci.HeadUser.ID == ctx.Repo.Owner.ID
291272
if isSameRepo {
292273
ci.HeadRepo = baseRepo
@@ -309,14 +290,15 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
309290
}
310291
return nil
311292
}
312-
ci.HeadBranch = headInfos[1]
293+
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1])
313294
ci.HeadUser = ci.HeadRepo.Owner
314295
isSameRepo = ci.HeadRepo.ID == ctx.Repo.Repository.ID
315296
}
316297
} else {
317298
ctx.NotFound(nil)
318299
return nil
319300
}
301+
320302
ctx.Data["HeadUser"] = ci.HeadUser
321303
ctx.Data["HeadBranch"] = ci.HeadBranch
322304
ctx.Repo.PullRequest.SameRepo = isSameRepo
@@ -1016,3 +998,26 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu
1016998
}
1017999
return diffLines, nil
10181000
}
1001+
1002+
func parseRefForRawDiff(ctx *context.Context, ci *common.CompareInfo, ref string) string {
1003+
if strings.HasSuffix(ref, ".diff") || strings.HasSuffix(ref, ".patch") {
1004+
var headRepo *repo_model.Repository
1005+
if ci.HeadRepo != nil {
1006+
headRepo = ci.HeadRepo
1007+
} else {
1008+
headRepo = ctx.Repo.Repository
1009+
}
1010+
ref2IsBranch := gitrepo.IsBranchExist(ctx, headRepo, ref)
1011+
ref2IsTag := gitrepo.IsTagExist(ctx, headRepo, ref)
1012+
if !ref2IsBranch && !ref2IsTag {
1013+
if strings.HasSuffix(ref, ".diff") {
1014+
ci.RawDiffType = git.RawDiffNormal
1015+
ref = strings.TrimSuffix(ref, ".diff")
1016+
} else if strings.HasSuffix(ref, ".patch") {
1017+
ci.RawDiffType = git.RawDiffPatch
1018+
ref = strings.TrimSuffix(ref, ".patch")
1019+
}
1020+
}
1021+
}
1022+
return ref
1023+
}

0 commit comments

Comments
 (0)