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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "net/http"
  6. "net/url"
  7. api "code.gitea.io/gitea/modules/structs"
  8. "code.gitea.io/gitea/modules/util"
  9. "code.gitea.io/gitea/routers/api/v1/utils"
  10. "code.gitea.io/gitea/services/context"
  11. )
  12. // GetGitAllRefs get ref or an list all the refs of a repository
  13. func GetGitAllRefs(ctx *context.APIContext) {
  14. // swagger:operation GET /repos/{owner}/{repo}/git/refs repository repoListAllGitRefs
  15. // ---
  16. // summary: Get specified ref or filtered repository's refs
  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. // responses:
  31. // "200":
  32. // # "$ref": "#/responses/Reference" TODO: swagger doesnt support different output formats by ref
  33. // "$ref": "#/responses/ReferenceList"
  34. // "404":
  35. // "$ref": "#/responses/notFound"
  36. getGitRefsInternal(ctx, "")
  37. }
  38. // GetGitRefs get ref or an filteresd list of refs of a repository
  39. func GetGitRefs(ctx *context.APIContext) {
  40. // swagger:operation GET /repos/{owner}/{repo}/git/refs/{ref} repository repoListGitRefs
  41. // ---
  42. // summary: Get specified ref or filtered repository's refs
  43. // produces:
  44. // - application/json
  45. // parameters:
  46. // - name: owner
  47. // in: path
  48. // description: owner of the repo
  49. // type: string
  50. // required: true
  51. // - name: repo
  52. // in: path
  53. // description: name of the repo
  54. // type: string
  55. // required: true
  56. // - name: ref
  57. // in: path
  58. // description: part or full name of the ref
  59. // type: string
  60. // required: true
  61. // responses:
  62. // "200":
  63. // # "$ref": "#/responses/Reference" TODO: swagger doesnt support different output formats by ref
  64. // "$ref": "#/responses/ReferenceList"
  65. // "404":
  66. // "$ref": "#/responses/notFound"
  67. getGitRefsInternal(ctx, ctx.Params("*"))
  68. }
  69. func getGitRefsInternal(ctx *context.APIContext, filter string) {
  70. refs, lastMethodName, err := utils.GetGitRefs(ctx, filter)
  71. if err != nil {
  72. ctx.Error(http.StatusInternalServerError, lastMethodName, err)
  73. return
  74. }
  75. if len(refs) == 0 {
  76. ctx.NotFound()
  77. return
  78. }
  79. apiRefs := make([]*api.Reference, len(refs))
  80. for i := range refs {
  81. apiRefs[i] = &api.Reference{
  82. Ref: refs[i].Name,
  83. URL: ctx.Repo.Repository.APIURL() + "/git/" + util.PathEscapeSegments(refs[i].Name),
  84. Object: &api.GitObject{
  85. SHA: refs[i].Object.String(),
  86. Type: refs[i].Type,
  87. URL: ctx.Repo.Repository.APIURL() + "/git/" + url.PathEscape(refs[i].Type) + "s/" + url.PathEscape(refs[i].Object.String()),
  88. },
  89. }
  90. }
  91. // If single reference is found and it matches filter exactly return it as object
  92. if len(apiRefs) == 1 && apiRefs[0].Ref == filter {
  93. ctx.JSON(http.StatusOK, &apiRefs[0])
  94. return
  95. }
  96. ctx.JSON(http.StatusOK, &apiRefs)
  97. }