From b4fbe744a103d6d58654260e1cff2159e273bf44 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Wed, 15 Feb 2023 23:40:45 +0800 Subject: [PATCH 1/5] fix panic --- routers/api/v1/repo/pull.go | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 8164b386945c1..e9c220e2ad2aa 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -28,6 +28,7 @@ import ( "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/utils" asymkey_service "code.gitea.io/gitea/services/asymkey" @@ -1437,34 +1438,23 @@ func GetPullRequestFiles(ctx *context.APIContext) { return } - listOptions := utils.GetListOptions(ctx) - totalNumberOfFiles := diff.NumFiles - totalNumberOfPages := int(math.Ceil(float64(totalNumberOfFiles) / float64(listOptions.PageSize))) - - start, end := listOptions.GetStartEnd() - - if end > totalNumberOfFiles { - end = totalNumberOfFiles - } + totalNumberOfPages := int(math.Ceil(float64(totalNumberOfFiles) / float64(maxFiles))) + lenFiles := len(diff.Files) - lenFiles := end - start - if lenFiles < 0 { - lenFiles = 0 - } apiFiles := make([]*api.ChangedFile, 0, lenFiles) - for i := start; i < end; i++ { + for i := 0; i < lenFiles; i++ { apiFiles = append(apiFiles, convert.ToChangedFile(diff.Files[i], pr.HeadRepo, endCommitID)) } - ctx.SetLinkHeader(totalNumberOfFiles, listOptions.PageSize) + ctx.SetLinkHeader(totalNumberOfFiles, maxFiles) ctx.SetTotalCountHeader(int64(totalNumberOfFiles)) - ctx.RespHeader().Set("X-Page", strconv.Itoa(listOptions.Page)) - ctx.RespHeader().Set("X-PerPage", strconv.Itoa(listOptions.PageSize)) + ctx.RespHeader().Set("X-Diff-End", diff.End) + ctx.RespHeader().Set("X-PerPage", strconv.Itoa(maxFiles)) ctx.RespHeader().Set("X-PageCount", strconv.Itoa(totalNumberOfPages)) - ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(listOptions.Page < totalNumberOfPages)) - ctx.AppendAccessControlExposeHeaders("X-Page", "X-PerPage", "X-PageCount", "X-HasMore") + ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(!util.IsEmptyString(diff.End))) + ctx.AppendAccessControlExposeHeaders("X-Diff-End", "X-PerPage", "X-PageCount", "X-HasMore") ctx.JSON(http.StatusOK, &apiFiles) } From 6ce37816b22c59a3c0c732252b81a384768b48ba Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 17 Feb 2023 23:15:00 +0800 Subject: [PATCH 2/5] fix --- routers/api/v1/repo/pull.go | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index e9c220e2ad2aa..d6c53a85f472a 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -28,7 +28,6 @@ import ( "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" - "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/utils" asymkey_service "code.gitea.io/gitea/services/asymkey" @@ -1356,10 +1355,6 @@ func GetPullRequestFiles(ctx *context.APIContext) { // type: integer // format: int64 // required: true - // - name: skip-to - // in: query - // description: skip to given file - // type: string // - name: whitespace // in: query // description: whitespace behavior @@ -1421,7 +1416,8 @@ func GetPullRequestFiles(ctx *context.APIContext) { startCommitID := prInfo.MergeBase endCommitID := headCommitID - maxLines, maxFiles := setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles + maxLines := setting.Git.MaxGitDiffLines + maxFiles := -1 // GetDiff will return all files diff, err := gitdiff.GetDiff(baseGitRepo, &gitdiff.DiffOptions{ @@ -1438,23 +1434,35 @@ func GetPullRequestFiles(ctx *context.APIContext) { return } + listOptions := utils.GetListOptions(ctx) + totalNumberOfFiles := diff.NumFiles - totalNumberOfPages := int(math.Ceil(float64(totalNumberOfFiles) / float64(maxFiles))) - lenFiles := len(diff.Files) + totalNumberOfPages := int(math.Ceil(float64(totalNumberOfFiles) / float64(listOptions.PageSize))) + + start, end := listOptions.GetStartEnd() + + if end > totalNumberOfFiles { + end = totalNumberOfFiles + } + + lenFiles := end - start + if lenFiles < 0 { + lenFiles = 0 + } apiFiles := make([]*api.ChangedFile, 0, lenFiles) - for i := 0; i < lenFiles; i++ { + for i := start; i < end; i++ { apiFiles = append(apiFiles, convert.ToChangedFile(diff.Files[i], pr.HeadRepo, endCommitID)) } - ctx.SetLinkHeader(totalNumberOfFiles, maxFiles) + ctx.SetLinkHeader(totalNumberOfFiles, listOptions.PageSize) ctx.SetTotalCountHeader(int64(totalNumberOfFiles)) - ctx.RespHeader().Set("X-Diff-End", diff.End) - ctx.RespHeader().Set("X-PerPage", strconv.Itoa(maxFiles)) + ctx.RespHeader().Set("X-Page", strconv.Itoa(listOptions.Page)) + ctx.RespHeader().Set("X-PerPage", strconv.Itoa(listOptions.PageSize)) ctx.RespHeader().Set("X-PageCount", strconv.Itoa(totalNumberOfPages)) - ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(!util.IsEmptyString(diff.End))) - ctx.AppendAccessControlExposeHeaders("X-Diff-End", "X-PerPage", "X-PageCount", "X-HasMore") + ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(listOptions.Page < totalNumberOfPages)) + ctx.AppendAccessControlExposeHeaders("X-Page", "X-PerPage", "X-PageCount", "X-HasMore") ctx.JSON(http.StatusOK, &apiFiles) } From 89965b870c920115e935edaefe6927af47a076b8 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 17 Feb 2023 23:19:49 +0800 Subject: [PATCH 3/5] =?UTF-8?q?url=20no=20longer=20accepts=20the=20?= =?UTF-8?q?=E2=80=98skip-to=E2=80=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routers/api/v1/repo/pull.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index d6c53a85f472a..bd73df640aff9 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1423,7 +1423,7 @@ func GetPullRequestFiles(ctx *context.APIContext) { &gitdiff.DiffOptions{ BeforeCommitID: startCommitID, AfterCommitID: endCommitID, - SkipTo: ctx.FormString("skip-to"), + SkipTo: "", MaxLines: maxLines, MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, MaxFiles: maxFiles, From aaa72493f3f15067b7def45c7bb61c0691e75e41 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 17 Feb 2023 23:29:19 +0800 Subject: [PATCH 4/5] swagger fix --- routers/api/v1/repo/pull.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index bd73df640aff9..50323deb56bcd 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1355,6 +1355,10 @@ func GetPullRequestFiles(ctx *context.APIContext) { // type: integer // format: int64 // required: true + // - name: skip-to + // in: query + // description: skip to given file + // type: string // - name: whitespace // in: query // description: whitespace behavior From 1f3da4f6d0ae2c26140aed96e31e5af104c6e211 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Mon, 20 Feb 2023 11:53:22 +0800 Subject: [PATCH 5/5] fix --- routers/api/v1/repo/pull.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 50323deb56bcd..6b42d07546309 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1421,16 +1421,16 @@ func GetPullRequestFiles(ctx *context.APIContext) { endCommitID := headCommitID maxLines := setting.Git.MaxGitDiffLines - maxFiles := -1 // GetDiff will return all files + // FIXME: If there are too many files in the repo, may cause some unpredictable issues. diff, err := gitdiff.GetDiff(baseGitRepo, &gitdiff.DiffOptions{ BeforeCommitID: startCommitID, AfterCommitID: endCommitID, - SkipTo: "", + SkipTo: ctx.FormString("skip-to"), MaxLines: maxLines, MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, - MaxFiles: maxFiles, + MaxFiles: -1, // GetDiff() will return all files WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.FormString("whitespace")), }) if err != nil {