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.

145 lines
3.4 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/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/routers/repo"
  9. "code.gitea.io/git"
  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. // description: success
  37. if !ctx.Repo.HasAccess() {
  38. ctx.Status(404)
  39. return
  40. }
  41. if ctx.Repo.Repository.IsBare {
  42. ctx.Status(404)
  43. return
  44. }
  45. blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
  46. if err != nil {
  47. if git.IsErrNotExist(err) {
  48. ctx.Status(404)
  49. } else {
  50. ctx.Error(500, "GetBlobByPath", err)
  51. }
  52. return
  53. }
  54. if err = repo.ServeBlob(ctx.Context, blob); err != nil {
  55. ctx.Error(500, "ServeBlob", err)
  56. }
  57. }
  58. // GetArchive get archive of a repository
  59. func GetArchive(ctx *context.APIContext) {
  60. // swagger:operation GET /repos/{owner}/{repo}/archive/{archive} repository repoGetArchive
  61. // ---
  62. // summary: Get an archive of a repository
  63. // produces:
  64. // - application/json
  65. // parameters:
  66. // - name: owner
  67. // in: path
  68. // description: owner of the repo
  69. // type: string
  70. // required: true
  71. // - name: repo
  72. // in: path
  73. // description: name of the repo
  74. // type: string
  75. // required: true
  76. // - name: archive
  77. // in: path
  78. // description: archive to download, consisting of a git reference and archive
  79. // type: string
  80. // required: true
  81. // responses:
  82. // 200:
  83. // description: success
  84. repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
  85. gitRepo, err := git.OpenRepository(repoPath)
  86. if err != nil {
  87. ctx.Error(500, "OpenRepository", err)
  88. return
  89. }
  90. ctx.Repo.GitRepo = gitRepo
  91. repo.Download(ctx.Context)
  92. }
  93. // GetEditorconfig get editor config of a repository
  94. func GetEditorconfig(ctx *context.APIContext) {
  95. // swagger:operation GET /repos/{owner}/{repo}/editorconfig/{filepath} repository repoGetEditorConfig
  96. // ---
  97. // summary: Get the EditorConfig definitions of a file in a repository
  98. // produces:
  99. // - application/json
  100. // parameters:
  101. // - name: owner
  102. // in: path
  103. // description: owner of the repo
  104. // type: string
  105. // required: true
  106. // - name: repo
  107. // in: path
  108. // description: name of the repo
  109. // type: string
  110. // required: true
  111. // - name: filepath
  112. // in: path
  113. // description: filepath of file to get
  114. // type: string
  115. // required: true
  116. // responses:
  117. // 200:
  118. // description: success
  119. ec, err := ctx.Repo.GetEditorconfig()
  120. if err != nil {
  121. if git.IsErrNotExist(err) {
  122. ctx.Error(404, "GetEditorconfig", err)
  123. } else {
  124. ctx.Error(500, "GetEditorconfig", err)
  125. }
  126. return
  127. }
  128. fileName := ctx.Params("filename")
  129. def := ec.GetDefinitionForFilename(fileName)
  130. if def == nil {
  131. ctx.Error(404, "GetDefinitionForFilename", err)
  132. return
  133. }
  134. ctx.JSON(200, def)
  135. }