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.

blob.go 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2019 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 repofiles
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/git"
  8. "code.gitea.io/gitea/modules/setting"
  9. api "code.gitea.io/sdk/gitea"
  10. )
  11. // GetBlobBySHA get the GitBlobResponse of a repository using a sha hash.
  12. func GetBlobBySHA(repo *models.Repository, sha string) (*api.GitBlobResponse, error) {
  13. gitRepo, err := git.OpenRepository(repo.RepoPath())
  14. if err != nil {
  15. return nil, err
  16. }
  17. gitBlob, err := gitRepo.GetBlob(sha)
  18. if err != nil {
  19. return nil, err
  20. }
  21. content := ""
  22. if gitBlob.Size() <= setting.API.DefaultMaxBlobSize {
  23. content, err = gitBlob.GetBlobContentBase64()
  24. if err != nil {
  25. return nil, err
  26. }
  27. }
  28. return &api.GitBlobResponse{
  29. SHA: gitBlob.ID.String(),
  30. URL: repo.APIURL() + "/git/blobs/" + gitBlob.ID.String(),
  31. Size: gitBlob.Size(),
  32. Encoding: "base64",
  33. Content: content,
  34. }, nil
  35. }