Skip to content

fix(gitindex): honor ignore files and branch membership in delta builds - #36

Merged
jsourcebot merged 3 commits into
mainfrom
jminnetian/fix-delta-builds-SOU-2867
Nov 24, 2026
Merged

jsourcebot merged 3 commits into
mainfrom
jminnetian/fix-delta-builds-SOU-2867

Conversation

@jsourcebot

@jsourcebot jsourcebot commented Nov 19, 2026 •

Copy link
Copy Markdown

Part of SOU-2867

Summary

Fixes four problems with delta builds (zoekt-git-index -delta). The first three left the index returning different results from a full build of the same commits. Sourcebot is about to turn delta builds on, so these need to land first.

  • Ignored files became searchable. A tracked file under a path that .sourcegraph/ignore excludes was indexed when it was added or modified, even though the ignore file had not changed.
  • A file that reached a second branch was split into two results. When a file that was already on main arrived on release (for example by a fast-forward), the index held two documents for it, one per branch. branch:main branch:release missed the file, and branch:main -branch:release found it.
  • Documents listed the same branch more than once. Documents are ranked by how many branches they list, so a modified file ranked higher than it should. A file on two branches could rank above an otherwise identical file on three.
  • Branches that moved together were slow. With 64 branches fast-forwarding to a commit that modifies 1,000 files, the delta build took 67 s. It now takes 0.4 s.

Cause

All four are in prepareDeltaBuild in gitindex/index.go, which diffs each branch's last indexed commit against its current one.

  1. It never read the ignore file. It only fell back to a full build when the ignore file itself changed.
  2. A document carries every branch that has its blob at its path. For an added file, the delta build indexed a new document for that one branch and left the older shards alone. So the document another branch already had for the same file stayed in the index, without the new branch.
  3. For each modified or deleted path in each branch's diff, the build looked the path up on every branch and appended those branches to the document. The step that was meant to remove the repeats wrote to a copy, so it never took effect.
  4. For the same reason, when several branches reported the same path, the work grew with the square of the number of branches. Each lookup also loaded the file's content, and often decoded its directory again.

Fix

  • The delta build reads the ignore file at each branch's current commit and applies it per branch. Rules can differ between branches.
  • It collects the changed paths from all the diffs first, then handles each path once. A branch's version of a path comes from that branch's own diff. The path is looked up only on the branches that didn't change it, through directories that are read once and shared between branches. Every branch that has the file ends up on its document exactly once, whether the file was added, modified or deleted elsewhere.
  • A path is tombstoned in the older shards if any branch had the file at its last indexed commit. This doesn't depend on the order of the branches. A file that is new to every branch that has it writes no tombstone, as before.
  • File modes come from the diff's tree entries, not from loading both versions of the file. A file that a submodule replaces, or the reverse, is now handled. Before, that change was skipped and the old document stayed.

Effect on when a delta build can run

There is no new fallback to a full build.

  • A change to .sourcegraph/ignore itself still falls back, because changed rules can add or remove files that the diff doesn't mention. The error text is unchanged, since callers match on it.
  • One fallback is gone. In a clone that leaves out large blobs (--filter=blob:limit=...), a change to such a file used to fail the diff with object not found, and the build fell back. The delta build now runs and returns the same results as a full build.

Performance

Wall time and peak memory of one delta build that changes 1,000 files. The repository has 3,700 files and is packed. Each figure is the median of two to six runs on one laptop, except the 67 s run, which ran once.

