1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-02-02 15:09:33 -05:00

Fix raw file visit

This commit is contained in:
Lunny Xiao 2024-10-24 16:50:09 -07:00
parent bcf6e82dd0
commit 30a2ff1af6
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
7 changed files with 24 additions and 7 deletions

View File

@ -823,13 +823,16 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
// For legacy and API support only full commit sha
parts := strings.Split(path, "/")
if len(parts) > 1 && len(parts[0]) == git.ObjectFormatFromName(repo.Repository.ObjectFormatName).FullLength() {
repo.TreePath = strings.Join(parts[1:], "/")
return parts[0]
}
if refName := getRefName(ctx, repo, RepoRefBlob); len(refName) > 0 {
return refName
if len(parts) > 1 {
if len(parts[0]) == git.ObjectFormatFromName(repo.Repository.ObjectFormatName).FullLength() {
repo.TreePath = strings.Join(parts[1:], "/")
return parts[0]
}
if refName := getRefName(ctx, repo, RepoRefBlob); len(refName) > 0 {
return refName
}
}
repo.TreePath = path
return repo.Repository.DefaultBranch
case RepoRefBranch:

View File

@ -1 +1 @@
65f1bf27bc3bf70f64657658635e66094edbcb4d
34865bf344ac1b427da08fcb845a2eab4706067c

View File

@ -191,5 +191,19 @@ func TestAPIGetContentsRefFormats(t *testing.T) {
raw, err = io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.EqualValues(t, content, string(raw))
// Test with a filepath with 40 characters
fileWith40c := setting.AppURL + "api/v1/repos/user2/repo1/raw/" + "a_file_path_with_40_characters_for_tests"
resp = MakeRequest(t, NewRequest(t, http.MethodGet, fileWith40c), http.StatusOK)
raw, err = io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.EqualValues(t, "# test\n", string(raw))
// Test with a filepath with 40 characters and name is a commit SHA
fileWith40c = setting.AppURL + "api/v1/repos/user2/repo1/raw/" + "65f1bf27bc3bf70f64657658635e66094edbcb4d"
resp = MakeRequest(t, NewRequest(t, http.MethodGet, fileWith40c), http.StatusOK)
raw, err = io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.EqualValues(t, "# 65f1bf27bc3bf70f64657658635e66094edbcb4d\n", string(raw))
})
}