Skip to content

Commit a031454

Browse files
authored
fix: Improve since/until when counting commits for X-Total-Count (#38243)
Follow up for #38204. --------- Signed-off-by: puni9869 <80308335+puni9869@users.noreply.github.com>
1 parent c52a07d commit a031454

3 files changed

Lines changed: 58 additions & 15 deletions

File tree

modules/gitrepo/commit_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,6 @@ func TestCommitsCount(t *testing.T) {
2121
assert.Equal(t, int64(3), commitsCount)
2222
}
2323

24-
func TestCommitsCountWithoutBase(t *testing.T) {
25-
bareRepo1 := &mockRepository{path: "repo1_bare"}
26-
27-
commitsCount, err := CommitsCount(t.Context(), bareRepo1,
28-
CommitsCountOptions{
29-
Not: "master",
30-
Revision: []string{"branch1"},
31-
})
32-
33-
assert.NoError(t, err)
34-
assert.Equal(t, int64(2), commitsCount)
35-
}
36-
3724
func TestCommitsCountWithSinceUntil(t *testing.T) {
3825
bareRepo1 := &mockRepository{path: "repo1_bare"}
3926
revision := []string{"8006ff9adbf0cb94da7dad9e537e53817f9fa5c0"}
@@ -65,6 +52,19 @@ func TestCommitsCountWithSinceUntil(t *testing.T) {
6552
}
6653
}
6754

55+
func TestCommitsCountWithoutBase(t *testing.T) {
56+
bareRepo1 := &mockRepository{path: "repo1_bare"}
57+
58+
commitsCount, err := CommitsCount(t.Context(), bareRepo1,
59+
CommitsCountOptions{
60+
Not: "master",
61+
Revision: []string{"branch1"},
62+
})
63+
64+
assert.NoError(t, err)
65+
assert.Equal(t, int64(2), commitsCount)
66+
}
67+
6868
func TestGetLatestCommitTime(t *testing.T) {
6969
bareRepo1 := &mockRepository{path: "repo1_bare"}
7070
lct, err := GetLatestCommitTime(t.Context(), bareRepo1)

routers/api/v1/repo/commits.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,27 @@ func GetAllCommits(ctx *context.APIContext) {
258258
ctx.APIErrorInternal(err)
259259
return
260260
} else if commitsCountTotal == 0 {
261-
ctx.APIErrorNotFound()
262-
return
261+
// when date filters are active, a zero count may just mean no
262+
// commits in the requested range — not that the path is invalid
263+
if since == "" && until == "" {
264+
ctx.APIErrorNotFound()
265+
return
266+
}
267+
// verify the path actually exists in the revision history
268+
totalWithoutDate, err := gitrepo.CommitsCount(ctx, ctx.Repo.Repository,
269+
gitrepo.CommitsCountOptions{
270+
Not: not,
271+
Revision: []string{sha},
272+
RelPath: []string{path},
273+
})
274+
if err != nil {
275+
ctx.APIErrorInternal(err)
276+
return
277+
}
278+
if totalWithoutDate == 0 {
279+
ctx.APIErrorNotFound()
280+
return
281+
}
263282
}
264283

265284
commits, _, err = ctx.Repo.GitRepo.CommitsByFileAndRange(

tests/integration/api_repo_git_commits_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,27 @@ func TestGetFileHistoryNotOnMaster(t *testing.T) {
241241

242242
assert.Equal(t, "1", resp.Header().Get("X-Total"))
243243
}
244+
245+
func TestGetFileHistoryEmptyDateRange(t *testing.T) {
246+
defer tests.PrepareTestEnv(t)()
247+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
248+
// Login as User2.
249+
session := loginUser(t, user.Name)
250+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
251+
252+
// readme.md exists in repo16 but no commits fall before 1970, so the date
253+
// filter yields an empty range: this must return 200 with an empty list,
254+
// not 404 (regression: a valid path with an empty date range was a 404).
255+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?path=readme.md&sha=good-sign&until=1970-01-01T00:00:00Z", user.Name).
256+
AddTokenAuth(token)
257+
resp := MakeRequest(t, req, http.StatusOK)
258+
259+
apiData := DecodeJSON(t, resp, []api.Commit{})
260+
assert.Empty(t, apiData)
261+
assert.Equal(t, "0", resp.Header().Get("X-Total"))
262+
263+
// a path that does not exist must still return 404 even with a date filter
264+
req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?path=does-not-exist.md&sha=good-sign&until=1970-01-01T00:00:00Z", user.Name).
265+
AddTokenAuth(token)
266+
MakeRequest(t, req, http.StatusNotFound)
267+
}

0 commit comments

Comments
 (0)