Skip to content

Commit d842bb5

Browse files
committed
fix
1 parent 88366f2 commit d842bb5

File tree

3 files changed

+52
-47
lines changed

3 files changed

+52
-47
lines changed

services/context/api.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
293293
return
294294
}
295295

296-
// NOTICE: the "ref" here for internal usage only (e.g. woodpecker)
297-
refName, _ := getRefNameLegacy(ctx.Base, ctx.Repo, ctx.FormTrim("ref"))
296+
refName, _ := getRefNameLegacy(ctx.Base, ctx.Repo, ctx.PathParam("*"), ctx.FormTrim("ref"))
298297
var err error
299298

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

services/context/repo.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -765,35 +765,30 @@ func getRefNameFromPath(repo *Repository, path string, isExist func(string) bool
765765
return ""
766766
}
767767

768-
func getRefNameLegacy(ctx *Base, repo *Repository, optionalExtraRef ...string) (string, RepoRefType) {
769-
extraRef := util.OptionalArg(optionalExtraRef)
770-
reqPath := ctx.PathParam("*")
771-
reqPath = path.Join(extraRef, reqPath)
772-
773-
if refName := getRefName(ctx, repo, RepoRefBranch); refName != "" {
768+
func getRefNameLegacy(ctx *Base, repo *Repository, reqPath, extraRef string) (string, RepoRefType) {
769+
reqRefPath := path.Join(extraRef, reqPath)
770+
reqRefPathParts := strings.Split(reqRefPath, "/")
771+
if refName := getRefName(ctx, repo, reqRefPath, RepoRefBranch); refName != "" {
774772
return refName, RepoRefBranch
775773
}
776-
if refName := getRefName(ctx, repo, RepoRefTag); refName != "" {
774+
if refName := getRefName(ctx, repo, reqRefPath, RepoRefTag); refName != "" {
777775
return refName, RepoRefTag
778776
}
779-
780-
// For legacy support only full commit sha
781-
parts := strings.Split(reqPath, "/")
782-
if git.IsStringLikelyCommitID(git.ObjectFormatFromName(repo.Repository.ObjectFormatName), parts[0]) {
777+
if git.IsStringLikelyCommitID(git.ObjectFormatFromName(repo.Repository.ObjectFormatName), reqRefPathParts[0]) {
783778
// FIXME: this logic is different from other types. Ideally, it should also try to GetCommit to check if it exists
784-
repo.TreePath = strings.Join(parts[1:], "/")
785-
return parts[0], RepoRefCommit
779+
repo.TreePath = strings.Join(reqRefPathParts[1:], "/")
780+
return reqRefPathParts[0], RepoRefCommit
786781
}
787-
788-
if refName := getRefName(ctx, repo, RepoRefBlob); len(refName) > 0 {
782+
if refName := getRefName(ctx, repo, reqPath, RepoRefBlob); refName != "" {
789783
return refName, RepoRefBlob
790784
}
785+
// FIXME: the old code falls back to default branch if "ref" doesn't exist, there could be an edge case:
786+
// "README?ref=no-such" would read the README file from the default branch, but the user might expect a 404
791787
repo.TreePath = reqPath
792788
return repo.Repository.DefaultBranch, RepoRefBranch
793789
}
794790

795-
func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
796-
path := ctx.PathParam("*")
791+
func getRefName(ctx *Base, repo *Repository, path string, pathType RepoRefType) string {
797792
switch pathType {
798793
case RepoRefBranch:
799794
ref := getRefNameFromPath(repo, path, repo.GitRepo.IsBranchExist)
@@ -889,7 +884,8 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
889884
}
890885

891886
// Get default branch.
892-
if len(ctx.PathParam("*")) == 0 {
887+
reqPath := ctx.PathParam("*")
888+
if reqPath == "" {
893889
refName = ctx.Repo.Repository.DefaultBranch
894890
if !ctx.Repo.GitRepo.IsBranchExist(refName) {
895891
brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 1)
@@ -914,12 +910,12 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
914910
return
915911
}
916912
ctx.Repo.IsViewBranch = true
917-
} else {
913+
} else { // there is a path in request
918914
guessLegacyPath := refType == RepoRefUnknown
919915
if guessLegacyPath {
920-
refName, refType = getRefNameLegacy(ctx.Base, ctx.Repo)
916+
refName, refType = getRefNameLegacy(ctx.Base, ctx.Repo, reqPath, "")
921917
} else {
922-
refName = getRefName(ctx.Base, ctx.Repo, refType)
918+
refName = getRefName(ctx.Base, ctx.Repo, reqPath, refType)
923919
}
924920
ctx.Repo.RefName = refName
925921
isRenamedBranch, has := ctx.Data["IsRenamedBranch"].(bool)

tests/integration/api_repo_get_contents_test.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/setting"
1919
api "code.gitea.io/gitea/modules/structs"
2020
repo_service "code.gitea.io/gitea/services/repository"
21+
"code.gitea.io/gitea/tests"
2122

2223
"github.com/stretchr/testify/assert"
2324
)
@@ -168,28 +169,37 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
168169
}
169170

170171
func TestAPIGetContentsRefFormats(t *testing.T) {
171-
onGiteaRun(t, func(t *testing.T, u *url.URL) {
172-
file := "README.md"
173-
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
174-
content := "# repo1\n\nDescription for repo1"
175-
176-
noRef := setting.AppURL + "api/v1/repos/user2/repo1/raw/" + file
177-
refInPath := setting.AppURL + "api/v1/repos/user2/repo1/raw/" + sha + "/" + file
178-
refInQuery := setting.AppURL + "api/v1/repos/user2/repo1/raw/" + file + "?ref=" + sha
179-
180-
resp := MakeRequest(t, NewRequest(t, http.MethodGet, noRef), http.StatusOK)
181-
raw, err := io.ReadAll(resp.Body)
182-
assert.NoError(t, err)
183-
assert.EqualValues(t, content, string(raw))
184-
185-
resp = MakeRequest(t, NewRequest(t, http.MethodGet, refInPath), http.StatusOK)
186-
raw, err = io.ReadAll(resp.Body)
187-
assert.NoError(t, err)
188-
assert.EqualValues(t, content, string(raw))
189-
190-
resp = MakeRequest(t, NewRequest(t, http.MethodGet, refInQuery), http.StatusOK)
191-
raw, err = io.ReadAll(resp.Body)
192-
assert.NoError(t, err)
193-
assert.EqualValues(t, content, string(raw))
194-
})
172+
defer tests.PrepareTestEnv(t, 1)()
173+
174+
file := "README.md"
175+
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
176+
content := "# repo1\n\nDescription for repo1"
177+
178+
resp := MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/"+file), http.StatusOK)
179+
raw, err := io.ReadAll(resp.Body)
180+
assert.NoError(t, err)
181+
assert.EqualValues(t, content, string(raw))
182+
183+
resp = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/"+sha+"/"+file), http.StatusOK)
184+
raw, err = io.ReadAll(resp.Body)
185+
assert.NoError(t, err)
186+
assert.EqualValues(t, content, string(raw))
187+
188+
resp = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/"+file+"?ref="+sha), http.StatusOK)
189+
raw, err = io.ReadAll(resp.Body)
190+
assert.NoError(t, err)
191+
assert.EqualValues(t, content, string(raw))
192+
193+
resp = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/"+file+"?ref=master"), http.StatusOK)
194+
raw, err = io.ReadAll(resp.Body)
195+
assert.NoError(t, err)
196+
assert.EqualValues(t, content, string(raw))
197+
198+
_ = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/docs/README.md?ref=main"), http.StatusNotFound)
199+
_ = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/README.md?ref=main"), http.StatusOK)
200+
_ = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/docs/README.md?ref=sub-home-md-img-check"), http.StatusOK)
201+
_ = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/README.md?ref=sub-home-md-img-check"), http.StatusNotFound)
202+
203+
// FIXME: this is an incorrect behavior, non-existing branch falls back to default branch
204+
_ = MakeRequest(t, NewRequest(t, http.MethodGet, "/api/v1/repos/user2/repo1/raw/README.md?ref=no-such"), http.StatusOK)
195205
}

0 commit comments

Comments
 (0)