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

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