diff options
Diffstat (limited to 'pkg/git/git.go')
-rw-r--r-- | pkg/git/git.go | 67 |
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 + } +} |