Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit 193e22e

Browse files
mrexodialafriks
authored andcommitted
Introduce Blob.DataAsyc (#94)
Signed-off-by: Duncan Ogilvie <[email protected]>
1 parent d47b98c commit 193e22e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

blob.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ package git
66

77
import (
88
"bytes"
9+
"fmt"
910
"io"
11+
"io/ioutil"
12+
"os"
13+
"os/exec"
1014
)
1115

1216
// Blob represents a Git object.
@@ -29,3 +33,36 @@ func (b *Blob) Data() (io.Reader, error) {
2933
func (b *Blob) DataPipeline(stdout, stderr io.Writer) error {
3034
return NewCommand("show", b.ID.String()).RunInDirPipeline(b.repo.Path, stdout, stderr)
3135
}
36+
37+
type cmdReadCloser struct {
38+
cmd *exec.Cmd
39+
stdout io.Reader
40+
}
41+
42+
func (c cmdReadCloser) Read(p []byte) (int, error) {
43+
return c.stdout.Read(p)
44+
}
45+
46+
func (c cmdReadCloser) Close() error {
47+
io.Copy(ioutil.Discard, c.stdout)
48+
return c.cmd.Wait()
49+
}
50+
51+
// DataAsync gets a ReadCloser for the contents of a blob without reading it all.
52+
// Calling the Close function on the result will discard all unread output.
53+
func (b *Blob) DataAsync() (io.ReadCloser, error) {
54+
cmd := exec.Command("git", "show", b.ID.String())
55+
cmd.Dir = b.repo.Path
56+
cmd.Stderr = os.Stderr
57+
58+
stdout, err := cmd.StdoutPipe()
59+
if err != nil {
60+
return nil, fmt.Errorf("StdoutPipe: %v", err)
61+
}
62+
63+
if err = cmd.Start(); err != nil {
64+
return nil, fmt.Errorf("Start: %v", err)
65+
}
66+
67+
return cmdReadCloser{stdout: stdout, cmd: cmd}, nil
68+
}

0 commit comments

Comments
 (0)