Skip to content

Commit 7a3bbfb

Browse files
committed
fix legacy tests
1 parent 40f0781 commit 7a3bbfb

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

services/context/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
293293
return
294294
}
295295

296-
refName, _ := getRefNameLegacy(ctx.Base, ctx.Repo, ctx.PathParam("*"), ctx.FormTrim("ref"))
296+
refName, _, _ := getRefNameLegacy(ctx.Base, ctx.Repo, ctx.PathParam("*"), ctx.FormTrim("ref"))
297297
var err error
298298

299299
if ctx.Repo.GitRepo.IsBranchExist(refName) {

services/context/repo.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -686,24 +686,24 @@ func getRefNameFromPath(repo *Repository, path string, isExist func(string) bool
686686
return ""
687687
}
688688

689-
func getRefNameLegacy(ctx *Base, repo *Repository, reqPath, extraRef string) (string, git.RefType) {
689+
func getRefNameLegacy(ctx *Base, repo *Repository, reqPath, extraRef string) (refName string, refType git.RefType, fallbackDefaultBranch bool) {
690690
reqRefPath := path.Join(extraRef, reqPath)
691691
reqRefPathParts := strings.Split(reqRefPath, "/")
692692
if refName := getRefName(ctx, repo, reqRefPath, git.RefTypeBranch); refName != "" {
693-
return refName, git.RefTypeBranch
693+
return refName, git.RefTypeBranch, false
694694
}
695695
if refName := getRefName(ctx, repo, reqRefPath, git.RefTypeTag); refName != "" {
696-
return refName, git.RefTypeTag
696+
return refName, git.RefTypeTag, false
697697
}
698698
if git.IsStringLikelyCommitID(git.ObjectFormatFromName(repo.Repository.ObjectFormatName), reqRefPathParts[0]) {
699699
// FIXME: this logic is different from other types. Ideally, it should also try to GetCommit to check if it exists
700700
repo.TreePath = strings.Join(reqRefPathParts[1:], "/")
701-
return reqRefPathParts[0], git.RefTypeCommit
701+
return reqRefPathParts[0], git.RefTypeCommit, false
702702
}
703703
// FIXME: the old code falls back to default branch if "ref" doesn't exist, there could be an edge case:
704704
// "README?ref=no-such" would read the README file from the default branch, but the user might expect a 404
705705
repo.TreePath = reqPath
706-
return repo.Repository.DefaultBranch, git.RefTypeBranch
706+
return repo.Repository.DefaultBranch, git.RefTypeBranch, true
707707
}
708708

709709
func getRefName(ctx *Base, repo *Repository, path string, refType git.RefType) string {
@@ -838,8 +838,9 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
838838
}
839839
} else { // there is a path in request
840840
guessLegacyPath := refType == ""
841+
fallbackDefaultBranch := false
841842
if guessLegacyPath {
842-
refShortName, refType = getRefNameLegacy(ctx.Base, ctx.Repo, reqPath, "")
843+
refShortName, refType, fallbackDefaultBranch = getRefNameLegacy(ctx.Base, ctx.Repo, reqPath, "")
843844
} else {
844845
refShortName = getRefName(ctx.Base, ctx.Repo, reqPath, refType)
845846
}
@@ -899,15 +900,19 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
899900
// redirect from old URL scheme to new URL scheme
900901
// * /user2/repo1/commits/master => /user2/repo1/commits/branch/master
901902
// * /user2/repo1/src/master => /user2/repo1/src/branch/master
902-
903+
// * /user2/repo1/src/README.md => /user2/repo1/src/branch/master/README.md (fallback to default branch)
903904
var redirect string
904905
refSubPath := "src"
905906
// remove the "/subpath/owner/repo/" prefix, the names are case-insensitive
906907
remainingLowerPath, cut := strings.CutPrefix(setting.AppSubURL+strings.ToLower(ctx.Req.URL.Path), strings.ToLower(ctx.Repo.RepoLink)+"/")
907908
if cut {
908909
refSubPath, _, _ = strings.Cut(remainingLowerPath, "/") // it could be "src" or "commits"
909910
}
910-
redirect = fmt.Sprintf("%s/%s/%s/%s", ctx.Repo.RepoLink, refSubPath, refType, ctx.PathParamRaw("*"))
911+
if fallbackDefaultBranch {
912+
redirect = fmt.Sprintf("%s/%s/%s/%s/%s", ctx.Repo.RepoLink, refSubPath, refType, refShortName, ctx.PathParamRaw("*"))
913+
} else {
914+
redirect = fmt.Sprintf("%s/%s/%s/%s", ctx.Repo.RepoLink, refSubPath, refType, ctx.PathParamRaw("*"))
915+
}
911916
if ctx.Req.URL.RawQuery != "" {
912917
redirect += "?" + ctx.Req.URL.RawQuery
913918
}

tests/integration/nonascii_branches_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ func TestNonAsciiBranches(t *testing.T) {
4646
{
4747
from: "master/badfile",
4848
to: "branch/master/badfile",
49-
status: http.StatusNotFound, // it does not exists
49+
status: http.StatusNotFound, // it does not exist
5050
},
5151
{
5252
from: "ГлавнаяВетка",
53-
to: "branch/%D0%93%D0%BB%D0%B0%D0%B2%D0%BD%D0%B0%D1%8F%D0%92%D0%B5%D1%82%D0%BA%D0%B0",
53+
to: "branch/%d0%93%d0%bb%d0%b0%d0%b2%d0%bd%d0%b0%d1%8f%d0%92%d0%b5%d1%82%d0%ba%d0%b0",
5454
status: http.StatusOK,
5555
},
5656
{
5757
from: "а/б/в",
58-
to: "branch/%D0%B0/%D0%B1/%D0%B2",
58+
to: "branch/%d0%b0/%d0%b1/%d0%b2",
5959
status: http.StatusOK,
6060
},
6161
{
6262
from: "Grüßen/README.md",
63-
to: "branch/Gr%C3%BC%C3%9Fen/README.md",
63+
to: "branch/Gr%c3%bc%c3%9fen/README.md",
6464
status: http.StatusOK,
6565
},
6666
{
@@ -70,7 +70,7 @@ func TestNonAsciiBranches(t *testing.T) {
7070
},
7171
{
7272
from: "Plus+Is+Not+Space/Файл.md",
73-
to: "branch/Plus+Is+Not+Space/%D0%A4%D0%B0%D0%B9%D0%BB.md",
73+
to: "branch/Plus+Is+Not+Space/%d0%a4%d0%b0%d0%b9%d0%bb.md",
7474
status: http.StatusOK,
7575
},
7676
{
@@ -80,29 +80,29 @@ func TestNonAsciiBranches(t *testing.T) {
8080
},
8181
{
8282
from: "ブランチ",
83-
to: "branch/%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81",
83+
to: "branch/%e3%83%96%e3%83%a9%e3%83%b3%e3%83%81",
8484
status: http.StatusOK,
8585
},
8686

8787
// Tags
8888
{
8989
from: "Тэг",
90-
to: "tag/%D0%A2%D1%8D%D0%B3",
90+
to: "tag/%d0%a2%d1%8d%d0%b3",
9191
status: http.StatusOK,
9292
},
9393
{
9494
from: "Ё/人",
95-
to: "tag/%D0%81/%E4%BA%BA",
95+
to: "tag/%d0%81/%e4%ba%ba",
9696
status: http.StatusOK,
9797
},
9898
{
9999
from: "タグ",
100-
to: "tag/%E3%82%BF%E3%82%B0",
100+
to: "tag/%e3%82%bf%e3%82%b0",
101101
status: http.StatusOK,
102102
},
103103
{
104104
from: "タグ/ファイル.md",
105-
to: "tag/%E3%82%BF%E3%82%B0/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.md",
105+
to: "tag/%e3%82%bf%e3%82%b0/%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab.md",
106106
status: http.StatusOK,
107107
},
108108

@@ -114,12 +114,12 @@ func TestNonAsciiBranches(t *testing.T) {
114114
},
115115
{
116116
from: "Файл.md",
117-
to: "branch/Plus+Is+Not+Space/%D0%A4%D0%B0%D0%B9%D0%BB.md",
117+
to: "branch/Plus+Is+Not+Space/%d0%a4%d0%b0%d0%b9%d0%bb.md",
118118
status: http.StatusOK,
119119
},
120120
{
121121
from: "ファイル.md",
122-
to: "branch/Plus+Is+Not+Space/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.md",
122+
to: "branch/Plus+Is+Not+Space/%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab.md",
123123
status: http.StatusNotFound, // it's not on default branch
124124
},
125125

@@ -131,7 +131,7 @@ func TestNonAsciiBranches(t *testing.T) {
131131
},
132132
{
133133
from: "%E3%82%BF%E3%82%b0",
134-
to: "tag/%E3%82%BF%E3%82%B0",
134+
to: "tag/%E3%82%BF%E3%82%b0",
135135
status: http.StatusOK,
136136
},
137137
{
@@ -141,12 +141,12 @@ func TestNonAsciiBranches(t *testing.T) {
141141
},
142142
{
143143
from: "%D0%81%2F%E4%BA%BA",
144-
to: "tag/%D0%81/%E4%BA%BA",
144+
to: "tag/%D0%81%2F%E4%BA%BA",
145145
status: http.StatusOK,
146146
},
147147
{
148148
from: "Ё%2F%E4%BA%BA",
149-
to: "tag/%D0%81/%E4%BA%BA",
149+
to: "tag/%d0%81%2F%E4%BA%BA",
150150
status: http.StatusOK,
151151
},
152152
{

0 commit comments

Comments
 (0)