Skip to content

gitindex: list tree files with git ls-tree - #1302

Closed
keegancsmith wants to merge 2 commits into
k/gitindex-diff-treefrom
k/gitindex-ls-tree
Closed

keegancsmith wants to merge 2 commits into
k/gitindex-diff-treefrom
k/gitindex-ls-tree

Conversation

@keegancsmith

@keegancsmith keegancsmith commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Stacked on #1301 (and #1300), and shares its ls-tree parsing.

This moves the normal build's tree walk in RepoWalker.CollectFiles from go-git's TreeWalker to git ls-tree -r -z, as a step towards dropping go-git from gitindex rather than for speed. The entries go through the same handleEntry as before, so submodules, the ignore file and branch bookkeeping are unchanged, and ZOEKT_DISABLE_CATFILE_BATCH=true keeps the go-git walk.

ls-tree fails when a tree object is missing, while the go-git walk skips that subtree and keeps going (#1218). The listing is collected before any entry is handled, so when it fails CollectFiles logs it and walks the tree with go-git instead, keeping the skip (and the error for an unreadable .sourcegraph tree). GIT_NO_LAZY_FETCH=1 stops git from fetching a missing tree from a promisor remote first. TestIndexGitRepo_MissingTree deletes a tree object and checks both modes index the rest.

One behavior difference: go-git's walker rejects entry names with control characters or .git-like components (pathutil.ValidTreePath). A file with a tab in its name was indexed under an empty name (checked), and by the same code path the contents of such a directory are not walked. ls-tree reports the real paths, so these are now indexed under their names.

Benchmark of CollectFiles on kubernetes HEAD (32,100 files), Go side only, 5 runs each on a 2 vCPU orb:

go-git walk git ls-tree
warm repository (go-git caches populated) 66–80 ms, 27 MB, 304k allocs 69–81 ms, 31 MB, 161k allocs
fresh repository each run, full clone 423–465 ms, 165 MB, 407k allocs 385–415 ms, 163 MB, 167k allocs
fresh repository each run, blobless clone 363–377 ms, 154 MB, 402k allocs 307–329 ms, 152 MB, 167k allocs

So it's roughly time-neutral with less than half the allocations. The fresh runs are dominated by go-git opening the repository (about 300 ms and 130 MB here, loading pack indexes), which only goes away once nothing in gitindex opens a *git.Repository anymore.

This moves the normal build's tree walk off go-git, one step towards
dropping it from gitindex. git ls-tree -r lists the files, and the entries
go through the same handleEntry as before.

ls-tree fails when a tree object is missing, while the go-git walk skips
that subtree (#1218). The entries are collected before any are handled, so
on failure CollectFiles walks the tree with go-git instead and keeps the
skip. ZOEKT_DISABLE_CATFILE_BATCH=true always uses the go-git walk.

go-git also rejected names with control characters, which then got indexed
under an empty name; ls-tree reports their real path.

Amp-Thread-ID: https://ampcode.com/threads/T-5b43e219-dc7b-4c53-85b0-8889c69ac42b
Co-authored-by: Amp <amp@ampcode.com>
@keegancsmith
keegancsmith added this pull request to stack #1303 November 26, 2026 17:42

Copy link
Copy Markdown
Member Author

Closing: roughly time-neutral and keeps the go-git walk as a fallback, so it adds a second implementation without removing anything. Revisit if go-git is removed once ZOEKT_DISABLE_CATFILE_BATCH is retired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant