Skip to content

Commit 87e5623

Browse files
committed
feat(api): add date range filtering to commit retrieval endpoints
- Add support for filtering commits by date range via new "since" and "until" parameters - Update API endpoints and command logic to handle the new parameters for fetching commits within given dates - Extend API documentation and Swagger specs to describe the new "since" and "until" query parameters - Refactor related function signatures and implementations to accept and pass "since" and "until" values Signed-off-by: appleboy <[email protected]>
1 parent 9723810 commit 87e5623

File tree

6 files changed

+56
-6
lines changed

6 files changed

+56
-6
lines changed

modules/git/commit.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ type CommitsCountOptions struct {
166166
Not string
167167
Revision []string
168168
RelPath []string
169+
Since string
170+
Until string
169171
}
170172

171173
// CommitsCount returns number of total commits of until given revision.
@@ -199,8 +201,8 @@ func (c *Commit) CommitsCount() (int64, error) {
199201
}
200202

201203
// CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize
202-
func (c *Commit) CommitsByRange(page, pageSize int, not string) ([]*Commit, error) {
203-
return c.repo.commitsByRange(c.ID, page, pageSize, not)
204+
func (c *Commit) CommitsByRange(page, pageSize int, not, since, until string) ([]*Commit, error) {
205+
return c.repo.commitsByRangeWithTime(c.ID, page, pageSize, not, since, until)
204206
}
205207

206208
// CommitsBefore returns all the commits before current revision

modules/git/repo_commit.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
8989
return commits[0], nil
9090
}
9191

92-
func (repo *Repository) commitsByRange(id ObjectID, page, pageSize int, not string) ([]*Commit, error) {
92+
// commitsByRangeWithTime returns the specific page commits before current revision, with not, since, until support
93+
func (repo *Repository) commitsByRangeWithTime(id ObjectID, page, pageSize int, not, since, until string) ([]*Commit, error) {
9394
cmd := NewCommand("log").
9495
AddOptionFormat("--skip=%d", (page-1)*pageSize).
9596
AddOptionFormat("--max-count=%d", pageSize).
@@ -99,6 +100,12 @@ func (repo *Repository) commitsByRange(id ObjectID, page, pageSize int, not stri
99100
if not != "" {
100101
cmd.AddOptionValues("--not", not)
101102
}
103+
if since != "" {
104+
cmd.AddOptionFormat("--since='%s'", since)
105+
}
106+
if until != "" {
107+
cmd.AddOptionFormat("--until='%s'", until)
108+
}
102109

103110
stdout, _, err := cmd.RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path})
104111
if err != nil {
@@ -212,6 +219,8 @@ type CommitsByFileAndRangeOptions struct {
212219
File string
213220
Not string
214221
Page int
222+
Since string
223+
Until string
215224
}
216225

217226
// CommitsByFileAndRange return the commits according revision file and the page
@@ -231,6 +240,12 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions)
231240
if opts.Not != "" {
232241
gitCmd.AddOptionValues("--not", opts.Not)
233242
}
243+
if opts.Since != "" {
244+
gitCmd.AddOptionFormat("--since=%s", opts.Since)
245+
}
246+
if opts.Until != "" {
247+
gitCmd.AddOptionFormat("--until=%s", opts.Until)
248+
}
234249

235250
gitCmd.AddDashesAndList(opts.File)
236251
err := gitCmd.Run(repo.Ctx, &RunOpts{

routers/api/v1/repo/commits.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ func GetAllCommits(ctx *context.APIContext) {
116116
// in: query
117117
// description: filepath of a file/dir
118118
// type: string
119+
// - name: since
120+
// in: query
121+
// description: Only commits after this date will be returned (ISO 8601 format)
122+
// type: string
123+
// format: date-time
124+
// - name: until
125+
// in: query
126+
// description: Only commits before this date will be returned (ISO 8601 format)
127+
// type: string
128+
// format: date-time
119129
// - name: stat
120130
// in: query
121131
// description: include diff stats for every commit (disable for speedup, default 'true')
@@ -148,6 +158,9 @@ func GetAllCommits(ctx *context.APIContext) {
148158
// "409":
149159
// "$ref": "#/responses/EmptyRepository"
150160

161+
since := ctx.FormString("since")
162+
until := ctx.FormString("until")
163+
151164
if ctx.Repo.Repository.IsEmpty {
152165
ctx.JSON(http.StatusConflict, api.APIError{
153166
Message: "Git Repository is empty.",
@@ -198,14 +211,16 @@ func GetAllCommits(ctx *context.APIContext) {
198211
RepoPath: ctx.Repo.GitRepo.Path,
199212
Not: not,
200213
Revision: []string{baseCommit.ID.String()},
214+
Since: since,
215+
Until: until,
201216
})
202217
if err != nil {
203218
ctx.APIErrorInternal(err)
204219
return
205220
}
206221

207222
// Query commits
208-
commits, err = baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize, not)
223+
commits, err = baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize, not, since, until)
209224
if err != nil {
210225
ctx.APIErrorInternal(err)
211226
return
@@ -221,6 +236,8 @@ func GetAllCommits(ctx *context.APIContext) {
221236
Not: not,
222237
Revision: []string{sha},
223238
RelPath: []string{path},
239+
Since: since,
240+
Until: until,
224241
})
225242

226243
if err != nil {
@@ -237,6 +254,8 @@ func GetAllCommits(ctx *context.APIContext) {
237254
File: path,
238255
Not: not,
239256
Page: listOptions.Page,
257+
Since: since,
258+
Until: until,
240259
})
241260
if err != nil {
242261
ctx.APIErrorInternal(err)

routers/web/feed/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
// ShowBranchFeed shows tags and/or releases on the repo as RSS / Atom feed
1717
func ShowBranchFeed(ctx *context.Context, repo *repo.Repository, formatType string) {
18-
commits, err := ctx.Repo.Commit.CommitsByRange(0, 10, "")
18+
commits, err := ctx.Repo.Commit.CommitsByRange(0, 10, "", "", "")
1919
if err != nil {
2020
ctx.ServerError("ShowBranchFeed", err)
2121
return

routers/web/repo/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func Commits(ctx *context.Context) {
7878
}
7979

8080
// Both `git log branchName` and `git log commitId` work.
81-
commits, err := ctx.Repo.Commit.CommitsByRange(page, pageSize, "")
81+
commits, err := ctx.Repo.Commit.CommitsByRange(page, pageSize, "", "", "")
8282
if err != nil {
8383
ctx.ServerError("CommitsByRange", err)
8484
return

templates/swagger/v1_json.tmpl

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)