Skip to content

Commit ff4a105

Browse files
committed
fix
1 parent 8a5b4f8 commit ff4a105

File tree

7 files changed

+22
-21
lines changed

7 files changed

+22
-21
lines changed

modules/structs/git_blob.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ package structs
55

66
// GitBlobResponse represents a git blob
77
type GitBlobResponse struct {
8-
Content string `json:"content"`
9-
Encoding string `json:"encoding"`
10-
URL string `json:"url"`
11-
SHA string `json:"sha"`
12-
Size int64 `json:"size"`
8+
Content *string `json:"content"`
9+
Encoding *string `json:"encoding"`
10+
URL string `json:"url"`
11+
SHA string `json:"sha"`
12+
Size int64 `json:"size"`
1313
}

routers/api/v1/repo/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,6 @@ func GetFiles(ctx *context.APIContext) {
10291029
if ctx.Written() {
10301030
return
10311031
}
1032-
filesResponse := files_service.GetContentsListFromTrees(ctx, ctx.Repo.Repository, refCommit, apiOpts.Files)
1032+
filesResponse := files_service.GetContentsListFromTreePaths(ctx, ctx.Repo.Repository, refCommit, apiOpts.Files)
10331033
ctx.JSON(http.StatusOK, util.SliceNilAsEmpty(filesResponse))
10341034
}

services/repository/files/content.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, refCommit *ut
175175
if err != nil {
176176
return nil, err
177177
}
178-
contentsResponse.Encoding = &blobResponse.Encoding
179-
contentsResponse.Content = &blobResponse.Content
178+
contentsResponse.Encoding = blobResponse.Encoding
179+
contentsResponse.Content = blobResponse.Content
180180
}
181181
} else if entry.IsDir() {
182182
contentsResponse.Type = string(ContentTypeDir)
@@ -240,11 +240,11 @@ func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git
240240
Size: gitBlob.Size(),
241241
}
242242
if gitBlob.Size() <= setting.API.DefaultMaxBlobSize {
243-
ret.Encoding = "base64"
244-
ret.Content, err = gitBlob.GetBlobContentBase64()
243+
content, err := gitBlob.GetBlobContentBase64()
245244
if err != nil {
246245
return nil, err
247246
}
247+
ret.Encoding, ret.Content = util.ToPointer("base64"), &content
248248
}
249249
return ret, nil
250250
}

services/repository/files/content_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"code.gitea.io/gitea/models/unittest"
1111
"code.gitea.io/gitea/modules/gitrepo"
1212
api "code.gitea.io/gitea/modules/structs"
13+
"code.gitea.io/gitea/modules/util"
1314
"code.gitea.io/gitea/routers/api/v1/utils"
1415
"code.gitea.io/gitea/services/contexttest"
1516

@@ -197,8 +198,8 @@ func TestGetBlobBySHA(t *testing.T) {
197198

198199
gbr, err := GetBlobBySHA(ctx, ctx.Repo.Repository, gitRepo, ctx.PathParam("sha"))
199200
expectedGBR := &api.GitBlobResponse{
200-
Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK",
201-
Encoding: "base64",
201+
Content: util.ToPointer("dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK"),
202+
Encoding: util.ToPointer("base64"),
202203
URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/65f1bf27bc3bf70f64657658635e66094edbcb4d",
203204
SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
204205
Size: 180,

services/repository/files/file.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@ import (
1919
"code.gitea.io/gitea/routers/api/v1/utils"
2020
)
2121

22-
func GetContentsListFromTrees(ctx context.Context, repo *repo_model.Repository, refCommit *utils.RefCommit, treeNames []string) []*api.ContentsResponse {
22+
func GetContentsListFromTreePaths(ctx context.Context, repo *repo_model.Repository, refCommit *utils.RefCommit, treePaths []string) []*api.ContentsResponse {
2323
var files []*api.ContentsResponse
2424
var size int64
25-
for _, file := range treeNames {
26-
fileContents, _ := GetContents(ctx, repo, refCommit, file, false) // ok if fails, then will be nil
27-
if fileContents != nil && *fileContents.Content != "" {
25+
for _, treePath := range treePaths {
26+
fileContents, _ := GetContents(ctx, repo, refCommit, treePath, false) // ok if fails, then will be nil
27+
if fileContents != nil && fileContents.Content != nil && *fileContents.Content != "" {
2828
// if content isn't empty (e.g. due to the single blob being too large), add file size to response size
2929
// the content is base64 encoded, so it's size increases to around 4/3 of the original size
3030
size += fileContents.Size * 4 / 3
3131
}
3232
if size > setting.API.DefaultMaxResponseSize {
33-
return files // return if max page size would be exceeded
33+
break // stop if max page size would be exceeded
3434
}
3535
files = append(files, fileContents)
3636
if len(files) == setting.API.DefaultPagingNum {
37-
return files // return if paging num or max size reached
37+
break // stop if paging num or max size reached
3838
}
3939
}
4040
return files
4141
}
4242

4343
func GetFilesResponseFromCommit(ctx context.Context, repo *repo_model.Repository, refCommit *utils.RefCommit, treeNames []string) (*api.FilesResponse, error) {
44-
files := GetContentsListFromTrees(ctx, repo, refCommit, treeNames)
44+
files := GetContentsListFromTreePaths(ctx, repo, refCommit, treeNames)
4545
fileCommitResponse, _ := GetFileCommitResponse(repo, refCommit.Commit) // ok if fails, then will be nil
4646
verification := GetPayloadCommitVerification(ctx, refCommit.Commit)
4747
filesResponse := &api.FilesResponse{

tests/integration/api_repo_files_get_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestAPIGetRequestedFiles(t *testing.T) {
139139
setting.API.DefaultMaxBlobSize = 20000
140140

141141
// if the total response size would exceed the DefaultMaxResponseSize, then the list stops
142-
setting.API.DefaultMaxResponseSize = 1064*4/3 + 1
142+
setting.API.DefaultMaxResponseSize = ret[1].Size*4/3 + 1
143143
ret = requestFiles(t, "/api/v1/repos/user2/repo1/files?ref=DefaultBranch", []string{"no-such.txt", "LICENSE", "README.md"})
144144
assertResponse(t, []*expected{nil, {"LICENSE", true}}, ret)
145145
setting.API.DefaultMaxBlobSize = 20000

tests/integration/api_repo_git_blobs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestAPIReposGitBlobs(t *testing.T) {
4141
DecodeJSON(t, resp, &gitBlobResponse)
4242
assert.NotNil(t, gitBlobResponse)
4343
expectedContent := "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK"
44-
assert.Equal(t, expectedContent, gitBlobResponse.Content)
44+
assert.Equal(t, expectedContent, *gitBlobResponse.Content)
4545

4646
// Tests a private repo with no token so will fail
4747
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/blobs/%s", user2.Name, repo16.Name, repo16ReadmeSHA)

0 commit comments

Comments
 (0)