Skip to content

Commit e85624a

Browse files
committed
cover all head ref format cases
1 parent 4899b8a commit e85624a

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
@@ -236,25 +236,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
236236
if infoPath == "" {
237237
infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch}
238238
} else {
239-
// check if head is a branch or tag only if 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-
258239
infos = strings.SplitN(infoPath, "...", 2)
259240
if len(infos) != 2 {
260241
if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 {
@@ -275,7 +256,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
275256
if len(headInfos) == 1 {
276257
isSameRepo = true
277258
ci.HeadUser = ctx.Repo.Owner
278-
ci.HeadBranch = headInfos[0]
259+
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[0])
279260
} else if len(headInfos) == 2 {
280261
headInfosSplit := strings.Split(headInfos[0], "/")
281262
if len(headInfosSplit) == 1 {
@@ -288,7 +269,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
288269
}
289270
return nil
290271
}
291-
ci.HeadBranch = headInfos[1]
272+
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1])
292273
isSameRepo = ci.HeadUser.ID == ctx.Repo.Owner.ID
293274
if isSameRepo {
294275
ci.HeadRepo = baseRepo
@@ -311,14 +292,15 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
311292
}
312293
return nil
313294
}
314-
ci.HeadBranch = headInfos[1]
295+
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1])
315296
ci.HeadUser = ci.HeadRepo.Owner
316297
isSameRepo = ci.HeadRepo.ID == ctx.Repo.Repository.ID
317298
}
318299
} else {
319300
ctx.NotFound(nil)
320301
return nil
321302
}
303+
322304
ctx.Data["HeadUser"] = ci.HeadUser
323305
ctx.Data["HeadBranch"] = ci.HeadBranch
324306
ctx.Repo.PullRequest.SameRepo = isSameRepo
@@ -1018,3 +1000,26 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu
10181000
}
10191001
return diffLines, nil
10201002
}
1003+
1004+
func parseRefForRawDiff(ctx *context.Context, ci *common.CompareInfo, ref string) string {
1005+
if strings.HasSuffix(ref, ".diff") || strings.HasSuffix(ref, ".patch") {
1006+
var headRepo *repo_model.Repository
1007+
if ci.HeadRepo != nil {
1008+
headRepo = ci.HeadRepo
1009+
} else {
1010+
headRepo = ctx.Repo.Repository
1011+
}
1012+
ref2IsBranch := gitrepo.IsBranchExist(ctx, headRepo, ref)
1013+
ref2IsTag := gitrepo.IsTagExist(ctx, headRepo, ref)
1014+
if !ref2IsBranch && !ref2IsTag {
1015+
if strings.HasSuffix(ref, ".diff") {
1016+
ci.RawDiffType = git.RawDiffNormal
1017+
ref = strings.TrimSuffix(ref, ".diff")
1018+
} else if strings.HasSuffix(ref, ".patch") {
1019+
ci.RawDiffType = git.RawDiffPatch
1020+
ref = strings.TrimSuffix(ref, ".patch")
1021+
}
1022+
}
1023+
}
1024+
return ref
1025+
}

0 commit comments

Comments
 (0)