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.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2014 The Gogs 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 git
  5. import (
  6. "bytes"
  7. "io"
  8. "strings"
  9. )
  10. // ObjectType git object type
  11. type ObjectType string
  12. const (
  13. // ObjectCommit commit object type
  14. ObjectCommit ObjectType = "commit"
  15. // ObjectTree tree object type
  16. ObjectTree ObjectType = "tree"
  17. // ObjectBlob blob object type
  18. ObjectBlob ObjectType = "blob"
  19. // ObjectTag tag object type
  20. ObjectTag ObjectType = "tag"
  21. )
  22. // HashObject takes a reader and returns SHA1 hash for that reader
  23. func (repo *Repository) HashObject(reader io.Reader) (SHA1, error) {
  24. idStr, err := repo.hashObject(reader)
  25. if err != nil {
  26. return SHA1{}, err
  27. }
  28. return NewIDFromString(idStr)
  29. }
  30. func (repo *Repository) hashObject(reader io.Reader) (string, error) {
  31. cmd := NewCommand("hash-object", "-w", "--stdin")
  32. stdout := new(bytes.Buffer)
  33. stderr := new(bytes.Buffer)
  34. err := cmd.RunInDirFullPipeline(repo.Path, stdout, stderr, reader)
  35. if err != nil {
  36. return "", err
  37. }
  38. return strings.TrimSpace(stdout.String()), nil
  39. }