Skip to content

git: remote, Refuse a fetch that lacks needed objects - #2668

Open
hiddeco wants to merge 1 commit into
mainfrom
fix/incomplete-fetch
Open

hiddeco wants to merge 1 commit into
mainfrom
fix/incomplete-fetch

Conversation

@hiddeco

@hiddeco hiddeco commented Nov 29, 2026

Copy link
Copy Markdown
Member

After a fetch, Remote.Fetch set the local refs to the IDs the remote advertised, whatever the pack held. A remote that answered with an empty or incomplete pack left refs/remotes/origin/main pointing at objects that are not in the repository, and the fetch returned no error.

Git checks what it received before it updates a ref. Served such packs by a test server, Git 2.54.0 fails with “did not send all necessary objects” and creates no ref, whether the tip’s tree, a parent commit, or a blob of the tip or of an older commit is missing. go-git now does the same. Once the pack is stored and before any ref is updated, the fetch looks for every commit, tree, and blob that the fetched tips reach and the local refs did not reach before. If one is missing it returns the new ErrIncompleteFetch and updates no ref. This covers protocol v0, v1, and v2, and clone and pull, which fetch through Remote. Callers that fetched from a remote serving incomplete history now get an error where they got broken refs.

Only an absent object counts as an incomplete fetch; a storage error during the check is returned as it is. An object that is merely in storage proves nothing, because a failed fetch leaves its objects behind, so only history under a local ref is taken as complete. A fetch with a depth ends the walk at the shallow commits. With a filter or a promisor remote, only the fetched tips themselves must exist, since what they reach may be fetched later. Objects written by a refused fetch stay in storage without a ref, as with Git. Git partial clone

The cost follows what was fetched and not the size of the repository. The check finds where new history joins old on the commit graph alone, and of a new commit’s tree it reads only the entries that differ from the parents the repository already had. On a packed repository of 30,321 objects, BenchmarkCheckFetched measures 1.5 ms for 50 fetched commits with one local ref and 2.0 ms with 10,001 refs. A whole history takes 43 ms and allocates 23 MB.

TestFetchRefusesIncompletePack fetches over HTTP with protocol v0 and v2 from a server that leaves out each kind of object. It covers plain, shallow, and deepening fetches, a shallow client, merges, tags, a directory reused in a new place, a commit left in storage by an earlier fetch, and a promisor remote. TestFetchReturnsReadErrorOfTheCompletenessCheck covers a storage that fails a read.

This complements #2657, which stops the client from concluding there is nothing to fetch on the server’s word.

After a fetch the local references were set to the ids the remote
had advertised, whatever the pack held. A remote that answered with
an empty pack, or one without a tree, a blob or a parent commit, left
`refs/remotes/origin/main` on history that is not in the repository,
and the fetch returned no error.

git checks what it received before it updates a reference. Served
such packs by a test server, git 2.54.0 failed each time with "did
not send all necessary objects" and created no reference: for a tip
commit without its tree, a missing parent commit, and a missing blob
of the tip or of an older commit. The objects it did receive stayed
in its object store.

`Remote.Fetch` now does the same once a pack has been stored. It
looks for every commit, tree and blob that the wanted objects reach
and that the local references did not reach before the fetch, down
to the shallow commits, and returns the new `ErrIncompleteFetch` if
one is not in storage. No reference is updated then, and the shallow
commits are put back to what they were. Objects already written stay
in storage, unreferenced, as with git. The check follows the
transport, so it holds for every protocol version, and for clone and
pull, which fetch through `Remote`.

Only an absent object is an incomplete fetch. Another failure to
read what was fetched is returned as it is; no reference is updated
in that case either.

An object that merely is in storage proves nothing, as a fetch that
failed leaves its objects behind. Only history under a local
reference counts as held. Which commits are new is settled on the
commit graph, as in a merge-base walk: commits are marked from the
wanted objects and from the local references, the newest first,
until every commit left to look at is marked from both sides. The
commit a reference points at is recognised by its hash when the walk
arrives there, so a fetch that continues a branch reads no other
reference. The commits of the remaining references are read only
once the walk has gone a sixteenth as many commits deep as there are
references: when a fetched branch starts below every reference, or
when a fetch brings that many commits. Of a new commit's tree, the
entries that a held parent has under the same name are not read;
trees are read as stored, without building an `object.Tree` for
each.

Two kinds of fetch leave objects out on purpose. With a depth, the
walk ends at the shallow commits and does not stop at the local
references, as deepening brings history below a commit a branch is
already at. In a repository with a promisor remote, or on a fetch
with a filter, only the wanted objects themselves are looked for:
what they reach may be fetched later.

Callers that fetched from a remote which serves incomplete history
now get an error where they got broken references.

`BenchmarkCheckFetched` measures the check on a packed repository of
30321 objects on disk: 43 ms and 23 MB allocated for the whole
history; 1.5 ms for 50 fetched commits with one local reference and
2.0 ms with 10001, loose or packed, the difference being the set of
their hashes; 13.5 ms when the fetched branch starts 3900 commits
below the local one with no reference at or below that commit. On
this repository's own history the whole of it, 29671 objects, takes
0.2 s, where a clone over the file transport takes 8.3 s.

`TestFetchRefusesIncompletePack` fetches over HTTP, with protocol v0
and v2, from a server that sends packs with each kind of object left
out: plain, shallow and deepening fetches, a shallow client, merges,
tags, a directory used again in a new place, a commit left in storage
by an earlier fetch, and a promisor remote.
`TestFetchReturnsReadErrorOfTheCompletenessCheck` fetches into a
storage that fails to read a commit.

Assisted-by: Claude Fable 5.2
Signed-off-by: Hidde Beydals <hidde@entire.io>
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