Skip to content

gitindex: diff delta build trees with git diff-tree - #1301

Closed
keegancsmith wants to merge 3 commits into
k/gitindex-rev-parsefrom
k/gitindex-diff-tree
Closed

keegancsmith wants to merge 3 commits into
k/gitindex-rev-parsefrom
k/gitindex-diff-tree

Conversation

@keegancsmith

@keegancsmith keegancsmith commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Stacked on #1300, which adds the repoGitDir and gitBinaryDisabled helpers this uses.

prepareDeltaBuild diffed the last indexed and current trees with go-git, whose Change.Files() loads every changed blob, and then looked up each changed path in every branch's tree one at a time. This switches to git diff-tree -r -z --no-renames, which reads trees only, plus one git ls-tree -r -z per branch to answer all of those lookups at once. The rest of the delta logic is unchanged; both implementations produce the same list of file changes, and ZOEKT_DISABLE_CATFILE_BATCH=true keeps the go-git diff. Like go-git, git never fetches from a promisor remote here (GIT_NO_LAZY_FETCH=1).

Benchmark on a kubernetes clone (32k files), Go side only, 5 runs each on a 2 vCPU orb, repository already open for go-git. Both produce identical changes and blob IDs for these pairs:

delta go-git git
HEAD~5..HEAD (27 changes) 79–91 ms, 32 MB, 116k allocs 44–47 ms, 5.8 MB, 64k allocs
HEAD~200..HEAD (1,624 changes) 326–360 ms, 83 MB, 714k allocs 60–68 ms, 7.5 MB, 69k allocs

Most of the small delta's time is the full ls-tree; passing the changed paths as pathspecs could cut that later if it matters.

Two edge cases now behave like a normal build instead of differently. Submodule entries are skipped, which is what Change.Files() already did. On a size-filtered clone where a changed blob is missing, go-git either failed the diff (falling back to a normal build) or treated the file as absent from the branch, which dropped it from the index; now its path and ID are known from the tree and cat-file indexes it as a skipped too-large document, like a normal build does.

TestDiffTrees checks both implementations against the same expected changes (modify, delete, add, mode change, file replaced by a directory, a gitlink, and a path with spaces and non-ASCII). The existing delta tests pass in both modes.

prepareDeltaBuild diffed trees with go-git, whose Change.Files loads every
changed blob, and then looked up each changed path in every branch's tree
one at a time. git diff-tree reads trees only, and one git ls-tree per
branch answers all the lookups. On kubernetes this is 5.5x faster for a
200 commit delta and 1.8x faster for a 5 commit one.

Submodule entries are skipped, as go-git's Change.Files already did. Since
blobs are no longer read, a size-filtered clone missing a changed blob now
gets the same skipped document as a normal build, instead of falling back
to a normal build or silently dropping the file. ZOEKT_DISABLE_CATFILE_BATCH
=true keeps the go-git diff.

Amp-Thread-ID: https://ampcode.com/threads/T-5b43e219-dc7b-4c53-85b0-8889c69ac42b
Co-authored-by: Amp <amp@ampcode.com>
keegancsmith and others added 2 commits November 26, 2026 17:46
They now also set GIT_DIR, like rev-parse, and a diff-tree listing that
doesn't end in a complete path is an error rather than silently ignored,
as gitserver's parser checks.

Amp-Thread-ID: https://ampcode.com/threads/T-5b43e219-dc7b-4c53-85b0-8889c69ac42b
Co-authored-by: Amp <amp@ampcode.com>

Copy link
Copy Markdown
Member Author

Closing: delta builds only run for allowlisted repos and the saving is ~300 ms per delta build, not worth a second diff implementation alongside the go-git fallback. Revisit if go-git is removed once ZOEKT_DISABLE_CATFILE_BATCH is retired.

An error occurred while trying to automatically change base from k/gitindex-rev-parse to main November 26, 2026 18:11
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