gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "net/http"
  6. "code.gitea.io/gitea/services/context"
  7. files_service "code.gitea.io/gitea/services/repository/files"
  8. )
  9. // GetBlob get the blob of a repository file.
  10. func GetBlob(ctx *context.APIContext) {
  11. // swagger:operation GET /repos/{owner}/{repo}/git/blobs/{sha} repository GetBlob
  12. // ---
  13. // summary: Gets the blob of a repository.
  14. // produces:
  15. // - application/json
  16. // parameters:
  17. // - name: owner
  18. // in: path
  19. // description: owner of the repo
  20. // type: string
  21. // required: true
  22. // - name: repo
  23. // in: path
  24. // description: name of the repo
  25. // type: string
  26. // required: true
  27. // - name: sha
  28. // in: path
  29. // description: sha of the commit
  30. // type: string
  31. // required: true
  32. // responses:
  33. // "200":
  34. // "$ref": "#/responses/GitBlobResponse"
  35. // "400":
  36. // "$ref": "#/responses/error"
  37. // "404":
  38. // "$ref": "#/responses/notFound"
  39. sha := ctx.PathParam("sha")
  40. if len(sha) == 0 {
  41. ctx.APIError(http.StatusBadRequest, "sha not provided")
  42. return
  43. }
  44. if blob, err := files_service.GetBlobBySHA(ctx.Repo.Repository, ctx.Repo.GitRepo, sha); err != nil {
  45. ctx.APIError(http.StatusBadRequest, err)
  46. } else {
  47. ctx.JSON(http.StatusOK, blob)
  48. }
  49. }