2

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?

Jilles
  • 21
  • 3

1 Answers1

2

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.

orirawlings
  • 724
  • 1
  • 6
  • 10
  • Actually it seems that `tree.Files()` uses the tree walker under the hood. At least that's true as of go-git `v5` – BeetleJuice Mar 16 '23 at 18:55