Skip to content

Commit 180386b

Browse files
committed
FIXME: how to correctly choose the head repository?
1 parent e442ab1 commit 180386b

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

routers/web/repo/compare.go

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,9 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
221221
// base<-head: master...head:feature
222222
// same repo: master...feature
223223

224-
var (
225-
isSameRepo bool
226-
infoPath string
227-
err error
228-
)
224+
var isSameRepo bool
229225

230-
infoPath = ctx.PathParam("*")
226+
infoPath := ctx.PathParam("*")
231227
var infos []string
232228
if infoPath == "" {
233229
infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch}
@@ -247,12 +243,14 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
247243
ci.BaseBranch = infos[0]
248244
ctx.Data["BaseBranch"] = ci.BaseBranch
249245

250-
// If there is no head repository, it means compare between same repository.
246+
var err error
247+
248+
// If there is no head repository, it means compare between the same repository.
251249
headInfos := strings.Split(infos[1], ":")
252250
if len(headInfos) == 1 {
253251
isSameRepo = true
254252
ci.HeadUser = ctx.Repo.Owner
255-
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[0])
253+
ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, ctx.Repo.Repository, headInfos[0])
256254
} else if len(headInfos) == 2 {
257255
headInfosSplit := strings.Split(headInfos[0], "/")
258256
if len(headInfosSplit) == 1 {
@@ -265,7 +263,8 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
265263
}
266264
return nil
267265
}
268-
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1])
266+
// FIXME: how to correctly choose the head repository? The logic below (3-8) is quite complex, the real head repo is determined there
267+
ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, ..., headInfos[1])
269268
isSameRepo = ci.HeadUser.ID == ctx.Repo.Owner.ID
270269
if isSameRepo {
271270
ci.HeadRepo = baseRepo
@@ -288,7 +287,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
288287
}
289288
return nil
290289
}
291-
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1])
290+
ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, ci.HeadRepo, headInfos[1])
292291
ci.HeadUser = ci.HeadRepo.Owner
293292
isSameRepo = ci.HeadRepo.ID == ctx.Repo.Repository.ID
294293
}
@@ -750,7 +749,6 @@ func CompareDiff(ctx *context.Context) {
750749
ctx.ServerError("GetRepoRawDiffForFile", err)
751750
return
752751
}
753-
ctx.Resp.Flush()
754752
return
755753
}
756754

@@ -996,25 +994,19 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu
996994
return diffLines, nil
997995
}
998996

999-
func parseRefForRawDiff(ctx *context.Context, ci *common.CompareInfo, ref string) string {
1000-
if strings.HasSuffix(ref, ".diff") || strings.HasSuffix(ref, ".patch") {
1001-
var headRepo *repo_model.Repository
1002-
if ci.HeadRepo != nil {
1003-
headRepo = ci.HeadRepo
1004-
} else {
1005-
headRepo = ctx.Repo.Repository
1006-
}
1007-
ref2IsBranch := gitrepo.IsBranchExist(ctx, headRepo, ref)
1008-
ref2IsTag := gitrepo.IsTagExist(ctx, headRepo, ref)
1009-
if !ref2IsBranch && !ref2IsTag {
1010-
if strings.HasSuffix(ref, ".diff") {
1011-
ci.RawDiffType = git.RawDiffNormal
1012-
ref = strings.TrimSuffix(ref, ".diff")
1013-
} else if strings.HasSuffix(ref, ".patch") {
1014-
ci.RawDiffType = git.RawDiffPatch
1015-
ref = strings.TrimSuffix(ref, ".patch")
1016-
}
1017-
}
997+
func parseRefForRawDiff(ctx *context.Context, refRepo *repo_model.Repository, refShortName string) (string, git.RawDiffType) {
998+
if !strings.HasSuffix(refShortName, ".diff") && !strings.HasSuffix(refShortName, ".patch") {
999+
return refShortName, ""
1000+
}
1001+
1002+
if gitrepo.IsBranchExist(ctx, refRepo, refShortName) || gitrepo.IsTagExist(ctx, refRepo, refShortName) {
1003+
return refShortName, ""
1004+
}
1005+
1006+
if s, ok := strings.CutSuffix(refShortName, ".diff"); ok {
1007+
return s, git.RawDiffNormal
1008+
} else if s, ok = strings.CutSuffix(refShortName, ".patch"); ok {
1009+
return s, git.RawDiffPatch
10181010
}
1019-
return ref
1011+
return refShortName, ""
10201012
}

0 commit comments

Comments
 (0)