本站源代码
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

286 linhas
7.0KB

  1. // Copyright 2018 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "math"
  8. "strconv"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/setting"
  14. api "code.gitea.io/gitea/modules/structs"
  15. )
  16. // GetSingleCommit get a commit via
  17. func GetSingleCommit(ctx *context.APIContext) {
  18. // swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
  19. // ---
  20. // summary: Get a single commit from a repository
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: owner
  25. // in: path
  26. // description: owner of the repo
  27. // type: string
  28. // required: true
  29. // - name: repo
  30. // in: path
  31. // description: name of the repo
  32. // type: string
  33. // required: true
  34. // - name: sha
  35. // in: path
  36. // description: the commit hash
  37. // type: string
  38. // required: true
  39. // responses:
  40. // "200":
  41. // "$ref": "#/responses/Commit"
  42. // "404":
  43. // "$ref": "#/responses/notFound"
  44. gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
  45. if err != nil {
  46. ctx.ServerError("OpenRepository", err)
  47. return
  48. }
  49. commit, err := gitRepo.GetCommit(ctx.Params(":sha"))
  50. if err != nil {
  51. ctx.NotFoundOrServerError("GetCommit", git.IsErrNotExist, err)
  52. return
  53. }
  54. json, err := toCommit(ctx, ctx.Repo.Repository, commit, nil)
  55. if err != nil {
  56. ctx.ServerError("toCommit", err)
  57. return
  58. }
  59. ctx.JSON(200, json)
  60. }
  61. // GetAllCommits get all commits via
  62. func GetAllCommits(ctx *context.APIContext) {
  63. // swagger:operation GET /repos/{owner}/{repo}/commits repository repoGetAllCommits
  64. // ---
  65. // summary: Get a list of all commits from a repository
  66. // produces:
  67. // - application/json
  68. // parameters:
  69. // - name: owner
  70. // in: path
  71. // description: owner of the repo
  72. // type: string
  73. // required: true
  74. // - name: repo
  75. // in: path
  76. // description: name of the repo
  77. // type: string
  78. // required: true
  79. // - name: sha
  80. // in: query
  81. // description: SHA or branch to start listing commits from (usually 'master')
  82. // type: string
  83. // - name: page
  84. // in: query
  85. // description: page number of requested commits
  86. // type: integer
  87. // responses:
  88. // "200":
  89. // "$ref": "#/responses/CommitList"
  90. // "404":
  91. // "$ref": "#/responses/notFound"
  92. // "409":
  93. // "$ref": "#/responses/EmptyRepository"
  94. if ctx.Repo.Repository.IsEmpty {
  95. ctx.JSON(409, api.APIError{
  96. Message: "Git Repository is empty.",
  97. URL: setting.API.SwaggerURL,
  98. })
  99. return
  100. }
  101. gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
  102. if err != nil {
  103. ctx.ServerError("OpenRepository", err)
  104. return
  105. }
  106. page := ctx.QueryInt("page")
  107. if page <= 0 {
  108. page = 1
  109. }
  110. sha := ctx.Query("sha")
  111. var baseCommit *git.Commit
  112. if len(sha) == 0 {
  113. // no sha supplied - use default branch
  114. head, err := gitRepo.GetHEADBranch()
  115. if err != nil {
  116. ctx.ServerError("GetHEADBranch", err)
  117. return
  118. }
  119. baseCommit, err = gitRepo.GetBranchCommit(head.Name)
  120. if err != nil {
  121. ctx.ServerError("GetCommit", err)
  122. return
  123. }
  124. } else {
  125. // get commit specified by sha
  126. baseCommit, err = gitRepo.GetCommit(sha)
  127. if err != nil {
  128. ctx.ServerError("GetCommit", err)
  129. return
  130. }
  131. }
  132. // Total commit count
  133. commitsCountTotal, err := baseCommit.CommitsCount()
  134. if err != nil {
  135. ctx.ServerError("GetCommitsCount", err)
  136. return
  137. }
  138. pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(git.CommitsRangeSize)))
  139. // Query commits
  140. commits, err := baseCommit.CommitsByRange(page)
  141. if err != nil {
  142. ctx.ServerError("CommitsByRange", err)
  143. return
  144. }
  145. userCache := make(map[string]*models.User)
  146. apiCommits := make([]*api.Commit, commits.Len())
  147. i := 0
  148. for commitPointer := commits.Front(); commitPointer != nil; commitPointer = commitPointer.Next() {
  149. commit := commitPointer.Value.(*git.Commit)
  150. // Create json struct
  151. apiCommits[i], err = toCommit(ctx, ctx.Repo.Repository, commit, userCache)
  152. if err != nil {
  153. ctx.ServerError("toCommit", err)
  154. return
  155. }
  156. i++
  157. }
  158. ctx.SetLinkHeader(int(commitsCountTotal), git.CommitsRangeSize)
  159. ctx.Header().Set("X-Page", strconv.Itoa(page))
  160. ctx.Header().Set("X-PerPage", strconv.Itoa(git.CommitsRangeSize))
  161. ctx.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10))
  162. ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount))
  163. ctx.Header().Set("X-HasMore", strconv.FormatBool(page < pageCount))
  164. ctx.JSON(200, &apiCommits)
  165. }
  166. func toCommit(ctx *context.APIContext, repo *models.Repository, commit *git.Commit, userCache map[string]*models.User) (*api.Commit, error) {
  167. var apiAuthor, apiCommitter *api.User
  168. // Retrieve author and committer information
  169. var cacheAuthor *models.User
  170. var ok bool
  171. if userCache == nil {
  172. cacheAuthor = ((*models.User)(nil))
  173. ok = false
  174. } else {
  175. cacheAuthor, ok = userCache[commit.Author.Email]
  176. }
  177. if ok {
  178. apiAuthor = cacheAuthor.APIFormat()
  179. } else {
  180. author, err := models.GetUserByEmail(commit.Author.Email)
  181. if err != nil && !models.IsErrUserNotExist(err) {
  182. return nil, err
  183. } else if err == nil {
  184. apiAuthor = author.APIFormat()
  185. if userCache != nil {
  186. userCache[commit.Author.Email] = author
  187. }
  188. }
  189. }
  190. var cacheCommitter *models.User
  191. if userCache == nil {
  192. cacheCommitter = ((*models.User)(nil))
  193. ok = false
  194. } else {
  195. cacheCommitter, ok = userCache[commit.Committer.Email]
  196. }
  197. if ok {
  198. apiCommitter = cacheCommitter.APIFormat()
  199. } else {
  200. committer, err := models.GetUserByEmail(commit.Committer.Email)
  201. if err != nil && !models.IsErrUserNotExist(err) {
  202. return nil, err
  203. } else if err == nil {
  204. apiCommitter = committer.APIFormat()
  205. if userCache != nil {
  206. userCache[commit.Committer.Email] = committer
  207. }
  208. }
  209. }
  210. // Retrieve parent(s) of the commit
  211. apiParents := make([]*api.CommitMeta, commit.ParentCount())
  212. for i := 0; i < commit.ParentCount(); i++ {
  213. sha, _ := commit.ParentID(i)
  214. apiParents[i] = &api.CommitMeta{
  215. URL: repo.APIURL() + "/git/commits/" + sha.String(),
  216. SHA: sha.String(),
  217. }
  218. }
  219. return &api.Commit{
  220. CommitMeta: &api.CommitMeta{
  221. URL: repo.APIURL() + "/git/commits/" + commit.ID.String(),
  222. SHA: commit.ID.String(),
  223. },
  224. HTMLURL: repo.HTMLURL() + "/commit/" + commit.ID.String(),
  225. RepoCommit: &api.RepoCommit{
  226. URL: repo.APIURL() + "/git/commits/" + commit.ID.String(),
  227. Author: &api.CommitUser{
  228. Identity: api.Identity{
  229. Name: commit.Committer.Name,
  230. Email: commit.Committer.Email,
  231. },
  232. Date: commit.Author.When.Format(time.RFC3339),
  233. },
  234. Committer: &api.CommitUser{
  235. Identity: api.Identity{
  236. Name: commit.Committer.Name,
  237. Email: commit.Committer.Email,
  238. },
  239. Date: commit.Committer.When.Format(time.RFC3339),
  240. },
  241. Message: commit.Summary(),
  242. Tree: &api.CommitMeta{
  243. URL: repo.APIURL() + "/git/trees/" + commit.ID.String(),
  244. SHA: commit.ID.String(),
  245. },
  246. },
  247. Author: apiAuthor,
  248. Committer: apiCommitter,
  249. Parents: apiParents,
  250. }, nil
  251. }
上海开阖软件有限公司 沪ICP备12045867号-1