Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions services/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) > 0 && 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:
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
65f1bf27bc3bf70f64657658635e66094edbcb4d
c566fa45406b2f02c181df08077a03f177cf9ba8
14 changes: 14 additions & 0 deletions tests/integration/api_repo_get_contents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/user13/repo11/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, "# a_file_path_with_40_characters_for_tests\n", string(raw))

// Test with a filepath with 40 characters and name is a commit SHA
fileWith40c = setting.AppURL + "api/v1/repos/user13/repo11/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, "# test\n", string(raw))
})
}
Loading