Using go-git to get flat list of files

Viewed 1073

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 := git.PlainOpen("./repository")
    commit, err := repo.CommitObject(ref.Hash())
    tree, err := commit.Tree()
    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?

1 Answers

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.

Related