Skip to content

Commit f813e9e

Browse files
peffgitster
authored andcommitted
for_each_packed_object: automatically open pack index
When for_each_packed_object is called, we call prepare_packed_git() to make sure we have the actual list of packs. But the latter does not actually open the pack indices, meaning that pack->nr_objects may simply be 0 if the pack has not otherwise been used since the program started. In practice, this didn't come up for the current callers, because they iterate the packed objects only after iterating all reachable objects (so for it to matter you would have to have a pack consisting only of unreachable objects). But it is a dangerous and confusing interface that should be fixed for future callers. Note that we do not end the iteration when a pack cannot be opened, but we do return an error. That lets you complete the iteration even in actively-repacked repository where an .idx file may racily go away, but it also lets callers know that they may not have gotten the complete list (which the current reachability-check caller does care about). We have to tweak one of the prune tests due to the changed return value; an earlier test creates bogus .idx files and does not clean them up. Having to make this tweak is a good thing; it means we will not prune in a broken repository, and the test confirms that we do not negatively impact a more lenient caller, count-objects. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ebb464f commit f813e9e

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

sha1_file.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3466,14 +3466,19 @@ int for_each_packed_object(each_packed_object_fn cb, void *data, unsigned flags)
34663466
{
34673467
struct packed_git *p;
34683468
int r = 0;
3469+
int pack_errors = 0;
34693470

34703471
prepare_packed_git();
34713472
for (p = packed_git; p; p = p->next) {
34723473
if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
34733474
continue;
3475+
if (open_pack_index(p)) {
3476+
pack_errors = 1;
3477+
continue;
3478+
}
34743479
r = for_each_object_in_pack(p, cb, data);
34753480
if (r)
34763481
break;
34773482
}
3478-
return r;
3483+
return r ? r : pack_errors;
34793484
}

t/t5304-prune.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ test_expect_success 'gc: prune old objects after local clone' '
218218
'
219219

220220
test_expect_success 'garbage report in count-objects -v' '
221+
test_when_finished "rm -f .git/objects/pack/fake*" &&
221222
: >.git/objects/pack/foo &&
222223
: >.git/objects/pack/foo.bar &&
223224
: >.git/objects/pack/foo.keep &&

0 commit comments

Comments
 (0)