Skip to content

Commit 5d4cc78

Browse files
jonathantanmygitster
authored andcommitted
fetch-pack: die if in commit graph but not obj db
When fetching, there is a step in which sought objects are first checked against the local repository; only objects that are not in the local repository are then fetched. This check first looks up the commit graph file, and returns "present" if the object is in there. However, the action of first looking up the commit graph file is not done everywhere in Git, especially if the type of the object at the time of lookup is not known. This means that in a repo corruption situation, a user may encounter an "object missing" error, attempt to fetch it, and still encounter the same error later when they reattempt their original action, because the object is present in the commit graph file but not in the object DB. Therefore, make it a fatal error when this occurs. (Note that we cannot proceed to include this object in the list of objects to be fetched without changing at least the fetch negotiation code: what would happen is that the client will send "want X" and "have X" and when I tested at $DAYJOB with a work server that uses JGit, the server reasonably returned an empty packfile. And changing the fetch negotiation code to only use the object DB when deciding what to report as "have" would be an unnecessary slowdown, I think.) This was discovered when a lazy fetch of a missing commit completed with nothing actually fetched, and the writing of the commit graph file after every fetch then attempted to read said missing commit, triggering a lazy fetch of said missing commit, resulting in an infinite loop with no user-visible indication (until they check the list of processes running on their computer). With this fix, there is no infinite loop. Note that although the repo corruption we discovered was caused by a bug in GC in a partial clone, the behavior that this patch teaches Git to warn about applies to any repo with commit graph enabled and with a missing commit, whether it is a partial clone or not. t5330, introduced in 3a1ea94 (commit-graph.c: no lazy fetch in lookup_commit_in_graph(), 2022-07-01), tests that an interaction between fetch and the commit graph does not cause an infinite loop. This patch changes the exit code in that situation, so that test had to be changed. Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bf1feb9 commit 5d4cc78

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

fetch-pack.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,29 @@ static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
122122
cb(negotiator, cache.items[i]);
123123
}
124124

125+
static void die_in_commit_graph_only(const struct object_id *oid)
126+
{
127+
die(_("You are attempting to fetch %s, which is in the commit graph file but not in the object database.\n"
128+
"This is probably due to repo corruption.\n"
129+
"If you are attempting to repair this repo corruption by refetching the missing object, use 'git fetch --refetch' with the missing object."),
130+
oid_to_hex(oid));
131+
}
132+
125133
static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
126-
int mark_tags_complete)
134+
int mark_tags_complete_and_check_obj_db)
127135
{
128136
enum object_type type;
129137
struct object_info info = { .typep = &type };
130138
struct commit *commit;
131139

132140
commit = lookup_commit_in_graph(the_repository, oid);
133-
if (commit)
141+
if (commit) {
142+
if (mark_tags_complete_and_check_obj_db) {
143+
if (!has_object(the_repository, oid, 0))
144+
die_in_commit_graph_only(oid);
145+
}
134146
return commit;
147+
}
135148

136149
while (1) {
137150
if (oid_object_info_extended(the_repository, oid, &info,
@@ -143,7 +156,7 @@ static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
143156

144157
if (!tag->tagged)
145158
return NULL;
146-
if (mark_tags_complete)
159+
if (mark_tags_complete_and_check_obj_db)
147160
tag->object.flags |= COMPLETE;
148161
oid = &tag->tagged->oid;
149162
} else {

t/t5330-no-lazy-fetch-with-commit-graph.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ test_expect_success 'fetch any commit from promisor with the usage of the commit
3838
git -C with-commit-graph config remote.origin.partialclonefilter blob:none &&
3939
test_commit -C with-commit any-commit &&
4040
anycommit=$(git -C with-commit rev-parse HEAD) &&
41-
GIT_TRACE="$(pwd)/trace.txt" \
41+
test_must_fail env GIT_TRACE="$(pwd)/trace.txt" \
4242
git -C with-commit-graph fetch origin $anycommit 2>err &&
43-
! grep "fatal: promisor-remote: unable to fork off fetch subprocess" err &&
43+
test_grep ! "fatal: promisor-remote: unable to fork off fetch subprocess" err &&
4444
grep "git fetch origin" trace.txt >actual &&
4545
test_line_count = 1 actual
4646
'

0 commit comments

Comments
 (0)