-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathblob.go
More file actions
37 lines (30 loc) · 1009 Bytes
/
blob.go
File metadata and controls
37 lines (30 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"bytes"
"context"
"io"
)
// Blob is a blob object.
type Blob struct {
*TreeEntry
}
// Bytes reads and returns the content of the blob all at once in bytes. This can
// be very slow and memory consuming for huge content.
func (b *Blob) Bytes(ctx context.Context) ([]byte, error) {
stdout := new(bytes.Buffer)
// 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.Pipe(ctx, stdout); err != nil {
return nil, err
}
return stdout.Bytes(), nil
}
// Pipe reads the content of the blob and pipes stdout to the supplied io.Writer.
func (b *Blob) Pipe(ctx context.Context, stdout io.Writer) error {
return pipe(ctx, b.parent.repo.path, []string{"show", "--end-of-options", b.id.String()}, nil, stdout)
}