From c8d92dd1be4cd417ba72f61b55f2a01cfbdb6d04 Mon Sep 17 00:00:00 2001 From: avadesian Date: Thu, 8 Jul 2021 10:15:28 +0800 Subject: [PATCH] fix nil pointer error --- modules/git/repo.go | 5 +++-- routers/repo/view.go | 9 ++++----- templates/repo/home.tmpl | 7 +++++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/modules/git/repo.go b/modules/git/repo.go index b544c4a45..386a8909d 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -453,8 +453,9 @@ func GetContributors(repoPath string) ([]Contributor, error){ contributorsInfo := make([]Contributor, len(contributorRows)) for i := 0; i < len(contributorRows); i++ { var oneCount string = strings.Trim(contributorRows[i], " ") - fmt.Println("-------",oneCount,"---------") - fmt.Printf("indexOf tab:%d",strings.Index(oneCount, "\t")) + if strings.Index(oneCount, "\t") < 0 { + continue + } number := oneCount[0:strings.Index(oneCount, "\t")] commitCnt, _ := strconv.Atoi(number) committer := oneCount[strings.Index(oneCount, "\t")+1:strings.LastIndex(oneCount, " ")] diff --git a/routers/repo/view.go b/routers/repo/view.go index bff84fab2..d0633f5cb 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -569,19 +569,18 @@ func safeURL(address string) string { type ContributorInfo struct { UserInfo *models.User - Email string + Email string // for contributor who is not a registered user } // 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)) + if err == nil && contributors != nil { + var contributorInfos []*ContributorInfo for _, c := range contributors { user,err := models.GetUserByEmail(c.Email) - if err != nil { + if err == nil { contributorInfos = append(contributorInfos, &ContributorInfo{ user, c.Email, }) diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index de0bd7cbd..63f80104e 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -176,7 +176,14 @@ 全部 {{svg "octicon-chevron-right" 16}} +
+ {{range .ContributorInfo}} + {{/*  {{.UserInfo.Name}}*/}} + {{if .Email}} +   {{.Email}} + {{end}} + {{end}}