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.

commits.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/gitea/modules/structs"
  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. defer gitRepo.Close()
  48. commit, err := gitRepo.GetCommit(ctx.Params(":sha"))
  49. if err != nil {
  50. ctx.NotFoundOrServerError("GetCommit", git.IsErrNotExist, err)
  51. return
  52. }
  53. // Retrieve author and committer information
  54. var apiAuthor, apiCommitter *api.User
  55. author, err := models.GetUserByEmail(commit.Author.Email)
  56. if err != nil && !models.IsErrUserNotExist(err) {
  57. ctx.ServerError("Get user by author email", err)
  58. return
  59. } else if err == nil {
  60. apiAuthor = author.APIFormat()
  61. }
  62. // Save one query if the author is also the committer
  63. if commit.Committer.Email == commit.Author.Email {
  64. apiCommitter = apiAuthor
  65. } else {
  66. committer, err := models.GetUserByEmail(commit.Committer.Email)
  67. if err != nil && !models.IsErrUserNotExist(err) {
  68. ctx.ServerError("Get user by committer email", err)
  69. return
  70. } else if err == nil {
  71. apiCommitter = committer.APIFormat()
  72. }
  73. }
  74. // Retrieve parent(s) of the commit
  75. apiParents := make([]*api.CommitMeta, commit.ParentCount())
  76. for i := 0; i < commit.ParentCount(); i++ {
  77. sha, _ := commit.ParentID(i)
  78. apiParents[i] = &api.CommitMeta{
  79. URL: ctx.Repo.Repository.APIURL() + "/git/commits/" + sha.String(),
  80. SHA: sha.String(),
  81. }
  82. }
  83. ctx.JSON(200, &api.Commit{
  84. CommitMeta: &api.CommitMeta{
  85. URL: setting.AppURL + ctx.Link[1:],
  86. SHA: commit.ID.String(),
  87. },
  88. HTMLURL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
  89. RepoCommit: &api.RepoCommit{
  90. URL: setting.AppURL + ctx.Link[1:],
  91. Author: &api.CommitUser{
  92. Identity: api.Identity{
  93. Name: commit.Author.Name,
  94. Email: commit.Author.Email,
  95. },
  96. Date: commit.Author.When.Format(time.RFC3339),
  97. },
  98. Committer: &api.CommitUser{
  99. Identity: api.Identity{
  100. Name: commit.Committer.Name,
  101. Email: commit.Committer.Email,
  102. },
  103. Date: commit.Committer.When.Format(time.RFC3339),
  104. },
  105. Message: commit.Message(),
  106. Tree: &api.CommitMeta{
  107. URL: ctx.Repo.Repository.APIURL() + "/git/trees/" + commit.ID.String(),
  108. SHA: commit.ID.String(),
  109. },
  110. },
  111. Author: apiAuthor,
  112. Committer: apiCommitter,
  113. Parents: apiParents,
  114. })
  115. }