[BUG] Fix panic on too high page number

- Fixes a panic where the file history router would panic if the page
number was set to a page where no commits would be returned. It now
returns a 404 in such case.
- Regresion of a5b1c1b0b3
- Panic log provided by @algernon.
- Minimal integration test added.

(cherry picked from commit 6a49e3f468)

Co-authored-by: Gergely Nagy <forgejo@gergo.csillger.hu>
This commit is contained in:
Gusted 2024-07-21 01:59:12 +02:00
parent 1f9c3040dc
commit 2eac7b1402
No known key found for this signature in database
GPG key ID: FD821B732837125F
2 changed files with 24 additions and 0 deletions

View file

@ -244,6 +244,12 @@ func FileHistory(ctx *context.Context) {
ctx.ServerError("CommitsByFileAndRange", err)
return
}
if len(commits) == 0 {
ctx.NotFound("CommitsByFileAndRange", nil)
return
}
oldestCommit := commits[len(commits)-1]
renamedFiles, err := git.GetCommitFileRenames(ctx, ctx.Repo.GitRepo.Path, oldestCommit.ID.String())

View file

@ -995,3 +995,21 @@ func TestViewRepoOpenWith(t *testing.T) {
testOpenWith([]string{"test://"})
})
}
func TestFileHistoryPager(t *testing.T) {
defer tests.PrepareTestEnv(t)()
t.Run("Normal page number", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master/README.md?page=1")
MakeRequest(t, req, http.StatusOK)
})
t.Run("Too high page number", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master/README.md?page=9999")
MakeRequest(t, req, http.StatusNotFound)
})
}