blob.go 906 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2015 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. )
  9. // Blob represents a Git object.
  10. type Blob struct {
  11. repo *Repository
  12. *TreeEntry
  13. }
  14. // Data gets content of blob all at once and wrap it as io.Reader.
  15. // This can be very slow and memory consuming for huge content.
  16. func (b *Blob) Data() (io.Reader, error) {
  17. stdout := new(bytes.Buffer)
  18. stderr := new(bytes.Buffer)
  19. // Preallocate memory to save ~50% memory usage on big files.
  20. stdout.Grow(int(b.Size() + 2048))
  21. if err := b.DataPipeline(stdout, stderr); err != nil {
  22. return nil, concatenateError(err, stderr.String())
  23. }
  24. return stdout, nil
  25. }
  26. func (b *Blob) DataPipeline(stdout, stderr io.Writer) error {
  27. return NewCommand("show", b.ID.String()).RunInDirPipeline(b.repo.Path, stdout, stderr)
  28. }