From 7b85452cccd3baf9b732bcd2db902e5981869521 Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Mon, 13 Dec 2021 11:44:28 +0800 Subject: [PATCH] =?UTF-8?q?fix-1052=20=E9=A1=B9=E7=9B=AE=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/git/commit.go | 9 +++++++++ modules/git/repo.go | 4 ++++ modules/git/repo_stats_custom.go | 4 ++-- routers/private/hook.go | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 5e492e27e..8e6857f4a 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -271,6 +271,15 @@ func AllCommitsCount(repoPath string) (int64, error) { return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64) } +func HEADCommitsCount(repoPath string) (int64, error) { + stdout, err := NewCommand("rev-list", "HEAD", "--count").RunInDir(repoPath) + if err != nil { + return 0, err + } + + return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64) +} + func commitsCount(repoPath, revision, relpath string) (int64, error) { cmd := NewCommand("rev-list", "--count") cmd.AddArguments(revision) diff --git a/modules/git/repo.go b/modules/git/repo.go index a2a4d28af..772f1d149 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -52,6 +52,10 @@ func (repo *Repository) GetAllCommitsCount() (int64, error) { return AllCommitsCount(repo.Path) } +func (repo *Repository) GetHeadCommitsCount() (int64, error) { + return HEADCommitsCount(repo.Path) +} + func (repo *Repository) parsePrettyFormatLogToList(logs []byte) (*list.List, error) { l := list.New() if len(logs) == 0 { diff --git a/modules/git/repo_stats_custom.go b/modules/git/repo_stats_custom.go index 95bad6678..d70a17052 100644 --- a/modules/git/repo_stats_custom.go +++ b/modules/git/repo_stats_custom.go @@ -126,7 +126,7 @@ func GetUserKPIStats(repoPath string) (map[string]*UserKPIStats, error) { func SetRepoKPIStats(repoPath string, fromTime time.Time, stats *RepoKPIStats, newContributers map[string]struct{}) error { since := fromTime.Format(time.RFC3339) - args := []string{"log", "--numstat", "--no-merges", "--branches=*", "--pretty=format:---%n%h%n%an%n%ae%n", "--date=iso", fmt.Sprintf("--since='%s'", since)} + args := []string{"log", "--numstat", "--no-merges", "HEAD", "--pretty=format:---%n%h%n%an%n%ae%n", "--date=iso", fmt.Sprintf("--since='%s'", since)} stdout, err := NewCommand(args...).RunInDirBytes(repoPath) if err != nil { @@ -212,7 +212,7 @@ func SetRepoKPIStats(repoPath string, fromTime time.Time, stats *RepoKPIStats, n func GetContributorsDetail(repoPath string, fromTime time.Time) ([]Contributor, error) { since := fromTime.Format(time.RFC3339) - cmd := NewCommand("shortlog", "-sne", "--all", fmt.Sprintf("--since='%s'", since)) + cmd := NewCommand("shortlog", "-sne", "HEAD", fmt.Sprintf("--since='%s'", since)) stdout, err := cmd.RunInDir(repoPath) if err != nil { return nil, err diff --git a/routers/private/hook.go b/routers/private/hook.go index a673362fa..79c8ecfc5 100755 --- a/routers/private/hook.go +++ b/routers/private/hook.go @@ -539,7 +539,7 @@ func updateRepoCommitCnt(ctx *macaron.Context, repo *models.Repository) error { } defer gitRepo.Close() - count, err := gitRepo.GetAllCommitsCount() + count, err := gitRepo.GetHeadCommitsCount() if err != nil { log.Error("GetAllCommitsCount failed:%v", err.Error(), ctx.Data["MsgID"]) return err