diff options
Diffstat (limited to 'pkg/git/git.go')
-rw-r--r-- | pkg/git/git.go | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/pkg/git/git.go b/pkg/git/git.go index 66338a1..6b58d35 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -2,6 +2,7 @@ package git import ( "archive/tar" + "bytes" "errors" "fmt" "io" @@ -197,32 +198,54 @@ func (g *GitRepository) validateRef() error { return nil } -func (g *GitRepository) FileContent(path string) (string, error) { +func (g *GitRepository) IsBinary(path string) (bool, error) { + tree, err := g.Tree("") + if err != nil { + return false, err + } + + file, err := tree.File(path) + if err != nil { + return false, err + } + + return file.IsBinary() +} + +func (g *GitRepository) FileContent(path string) ([]byte, error) { + err := g.validateRef() + if err != nil { + return nil, err + } + c, err := g.repository.CommitObject(g.ref) if err != nil { - return "", err + return nil, err } tree, err := c.Tree() if err != nil { - return "", err + return nil, err } file, err := tree.File(path) if err != nil { - return "", err + return nil, err } - isbin, err := file.IsBinary() + r, err := file.Blob.Reader() if err != nil { - return "", err + return nil, err } + defer r.Close() - if !isbin { - return file.Contents() - } else { - return "Binary file", nil + var buf bytes.Buffer + _, err = io.Copy(&buf, r) + if err != nil { + return nil, err } + + return buf.Bytes(), nil } func (g *GitRepository) WriteTar(w io.Writer, prefix string) error { |