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

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