Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass 'not' to commit count #24473

Merged
merged 34 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
14e9c46
Pass not to commit count
mattwalo32 May 1, 2023
266e2fc
Merge branch 'main' into fix-commit-count
mattwalo32 May 1, 2023
c29eb71
Pass empty arg
mattwalo32 May 1, 2023
13a0c28
Merge branch 'main' into fix-commit-count
mattwalo32 May 1, 2023
a64283e
Bind empty string to cache fn
mattwalo32 May 1, 2023
c2fdad2
Update cache.go
mattwalo32 May 1, 2023
faac614
Merge branch 'main' into fix-commit-count
mattwalo32 May 1, 2023
76adc14
Format
mattwalo32 May 1, 2023
80e08bb
Fix param order
mattwalo32 May 1, 2023
b5d3a78
Use struct to pass opts
mattwalo32 May 2, 2023
b19cf49
Call counts with opts struct
mattwalo32 May 2, 2023
b5bd467
use struct for CommitsByFileAndRange
mattwalo32 May 2, 2023
94291cb
Format
mattwalo32 May 2, 2023
e3342a8
Test passing not
mattwalo32 May 2, 2023
7fe2568
Add tests
mattwalo32 May 2, 2023
2f3cfc2
Merge branch 'main' into fix-commit-count
mattwalo32 May 2, 2023
bd39685
Remove not param
mattwalo32 May 3, 2023
b772430
Merge branch 'main' of https://github.com/mattwalo32/gitea into fix-c…
mattwalo32 May 3, 2023
884a70d
Combine CommitsCount and CommitsCountFiles
mattwalo32 May 4, 2023
226600e
Ignore blank param
mattwalo32 May 4, 2023
f3841d1
Merge branch 'main' into fix-commit-count
mattwalo32 May 4, 2023
d0aacef
Merge branch 'main' into fix-commit-count
mattwalo32 May 4, 2023
fc9883d
Remove defaults
mattwalo32 May 5, 2023
1154aec
Merge branch 'fix-commit-count' of https://github.com/mattwalo32/gite…
mattwalo32 May 5, 2023
de452cc
Merge branch 'main' into fix-commit-count
mattwalo32 May 5, 2023
8fb7f78
Reorder args
mattwalo32 May 5, 2023
f07a4e3
Merge branch 'main' into fix-commit-count
mattwalo32 May 5, 2023
7ccbcf4
t push
mattwalo32 May 5, 2023
04bfe05
Merge branch 'main' into fix-commit-count
lunny May 6, 2023
cc65779
Merge branch 'main' into fix-commit-count
mattwalo32 May 6, 2023
54c7a88
Merge branch 'main' into fix-commit-count
mattwalo32 May 7, 2023
399766a
Remove not param from fileCommitsCount
mattwalo32 May 7, 2023
542e785
Merge branch 'main' into fix-commit-count
GiteaBot May 8, 2023
4ca7478
Merge branch 'main' into fix-commit-count
GiteaBot May 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (r *Repository) GetCommitsCount() (int64, error) {
contextName = r.CommitID
}
return cache.GetInt64(r.Repository.GetCommitsCountCacheKey(contextName, r.IsViewBranch || r.IsViewTag), func() (int64, error) {
return r.Commit.CommitsCount()
return r.Commit.CommitsCount("")
})
}

Expand All @@ -208,7 +208,13 @@ func (r *Repository) GetCommitGraphsCount(ctx context.Context, hidePRRefs bool,
if len(branches) == 0 {
return git.AllCommitsCount(ctx, r.Repository.RepoPath(), hidePRRefs, files...)
}
return git.CommitsCountFiles(ctx, r.Repository.RepoPath(), branches, files)
return git.CommitsCountFiles(ctx,
git.CommitsCountOptions{
RepoPath: r.Repository.RepoPath(),
Not: "",
Revision: branches,
RelPath: files,
})
})
}

Expand Down
36 changes: 27 additions & 9 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,28 @@ func AllCommitsCount(ctx context.Context, repoPath string, hidePRRefs bool, file
return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
}

// CommitsCountOptions the options when counting commits
type CommitsCountOptions struct {
RepoPath string
Not string
Revision []string
RelPath []string
}

