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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/routers/repo"
  10. "code.gitea.io/git"
  11. )
  12. // GetRawFile get a file by path on a repository
  13. func GetRawFile(ctx *context.APIContext) {
  14. // swagger:operation GET /repos/{owner}/{repo}/raw/{filepath} repository repoGetRawFile
  15. // ---
  16. // summary: Get a file from a repository
  17. // produces:
  18. // - application/json
  19. // parameters:
  20. // - name: owner
  21. // in: path
  22. // description: owner of the repo
  23. // type: string
  24. // required: true
  25. // - name: repo
  26. // in: path
  27. // description: name of the repo
  28. // type: string
  29. // required: true
  30. // - name: filepath
  31. // in: path
  32. // description: filepath of the file to get
  33. // type: string
  34. // required: true
  35. // responses:
  36. // 200:
  37. // description: success
  38. if ctx.Repo.Repository.IsEmpty {
  39. ctx.Status(404)
  40. return
  41. }
  42. blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
  43. if err != nil {
  44. if git.IsErrNotExist(err) {
  45. ctx.Status(404)
  46. } else {
  47. ctx.Error(500, "GetBlobByPath", err)
  48. }
  49. return
  50. }
  51. if err = repo.ServeBlob(ctx.Context, blob); err != nil {
  52. ctx.Error(500, "ServeBlob", err)
  53. }
  54. }
  55. // GetArchive get archive of a repository
  56. func GetArchive(ctx *context.APIContext) {
  57. // swagger:operation GET /repos/{owner}/{repo}/archive/{archive} repository repoGetArchive
  58. // ---
  59. // summary: Get an archive of a repository
  60. // produces:
  61. // - application/json
  62. // parameters:
  63. // - name: owner
  64. // in: path
  65. // description: owner of the repo
  66. // type: string
  67. // required: true
  68. // - name: repo
  69. // in: path
  70. // description: name of the repo
  71. // type: string
  72. // required: true
  73. // - name: archive
  74. // in: path
  75. // description: archive to download, consisting of a git reference and archive
  76. // type: string
  77. // required: true
  78. // responses:
  79. // 200:
  80. // description: success
  81. repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
  82. gitRepo, err := git.OpenRepository(repoPath)
  83. if err != nil {
  84. ctx.Error(500, "OpenRepository", err)
  85. return
  86. }
  87. ctx.Repo.GitRepo = gitRepo
  88. repo.Download(ctx.Context)
  89. }
  90. // GetEditorconfig get editor config of a repository
  91. func GetEditorconfig(ctx *context.APIContext) {
  92. // swagger:operation GET /repos/{owner}/{repo}/editorconfig/{filepath} repository repoGetEditorConfig
  93. // ---
  94. // summary: Get the EditorConfig definitions of a file in a repository
  95. // produces:
  96. // - application/json
  97. // parameters:
  98. // - name: owner
  99. // in: path
  100. // description: owner of the repo
  101. // type: string
  102. // required: true
  103. // - name: repo
  104. // in: path
  105. // description: name of the repo
  106. // type: string
  107. // required: true
  108. // - name: filepath
  109. // in: path
  110. // description: filepath of file to get
  111. // type: string
  112. // required: true
  113. // responses:
  114. // 200:
  115. // description: success
  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. }