aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pkg/git/git.go43
-rw-r--r--pkg/handler/git/handler.go29
-rw-r--r--pkg/service/git.go34
3 files changed, 84 insertions, 22 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 {
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go
index 8bb4002..4c5d198 100644
--- a/pkg/handler/git/handler.go
+++ b/pkg/handler/git/handler.go
@@ -35,8 +35,9 @@ type (
ListCommits(name string, ref string, count int) ([]*object.Commit, error)
GetHead(name string) (*plumbing.Reference, error)
GetTree(name, ref, path string) (*object.Tree, error)
- GetFileContent(name, ref, path string) (string, error)
- GetAbout(name string) (string, error)
+ IsBinary(name, ref, path string) (bool, error)
+ GetFileContent(name, ref, path string) ([]byte, error)
+ GetAbout(name string) ([]byte, error)
ListTags(name string) ([]*plumbing.Reference, error)
ListBranches(name string) ([]*plumbing.Reference, error)
WriteTarGZip(w io.Writer, name, ref, prefix string) error
@@ -165,7 +166,7 @@ func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) error {
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
- doc := p.Parse([]byte(file))
+ doc := p.Parse(file)
htmlFlag := markdownhtml.CommonFlags | markdownhtml.HrefTargetBlank
opts := markdownhtml.RendererOptions{Flags: htmlFlag}
@@ -244,6 +245,25 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) error {
ref := r.PathValue("ref")
rest := r.PathValue("rest")
+ isBin, err := g.gitService.IsBinary(name, ref, rest)
+ if err != nil {
+ return err
+ }
+
+ // if it is binary no need to over all the chroma process
+ if isBin {
+ gitList := &templates.GitItemPage{
+ Name: name,
+ Ref: ref,
+ GitItemBase: &templates.GitItemBlobPage{
+ File: rest,
+ Content: []byte("Binary file"),
+ },
+ }
+ templates.WritePageTemplate(w, gitList)
+ return nil
+ }
+
file, err := g.gitService.GetFileContent(name, ref, rest)
if err != nil {
return err
@@ -255,7 +275,8 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) error {
formatter := html.New(
html.WithLineNumbers(true),
)
- iterator, err := lexer.Tokenise(nil, file)
+
+ iterator, err := lexer.Tokenise(nil, string(file))
if err != nil {
return err
}
diff --git a/pkg/service/git.go b/pkg/service/git.go
index 654d6ac..8e25261 100644
--- a/pkg/service/git.go
+++ b/pkg/service/git.go
@@ -139,37 +139,55 @@ func (g *GitService) GetTree(name, ref, path string) (*object.Tree, error) {
return repo.Tree(path)
}
-func (g *GitService) GetFileContent(name, ref, path string) (string, error) {
+func (g *GitService) IsBinary(name, ref, path string) (bool, error) {
r := g.configRepo.GetByName(name)
if r == nil {
- return "", RepositoryNotFoundErr
+ return false, RepositoryNotFoundErr
}
repo, err := git.OpenRepository(r.Path)
if err != nil {
- return "", err
+ return false, err
}
err = repo.SetRef(ref)
if err != nil {
- return "", err
+ return false, err
+ }
+
+ return repo.IsBinary(path)
+}
+
+func (g *GitService) GetFileContent(name, ref, path string) ([]byte, error) {
+ r := g.configRepo.GetByName(name)
+ if r == nil {
+ return nil, RepositoryNotFoundErr
+ }
+
+ repo, err := git.OpenRepository(r.Path)
+ if err != nil {
+ return nil, err
+ }
+ err = repo.SetRef(ref)
+ if err != nil {
+ return nil, err
}
return repo.FileContent(path)
}
-func (g *GitService) GetAbout(name string) (string, error) {
+func (g *GitService) GetAbout(name string) ([]byte, error) {
r := g.configRepo.GetByName(name)
if r == nil {
- return "", RepositoryNotFoundErr
+ return nil, RepositoryNotFoundErr
}
repo, err := git.OpenRepository(r.Path)
if err != nil {
- return "", err
+ return nil, err
}
err = repo.SetRef("")
if err != nil {
- return "", err
+ return nil, err
}
return repo.FileContent(r.About)