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

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