Skip to content
Merged
7 changes: 3 additions & 4 deletions blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@ type Blob struct {
// can be very slow and memory consuming for huge content.
func (b *Blob) Bytes(ctx context.Context) ([]byte, error) {
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
Comment thread
unknwon marked this conversation as resolved.

// Preallocate memory to save ~50% memory usage on big files.
if size := b.Size(ctx); size > 0 && size < int64(^uint(0)>>1) {
stdout.Grow(int(size))
}

if err := b.Pipeline(ctx, stdout, stderr); err != nil {
return nil, concatenateError(err, stderr.String())
if err := b.Pipeline(ctx, stdout, nil); err != nil {
return nil, err
}
return stdout.Bytes(), nil
}

// Pipeline reads the content of the blob and pipes stdout and stderr to
// supplied io.Writer.
func (b *Blob) Pipeline(ctx context.Context, stdout, stderr io.Writer) error {
return NewCommand(ctx, "show", b.id.String()).RunInDirPipeline(stdout, stderr, b.parent.repo.path)
return gitPipeline(ctx, b.parent.repo.path, []string{"show", b.id.String()}, nil, stdout, stderr, nil)
}
Loading