From dc9cd9e168ad55b1134731a00aa206daaad5024a Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 28 Feb 2023 10:48:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=B4=A1=E7=8C=AE?= =?UTF-8?q?=E8=80=85=E6=96=B0=E6=96=B9=E6=A1=88=E8=A7=A3=E5=86=B3=E6=95=88?= =?UTF-8?q?=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/git/repo_contributors.go | 88 ++++++++++++++++++++++++++++++++ routers/hat/repo/repo.go | 5 +- 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 modules/git/repo_contributors.go diff --git a/modules/git/repo_contributors.go b/modules/git/repo_contributors.go new file mode 100644 index 0000000..f7646cc --- /dev/null +++ b/modules/git/repo_contributors.go @@ -0,0 +1,88 @@ +package git + +import ( + "bufio" + "context" + "os" + "strconv" + "strings" + + gitea_git "code.gitea.io/gitea/modules/git" +) + +type RepoContributor struct { + Name string `json:"name"` + Email string `json:"email"` + Commits int `json:"commits"` +} + +func GetRepoContributors(repo *gitea_git.Repository, page, pageSize int) (int, []*RepoContributor, error) { + var total, skip int + var contributors []*RepoContributor + + skip = (page - 1) * pageSize + + stdoutReader, stdoutWriter, err := os.Pipe() + if err != nil { + return total, nil, err + } + defer func() { + _ = stdoutReader.Close() + _ = stdoutWriter.Close() + }() + cmd := gitea_git.NewCommand(repo.Ctx, "shortlog", "-sne", "HEAD") + + stderr := new(strings.Builder) + err = cmd.Run(&gitea_git.RunOpts{ + Env: []string{}, + Dir: repo.Path, + Stdout: stdoutWriter, + Stderr: stderr, + PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error { + _ = stdoutWriter.Close() + scanner := bufio.NewScanner(stdoutReader) + scanner.Split(bufio.ScanLines) + var ca int + for scanner.Scan() { + total++ + if skip > 0 { + skip-- + } else { + if ca < pageSize { + l := strings.TrimSpace(scanner.Text()) + commits := l[0:strings.Index(l, "\t")] + commitsInt, err := strconv.Atoi(commits) + if err != nil { + return err + } + name := l[strings.Index(l, "\t")+1 : strings.Index(l, " <")] + email := l[strings.Index(l, "<")+1 : strings.Index(l, ">")] + contributors = append(contributors, &RepoContributor{ + Commits: commitsInt, + Name: name, + Email: email, + }) + ca++ + } + } + } + + _ = stdoutReader.Close() + return nil + }, + }) + + // skip := (page - 1) * pageSize + // for _, con := range contributorsArr { + // if skip > 0 { + // contributors = append(contributors, &RepoContributor{ + // Commits: con, + // }) + // skip-- + + // } + + // } + + return total, contributors, nil +} diff --git a/routers/hat/repo/repo.go b/routers/hat/repo/repo.go index a92c2c0..8e206ba 100644 --- a/routers/hat/repo/repo.go +++ b/routers/hat/repo/repo.go @@ -483,11 +483,10 @@ func SetDiffViewStyle(ctx *context.Context) { func GetContributors(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) - branch := ctx.FormString("branch") - total, list, err := hat_git.GetPaginateCodeAuthorsWithoutSince(ctx.Repo.GitRepo, branch, listOptions.Page, listOptions.PageSize) + total, list, err := hat_git.GetRepoContributors(ctx.Repo.GitRepo, listOptions.Page, listOptions.PageSize) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetContributors", err) + ctx.Error(http.StatusInternalServerError, "GetRepoContributors", err) return }