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.

114 lines
2.9 KiB

  1. // Copyright 2018 The Gitea 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/modules/context"
  7. "code.gitea.io/gitea/modules/git"
  8. api "code.gitea.io/sdk/gitea"
  9. )
  10. // GetGitAllRefs get ref or an list all the refs of a repository
  11. func GetGitAllRefs(ctx *context.APIContext) {
  12. // swagger:operation GET /repos/{owner}/{repo}/git/refs repository repoListAllGitRefs
  13. // ---
  14. // summary: Get specified ref or filtered repository's refs
  15. // produces:
  16. // - application/json
  17. // parameters:
  18. // - name: owner
  19. // in: path
  20. // description: owner of the repo
  21. // type: string
  22. // required: true
  23. // - name: repo
  24. // in: path
  25. // description: name of the repo
  26. // type: string
  27. // required: true
  28. // responses:
  29. // "200":
  30. // "$ref": "#/responses/Reference"
  31. // "$ref": "#/responses/ReferenceList"
  32. // "404":
  33. // "$ref": "#/responses/notFound"
  34. getGitRefsInternal(ctx, "")
  35. }
  36. // GetGitRefs get ref or an filteresd list of refs of a repository
  37. func GetGitRefs(ctx *context.APIContext) {
  38. // swagger:operation GET /repos/{owner}/{repo}/git/refs/{ref} repository repoListGitRefs
  39. // ---
  40. // summary: Get specified ref or filtered repository's refs
  41. // produces:
  42. // - application/json
  43. // parameters:
  44. // - name: owner
  45. // in: path
  46. // description: owner of the repo
  47. // type: string
  48. // required: true
  49. // - name: repo
  50. // in: path
  51. // description: name of the repo
  52. // type: string
  53. // required: true
  54. // - name: ref
  55. // in: path
  56. // description: part or full name of the ref
  57. // type: string
  58. // required: true
  59. // responses:
  60. // "200":
  61. // "$ref": "#/responses/Reference"
  62. // "$ref": "#/responses/ReferenceList"
  63. // "404":
  64. // "$ref": "#/responses/notFound"
  65. getGitRefsInternal(ctx, ctx.Params("*"))
  66. }
  67. func getGitRefsInternal(ctx *context.APIContext, filter string) {
  68. gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
  69. if err != nil {
  70. ctx.Error(500, "OpenRepository", err)
  71. return
  72. }
  73. if len(filter) > 0 {
  74. filter = "refs/" + filter
  75. }
  76. refs, err := gitRepo.GetRefsFiltered(filter)
  77. if err != nil {
  78. ctx.Error(500, "GetRefsFiltered", err)
  79. return
  80. }
  81. if len(refs) == 0 {
  82. ctx.NotFound()
  83. return
  84. }
  85. apiRefs := make([]*api.Reference, len(refs))
  86. for i := range refs {
  87. apiRefs[i] = &api.Reference{
  88. Ref: refs[i].Name,
  89. URL: ctx.Repo.Repository.APIURL() + "/git/" + refs[i].Name,
  90. Object: &api.GitObject{
  91. SHA: refs[i].Object.String(),
  92. Type: refs[i].Type,
  93. // TODO: Add commit/tag info URL
  94. //URL: ctx.Repo.Repository.APIURL() + "/git/" + refs[i].Type + "s/" + refs[i].Object.String(),
  95. },
  96. }
  97. }
  98. // If single reference is found and it matches filter exactly return it as object
  99. if len(apiRefs) == 1 && apiRefs[0].Ref == filter {
  100. ctx.JSON(200, &apiRefs[0])
  101. return
  102. }
  103. ctx.JSON(200, &apiRefs)
  104. }