mirror of
https://github.com/go-gitea/gitea.git
synced 2026-12-04 15:54:57 +00:00
perf: stop parsing the whole diff for each PR files API page (#40453)
`GET /repos/{owner}/{repo}/pulls/{index}/files` parsed every file's
patch on each request, then returned one page. Stop parsing after the
last file of the requested page.
Median HTTP response time in ms for a 2,414-file PR, 30 files per page:
| Page | Before | After |
|---|--:|--:|
| 1 | 567 | 319 |
| 41 | 563 | 457 |
| 81 (last) | 561 | 556 |
Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
1 parent
3ad403dc09
commit
f9c53fcabf
2 files changed
+13
-4
No files matched your search
@@ -1492,7 +1492,7 @@ func GetPullRequestFiles(ctx *context.APIContext) {
|
||||
startCommitID := compareInfo.CompareBase
|
||||
endCommitID := headCommitID
|
||||
|
||||
maxLines := setting.Git.MaxGitDiffLines
|
||||
listOptions := utils.GetListOptions(ctx)
|
||||
|
||||
// FIXME: If there are too many files in the repo, may cause some unpredictable issues.
|
||||
diffCommonOptions := gitdiff.DiffCommonOptions{
|
||||
@@ -1504,9 +1504,9 @@ func GetPullRequestFiles(ctx *context.APIContext) {
|
||||
&gitdiff.DiffOptions{
|
||||
DiffCommonOptions: diffCommonOptions,
|
||||
SkipTo: ctx.FormString("skip-to"),
|
||||
MaxLines: maxLines,
|
||||
MaxLines: setting.Git.MaxGitDiffLines,
|
||||
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
|
||||
MaxFiles: -1, // GetDiff() will return all files
|
||||
MaxFiles: listOptions.Page * listOptions.PageSize,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
@@ -1519,7 +1519,6 @@ func GetPullRequestFiles(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
|
||||
listOptions := utils.GetListOptions(ctx)
|
||||
totalNumberOfFiles := diffShortStat.NumFiles
|
||||
totalNumberOfPages := int(math.Ceil(float64(totalNumberOfFiles) / float64(listOptions.PageSize)))
|
||||
|
||||
|
||||
@@ -145,6 +145,16 @@ func TestAPIViewPulls(t *testing.T) {
|
||||
}))
|
||||
}
|
||||
|
||||
t.Run("APIGetPullFilesPagination", func(t *testing.T) {
|
||||
req := NewRequest(t, "GET", "/api/v1/repos/user2/commitsonpr/pulls/1/files?limit=2&page=2&skip-to=test10.txt")
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
assert.Equal(t, "true", resp.Header().Get("X-HasMore"))
|
||||
files := DecodeJSON(t, resp, []*api.ChangedFile{})
|
||||
require.Len(t, files, 2)
|
||||
assert.Equal(t, "test3.txt", files[0].Filename)
|
||||
assert.Equal(t, "test4.txt", files[1].Filename)
|
||||
})
|
||||
|
||||
t.Run("DefaultState", func(t *testing.T) {
|
||||
writeToken := getTokenForLoggedInUser(t, ctx.Session, auth_model.AccessTokenScopeWriteRepository)
|
||||
req := NewRequestWithJSON(t, http.MethodPatch, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/3", owner.Name, repo.Name), &api.EditPullRequestOption{
|
||||
|
||||
Reference in new issue
Block a user