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.

repo_object.go 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package git
  6. import (
  7. "bytes"
  8. "io"
  9. "strings"
  10. )
  11. // ObjectType git object type
  12. type ObjectType string
  13. const (
  14. // ObjectCommit commit object type
  15. ObjectCommit ObjectType = "commit"
  16. // ObjectTree tree object type
  17. ObjectTree ObjectType = "tree"
  18. // ObjectBlob blob object type
  19. ObjectBlob ObjectType = "blob"
  20. // ObjectTag tag object type
  21. ObjectTag ObjectType = "tag"
  22. // ObjectBranch branch object type
  23. ObjectBranch ObjectType = "branch"
  24. )
  25. // HashObject takes a reader and returns SHA1 hash for that reader
  26. func (repo *Repository) HashObject(reader io.Reader) (SHA1, error) {
  27. idStr, err := repo.hashObject(reader)
  28. if err != nil {
  29. return SHA1{}, err
  30. }
  31. return NewIDFromString(idStr)
  32. }
  33. func (repo *Repository) hashObject(reader io.Reader) (string, error) {
  34. cmd := NewCommand("hash-object", "-w", "--stdin")
  35. stdout := new(bytes.Buffer)
  36. stderr := new(bytes.Buffer)
  37. err := cmd.RunInDirFullPipeline(repo.Path, stdout, stderr, reader)
  38. if err != nil {
  39. return "", err
  40. }
  41. return strings.TrimSpace(stdout.String()), nil
  42. }
  43. // GetRefType gets the type of the ref based on the string
  44. func (repo *Repository) GetRefType(ref string) ObjectType {
  45. if repo.IsTagExist(ref) {
  46. return ObjectTag
  47. } else if repo.IsBranchExist(ref) {
  48. return ObjectBranch
  49. } else if repo.IsCommitExist(ref) {
  50. return ObjectCommit
  51. } else if _, err := repo.GetBlob(ref); err == nil {
  52. return ObjectBlob
  53. }
  54. return ObjectType("invalid")
  55. }