Skip to content

Commit f766b00

Browse files
mattwalo32GiteaBot
andauthored
Add ability to specify '--not' from GetAllCommits (#24409)
For my specific use case, I'd like to get all commits that are on one branch but NOT on the other branch. For instance, I'd like to get all the commits on `Branch1` that are not also on `master` (I.e. all commits that were made after `Branch1` was created). This PR adds a `not` query param that gets passed down to the `git log` command to allow the user to exclude items from `GetAllCommits`. See [git documentation](https://git-scm.com/docs/git-log#Documentation/git-log.txt---not) --------- Co-authored-by: Giteabot <[email protected]>
1 parent 241b74f commit f766b00

File tree

6 files changed

+29
-10
lines changed

6 files changed

+29
-10
lines changed

modules/git/commit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ func (c *Commit) CommitsCount() (int64, error) {
187187
}
188188

189189
// CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize
190-
func (c *Commit) CommitsByRange(page, pageSize int) ([]*Commit, error) {
191-
return c.repo.commitsByRange(c.ID, page, pageSize)
190+
func (c *Commit) CommitsByRange(page, pageSize int, not string) ([]*Commit, error) {
191+
return c.repo.commitsByRange(c.ID, page, pageSize, not)
192192
}
193193

194194
// CommitsBefore returns all the commits before current revision

modules/git/repo_commit.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,22 @@ func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
9090
return commits[0], nil
9191
}
9292

93-
func (repo *Repository) commitsByRange(id SHA1, page, pageSize int) ([]*Commit, error) {
94-
stdout, _, err := NewCommand(repo.Ctx, "log").
95-
AddOptionFormat("--skip=%d", (page-1)*pageSize).AddOptionFormat("--max-count=%d", pageSize).AddArguments(prettyLogFormat).
96-
AddDynamicArguments(id.String()).
97-
RunStdBytes(&RunOpts{Dir: repo.Path})
93+
func (repo *Repository) commitsByRange(id SHA1, page, pageSize int, not string) ([]*Commit, error) {
94+
cmd := NewCommand(repo.Ctx, "log").
95+
AddOptionFormat("--skip=%d", (page-1)*pageSize).
96+
AddOptionFormat("--max-count=%d", pageSize).
97+
AddArguments(prettyLogFormat).
98+
AddDynamicArguments(id.String())
99+
100+
if not != "" {
101+
cmd.AddOptionValues("--not", not)
102+
}
103+
104+
stdout, _, err := cmd.RunStdBytes(&RunOpts{Dir: repo.Path})
98105
if err != nil {
99106
return nil, err
100107
}
108+
101109
return repo.parsePrettyFormatLogToList(stdout)
102110
}
103111

routers/api/v1/repo/commits.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ func GetAllCommits(ctx *context.APIContext) {
115115
// in: query
116116
// description: page size of results (ignored if used with 'path')
117117
// type: integer
118+
// - name: not
119+
// in: query
120+
// description: commits that match the given specifier will not be listed.
121+
// type: string
118122
// responses:
119123
// "200":
120124
// "$ref": "#/responses/CommitList"
@@ -181,7 +185,8 @@ func GetAllCommits(ctx *context.APIContext) {
181185
}
182186

183187
// Query commits
184-
commits, err = baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize)
188+
not := ctx.FormString("not")
189+
commits, err = baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize, not)
185190
if err != nil {
186191
ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
187192
return

routers/web/feed/branch.go

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

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

routers/web/repo/commit.go

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

7272
// Both `git log branchName` and `git log commitId` work.
73-
commits, err := ctx.Repo.Commit.CommitsByRange(page, pageSize)
73+
commits, err := ctx.Repo.Commit.CommitsByRange(page, pageSize, "")
7474
if err != nil {
7575
ctx.ServerError("CommitsByRange", err)
7676
return

templates/swagger/v1_json.tmpl

Lines changed: 6 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)