From a713600ff8bb34d01dda51cbe76d5c98ea79352d Mon Sep 17 00:00:00 2001 From: avadesian Date: Tue, 6 Jul 2021 10:45:49 +0800 Subject: [PATCH] prepare contributors info data for repo home --- modules/git/repo.go | 31 +++++++++++++++++++++++++++++++ routers/repo/view.go | 23 +++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/modules/git/repo.go b/modules/git/repo.go index 644ff0928..ab95bfccd 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -434,3 +434,34 @@ func GetDivergingCommits(repoPath string, baseBranch string, targetBranch string return DivergeObject{ahead, behind}, nil } + +type Contributor struct { + CommitCnt int + Committer string + Email string +} + +func GetContributors(repoPath string) ([]Contributor, error){ + cmd := NewCommand("shortlog", "-sne", "--all") + stdout, err := cmd.RunInDir(repoPath) + if err != nil { + return nil, err + } + contributorRows := strings.Split(stdout, "\n") + if len(contributorRows) > 0 { + contributorsInfo := make([]Contributor, len(contributorRows)) + for i := 0; i < len(contributorRows); i++ { + var oneCount string = strings.Trim(contributorRows[i], " ") + number := oneCount[0:strings.Index(oneCount," ")] + commitCnt, _ := strconv.Atoi(number) + committer := oneCount[strings.Index(oneCount," "):strings.LastIndex(oneCount," ")] + committer = strings.Trim(committer, " ") + email := oneCount[strings.LastIndex(oneCount," "):] + contributorsInfo[i] = Contributor{ + commitCnt, committer, email, + } + } + return contributorsInfo, nil + } + return nil, nil +} diff --git a/routers/repo/view.go b/routers/repo/view.go index 9c9cdc06b..bff84fab2 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -567,9 +567,32 @@ func safeURL(address string) string { return u.String() } +type ContributorInfo struct { + UserInfo *models.User + Email string +} // Home render repository home page func Home(ctx *context.Context) { if len(ctx.Repo.Units) > 0 { + //get repo contributors info + contributors, err := git.GetContributors(ctx.Repo.Repository.RepoPath()) + if err != nil && contributors != nil { + fmt.Printf("contributors:%v",contributors) + contributorInfos := make([]*ContributorInfo, len(contributors)) + for _, c := range contributors { + user,err := models.GetUserByEmail(c.Email) + if err != nil { + contributorInfos = append(contributorInfos, &ContributorInfo{ + user, c.Email, + }) + }else{ + contributorInfos = append(contributorInfos, &ContributorInfo{ + nil, c.Email, + }) + } + } + ctx.Data["ContributorInfo"] = contributorInfos + } if ctx.Repo.Repository.IsBeingCreated() { task, err := models.GetMigratingTask(ctx.Repo.Repository.ID) if err != nil {