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

Introduce Blob.DataAsync #94

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ package git

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
)

// Blob represents a Git object.
Expand All @@ -29,3 +33,36 @@ func (b *Blob) Data() (io.Reader, error) {
func (b *Blob) DataPipeline(stdout, stderr io.Writer) error {
return NewCommand("show", b.ID.String()).RunInDirPipeline(b.repo.Path, stdout, stderr)
}

type cmdReadCloser struct {
cmd *exec.Cmd
stdout io.Reader
}

func (c cmdReadCloser) Read(p []byte) (int, error) {
return c.stdout.Read(p)
}

func (c cmdReadCloser) Close() error {
io.Copy(ioutil.Discard, c.stdout)
return c.cmd.Wait()
}

// DataAsync gets a ReadCloser for the contents of a blob without reading it all.
// Calling the Close function on the result will discard all unread output.
func (b *Blob) DataAsync() (io.ReadCloser, error) {
cmd := exec.Command("git", "show", b.ID.String())
cmd.Dir = b.repo.Path
cmd.Stderr = os.Stderr

stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("StdoutPipe: %v", err)
}

if err = cmd.Start(); err != nil {
return nil, fmt.Errorf("Start: %v", err)
}

return cmdReadCloser{stdout: stdout, cmd: cmd}, nil
}