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.

142 lines
3.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "code.gitea.io/git"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/routers/repo"
  10. )
  11. // GetRawFile get a file by path on a repository
  12. func GetRawFile(ctx *context.APIContext) {
  13. // swagger:operation GET /repos/{owner}/{repo}/raw/{filepath} repository repoGetRawFile
  14. // ---
  15. // summary: Get a file from a repository
  16. // produces:
  17. // - application/json
  18. // parameters:
  19. // - name: owner
  20. // in: path
  21. // description: owner of the repo
  22. // type: string
  23. // required: true
  24. // - name: repo
  25. // in: path
  26. // description: name of the repo
  27. // type: string
  28. // required: true
  29. // - name: filepath
  30. // in: path
  31. // description: filepath of the file to get
  32. // type: string
  33. // required: true
  34. // responses:
  35. // 200:
  36. if !ctx.Repo.HasAccess() {
  37. ctx.Status(404)
  38. return
  39. }
  40. if ctx.Repo.Repository.IsBare {
  41. ctx.Status(404)
  42. return
  43. }
  44. blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
  45. if err != nil {
  46. if git.IsErrNotExist(err) {
  47. ctx.Status(404)
  48. } else {
  49. ctx.Error(500, "GetBlobByPath", err)
  50. }
  51. return
  52. }
  53. if err = repo.ServeBlob(ctx.Context, blob); err != nil {
  54. ctx.Error(500, "ServeBlob", err)
  55. }
  56. }
  57. // GetArchive get archive of a repository
  58. func GetArchive(ctx *context.APIContext) {
  59. // swagger:operation GET /repos/{owner}/{repo}/archive/{filepath} repository repoGetArchive
  60. // ---
  61. // summary: Get an archive of 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: archive
  76. // in: path
  77. // description: archive to download, consisting of a git reference and archive
  78. // type: string
  79. // required: true
  80. // responses:
  81. // 200:
  82. repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
  83. gitRepo, err := git.OpenRepository(repoPath)
  84. if err != nil {
  85. ctx.Error(500, "OpenRepository", err)
  86. return
  87. }
  88. ctx.Repo.GitRepo = gitRepo
  89. repo.Download(ctx.Context)
  90. }
  91. // GetEditorconfig get editor config of a repository
  92. func GetEditorconfig(ctx *context.APIContext) {
  93. // swagger:operation GET /repos/{owner}/{repo}/editorconfig/{filepath} repository repoGetEditorConfig
  94. // ---
  95. // summary: Get the EditorConfig definitions of a file in a repository
  96. // produces:
  97. // - application/json
  98. // parameters:
  99. // - name: owner
  100. // in: path
  101. // description: owner of the repo
  102. // type: string
  103. // required: true
  104. // - name: repo
  105. // in: path
  106. // description: name of the repo
  107. // type: string
  108. // required: true
  109. // - name: filepath
  110. // in: path
  111. // description: filepath of file to get
  112. // type: string
  113. // required: true
  114. // responses:
  115. // 200:
  116. ec, err := ctx.Repo.GetEditorconfig()
  117. if err != nil {
  118. if git.IsErrNotExist(err) {
  119. ctx.Error(404, "GetEditorconfig", err)
  120. } else {
  121. ctx.Error(500, "GetEditorconfig", err)
  122. }
  123. return
  124. }
  125. fileName := ctx.Params("filename")
  126. def := ec.GetDefinitionForFilename(fileName)
  127. if def == nil {
  128. ctx.Error(404, "GetDefinitionForFilename", err)
  129. return
  130. }
  131. ctx.JSON(200, def)
  132. }