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.

git_ref.go 3.1KB

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