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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright 2018 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package repo
  5. import (
  6. "fmt"
  7. "math"
  8. "net/http"
  9. "strconv"
  10. user_model "code.gitea.io/gitea/models/user"
  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. "code.gitea.io/gitea/routers/api/v1/utils"
  16. "code.gitea.io/gitea/services/convert"
  17. )
  18. // GetSingleCommit get a commit via sha
  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: a git ref or commit sha
  39. // type: string
  40. // required: true
  41. // - name: stat
  42. // in: query
  43. // description: include diff stats for every commit (disable for speedup, default 'true')
  44. // type: boolean
  45. // - name: verification
  46. // in: query
  47. // description: include verification for every commit (disable for speedup, default 'true')
  48. // type: boolean
  49. // - name: files
  50. // in: query
  51. // description: include a list of affected files for every commit (disable for speedup, default 'true')
  52. // type: boolean
  53. // responses:
  54. // "200":
  55. // "$ref": "#/responses/Commit"
  56. // "422":
  57. // "$ref": "#/responses/validationError"
  58. // "404":
  59. // "$ref": "#/responses/notFound"
  60. sha := ctx.Params(":sha")
  61. if !git.IsValidRefPattern(sha) {
  62. ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha))
  63. return
  64. }
  65. getCommit(ctx, sha, convert.ParseCommitOptions(ctx))
  66. }
  67. func getCommit(ctx *context.APIContext, identifier string, toCommitOpts convert.ToCommitOptions) {
  68. commit, err := ctx.Repo.GitRepo.GetCommit(identifier)
  69. if err != nil {
  70. if git.IsErrNotExist(err) {
  71. ctx.NotFound(identifier)
  72. return
  73. }
  74. ctx.Error(http.StatusInternalServerError, "gitRepo.GetCommit", err)
  75. return
  76. }
  77. json, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil, toCommitOpts)
  78. if err != nil {
  79. ctx.Error(http.StatusInternalServerError, "toCommit", err)
  80. return
  81. }
  82. ctx.JSON(http.StatusOK, json)
  83. }
  84. // GetAllCommits get all commits via
  85. func GetAllCommits(ctx *context.APIContext) {
  86. // swagger:operation GET /repos/{owner}/{repo}/commits repository repoGetAllCommits
  87. // ---
  88. // summary: Get a list of all commits from a repository
  89. // produces:
  90. // - application/json
  91. // parameters:
  92. // - name: owner
  93. // in: path
  94. // description: owner of the repo
  95. // type: string
  96. // required: true
  97. // - name: repo
  98. // in: path
  99. // description: name of the repo
  100. // type: string
  101. // required: true
  102. // - name: sha
  103. // in: query
  104. // description: SHA or branch to start listing commits from (usually 'master')
  105. // type: string
  106. // - name: path
  107. // in: query
  108. // description: filepath of a file/dir
  109. // type: string
  110. // - name: stat
  111. // in: query
  112. // description: include diff stats for every commit (disable for speedup, default 'true')
  113. // type: boolean
  114. // - name: verification
  115. // in: query
  116. // description: include verification for every commit (disable for speedup, default 'true')
  117. // type: boolean
  118. // - name: files
  119. // in: query
  120. // description: include a list of affected files for every commit (disable for speedup, default 'true')
  121. // type: boolean
  122. // - name: page
  123. // in: query
  124. // description: page number of results to return (1-based)
  125. // type: integer
  126. // - name: limit
  127. // in: query
  128. // description: page size of results (ignored if used with 'path')
  129. // type: integer
  130. // - name: not
  131. // in: query
  132. // description: commits that match the given specifier will not be listed.
  133. // type: string
  134. // responses:
  135. // "200":
  136. // "$ref": "#/responses/CommitList"
  137. // "404":
  138. // "$ref": "#/responses/notFound"
  139. // "409":
  140. // "$ref": "#/responses/EmptyRepository"
  141. if ctx.Repo.Repository.IsEmpty {
  142. ctx.JSON(http.StatusConflict, api.APIError{
  143. Message: "Git Repository is empty.",
  144. URL: setting.API.SwaggerURL,
  145. })
  146. return
  147. }
  148. listOptions := utils.GetListOptions(ctx)
  149. if listOptions.Page <= 0 {
  150. listOptions.Page = 1
  151. }
  152. if listOptions.PageSize > setting.Git.CommitsRangeSize {
  153. listOptions.PageSize = setting.Git.CommitsRangeSize
  154. }
  155. sha := ctx.FormString("sha")
  156. path := ctx.FormString("path")
  157. not := ctx.FormString("not")
  158. var (
  159. commitsCountTotal int64
  160. commits []*git.Commit
  161. err error
  162. )
  163. if len(path) == 0 {
  164. var baseCommit *git.Commit
  165. if len(sha) == 0 {
  166. // no sha supplied - use default branch
  167. head, err := ctx.Repo.GitRepo.GetHEADBranch()
  168. if err != nil {
  169. ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err)
  170. return
  171. }
  172. baseCommit, err = ctx.Repo.GitRepo.GetBranchCommit(head.Name)
  173. if err != nil {
  174. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  175. return
  176. }
  177. } else {
  178. // get commit specified by sha
  179. baseCommit, err = ctx.Repo.GitRepo.GetCommit(sha)
  180. if err != nil {
  181. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  182. return
  183. }
  184. }
  185. // Total commit count
  186. commitsCountTotal, err = git.CommitsCount(ctx.Repo.GitRepo.Ctx, git.CommitsCountOptions{
  187. RepoPath: ctx.Repo.GitRepo.Path,
  188. Not: not,
  189. Revision: []string{baseCommit.ID.String()},
  190. })
  191. if err != nil {
  192. ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err)
  193. return
  194. }
  195. // Query commits
  196. commits, err = baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize, not)
  197. if err != nil {
  198. ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
  199. return
  200. }
  201. } else {
  202. if len(sha) == 0 {
  203. sha = ctx.Repo.Repository.DefaultBranch
  204. }
  205. commitsCountTotal, err = git.CommitsCount(ctx,
  206. git.CommitsCountOptions{
  207. RepoPath: ctx.Repo.GitRepo.Path,
  208. Not: not,
  209. Revision: []string{sha},
  210. RelPath: []string{path},
  211. })
  212. if err != nil {
  213. ctx.Error(http.StatusInternalServerError, "FileCommitsCount", err)
  214. return
  215. } else if commitsCountTotal == 0 {
  216. ctx.NotFound("FileCommitsCount", nil)
  217. return
  218. }
  219. commits, err = ctx.Repo.GitRepo.CommitsByFileAndRange(
  220. git.CommitsByFileAndRangeOptions{
  221. Revision: sha,
  222. File: path,
  223. Not: not,
  224. Page: listOptions.Page,
  225. })
  226. if err != nil {
  227. ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", err)
  228. return
  229. }
  230. }
  231. pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(listOptions.PageSize)))
  232. userCache := make(map[string]*user_model.User)
  233. apiCommits := make([]*api.Commit, len(commits))
  234. for i, commit := range commits {
  235. // Create json struct
  236. apiCommits[i], err = convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache, convert.ParseCommitOptions(ctx))
  237. if err != nil {
  238. ctx.Error(http.StatusInternalServerError, "toCommit", err)
  239. return
  240. }
  241. }
  242. ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize)
  243. ctx.SetTotalCountHeader(commitsCountTotal)
  244. // kept for backwards compatibility
  245. ctx.RespHeader().Set("X-Page", strconv.Itoa(listOptions.Page))
  246. ctx.RespHeader().Set("X-PerPage", strconv.Itoa(listOptions.PageSize))
  247. ctx.RespHeader().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10))
  248. ctx.RespHeader().Set("X-PageCount", strconv.Itoa(pageCount))
  249. ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(listOptions.Page < pageCount))
  250. ctx.AppendAccessControlExposeHeaders("X-Page", "X-PerPage", "X-Total", "X-PageCount", "X-HasMore")
  251. ctx.JSON(http.StatusOK, &apiCommits)
  252. }
  253. // DownloadCommitDiffOrPatch render a commit's raw diff or patch
  254. func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
  255. // swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha}.{diffType} repository repoDownloadCommitDiffOrPatch
  256. // ---
  257. // summary: Get a commit's diff or patch
  258. // produces:
  259. // - text/plain
  260. // parameters:
  261. // - name: owner
  262. // in: path
  263. // description: owner of the repo
  264. // type: string
  265. // required: true
  266. // - name: repo
  267. // in: path
  268. // description: name of the repo
  269. // type: string
  270. // required: true
  271. // - name: sha
  272. // in: path
  273. // description: SHA of the commit to get
  274. // type: string
  275. // required: true
  276. // - name: diffType
  277. // in: path
  278. // description: whether the output is diff or patch
  279. // type: string
  280. // enum: [diff, patch]
  281. // required: true
  282. // responses:
  283. // "200":
  284. // "$ref": "#/responses/string"
  285. // "404":
  286. // "$ref": "#/responses/notFound"
  287. sha := ctx.Params(":sha")
  288. diffType := git.RawDiffType(ctx.Params(":diffType"))
  289. if err := git.GetRawDiff(ctx.Repo.GitRepo, sha, diffType, ctx.Resp); err != nil {
  290. if git.IsErrNotExist(err) {
  291. ctx.NotFound(sha)
  292. return
  293. }
  294. ctx.Error(http.StatusInternalServerError, "DownloadCommitDiffOrPatch", err)
  295. return
  296. }
  297. }