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

git_commit.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package convert
  5. import (
  6. "time"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/log"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/util"
  12. )
  13. // ToCommitUser convert a git.Signature to an api.CommitUser
  14. func ToCommitUser(sig *git.Signature) *api.CommitUser {
  15. return &api.CommitUser{
  16. Identity: api.Identity{
  17. Name: sig.Name,
  18. Email: sig.Email,
  19. },
  20. Date: sig.When.UTC().Format(time.RFC3339),
  21. }
  22. }
  23. // ToCommitMeta convert a git.Tag to an api.CommitMeta
  24. func ToCommitMeta(repo *models.Repository, tag *git.Tag) *api.CommitMeta {
  25. return &api.CommitMeta{
  26. SHA: tag.Object.String(),
  27. URL: util.URLJoin(repo.APIURL(), "git/commits", tag.ID.String()),
  28. Created: tag.Tagger.When,
  29. }
  30. }
  31. // ToPayloadCommit convert a git.Commit to api.PayloadCommit
  32. func ToPayloadCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit {
  33. authorUsername := ""
  34. if author, err := models.GetUserByEmail(c.Author.Email); err == nil {
  35. authorUsername = author.Name
  36. } else if !models.IsErrUserNotExist(err) {
  37. log.Error("GetUserByEmail: %v", err)
  38. }
  39. committerUsername := ""
  40. if committer, err := models.GetUserByEmail(c.Committer.Email); err == nil {
  41. committerUsername = committer.Name
  42. } else if !models.IsErrUserNotExist(err) {
  43. log.Error("GetUserByEmail: %v", err)
  44. }
  45. return &api.PayloadCommit{
  46. ID: c.ID.String(),
  47. Message: c.Message(),
  48. URL: util.URLJoin(repo.HTMLURL(), "commit", c.ID.String()),
  49. Author: &api.PayloadUser{
  50. Name: c.Author.Name,
  51. Email: c.Author.Email,
  52. UserName: authorUsername,
  53. },
  54. Committer: &api.PayloadUser{
  55. Name: c.Committer.Name,
  56. Email: c.Committer.Email,
  57. UserName: committerUsername,
  58. },
  59. Timestamp: c.Author.When,
  60. Verification: ToVerification(c),
  61. }
  62. }
  63. // ToCommit convert a git.Commit to api.Commit
  64. func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]*models.User) (*api.Commit, error) {
  65. var apiAuthor, apiCommitter *api.User
  66. // Retrieve author and committer information
  67. var cacheAuthor *models.User
  68. var ok bool
  69. if userCache == nil {
  70. cacheAuthor = (*models.User)(nil)
  71. ok = false
  72. } else {
  73. cacheAuthor, ok = userCache[commit.Author.Email]
  74. }
  75. if ok {
  76. apiAuthor = ToUser(cacheAuthor, false, false)
  77. } else {
  78. author, err := models.GetUserByEmail(commit.Author.Email)
  79. if err != nil && !models.IsErrUserNotExist(err) {
  80. return nil, err
  81. } else if err == nil {
  82. apiAuthor = ToUser(author, false, false)
  83. if userCache != nil {
  84. userCache[commit.Author.Email] = author
  85. }
  86. }
  87. }
  88. var cacheCommitter *models.User
  89. if userCache == nil {
  90. cacheCommitter = (*models.User)(nil)
  91. ok = false
  92. } else {
  93. cacheCommitter, ok = userCache[commit.Committer.Email]
  94. }
  95. if ok {
  96. apiCommitter = ToUser(cacheCommitter, false, false)
  97. } else {
  98. committer, err := models.GetUserByEmail(commit.Committer.Email)
  99. if err != nil && !models.IsErrUserNotExist(err) {
  100. return nil, err
  101. } else if err == nil {
  102. apiCommitter = ToUser(committer, false, false)
  103. if userCache != nil {
  104. userCache[commit.Committer.Email] = committer
  105. }
  106. }
  107. }
  108. // Retrieve parent(s) of the commit
  109. apiParents := make([]*api.CommitMeta, commit.ParentCount())
  110. for i := 0; i < commit.ParentCount(); i++ {
  111. sha, _ := commit.ParentID(i)
  112. apiParents[i] = &api.CommitMeta{
  113. URL: repo.APIURL() + "/git/commits/" + sha.String(),
  114. SHA: sha.String(),
  115. }
  116. }
  117. return &api.Commit{
  118. CommitMeta: &api.CommitMeta{
  119. URL: repo.APIURL() + "/git/commits/" + commit.ID.String(),
  120. SHA: commit.ID.String(),
  121. },
  122. HTMLURL: repo.HTMLURL() + "/commit/" + commit.ID.String(),
  123. RepoCommit: &api.RepoCommit{
  124. URL: repo.APIURL() + "/git/commits/" + commit.ID.String(),
  125. Author: &api.CommitUser{
  126. Identity: api.Identity{
  127. Name: commit.Committer.Name,
  128. Email: commit.Committer.Email,
  129. },
  130. Date: commit.Author.When.Format(time.RFC3339),
  131. },
  132. Committer: &api.CommitUser{
  133. Identity: api.Identity{
  134. Name: commit.Committer.Name,
  135. Email: commit.Committer.Email,
  136. },
  137. Date: commit.Committer.When.Format(time.RFC3339),
  138. },
  139. Message: commit.Message(),
  140. Tree: &api.CommitMeta{
  141. URL: repo.APIURL() + "/git/trees/" + commit.ID.String(),
  142. SHA: commit.ID.String(),
  143. },
  144. },
  145. Author: apiAuthor,
  146. Committer: apiCommitter,
  147. Parents: apiParents,
  148. }, nil
  149. }