gitea源码

tree.go 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "net/http"
  6. "code.gitea.io/gitea/services/context"
  7. files_service "code.gitea.io/gitea/services/repository/files"
  8. )
  9. // GetTree get the tree of a repository.
  10. func GetTree(ctx *context.APIContext) {
  11. // swagger:operation GET /repos/{owner}/{repo}/git/trees/{sha} repository GetTree
  12. // ---
  13. // summary: Gets the tree of a repository.
  14. // produces:
  15. // - application/json
  16. // parameters:
  17. // - name: owner
  18. // in: path
  19. // description: owner of the repo
  20. // type: string
  21. // required: true
  22. // - name: repo
  23. // in: path
  24. // description: name of the repo
  25. // type: string
  26. // required: true
  27. // - name: sha
  28. // in: path
  29. // description: sha of the commit
  30. // type: string
  31. // required: true
  32. // - name: recursive
  33. // in: query
  34. // description: show all directories and files
  35. // required: false
  36. // type: boolean
  37. // - name: page
  38. // in: query
  39. // description: page number; the 'truncated' field in the response will be true if there are still more items after this page, false if the last page
  40. // required: false
  41. // type: integer
  42. // - name: per_page
  43. // in: query
  44. // description: number of items per page
  45. // required: false
  46. // type: integer
  47. // responses:
  48. // "200":
  49. // "$ref": "#/responses/GitTreeResponse"
  50. // "400":
  51. // "$ref": "#/responses/error"
  52. // "404":
  53. // "$ref": "#/responses/notFound"
  54. sha := ctx.PathParam("sha")
  55. if len(sha) == 0 {
  56. ctx.APIError(http.StatusBadRequest, "sha not provided")
  57. return
  58. }
  59. if tree, err := files_service.GetTreeBySHA(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, sha, ctx.FormInt("page"), ctx.FormInt("per_page"), ctx.FormBool("recursive")); err != nil {
  60. ctx.APIError(http.StatusBadRequest, err.Error())
  61. } else {
  62. ctx.SetTotalCountHeader(int64(tree.TotalCount))
  63. ctx.JSON(http.StatusOK, tree)
  64. }
  65. }