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.8KB

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