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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. "fmt"
  8. "math"
  9. "net/http"
  10. "strconv"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/convert"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/setting"
  16. api "code.gitea.io/gitea/modules/structs"
  17. "code.gitea.io/gitea/modules/validation"
  18. "code.gitea.io/gitea/routers/api/v1/utils"
  19. )
  20. // GetSingleCommit get a commit via sha
  21. func GetSingleCommit(ctx *context.APIContext) {
  22. // swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
  23. // ---
  24. // summary: Get a single commit from a repository
  25. // produces:
  26. // - application/json
  27. // parameters:
  28. // - name: owner
  29. // in: path
  30. // description: owner of the repo
  31. // type: string
  32. // required: true
  33. // - name: repo
  34. // in: path
  35. // description: name of the repo
  36. // type: string
  37. // required: true
  38. // - name: sha
  39. // in: path
  40. // description: a git ref or commit sha
  41. // type: string
  42. // required: true
  43. // responses:
  44. // "200":
  45. // "$ref": "#/responses/Commit"
  46. // "422":
  47. // "$ref": "#/responses/validationError"
  48. // "404":
  49. // "$ref": "#/responses/notFound"
  50. sha := ctx.Params(":sha")
  51. if (validation.GitRefNamePatternInvalid.MatchString(sha) || !validation.CheckGitRefAdditionalRulesValid(sha)) && !git.SHAPattern.MatchString(sha) {
  52. ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha))
  53. return
  54. }
  55. getCommit(ctx, sha)
  56. }
  57. func getCommit(ctx *context.APIContext, identifier string) {
  58. gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
  59. if err != nil {
  60. ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
  61. return
  62. }
  63. defer gitRepo.Close()
  64. commit, err := gitRepo.GetCommit(identifier)
  65. if err != nil {
  66. if git.IsErrNotExist(err) {
  67. ctx.NotFound(identifier)
  68. return
  69. }
  70. ctx.Error(http.StatusInternalServerError, "gitRepo.GetCommit", err)
  71. return
  72. }
  73. json, err := convert.ToCommit(ctx.Repo.Repository, commit, nil)
  74. if err != nil {
  75. ctx.Error(http.StatusInternalServerError, "toCommit", err)
  76. return
  77. }
  78. ctx.JSON(http.StatusOK, json)
  79. }
  80. // GetAllCommits get all commits via
  81. func GetAllCommits(ctx *context.APIContext) {
  82. // swagger:operation GET /repos/{owner}/{repo}/commits repository repoGetAllCommits
  83. // ---
  84. // summary: Get a list of all commits from a repository
  85. // produces:
  86. // - application/json
  87. // parameters:
  88. // - name: owner
  89. // in: path
  90. // description: owner of the repo
  91. // type: string
  92. // required: true
  93. // - name: repo
  94. // in: path
  95. // description: name of the repo
  96. // type: string
  97. // required: true
  98. // - name: sha
  99. // in: query
  100. // description: SHA or branch to start listing commits from (usually 'master')
  101. // type: string
  102. // - name: page
  103. // in: query
  104. // description: page number of results to return (1-based)
  105. // type: integer
  106. // - name: limit
  107. // in: query
  108. // description: page size of results
  109. // type: integer
  110. // responses:
  111. // "200":
  112. // "$ref": "#/responses/CommitList"
  113. // "404":
  114. // "$ref": "#/responses/notFound"
  115. // "409":
  116. // "$ref": "#/responses/EmptyRepository"
  117. if ctx.Repo.Repository.IsEmpty {
  118. ctx.JSON(http.StatusConflict, api.APIError{
  119. Message: "Git Repository is empty.",
  120. URL: setting.API.SwaggerURL,
  121. })
  122. return
  123. }
  124. gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
  125. if err != nil {
  126. ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
  127. return
  128. }
  129. defer gitRepo.Close()
  130. listOptions := utils.GetListOptions(ctx)
  131. if listOptions.Page <= 0 {
  132. listOptions.Page = 1
  133. }
  134. if listOptions.PageSize > setting.Git.CommitsRangeSize {
  135. listOptions.PageSize = setting.Git.CommitsRangeSize
  136. }
  137. sha := ctx.FormString("sha")
  138. var baseCommit *git.Commit
  139. if len(sha) == 0 {
  140. // no sha supplied - use default branch
  141. head, err := gitRepo.GetHEADBranch()
  142. if err != nil {
  143. ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err)
  144. return
  145. }
  146. baseCommit, err = gitRepo.GetBranchCommit(head.Name)
  147. if err != nil {
  148. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  149. return
  150. }
  151. } else {
  152. // get commit specified by sha
  153. baseCommit, err = gitRepo.GetCommit(sha)
  154. if err != nil {
  155. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  156. return
  157. }
  158. }
  159. // Total commit count
  160. commitsCountTotal, err := baseCommit.CommitsCount()
  161. if err != nil {
  162. ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err)
  163. return
  164. }
  165. pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(listOptions.PageSize)))
  166. // Query commits
  167. commits, err := baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize)
  168. if err != nil {
  169. ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
  170. return
  171. }
  172. userCache := make(map[string]*models.User)
  173. apiCommits := make([]*api.Commit, len(commits))
  174. for i, commit := range commits {
  175. // Create json struct
  176. apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache)
  177. if err != nil {
  178. ctx.Error(http.StatusInternalServerError, "toCommit", err)
  179. return
  180. }
  181. }
  182. ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize)
  183. ctx.SetTotalCountHeader(commitsCountTotal)
  184. // kept for backwards compatibility
  185. ctx.Header().Set("X-Page", strconv.Itoa(listOptions.Page))
  186. ctx.Header().Set("X-PerPage", strconv.Itoa(listOptions.PageSize))
  187. ctx.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10))
  188. ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount))
  189. ctx.Header().Set("X-HasMore", strconv.FormatBool(listOptions.Page < pageCount))
  190. ctx.AppendAccessControlExposeHeaders("X-Page", "X-PerPage", "X-Total", "X-PageCount", "X-HasMore")
  191. ctx.JSON(http.StatusOK, &apiCommits)
  192. }