diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-23 17:04:27 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-23 17:04:27 +0200 |
commit | 72495f4538215051540eb05c14db0ed16142e06e (patch) | |
tree | 293f0f4a9ad4954b8a2390d392e0a4a3bc9a3569 /pkg/git | |
parent | 6b96b76d66a929a2b428505809fda23a19005c63 (diff) | |
download | cerrado-72495f4538215051540eb05c14db0ed16142e06e.tar.gz cerrado-72495f4538215051540eb05c14db0ed16142e06e.tar.bz2 cerrado-72495f4538215051540eb05c14db0ed16142e06e.zip |
ref: Move binary handling up to the handler
Now it is up to the handler to decide whether it wants to render a file
or not, git only returns the content.
Diffstat (limited to 'pkg/git')
-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 { |