gitea源码

git_ref.go 2.9KB

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