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.

122 lines
3.3 KiB

  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. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/sdk/gitea"
  13. )
  14. // GetSingleCommit get a commit via
  15. func GetSingleCommit(ctx *context.APIContext) {
  16. // swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
  17. // ---
  18. // summary: Get a single commit from a repository
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: owner
  23. // in: path
  24. // description: owner of the repo
  25. // type: string
  26. // required: true
  27. // - name: repo
  28. // in: path
  29. // description: name of the repo
  30. // type: string
  31. // required: true
  32. // - name: sha
  33. // in: path
  34. // description: the commit hash
  35. // type: string
  36. // required: true
  37. // responses:
  38. // "200":
  39. // "$ref": "#/responses/Commit"
  40. // "404":
  41. // "$ref": "#/responses/notFound"
  42. gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
  43. if err != nil {
  44. ctx.ServerError("OpenRepository", err)
  45. return
  46. }
  47. commit, err := gitRepo.GetCommit(ctx.Params(":sha"))
  48. if err != nil {
  49. ctx.NotFoundOrServerError("GetCommit", git.IsErrNotExist, err)
  50. return
  51. }
  52. // Retrieve author and committer information
  53. var apiAuthor, apiCommitter *api.User
  54. author, err := models.GetUserByEmail(commit.Author.Email)
  55. if err != nil && !models.IsErrUserNotExist(err) {
  56. ctx.ServerError("Get user by author email", err)
  57. return
  58. } else if err == nil {
  59. apiAuthor = author.APIFormat()
  60. }
  61. // Save one query if the author is also the committer
  62. if commit.Committer.Email == commit.Author.Email {
  63. apiCommitter = apiAuthor
  64. } else {
  65. committer, err := models.GetUserByEmail(commit.Committer.Email)
  66. if err != nil && !models.IsErrUserNotExist(err) {
  67. ctx.ServerError("Get user by committer email", err)
  68. return
  69. } else if err == nil {
  70. apiCommitter = committer.APIFormat()
  71. }
  72. }
  73. // Retrieve parent(s) of the commit
  74. apiParents := make([]*api.CommitMeta, commit.ParentCount())
  75. for i := 0; i < commit.ParentCount(); i++ {
  76. sha, _ := commit.ParentID(i)
  77. apiParents[i] = &api.CommitMeta{
  78. URL: ctx.Repo.Repository.APIURL() + "/git/commits/" + sha.String(),
  79. SHA: sha.String(),
  80. }
  81. }
  82. ctx.JSON(200, &api.Commit{
  83. CommitMeta: &api.CommitMeta{
  84. URL: setting.AppURL + ctx.Link[1:],
  85. SHA: commit.ID.String(),
  86. },
  87. HTMLURL: ctx.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(),
  88. RepoCommit: &api.RepoCommit{
  89. URL: setting.AppURL + ctx.Link[1:],
  90. Author: &api.CommitUser{
  91. Identity: api.Identity{
  92. Name: commit.Author.Name,
  93. Email: commit.Author.Email,
  94. },
  95. Date: commit.Author.When.Format(time.RFC3339),
  96. },
  97. Committer: &api.CommitUser{
  98. Identity: api.Identity{
  99. Name: commit.Committer.Name,
  100. Email: commit.Committer.Email,
  101. },
  102. Date: commit.Committer.When.Format(time.RFC3339),
  103. },
  104. Message: commit.Message(),
  105. Tree: &api.CommitMeta{
  106. URL: ctx.Repo.Repository.APIURL() + "/trees/" + commit.ID.String(),
  107. SHA: commit.ID.String(),
  108. },
  109. },
  110. Author: apiAuthor,
  111. Committer: apiCommitter,
  112. Parents: apiParents,
  113. })
  114. }