// CommitsCountFiles returns number of total commits of until given revision.
func CommitsCountFiles(ctx context.Context, repoPath string, revision, relpath []string) (int64, error) {
func CommitsCountFiles(ctx context.Context, opts CommitsCountOptions) (int64, error) {
cmd := NewCommand(ctx, "rev-list", "--count")
cmd.AddDynamicArguments(revision...)
if len(relpath) > 0 {
cmd.AddDashesAndList(relpath...)

cmd.AddDynamicArguments(opts.Revision...)
if len(opts.RelPath) > 0 {
cmd.AddDashesAndList(opts.RelPath...)
}

stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath})
if opts.Not != "" {
cmd.AddOptionValues("--not", opts.Not)
}
mattwalo32 marked this conversation as resolved.
Show resolved Hide resolved

stdout, _, err := cmd.RunStdString(&RunOpts{Dir: opts.RepoPath})
if err != nil {
return 0, err
}
Expand All @@ -177,13 +190,18 @@ func CommitsCountFiles(ctx context.Context, repoPath string, revision, relpath [
}

// CommitsCount returns number of total commits of until given revision.
func CommitsCount(ctx context.Context, repoPath string, revision ...string) (int64, error) {
return CommitsCountFiles(ctx, repoPath, revision, []string{})
func CommitsCount(ctx context.Context, opts CommitsCountOptions) (int64, error) {
return CommitsCountFiles(ctx, opts)
mattwalo32 marked this conversation as resolved.
Show resolved Hide resolved
}

// CommitsCount returns number of total commits of until current revision.
func (c *Commit) CommitsCount() (int64, error) {
return CommitsCount(c.repo.Ctx, c.repo.Path, c.ID.String())
func (c *Commit) CommitsCount(not string) (int64, error) {
mattwalo32 marked this conversation as resolved.
Show resolved Hide resolved
return CommitsCount(c.repo.Ctx, CommitsCountOptions{
RepoPath: c.repo.Path,
Not: not,
Revision: []string{c.ID.String()},
RelPath: []string{},
mattwalo32 marked this conversation as resolved.
Show resolved Hide resolved
})
}

// CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize
Expand Down
24 changes: 23 additions & 1 deletion modules/git/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,33 @@ import (
func TestCommitsCount(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")

commitsCount, err := CommitsCount(DefaultContext, bareRepo1Path, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0")
commitsCount, err := CommitsCount(DefaultContext,
CommitsCountOptions{
RepoPath: bareRepo1Path,
Not: "",
Revision: []string{"8006ff9adbf0cb94da7dad9e537e53817f9fa5c0"},
RelPath: []string{},
mattwalo32 marked this conversation as resolved.
Show resolved Hide resolved
})

assert.NoError(t, err)
assert.Equal(t, int64(3), commitsCount)
}

func TestCommitsCountWithoutBase(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")

commitsCount, err := CommitsCount(DefaultContext,
CommitsCountOptions{
RepoPath: bareRepo1Path,
Not: "master",
Revision: []string{"branch1"},
RelPath: []string{},
mattwalo32 marked this conversation as resolved.
Show resolved Hide resolved
})

assert.NoError(t, err)
assert.Equal(t, int64(2), commitsCount)
}

func TestGetFullCommitID(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")

Expand Down
49 changes: 39 additions & 10 deletions modules/git/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,26 @@ func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bo
}

// FileCommitsCount return the number of files at a revision
func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
return CommitsCountFiles(repo.Ctx, repo.Path, []string{revision}, []string{file})
func (repo *Repository) FileCommitsCount(revision, file, not string) (int64, error) {
return CommitsCountFiles(repo.Ctx,
CommitsCountOptions{
RepoPath: repo.Path,
Not: not,
Revision: []string{revision},
RelPath: []string{file},
})
}

type CommitsByFileAndRangeOptions struct {
Revision string
File string
Not string
Page int
}

// CommitsByFileAndRange return the commits according revision file and the page
func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) ([]*Commit, error) {
skip := (page - 1) * setting.Git.CommitsRangeSize
func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions) ([]*Commit, error) {
skip := (opts.Page - 1) * setting.Git.CommitsRangeSize

stdoutReader, stdoutWriter := io.Pipe()
defer func() {
Expand All @@ -220,10 +233,15 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (
go func() {
stderr := strings.Builder{}
gitCmd := NewCommand(repo.Ctx, "rev-list").
AddOptionFormat("--max-count=%d", setting.Git.CommitsRangeSize*page).
AddOptionFormat("--max-count=%d", setting.Git.CommitsRangeSize*opts.Page).
AddOptionFormat("--skip=%d", skip)
gitCmd.AddDynamicArguments(revision)
gitCmd.AddDashesAndList(file)
gitCmd.AddDynamicArguments(opts.Revision)

if opts.Not != "" {
gitCmd.AddOptionValues("--not", opts.Not)
mattwalo32 marked this conversation as resolved.
Show resolved Hide resolved
}

gitCmd.AddDashesAndList(opts.File)
err := gitCmd.Run(&RunOpts{
Dir: repo.Path,
Stdout: stdoutWriter,
Expand Down Expand Up @@ -365,11 +383,22 @@ func (repo *Repository) CommitsBetweenIDs(last, before string) ([]*Commit, error

// CommitsCountBetween return numbers of commits between two commits
func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
count, err := CommitsCountFiles(repo.Ctx, repo.Path, []string{start + ".." + end}, []string{})
count, err := CommitsCountFiles(repo.Ctx, CommitsCountOptions{
RepoPath: repo.Path,
Not: "",
Revision: []string{start + ".." + end},
RelPath: []string{},
mattwalo32 marked this conversation as resolved.
Show resolved Hide resolved
})

if err != nil && strings.Contains(err.Error(), "no merge base") {
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
// previously it would return the results of git rev-list before last so let's try that...
return CommitsCountFiles(repo.Ctx, repo.Path, []string{start, end}, []string{})
return CommitsCountFiles(repo.Ctx, CommitsCountOptions{
RepoPath: repo.Path,
Not: "",
Revision: []string{start, end},
RelPath: []string{},
mattwalo32 marked this conversation as resolved.
Show resolved Hide resolved
})
}

return count, err
Expand Down Expand Up @@ -485,7 +514,7 @@ func (repo *Repository) AddLastCommitCache(cacheKey, fullName, sha string) error
if err != nil {
return 0, err
}
return commit.CommitsCount()
return commit.CommitsCount("")
})
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion modules/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func PushUpdateAddTag(ctx context.Context, repo *repo_model.Repository, gitRepo
createdAt = sig.When
}

commitsCount, err := commit.CommitsCount()
commitsCount, err := commit.CommitsCount("")
if err != nil {
return fmt.Errorf("unable to get CommitsCount: %w", err)
}
Expand Down
15 changes: 11 additions & 4 deletions routers/api/v1/repo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func GetAllCommits(ctx *context.APIContext) {

sha := ctx.FormString("sha")
path := ctx.FormString("path")
not := ctx.FormString("not")

var (
commitsCountTotal int64
Expand Down Expand Up @@ -178,14 +179,13 @@ func GetAllCommits(ctx *context.APIContext) {
}

// Total commit count
commitsCountTotal, err = baseCommit.CommitsCount()
commitsCountTotal, err = baseCommit.CommitsCount(not)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err)
return
}

// Query commits
not := ctx.FormString("not")
commits, err = baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize, not)
if err != nil {
ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
Expand All @@ -196,7 +196,7 @@ func GetAllCommits(ctx *context.APIContext) {
sha = ctx.Repo.Repository.DefaultBranch
}

commitsCountTotal, err = ctx.Repo.GitRepo.FileCommitsCount(sha, path)
commitsCountTotal, err = ctx.Repo.GitRepo.FileCommitsCount(sha, path, not)
if err != nil {
ctx.Error(http.StatusInternalServerError, "FileCommitsCount", err)
return
Expand All @@ -205,7 +205,14 @@ func GetAllCommits(ctx *context.APIContext) {
return
}

commits, err = ctx.Repo.GitRepo.CommitsByFileAndRange(sha, path, listOptions.Page)
commits, err = ctx.Repo.GitRepo.CommitsByFileAndRange(
git.CommitsByFileAndRangeOptions{
Revision: sha,
File: path,
Not: not,
Page: listOptions.Page,
})

if err != nil {
ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", err)
return
Expand Down
12 changes: 9 additions & 3 deletions routers/api/v1/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func getWikiPage(ctx *context.APIContext, wikiName wiki_service.WebPath) *api.Wi
}

// get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename)
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename, "")

// Get last change information.
lastCommit, err := wikiRepo.GetCommitByPath(pageFilename)
Expand Down Expand Up @@ -421,15 +421,21 @@ func ListPageRevisions(ctx *context.APIContext) {
}

// get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename)
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename, "")

page := ctx.FormInt("page")
if page <= 1 {
page = 1
}

// get Commit Count
commitsHistory, err := wikiRepo.CommitsByFileAndRange("master", pageFilename, page)
commitsHistory, err := wikiRepo.CommitsByFileAndRange(
git.CommitsByFileAndRangeOptions{
Revision: "master",
File: pageFilename,
Not: "",
Page: page,
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", err)
return
Expand Down
9 changes: 8 additions & 1 deletion routers/web/feed/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/util"

"github.com/gorilla/feeds"
Expand All @@ -21,7 +22,13 @@ func ShowFileFeed(ctx *context.Context, repo *repo.Repository, formatType string
if len(fileName) == 0 {
return
}
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(ctx.Repo.RefName, fileName, 1)
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
git.CommitsByFileAndRangeOptions{
Revision: ctx.Repo.RefName,
File: fileName,
Not: "",
Page: 1,
})
if err != nil {
ctx.ServerError("ShowBranchFeed", err)
return
Expand Down
10 changes: 8 additions & 2 deletions routers/web/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func FileHistory(ctx *context.Context) {
return
}

commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefName, fileName)
commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefName, fileName, "")
if err != nil {
ctx.ServerError("FileCommitsCount", err)
return
Expand All @@ -230,7 +230,13 @@ func FileHistory(ctx *context.Context) {
page = 1
}

commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(ctx.Repo.RefName, fileName, page)
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
git.CommitsByFileAndRangeOptions{
Revision: ctx.Repo.RefName,
File: fileName,
Not: "",
Page: page,
})
if err != nil {
ctx.ServerError("CommitsByFileAndRange", err)
return
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *repo_model
if err != nil {
return fmt.Errorf("GetBranchCommit: %w", err)
}
countCache[release.Target], err = commit.CommitsCount()
countCache[release.Target], err = commit.CommitsCount("")
if err != nil {
return fmt.Errorf("CommitsCount: %w", err)
}
Expand Down
12 changes: 9 additions & 3 deletions routers/web/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
}

// get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename, "")
ctx.Data["CommitCount"] = commitsCount

return wikiRepo, entry
Expand Down Expand Up @@ -366,7 +366,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
ctx.Data["footerContent"] = ""

// get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename, "")
ctx.Data["CommitCount"] = commitsCount

// get page
Expand All @@ -376,7 +376,13 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
}

// get Commit Count
commitsHistory, err := wikiRepo.CommitsByFileAndRange(wiki_service.DefaultBranch, pageFilename, page)
commitsHistory, err := wikiRepo.CommitsByFileAndRange(
git.CommitsByFileAndRangeOptions{
Revision: wiki_service.DefaultBranch,
File: pageFilename,
Not: "",
Page: page,
})
if err != nil {
if wikiRepo != nil {
wikiRepo.Close()
Expand Down
2 changes: 1 addition & 1 deletion services/migrations/gitea_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
return fmt.Errorf("GetTagCommit[%v]: %w", rel.TagName, err)
}
rel.Sha1 = commit.ID.String()
rel.NumCommits, err = commit.CommitsCount()
rel.NumCommits, err = commit.CommitsCount("")
if err != nil {
return fmt.Errorf("CommitsCount: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion services/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Rel
}

rel.Sha1 = commit.ID.String()
rel.NumCommits, err = commit.CommitsCount()
rel.NumCommits, err = commit.CommitsCount("")
if err != nil {
return false, fmt.Errorf("CommitsCount: %w", err)
}
Expand Down
Loading