Skip to content

Commit 5335602

Browse files
wxiaoguangbadhezi
authored andcommitted
FIXME: how to correctly choose the head repository?
1 parent 233c710 commit 5335602

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
@@ -223,13 +223,9 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
223223
// base<-head: master...head:feature
224224
// same repo: master...feature
225225

226-
var (
227-
isSameRepo bool
228-
infoPath string
229-
err error
230-
)
226+
var isSameRepo bool
231227

232-
infoPath = ctx.PathParam("*")
228+
infoPath := ctx.PathParam("*")
233229
var infos []string
234230
if infoPath == "" {
235231
infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch}
@@ -249,12 +245,14 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
249245
ci.BaseBranch = infos[0]
250246
ctx.Data["BaseBranch"] = ci.BaseBranch
251247

252-
// If there is no head repository, it means compare between same repository.
248+
var err error
249+
250+
// If there is no head repository, it means compare between the same repository.
253251
headInfos := strings.Split(infos[1], ":")
254252
if len(headInfos) == 1 {
255253
isSameRepo = true
256254
ci.HeadUser = ctx.Repo.Owner
257-
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[0])
255+
ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, ctx.Repo.Repository, headInfos[0])
258256
} else if len(headInfos) == 2 {
259257
headInfosSplit := strings.Split(headInfos[0], "/")
260258
if len(headInfosSplit) == 1 {
@@ -267,7 +265,8 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
267265
}
268266
return nil
269267
}
270-
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1])
268+
// FIXME: how to correctly choose the head repository? The logic below (3-8) is quite complex, the real head repo is determined there
269+
ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, ..., headInfos[1])
271270
isSameRepo = ci.HeadUser.ID == ctx.Repo.Owner.ID
272271
if isSameRepo {
273272
ci.HeadRepo = baseRepo
@@ -290,7 +289,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
290289
}
291290
return nil
292291
}
293-
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1])
292+
ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, ci.HeadRepo, headInfos[1])
294293
ci.HeadUser = ci.HeadRepo.Owner
295294
isSameRepo = ci.HeadRepo.ID == ctx.Repo.Repository.ID
296295
}
@@ -752,7 +751,6 @@ func CompareDiff(ctx *context.Context) {
752751
ctx.ServerError("GetRepoRawDiffForFile", err)
753752
return
754753
}
755-
ctx.Resp.Flush()
756754
return
757755
}
758756

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

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

0 commit comments

Comments
 (0)