aboutsummaryrefslogtreecommitdiff
path: root/pkg/git/git.go
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-06-07 14:02:25 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2024-06-07 14:02:25 +0200
commit18aa098f50e2a2c7db01dd4d04dde460fd40f5d5 (patch)
tree5e3b3d3ba3206f2765db1a23a378275852154a51 /pkg/git/git.go
parentd0e0c1eb99303e1000140d4b98c610077278dc42 (diff)
downloadcerrado-18aa098f50e2a2c7db01dd4d04dde460fd40f5d5.tar.gz
cerrado-18aa098f50e2a2c7db01dd4d04dde460fd40f5d5.tar.bz2
cerrado-18aa098f50e2a2c7db01dd4d04dde460fd40f5d5.zip
feat: Add blob endpoint
Diffstat (limited to 'pkg/git/git.go')
-rw-r--r--pkg/git/git.go67
1 files changed, 66 insertions, 1 deletions
diff --git a/pkg/git/git.go b/pkg/git/git.go
index ce72465..6a7b91f 100644
--- a/pkg/git/git.go
+++ b/pkg/git/git.go
@@ -13,7 +13,8 @@ import (
var ()
var (
- MissingRefErr = errors.New("Reference not found")
+ MissingRefErr = errors.New("Reference not found")
+ TreeForFileErr = errors.New("Trying to get tree of a file")
)
type (
@@ -145,9 +146,73 @@ func (g *GitRepository) Branches() ([]*plumbing.Reference, error) {
return branches, nil
}
+func (g *GitRepository) Tree(path string) (*object.Tree, error) {
+ err := g.validateRef()
+ if err != nil {
+ return nil, err
+ }
+
+ c, err := g.repository.CommitObject(g.ref)
+ if err != nil {
+ return nil, err
+ }
+
+ tree, err := c.Tree()
+ if err != nil {
+ return nil, err
+ }
+
+ if path == "" {
+ return tree, nil
+ } else {
+ o, err := tree.FindEntry(path)
+ if err != nil {
+ return nil, err
+ }
+
+ if !o.Mode.IsFile() {
+ subtree, err := tree.Tree(path)
+ if err != nil {
+ return nil, err
+ }
+ return subtree, nil
+ } else {
+ return nil, TreeForFileErr
+ }
+ }
+}
+
func (g *GitRepository) validateRef() error {
if !g.setRef {
return g.SetRef("")
}
return nil
}
+
+func (g *GitRepository) FileContent(path string) (string, error) {
+ c, err := g.repository.CommitObject(g.ref)
+ if err != nil {
+ return "", err
+ }
+
+ tree, err := c.Tree()
+ if err != nil {
+ return "", err
+ }
+
+ file, err := tree.File(path)
+ if err != nil {
+ return "", err
+ }
+
+ isbin, err := file.IsBinary()
+ if err != nil {
+ return "", err
+ }
+
+ if !isbin {
+ return file.Contents()
+ } else {
+ return "Binary file", nil
+ }
+}