Skip to content

Commit 30a2ff1

Browse files
committed
Fix raw file visit
1 parent bcf6e82 commit 30a2ff1

File tree

7 files changed

+24
-7
lines changed

7 files changed

+24
-7
lines changed

services/context/repo.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -823,13 +823,16 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
823823
// For legacy and API support only full commit sha
824824
parts := strings.Split(path, "/")
825825

826-
if len(parts) > 1 && len(parts[0]) == git.ObjectFormatFromName(repo.Repository.ObjectFormatName).FullLength() {
827-
repo.TreePath = strings.Join(parts[1:], "/")
828-
return parts[0]
829-
}
830-
if refName := getRefName(ctx, repo, RepoRefBlob); len(refName) > 0 {
831-
return refName
826+
if len(parts) > 1 {
827+
if len(parts[0]) == git.ObjectFormatFromName(repo.Repository.ObjectFormatName).FullLength() {
828+
repo.TreePath = strings.Join(parts[1:], "/")
829+
return parts[0]
830+
}
831+
if refName := getRefName(ctx, repo, RepoRefBlob); len(refName) > 0 {
832+
return refName
833+
}
832834
}
835+
833836
repo.TreePath = path
834837
return repo.Repository.DefaultBranch
835838
case RepoRefBranch:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
65f1bf27bc3bf70f64657658635e66094edbcb4d
1+
34865bf344ac1b427da08fcb845a2eab4706067c

tests/integration/api_repo_get_contents_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,19 @@ func TestAPIGetContentsRefFormats(t *testing.T) {
191191
raw, err = io.ReadAll(resp.Body)
192192
assert.NoError(t, err)
193193
assert.EqualValues(t, content, string(raw))
194+
195+
// Test with a filepath with 40 characters
196+
fileWith40c := setting.AppURL + "api/v1/repos/user2/repo1/raw/" + "a_file_path_with_40_characters_for_tests"
197+
resp = MakeRequest(t, NewRequest(t, http.MethodGet, fileWith40c), http.StatusOK)
198+
raw, err = io.ReadAll(resp.Body)
199+
assert.NoError(t, err)
200+
assert.EqualValues(t, "# test\n", string(raw))
201+
202+
// Test with a filepath with 40 characters and name is a commit SHA
203+
fileWith40c = setting.AppURL + "api/v1/repos/user2/repo1/raw/" + "65f1bf27bc3bf70f64657658635e66094edbcb4d"
204+
resp = MakeRequest(t, NewRequest(t, http.MethodGet, fileWith40c), http.StatusOK)
205+
raw, err = io.ReadAll(resp.Body)
206+
assert.NoError(t, err)
207+
assert.EqualValues(t, "# 65f1bf27bc3bf70f64657658635e66094edbcb4d\n", string(raw))
194208
})
195209
}

0 commit comments

Comments
 (0)