Skip to content

Don't try to interpret treepath as hash when path contains no hash-path-separator ('/') (#17272) #32338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
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