Delta build main This PR
64 branches fast-forward to a commit that adds the files 0.49 s, 111 MiB 0.46 s, 110 MiB
64 branches fast-forward to a commit that modifies the files 67 s, 279 MiB 0.40 s, 108 MiB
64 branches with different trees pick up the same new files 0.50 s 0.49 s
One of 64 branches adds the files 0.32 s 0.34 s
One of 64 branches modifies the files 1.52 s 0.58 s
One of 128 branches adds the files (on #35) 0.41 s 0.46 s
One of 128 branches modifies the files (on #35) 2.8 s 0.67 s

The two "adds" rows with one branch are the only place this PR does more work than main. To keep one document per file, an added path has to be looked up on the other branches. That costs about 0.4 µs per path and branch.

If you measured an earlier revision of this PR (5634b7f): it had the square-of-branches cost for added files too (62 s and 307 MiB in the first row). The third commit removes it.

Tests

  • TestIndexDeltaBasic now builds a clean full index after every delta step and compares the documents and their branches with the delta index. It also checks that no document lists a branch twice, which fails on main. New cases cover the ignore rules, a file reaching a second branch, and a file that is deleted on one branch and added on another in the same build, in both branch orders.
  • go test ./... passes.
  • Manual checks with the zoekt-git-index and zoekt binaries, on this branch and with these commits applied onto feat(index): support more than 64 branches per repository #35's head (8b64c6f):
    • Six queries against a delta index and a full index of one repository return the same paths, counts and branches.
    • With three branches, a search that matches a file on all three and a file on two returns them in the same order from both indexes. On 5634b7f the order was reversed.
  • A throwaway randomized check, not committed. It builds random histories on 1 to 5 branches: per-branch ignore files, branches moving to each other's commits, every branch picking up one commit, adds, edits, deletes, symlinks, executables, submodule entries, and paths that switch between file and directory. After each delta build it compares the index with a full build, and checks that no document lists a branch twice and that no path without a document gets a tombstone. It found 0 problems in 650 delta builds on this branch and in 191 on feat(index): support more than 64 branches per repository #35's head with these commits. The same check fails on main.

Notes for reviewers

🤖 Generated with Claude Code


Note

Medium Risk
Changes core delta indexing logic that must stay equivalent to full builds for search correctness; large refactor with broad test coverage but high impact on incremental indexing.

Overview
Delta git indexing (prepareDeltaBuild) is reworked so incremental builds match full indexes and scale when many branches move together.

Per-branch .sourcegraph/ignore is loaded at each branch’s tip and applied when selecting paths to index (changes to the ignore file itself still force a full build). Changed paths are aggregated across branch diffs and handled once: branches that changed a path use the diff version; unchanged branches get the current tree entry via a shared treeFiles cache. Documents list each branch at most once, and paths that existed in older shards are tombstoned so shared multi-branch blobs stay consistent. File vs non-file changes use diff tree modes (isIndexedFile) instead of loading both blob sides.

Tests add multi-branch and ignore scenarios, assert no duplicate branch labels on documents, and compare every delta step to a clean normal build (including branch membership).

Reviewed by Cursor Bugbot for commit 36e5276. Bugbot is set up for automated code reviews on this repo. Configure here.

jsourcebot and others added 3 commits November 19, 2026 12:00
…to another branch

A document carries every branch that has its blob at its path. When a
delta build saw a file added on a branch, it indexed a new document for
that branch alone and left the older shards untouched. If another branch
already had the same file, the index ended up with two documents for it,
each on a subset of the branches. A search that required both branches
missed the file, and a search that excluded one of them still found it.

A delta build now handles an added file the way it handles a modified or
deleted one: it indexes the file's current version from every branch and
tombstones the path in the older shards. It skips the tombstone when the
file is new to its branch and no other branch has it, since no older
shard has that path.

The delta tests now compare each delta build with a clean normal build
of the same commits, including the branches of every document.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…uild indexes

A normal build leaves out the files that a branch's .sourcegraph/ignore
matches. A delta build never read the ignore file. It only fell back to
a normal build when the ignore file itself changed. So when a tracked
file under an ignored path was added or modified, the delta build
indexed it and its contents became searchable.

A delta build now reads the ignore file at each branch's current commit.
It skips a change to a path that the changed branch ignores, and it
leaves a branch off a document when that branch ignores the path. A
change to the ignore file still falls back to a normal build, because it
can add or remove files that the diff doesn't mention.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A delta build diffs every branch against its last indexed commit. When
several branches picked up the same commit, each branch's diff reported
the same paths. For each report, the build looked the path up on every
branch and added those branches to the document again. So the work grew
with the square of the number of branches, and a document listed the
same branch many times. Modified and deleted files already behaved this
way. The two previous commits made added files do it too.

The step that removed the repeated branches never took effect, because
it wrote to a copy of the document. Documents are ranked by how many
branches they list, so a file on two branches could rank above an
otherwise identical file on three.

A delta build now collects the changed paths from all the diffs first.
It takes a branch's version of a path from that branch's diff, and
looks the path up only on the branches that didn't change it. Those
lookups read each directory once and share it between branches. A
branch is added to a document once. A path is tombstoned if any branch
had the file at its last indexed commit, whichever branch comes first.
A file that is new to every branch that has it gets no tombstone.

The build reads file modes from the diff instead of loading both
versions of each file. Two things follow. A file that a submodule
replaces, or the reverse, is now handled. Before, the change was
skipped. And a change to a file whose content the clone doesn't have
(a clone with a blob size filter) no longer makes the delta build fall
back to a normal build.

64 branches fast-forwarding to a commit with 1,000 new files took 62s
and 307 MiB before this commit. It now takes 0.46s and 110 MiB. The
same with 1,000 modified files went from 67s to 0.40s.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@jsourcebot
jsourcebot merged commit 0a812bd into main Nov 24, 2026
21 checks passed
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