From 14e9c462c2907f6ee225acdb5334572fbce88ca1 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Mon, 1 May 2023 10:35:22 -0700 Subject: [PATCH 01/18] Pass not to commit count --- modules/context/repo.go | 2 +- modules/git/commit.go | 14 +++++++++----- modules/git/commit_test.go | 2 +- modules/git/repo_commit.go | 10 +++++----- modules/repository/repo.go | 2 +- routers/api/v1/repo/commits.go | 6 +++--- routers/api/v1/repo/wiki.go | 4 ++-- routers/web/repo/commit.go | 2 +- routers/web/repo/release.go | 2 +- routers/web/repo/wiki.go | 4 ++-- services/migrations/gitea_uploader.go | 2 +- services/release/release.go | 2 +- services/repository/push.go | 2 +- 13 files changed, 29 insertions(+), 25 deletions(-) diff --git a/modules/context/repo.go b/modules/context/repo.go index 6b811054c231..9660a5a25b91 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -208,7 +208,7 @@ 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, r.Repository.RepoPath(), "", branches, files) }) } diff --git a/modules/git/commit.go b/modules/git/commit.go index f28c315cb572..1fca7f7915b2 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -161,13 +161,17 @@ func AllCommitsCount(ctx context.Context, repoPath string, hidePRRefs bool, file } // 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, repoPath, not string, revision, relpath []string) (int64, error) { cmd := NewCommand(ctx, "rev-list", "--count") cmd.AddDynamicArguments(revision...) if len(relpath) > 0 { cmd.AddDashesAndList(relpath...) } + if not != "" { + cmd.AddOptionValues("--not", not) + } + stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath}) if err != nil { return 0, err @@ -177,13 +181,13 @@ 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, repoPath, not string, revision ...string) (int64, error) { + return CommitsCountFiles(ctx, repoPath, not, revision, []string{}) } // 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) { + return CommitsCount(c.repo.Ctx, c.repo.Path, not, c.ID.String()) } // CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize diff --git a/modules/git/commit_test.go b/modules/git/commit_test.go index 1d6fb0018327..304369f61993 100644 --- a/modules/git/commit_test.go +++ b/modules/git/commit_test.go @@ -14,7 +14,7 @@ import ( func TestCommitsCount(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") - commitsCount, err := CommitsCount(DefaultContext, bareRepo1Path, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0") + commitsCount, err := CommitsCount(DefaultContext, bareRepo1Path, "", "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0") assert.NoError(t, err) assert.Equal(t, int64(3), commitsCount) } diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 30a82eb2970e..a3a82138ca7d 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -204,8 +204,8 @@ 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, repo.Path, not, []string{revision}, []string{file}) } // CommitsByFileAndRange return the commits according revision file and the page @@ -365,11 +365,11 @@ 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, repo.Path, "", []string{start + ".." + end}, []string{}) 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, repo.Path, "", []string{start, end}, []string{}) } return count, err @@ -485,7 +485,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 diff --git a/modules/repository/repo.go b/modules/repository/repo.go index a1dba8fc6af0..10b5ccb2ed24 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -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) } diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 401403c83dee..a6d2fb1e6c5d 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -146,6 +146,7 @@ func GetAllCommits(ctx *context.APIContext) { sha := ctx.FormString("sha") path := ctx.FormString("path") + not := ctx.FormString("not") var ( commitsCountTotal int64 @@ -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) @@ -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 diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 0b9a36ec4700..3b1cb24536b2 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -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) @@ -421,7 +421,7 @@ 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 { diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 93294f8dddb8..c2986cf67ab2 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -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 diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 689994c146cf..e837f1cc4d9b 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -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) } diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 374d1bf2e069..bcf8e519917e 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -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 @@ -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 diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 0eb34b5fe562..6eb22629522a 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -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) } diff --git a/services/release/release.go b/services/release/release.go index a9a523119776..cb2dbdb0c5ef 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -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) } diff --git a/services/repository/push.go b/services/repository/push.go index 7f174c71b3f8..57cb4e289139 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -366,7 +366,7 @@ func pushUpdateAddTags(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("CommitsCount: %w", err) } From c29eb71ec5cf56cbd29f661079c3f60c67880d81 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Mon, 1 May 2023 11:57:08 -0700 Subject: [PATCH 02/18] Pass empty arg --- modules/context/repo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/context/repo.go b/modules/context/repo.go index 9660a5a25b91..8d1150b3c56f 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -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("") }) } From a64283ec5d1953481a92db0c26a40e42855fa4a1 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Mon, 1 May 2023 12:28:12 -0700 Subject: [PATCH 03/18] Bind empty string to cache fn --- services/repository/cache.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/services/repository/cache.go b/services/repository/cache.go index 6fd4fa7250f5..afc90ede9100 100644 --- a/services/repository/cache.go +++ b/services/repository/cache.go @@ -34,8 +34,12 @@ func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep } if gitRepo.LastCommitCache == nil { - commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(getRefName(fullRefName), true), commit.CommitsCount) - if err != nil { + getCount := func() (int64, error) { + return commit.CommitsCount("") + } + + commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(getRefName(fullRefName), true), getCount) + if err != nil { return err } gitRepo.LastCommitCache = git.NewLastCommitCache(commitsCount, repo.FullName(), gitRepo, cache.GetCache()) From c2fdad2cf12797e5f694d144d60c7b818e3e2a19 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Mon, 1 May 2023 13:39:31 -0700 Subject: [PATCH 04/18] Update cache.go --- services/repository/cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/repository/cache.go b/services/repository/cache.go index afc90ede9100..915e022f9543 100644 --- a/services/repository/cache.go +++ b/services/repository/cache.go @@ -39,7 +39,7 @@ func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep } commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(getRefName(fullRefName), true), getCount) - if err != nil { + if err != nil { return err } gitRepo.LastCommitCache = git.NewLastCommitCache(commitsCount, repo.FullName(), gitRepo, cache.GetCache()) From 76adc142d1ba5a0ffce2b6fcdb84bc2818225a0f Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Mon, 1 May 2023 13:47:55 -0700 Subject: [PATCH 05/18] Format --- services/repository/cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/repository/cache.go b/services/repository/cache.go index 915e022f9543..939707240d3e 100644 --- a/services/repository/cache.go +++ b/services/repository/cache.go @@ -39,7 +39,7 @@ func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep } commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(getRefName(fullRefName), true), getCount) - if err != nil { + if err != nil { return err } gitRepo.LastCommitCache = git.NewLastCommitCache(commitsCount, repo.FullName(), gitRepo, cache.GetCache()) From 80e08bb7cb0b2eded231af0b5e8d47d1920fa618 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Mon, 1 May 2023 14:24:13 -0700 Subject: [PATCH 06/18] Fix param order --- routers/api/v1/repo/wiki.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 3b1cb24536b2..17628f564525 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -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) @@ -421,7 +421,7 @@ 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 { From b5d3a78c1346cadd2c3bbd95ec9256194516482d Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Tue, 2 May 2023 08:47:36 -0700 Subject: [PATCH 07/18] Use struct to pass opts --- modules/git/commit.go | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 1fca7f7915b2..4c56168fc55a 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -160,19 +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, not 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...) } - if not != "" { - cmd.AddOptionValues("--not", not) + if opts.Not != "" { + cmd.AddOptionValues("--not", opts.Not) } - stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath}) + stdout, _, err := cmd.RunStdString(&RunOpts{Dir: opts.RepoPath}) if err != nil { return 0, err } @@ -181,13 +190,18 @@ func CommitsCountFiles(ctx context.Context, repoPath, not string, revision, relp } // CommitsCount returns number of total commits of until given revision. -func CommitsCount(ctx context.Context, repoPath, not string, revision ...string) (int64, error) { - return CommitsCountFiles(ctx, repoPath, not, revision, []string{}) +func CommitsCount(ctx context.Context, opts CommitsCountOptions) (int64, error) { + return CommitsCountFiles(ctx, opts) } // CommitsCount returns number of total commits of until current revision. func (c *Commit) CommitsCount(not string) (int64, error) { - return CommitsCount(c.repo.Ctx, c.repo.Path, not, c.ID.String()) + return CommitsCount(c.repo.Ctx, CommitsCountOptions{ + RepoPath: c.repo.Path, + Not: not, + Revision: []string{c.ID.String()}, + RelPath: []string{}, + }) } // CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize From b19cf492b98928de797a748e1764b59419a59d31 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Tue, 2 May 2023 08:56:05 -0700 Subject: [PATCH 08/18] Call counts with opts struct --- modules/context/repo.go | 8 +++++++- modules/git/commit_test.go | 9 ++++++++- modules/git/repo_commit.go | 23 ++++++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/modules/context/repo.go b/modules/context/repo.go index 8d1150b3c56f..209b8c325df7 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -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, + }) }) } diff --git a/modules/git/commit_test.go b/modules/git/commit_test.go index 304369f61993..75a7c2bcbab8 100644 --- a/modules/git/commit_test.go +++ b/modules/git/commit_test.go @@ -14,7 +14,14 @@ 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{}, + }) + assert.NoError(t, err) assert.Equal(t, int64(3), commitsCount) } diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index a3a82138ca7d..aff9d96d4f7f 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -205,7 +205,13 @@ func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bo // FileCommitsCount return the number of files at a revision func (repo *Repository) FileCommitsCount(revision, file, not string) (int64, error) { - return CommitsCountFiles(repo.Ctx, repo.Path, not, []string{revision}, []string{file}) + return CommitsCountFiles(repo.Ctx, + CommitsCountOptions{ + RepoPath: repo.Path, + Not: not, + Revision: []string{revision}, + RelPath: []string{file}, + }) } // CommitsByFileAndRange return the commits according revision file and the page @@ -365,11 +371,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{}, + }) + 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{}, + }) } return count, err From b5bd467cd3973887584c33e2d2016439f87731be Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Tue, 2 May 2023 09:07:53 -0700 Subject: [PATCH 09/18] use struct for CommitsByFileAndRange --- modules/git/repo_commit.go | 22 +++++++++++++++++----- routers/api/v1/repo/commits.go | 9 ++++++++- routers/api/v1/repo/wiki.go | 9 ++++++++- routers/web/feed/file.go | 10 +++++++++- routers/web/repo/commit.go | 9 ++++++++- routers/web/repo/wiki.go | 9 ++++++++- 6 files changed, 58 insertions(+), 10 deletions(-) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index aff9d96d4f7f..38fbe7053a15 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -214,9 +214,16 @@ func (repo *Repository) FileCommitsCount(revision, file, not string) (int64, err }) } +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() { @@ -226,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) + } + + gitCmd.AddDashesAndList(opts.File) err := gitCmd.Run(&RunOpts{ Dir: repo.Path, Stdout: stdoutWriter, diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index a6d2fb1e6c5d..21182501548d 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -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 diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 17628f564525..37550e7c74f6 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -429,7 +429,14 @@ func ListPageRevisions(ctx *context.APIContext) { } // 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 diff --git a/routers/web/feed/file.go b/routers/web/feed/file.go index 6a8d0c454d8c..5f479cea7114 100644 --- a/routers/web/feed/file.go +++ b/routers/web/feed/file.go @@ -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" @@ -21,7 +22,14 @@ 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 diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index c2986cf67ab2..c858b5cee616 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -230,7 +230,14 @@ 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 diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index bcf8e519917e..4ebb8e7e08b4 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -376,7 +376,14 @@ 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() From 94291cbc9665ed9b42dcf0cae776bcb7d44c99b7 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Tue, 2 May 2023 09:11:04 -0700 Subject: [PATCH 10/18] Format --- routers/api/v1/repo/wiki.go | 1 - routers/web/feed/file.go | 1 - routers/web/repo/commit.go | 1 - routers/web/repo/wiki.go | 1 - 4 files changed, 4 deletions(-) diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 37550e7c74f6..88b7fb331faf 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -436,7 +436,6 @@ func ListPageRevisions(ctx *context.APIContext) { Not: "", Page: page, }) - if err != nil { ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", err) return diff --git a/routers/web/feed/file.go b/routers/web/feed/file.go index 5f479cea7114..8eaa82f88ea2 100644 --- a/routers/web/feed/file.go +++ b/routers/web/feed/file.go @@ -29,7 +29,6 @@ func ShowFileFeed(ctx *context.Context, repo *repo.Repository, formatType string Not: "", Page: 1, }) - if err != nil { ctx.ServerError("ShowBranchFeed", err) return diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index c858b5cee616..a6ed263be145 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -237,7 +237,6 @@ func FileHistory(ctx *context.Context) { Not: "", Page: page, }) - if err != nil { ctx.ServerError("CommitsByFileAndRange", err) return diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 4ebb8e7e08b4..f868d3ac33f6 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -383,7 +383,6 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) Not: "", Page: page, }) - if err != nil { if wikiRepo != nil { wikiRepo.Close() From e3342a89be3f1eb1b1cf13945a3364f153cc4ac9 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Tue, 2 May 2023 09:17:14 -0700 Subject: [PATCH 11/18] Test passing not --- modules/git/commit_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/modules/git/commit_test.go b/modules/git/commit_test.go index 75a7c2bcbab8..9763390fd25b 100644 --- a/modules/git/commit_test.go +++ b/modules/git/commit_test.go @@ -26,6 +26,21 @@ func TestCommitsCount(t *testing.T) { 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{}, + }) + + assert.NoError(t, err) + assert.Equal(t, int64(2), commitsCount) +} + func TestGetFullCommitID(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") From 7fe25684f5262115b9a90482dab1037c779bd65c Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Tue, 2 May 2023 09:36:57 -0700 Subject: [PATCH 12/18] Add tests --- .../integration/api_repo_git_commits_test.go | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/integration/api_repo_git_commits_test.go b/tests/integration/api_repo_git_commits_test.go index 17750a2799f6..d9c3d7b95283 100644 --- a/tests/integration/api_repo_git_commits_test.go +++ b/tests/integration/api_repo_git_commits_test.go @@ -58,6 +58,29 @@ func TestAPIReposGitCommitList(t *testing.T) { session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) + // Test getting commits (Page 1) + req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo20/commits?token="+token+"¬=master&sha=remove-files-a", user.Name) + resp := MakeRequest(t, req, http.StatusOK) + + var apiData []api.Commit + DecodeJSON(t, resp, &apiData) + + assert.Len(t, apiData, 2) + assert.EqualValues(t, "cfe3b3c1fd36fba04f9183287b106497e1afe986", apiData[0].CommitMeta.SHA) + compareCommitFiles(t, []string{"link_hi", "test.csv"}, apiData[0].Files) + assert.EqualValues(t, "c8e31bc7688741a5287fcde4fbb8fc129ca07027", apiData[1].CommitMeta.SHA) + compareCommitFiles(t, []string{"test.csv"}, apiData[1].Files) + + assert.EqualValues(t, resp.Header().Get("X-Total"), "2") +} + +func TestAPIReposGitCommitListNotMaster(t *testing.T) { + defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + // Login as User2. + session := loginUser(t, user.Name) + token := getTokenForLoggedInUser(t, session) + // Test getting commits (Page 1) req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token, user.Name) resp := MakeRequest(t, req, http.StatusOK) @@ -72,6 +95,8 @@ func TestAPIReposGitCommitList(t *testing.T) { compareCommitFiles(t, []string{"readme.md"}, apiData[1].Files) assert.EqualValues(t, "5099b81332712fe655e34e8dd63574f503f61811", apiData[2].CommitMeta.SHA) compareCommitFiles(t, []string{"readme.md"}, apiData[2].Files) + + assert.EqualValues(t, resp.Header().Get("X-Total"), "3") } func TestAPIReposGitCommitListPage2Empty(t *testing.T) { @@ -148,4 +173,26 @@ func TestGetFileHistory(t *testing.T) { assert.Len(t, apiData, 1) assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA) compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files) + + assert.EqualValues(t, resp.Header().Get("X-Total"), "1") +} + +func TestGetFileHistoryNotOnMaster(t *testing.T) { + defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + // Login as User2. + session := loginUser(t, user.Name) + token := getTokenForLoggedInUser(t, session) + + req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo20/commits?path=test.csv&token="+token+"&sha=add-csv¬=master", user.Name) + resp := MakeRequest(t, req, http.StatusOK) + + var apiData []api.Commit + DecodeJSON(t, resp, &apiData) + + assert.Len(t, apiData, 1) + assert.Equal(t, "c8e31bc7688741a5287fcde4fbb8fc129ca07027", apiData[0].CommitMeta.SHA) + compareCommitFiles(t, []string{"test.csv"}, apiData[0].Files) + + assert.EqualValues(t, resp.Header().Get("X-Total"), "1") } From bd39685e2390ca363b06e5571831b270391082f5 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Wed, 3 May 2023 08:50:09 -0700 Subject: [PATCH 13/18] Remove not param --- modules/context/repo.go | 2 +- modules/git/commit.go | 4 ++-- modules/git/repo_commit.go | 2 +- modules/repository/repo.go | 2 +- routers/api/v1/repo/commits.go | 8 +++++++- routers/web/repo/release.go | 2 +- services/migrations/gitea_uploader.go | 2 +- services/release/release.go | 2 +- services/repository/cache.go | 2 +- services/repository/push.go | 2 +- 10 files changed, 17 insertions(+), 11 deletions(-) diff --git a/modules/context/repo.go b/modules/context/repo.go index 209b8c325df7..153558d18196 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -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() }) } diff --git a/modules/git/commit.go b/modules/git/commit.go index 4c56168fc55a..70392923a937 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -195,10 +195,10 @@ func CommitsCount(ctx context.Context, opts CommitsCountOptions) (int64, error) } // CommitsCount returns number of total commits of until current revision. -func (c *Commit) CommitsCount(not string) (int64, error) { +func (c *Commit) CommitsCount() (int64, error) { return CommitsCount(c.repo.Ctx, CommitsCountOptions{ RepoPath: c.repo.Path, - Not: not, + Not: "", Revision: []string{c.ID.String()}, RelPath: []string{}, }) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 38fbe7053a15..c603a646b1f1 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -514,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 diff --git a/modules/repository/repo.go b/modules/repository/repo.go index 10b5ccb2ed24..a1dba8fc6af0 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -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) } diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 21182501548d..769896d00552 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -179,7 +179,13 @@ func GetAllCommits(ctx *context.APIContext) { } // Total commit count - commitsCountTotal, err = baseCommit.CommitsCount(not) + commitsCountTotal, err = git.CommitsCount(ctx.Repo.GitRepo.Ctx, git.CommitsCountOptions{ + RepoPath: ctx.Repo.GitRepo.Path, + Not: not, + Revision: []string{baseCommit.ID.String()}, + RelPath: []string{}, + }) + if err != nil { ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err) return diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index e837f1cc4d9b..689994c146cf 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -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) } diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 6eb22629522a..0eb34b5fe562 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -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) } diff --git a/services/release/release.go b/services/release/release.go index cb2dbdb0c5ef..a9a523119776 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -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) } diff --git a/services/repository/cache.go b/services/repository/cache.go index 939707240d3e..3a1554d98e34 100644 --- a/services/repository/cache.go +++ b/services/repository/cache.go @@ -35,7 +35,7 @@ func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep if gitRepo.LastCommitCache == nil { getCount := func() (int64, error) { - return commit.CommitsCount("") + return commit.CommitsCount() } commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(getRefName(fullRefName), true), getCount) diff --git a/services/repository/push.go b/services/repository/push.go index 57cb4e289139..7f174c71b3f8 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -366,7 +366,7 @@ func pushUpdateAddTags(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("CommitsCount: %w", err) } From 884a70d9ebb751a28e835c34814c4c0f1e5cf9bb Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Thu, 4 May 2023 08:21:45 -0700 Subject: [PATCH 14/18] Combine CommitsCount and CommitsCountFiles --- modules/context/repo.go | 2 +- modules/git/commit.go | 9 ++------- modules/git/repo_commit.go | 6 +++--- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/modules/context/repo.go b/modules/context/repo.go index 153558d18196..afd6d6e86e57 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -208,7 +208,7 @@ 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, + return git.CommitsCount(ctx, git.CommitsCountOptions{ RepoPath: r.Repository.RepoPath(), Not: "", diff --git a/modules/git/commit.go b/modules/git/commit.go index 70392923a937..9955d62f22db 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -168,8 +168,8 @@ type CommitsCountOptions struct { RelPath []string } -// CommitsCountFiles returns number of total commits of until given revision. -func CommitsCountFiles(ctx context.Context, opts CommitsCountOptions) (int64, error) { +// CommitsCount returns number of total commits of until given revision. +func CommitsCount(ctx context.Context, opts CommitsCountOptions) (int64, error) { cmd := NewCommand(ctx, "rev-list", "--count") cmd.AddDynamicArguments(opts.Revision...) @@ -189,11 +189,6 @@ func CommitsCountFiles(ctx context.Context, opts CommitsCountOptions) (int64, er return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64) } -// CommitsCount returns number of total commits of until given revision. -func CommitsCount(ctx context.Context, opts CommitsCountOptions) (int64, error) { - return CommitsCountFiles(ctx, opts) -} - // CommitsCount returns number of total commits of until current revision. func (c *Commit) CommitsCount() (int64, error) { return CommitsCount(c.repo.Ctx, CommitsCountOptions{ diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index c603a646b1f1..a742c4040b38 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -205,7 +205,7 @@ func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bo // FileCommitsCount return the number of files at a revision func (repo *Repository) FileCommitsCount(revision, file, not string) (int64, error) { - return CommitsCountFiles(repo.Ctx, + return CommitsCount(repo.Ctx, CommitsCountOptions{ RepoPath: repo.Path, Not: not, @@ -383,7 +383,7 @@ 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, CommitsCountOptions{ + count, err := CommitsCount(repo.Ctx, CommitsCountOptions{ RepoPath: repo.Path, Not: "", Revision: []string{start + ".." + end}, @@ -393,7 +393,7 @@ func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) { 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, CommitsCountOptions{ + return CommitsCount(repo.Ctx, CommitsCountOptions{ RepoPath: repo.Path, Not: "", Revision: []string{start, end}, From 226600ed7695e02d2dd9e9045c7af45a7e6949be Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Thu, 4 May 2023 08:27:30 -0700 Subject: [PATCH 15/18] Ignore blank param --- modules/context/repo.go | 1 - modules/git/commit.go | 1 - modules/git/commit_test.go | 1 - modules/git/repo_commit.go | 2 -- routers/api/v1/repo/wiki.go | 1 - routers/web/feed/file.go | 1 - routers/web/repo/commit.go | 1 - routers/web/repo/wiki.go | 1 - 8 files changed, 9 deletions(-) diff --git a/modules/context/repo.go b/modules/context/repo.go index afd6d6e86e57..a1c8f43644d5 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -211,7 +211,6 @@ func (r *Repository) GetCommitGraphsCount(ctx context.Context, hidePRRefs bool, return git.CommitsCount(ctx, git.CommitsCountOptions{ RepoPath: r.Repository.RepoPath(), - Not: "", Revision: branches, RelPath: files, }) diff --git a/modules/git/commit.go b/modules/git/commit.go index 9955d62f22db..8fcd4fc060d5 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -193,7 +193,6 @@ func CommitsCount(ctx context.Context, opts CommitsCountOptions) (int64, error) func (c *Commit) CommitsCount() (int64, error) { return CommitsCount(c.repo.Ctx, CommitsCountOptions{ RepoPath: c.repo.Path, - Not: "", Revision: []string{c.ID.String()}, RelPath: []string{}, }) diff --git a/modules/git/commit_test.go b/modules/git/commit_test.go index 9763390fd25b..63ae2735ce16 100644 --- a/modules/git/commit_test.go +++ b/modules/git/commit_test.go @@ -17,7 +17,6 @@ func TestCommitsCount(t *testing.T) { commitsCount, err := CommitsCount(DefaultContext, CommitsCountOptions{ RepoPath: bareRepo1Path, - Not: "", Revision: []string{"8006ff9adbf0cb94da7dad9e537e53817f9fa5c0"}, RelPath: []string{}, }) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index a742c4040b38..e5bdd82ff9d9 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -385,7 +385,6 @@ func (repo *Repository) CommitsBetweenIDs(last, before string) ([]*Commit, error func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) { count, err := CommitsCount(repo.Ctx, CommitsCountOptions{ RepoPath: repo.Path, - Not: "", Revision: []string{start + ".." + end}, RelPath: []string{}, }) @@ -395,7 +394,6 @@ func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) { // previously it would return the results of git rev-list before last so let's try that... return CommitsCount(repo.Ctx, CommitsCountOptions{ RepoPath: repo.Path, - Not: "", Revision: []string{start, end}, RelPath: []string{}, }) diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 88b7fb331faf..97b514a96591 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -433,7 +433,6 @@ func ListPageRevisions(ctx *context.APIContext) { git.CommitsByFileAndRangeOptions{ Revision: "master", File: pageFilename, - Not: "", Page: page, }) if err != nil { diff --git a/routers/web/feed/file.go b/routers/web/feed/file.go index 8eaa82f88ea2..56a9c54ddcbb 100644 --- a/routers/web/feed/file.go +++ b/routers/web/feed/file.go @@ -26,7 +26,6 @@ func ShowFileFeed(ctx *context.Context, repo *repo.Repository, formatType string git.CommitsByFileAndRangeOptions{ Revision: ctx.Repo.RefName, File: fileName, - Not: "", Page: 1, }) if err != nil { diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index a6ed263be145..3f3e15a231d6 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -234,7 +234,6 @@ func FileHistory(ctx *context.Context) { git.CommitsByFileAndRangeOptions{ Revision: ctx.Repo.RefName, File: fileName, - Not: "", Page: page, }) if err != nil { diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index f868d3ac33f6..ecff3c55ce7a 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -380,7 +380,6 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) git.CommitsByFileAndRangeOptions{ Revision: wiki_service.DefaultBranch, File: pageFilename, - Not: "", Page: page, }) if err != nil { From fc9883d7140fc8af27b308c434395558d07577d5 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Thu, 4 May 2023 21:24:51 -0700 Subject: [PATCH 16/18] Remove defaults --- modules/git/commit.go | 1 - modules/git/commit_test.go | 2 -- modules/git/repo_commit.go | 2 -- routers/api/v1/repo/commits.go | 1 - services/repository/cache.go | 6 +----- 5 files changed, 1 insertion(+), 11 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 8fcd4fc060d5..db3a7f577254 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -194,7 +194,6 @@ func (c *Commit) CommitsCount() (int64, error) { return CommitsCount(c.repo.Ctx, CommitsCountOptions{ RepoPath: c.repo.Path, Revision: []string{c.ID.String()}, - RelPath: []string{}, }) } diff --git a/modules/git/commit_test.go b/modules/git/commit_test.go index 63ae2735ce16..acf4beb029fc 100644 --- a/modules/git/commit_test.go +++ b/modules/git/commit_test.go @@ -18,7 +18,6 @@ func TestCommitsCount(t *testing.T) { CommitsCountOptions{ RepoPath: bareRepo1Path, Revision: []string{"8006ff9adbf0cb94da7dad9e537e53817f9fa5c0"}, - RelPath: []string{}, }) assert.NoError(t, err) @@ -33,7 +32,6 @@ func TestCommitsCountWithoutBase(t *testing.T) { RepoPath: bareRepo1Path, Not: "master", Revision: []string{"branch1"}, - RelPath: []string{}, }) assert.NoError(t, err) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index e5bdd82ff9d9..6d7d2488322c 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -386,7 +386,6 @@ func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) { count, err := CommitsCount(repo.Ctx, CommitsCountOptions{ RepoPath: repo.Path, Revision: []string{start + ".." + end}, - RelPath: []string{}, }) if err != nil && strings.Contains(err.Error(), "no merge base") { @@ -395,7 +394,6 @@ func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) { return CommitsCount(repo.Ctx, CommitsCountOptions{ RepoPath: repo.Path, Revision: []string{start, end}, - RelPath: []string{}, }) } diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 769896d00552..8d5004d3d451 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -183,7 +183,6 @@ func GetAllCommits(ctx *context.APIContext) { RepoPath: ctx.Repo.GitRepo.Path, Not: not, Revision: []string{baseCommit.ID.String()}, - RelPath: []string{}, }) if err != nil { diff --git a/services/repository/cache.go b/services/repository/cache.go index 3a1554d98e34..6fd4fa7250f5 100644 --- a/services/repository/cache.go +++ b/services/repository/cache.go @@ -34,11 +34,7 @@ func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep } if gitRepo.LastCommitCache == nil { - getCount := func() (int64, error) { - return commit.CommitsCount() - } - - commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(getRefName(fullRefName), true), getCount) + commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(getRefName(fullRefName), true), commit.CommitsCount) if err != nil { return err } From 8fb7f786771ce598b8bd6bbfe78bce009dc0f116 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Fri, 5 May 2023 13:59:21 -0700 Subject: [PATCH 17/18] Reorder args --- modules/git/commit.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index db3a7f577254..ff654f394d22 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -173,14 +173,15 @@ func CommitsCount(ctx context.Context, opts CommitsCountOptions) (int64, error) cmd := NewCommand(ctx, "rev-list", "--count") cmd.AddDynamicArguments(opts.Revision...) - if len(opts.RelPath) > 0 { - cmd.AddDashesAndList(opts.RelPath...) - } if opts.Not != "" { cmd.AddOptionValues("--not", opts.Not) } + if len(opts.RelPath) > 0 { + cmd.AddDashesAndList(opts.RelPath...) + } + stdout, _, err := cmd.RunStdString(&RunOpts{Dir: opts.RepoPath}) if err != nil { return 0, err From 399766aa85a959af743fa5264386a13da77f47bd Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Sun, 7 May 2023 09:56:04 -0700 Subject: [PATCH 18/18] Remove not param from fileCommitsCount --- modules/git/repo_commit.go | 3 +-- routers/api/v1/repo/commits.go | 9 ++++++++- routers/api/v1/repo/wiki.go | 4 ++-- routers/web/repo/commit.go | 2 +- routers/web/repo/wiki.go | 4 ++-- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 6d7d2488322c..6fc306362911 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -204,11 +204,10 @@ func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bo } // FileCommitsCount return the number of files at a revision -func (repo *Repository) FileCommitsCount(revision, file, not string) (int64, error) { +func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) { return CommitsCount(repo.Ctx, CommitsCountOptions{ RepoPath: repo.Path, - Not: not, Revision: []string{revision}, RelPath: []string{file}, }) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 8d5004d3d451..a5e2e7baef0d 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -201,7 +201,14 @@ func GetAllCommits(ctx *context.APIContext) { sha = ctx.Repo.Repository.DefaultBranch } - commitsCountTotal, err = ctx.Repo.GitRepo.FileCommitsCount(sha, path, not) + commitsCountTotal, err = git.CommitsCount(ctx, + git.CommitsCountOptions{ + RepoPath: ctx.Repo.GitRepo.Path, + Not: not, + Revision: []string{sha}, + RelPath: []string{path}, + }) + if err != nil { ctx.Error(http.StatusInternalServerError, "FileCommitsCount", err) return diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 97b514a96591..e33790a3789a 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -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) @@ -421,7 +421,7 @@ 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 { diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 3f3e15a231d6..e88f1139f8b7 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -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 diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index ecff3c55ce7a..a335c114be69 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -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 @@ -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