You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

commits.go 7.2 kB

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