diff options
author | Michael Muré <batolettre@gmail.com> | 2020-09-27 21:14:25 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-09-29 20:42:21 +0200 |
commit | d8b49e025a01e45cfc90bb11b8a0645b616ec561 (patch) | |
tree | c88388d6935326f443e9286c7c3a33e2e5a6e857 /repository/gogit.go | |
parent | eb88f0e4463ea1aef5494314fa2a9607aaa262dd (diff) | |
download | git-bug-d8b49e025a01e45cfc90bb11b8a0645b616ec561.tar.gz |
repo: ReadTree must accept either a commit or a tree hash
Diffstat (limited to 'repository/gogit.go')
-rw-r--r-- | repository/gogit.go | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/repository/gogit.go b/repository/gogit.go index 684f8a72..6d3aede9 100644 --- a/repository/gogit.go +++ b/repository/gogit.go @@ -333,13 +333,34 @@ func (repo *GoGitRepo) StoreTree(mapping []TreeEntry) (Hash, error) { // ReadTree will return the list of entries in a Git tree func (repo *GoGitRepo) ReadTree(hash Hash) ([]TreeEntry, error) { - obj, err := repo.r.TreeObject(plumbing.NewHash(hash.String())) + h := plumbing.NewHash(hash.String()) + + // the given hash could be a tree or a commit + obj, err := repo.r.Storer.EncodedObject(plumbing.AnyObject, h) + if err != nil { + return nil, err + } + + var tree *object.Tree + switch obj.Type() { + case plumbing.TreeObject: + tree, err = object.DecodeTree(repo.r.Storer, obj) + case plumbing.CommitObject: + var commit *object.Commit + commit, err = object.DecodeCommit(repo.r.Storer, obj) + if err != nil { + return nil, err + } + tree, err = commit.Tree() + default: + return nil, fmt.Errorf("given hash is not a tree") + } if err != nil { return nil, err } - treeEntries := make([]TreeEntry, len(obj.Entries)) - for i, entry := range obj.Entries { + treeEntries := make([]TreeEntry, len(tree.Entries)) + for i, entry := range tree.Entries { objType := Blob if entry.Mode == filemode.Dir { objType = Tree |