Reputation: 31
I am working on an application that lists all files in a .git repository. I have a working way to turn a tree into a flat list but it is very slow (300ms)
This is the source code for a Tree object https://github.com/go-git/go-git/blob/master/plumbing/object/tree.go
My working solution:
repo, err := gogit.PlainOpen("./repository")
if err != nil {
return err
}
ref, err := repo.Head()
if err != nil {
return err
}
commit, err := repo.CommitObject(ref.Hash())
if err != nil {
return err
}
tree, err := commit.Tree()
if err != nil {
return err
}
var files []string
tree.Files().ForEach(func(f *object.File) error {
files = append(files, f.Name)
return nil
})
return files
However, as mentioned before this takes ~300ms to run. While doing git ls-files
takes < 50ms. As someone starting out with Go, am I missing something obvious?
Upvotes: 3
Views: 2552
Reputation: 744
I think tree.Files()
is slow because it retrieves each blob
listed in the tree (and sub-trees). If all you are trying to obtain are the paths to each blob (i.e. the file names), you'd probably be better off using a NewTreeWalker
instead to get just the names of each entry.
Upvotes: 2