You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

contributor.go 1.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package repository
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/git"
  5. )
  6. func GetRepoTopNContributors(repo *models.Repository, N int) ([]*models.ContributorInfo, int) {
  7. var contributorInfos []*models.ContributorInfo
  8. branchName := GetDefaultBranchName(repo)
  9. if branchName == "" {
  10. return contributorInfos, 0
  11. }
  12. contributors, err := git.GetContributors(repo.RepoPath(), branchName)
  13. if err == nil && contributors != nil {
  14. contributorInfoHash := make(map[string]*models.ContributorInfo)
  15. for _, c := range contributors {
  16. if len(contributorInfos) >= N {
  17. break
  18. }
  19. if c.Email == "" {
  20. continue
  21. }
  22. // get user info from committer email
  23. user, err := models.GetUserByActivateEmail(c.Email)
  24. if err == nil {
  25. // committer is system user, get info through user's primary email
  26. if existedContributorInfo, ok := contributorInfoHash[user.Email]; ok {
  27. // existed: same primary email, different committer name
  28. existedContributorInfo.CommitCnt += c.CommitCnt
  29. } else {
  30. // new committer info
  31. var newContributor = &models.ContributorInfo{
  32. user, user.RelAvatarLink(), user.Name, user.Email, c.CommitCnt,
  33. }
  34. contributorInfos = append(contributorInfos, newContributor)
  35. contributorInfoHash[user.Email] = newContributor
  36. }
  37. } else {
  38. // committer is not system user
  39. if existedContributorInfo, ok := contributorInfoHash[c.Email]; ok {
  40. // existed: same primary email, different committer name
  41. existedContributorInfo.CommitCnt += c.CommitCnt
  42. } else {
  43. var newContributor = &models.ContributorInfo{
  44. user, "", "", c.Email, c.CommitCnt,
  45. }
  46. contributorInfos = append(contributorInfos, newContributor)
  47. contributorInfoHash[c.Email] = newContributor
  48. }
  49. }
  50. }
  51. }
  52. return contributorInfos, len(contributors)
  53. }