git-module

git clone git://git.lin.moe/fork/git-module.git

 1package git
 2
 3import (
 4	"bytes"
 5	"context"
 6	"io"
 7)
 8
 9// Blob is a blob object.
10type Blob struct {
11	*TreeEntry
12}
13
14// Bytes reads and returns the content of the blob all at once in bytes. This can
15// be very slow and memory consuming for huge content.
16func (b *Blob) Bytes(ctx context.Context) ([]byte, error) {
17	stdout := new(bytes.Buffer)
18
19	// Preallocate memory to save ~50% memory usage on big files.
20	if size := b.Size(ctx); size > 0 && size < int64(^uint(0)>>1) {
21		stdout.Grow(int(size))
22	}
23
24	if err := b.Pipe(ctx, stdout); err != nil {
25		return nil, err
26	}
27	return stdout.Bytes(), nil
28}
29
30// Pipe reads the content of the blob and pipes stdout to the supplied io.Writer.
31func (b *Blob) Pipe(ctx context.Context, stdout io.Writer) error {
32	return pipe(ctx, b.parent.repo.path, []string{"show", "--end-of-options", b.id.String()}, nil, stdout)
33}