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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/gitea/modules/structs"
  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 getGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) {
  68. gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
  69. if err != nil {
  70. return nil, "OpenRepository", err
  71. }
  72. if len(filter) > 0 {
  73. filter = "refs/" + filter
  74. }
  75. refs, err := gitRepo.GetRefsFiltered(filter)
  76. return refs, "GetRefsFiltered", err
  77. }
  78. func getGitRefsInternal(ctx *context.APIContext, filter string) {
  79. refs, lastMethodName, err := getGitRefs(ctx, filter)
  80. if err != nil {
  81. ctx.Error(500, lastMethodName, err)
  82. return
  83. }
  84. if len(refs) == 0 {
  85. ctx.NotFound()
  86. return
  87. }
  88. apiRefs := make([]*api.Reference, len(refs))
  89. for i := range refs {
  90. apiRefs[i] = &api.Reference{
  91. Ref: refs[i].Name,
  92. URL: ctx.Repo.Repository.APIURL() + "/git/" + refs[i].Name,
  93. Object: &api.GitObject{
  94. SHA: refs[i].Object.String(),
  95. Type: refs[i].Type,
  96. URL: ctx.Repo.Repository.APIURL() + "/git/" + refs[i].Type + "s/" + refs[i].Object.String(),
  97. },
  98. }
  99. }
  100. // If single reference is found and it matches filter exactly return it as object
  101. if len(apiRefs) == 1 && apiRefs[0].Ref == filter {
  102. ctx.JSON(200, &apiRefs[0])
  103. return
  104. }
  105. ctx.JSON(200, &apiRefs)
  106. }