pack-bitmap: fix bug with exact ref match in "pack.preferBitmapTips"

The "pack.preferBitmapTips" configuration allows the user to specify which references should be preferred when generating bitmaps. This option is typically expected to be set to a reference prefix, like for example "refs/heads/". It's not unreasonable though for a user to configure one specific reference as preferred. But if they do, they'll hit a `BUG()`: $ git -c pack.preferBitmapTips=refs/heads/main repack -adb BUG: ../refs/iterator.c:366: attempt to trim too many characters error: pack-objects died of signal 6 The root cause for this bug is how we enumerate these references. We call `refs_for_each_ref_in()`, which will: - Yield all references that have a user-specified prefix. - Trim each of these references so that the prefix is removed. Typically, this function is called with a trailing slash, like "refs/heads/", and in that case things work alright. But if the function is called with the name of an existing reference then we'll try to trim the full reference name, which would leave us with an empty name. And as this would not really leave us with anything sensible, we call `BUG()` instead of yielding this reference. One could argue that this is a bug in `refs_for_each_ref_in()`. But the question then becomes what the correct behaviour would be: - Do we want to skip exact matches? In our case we certainly don't want that, as the user has asked us to generate a bitmap for it. - Do we want to yield the reference with the empty refname? That would lead to a somewhat weird result. Neither of these feel like viable options, so calling `BUG()` feels like a sensible way out. The root cause ultimately is that we even try to trim the whole refname in the first place. There are two possible ways to fix this issue: - We can fix the bug by using `refs_for_each_fullref_in()` instead, which does not strip the prefix at all. Consequently, we would now start to accept all references that start with the configured prefix, including exact matches. So if we had "refs/heads/main", we would both match "refs/heads/main" and "refs/heads/main-branch". - Or we can fix the bug by appending a slash to the prefix if it doesn't already have one. This would mean that we only match ref hierarchies that start with this prefix. While the first fix leaves the user with strictly _more_ configuration options, we have already fixed a similar case in 10e8a9352b (refs.c: stop matching non-directory prefixes in exclude patterns, 2025-03-06) by using the second option. So for the sake of consistency, let's apply the same fix here. Clarify the documentation accordingly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Feb 19, 2026 at 08:57 UTC 7174098834e30d2da1834bbfd0aaf575f6d6bb1a
4 files changed +101 -5
Documentation/config/pack.adoc
+5 -4
@@ -160,12 +160,13 @@ pack.usePathWalk::
160 processes. See linkgit:git-pack-objects[1] for full details.
161
162 pack.preferBitmapTips::
163 + Specifies a ref hierarchy (e.g., "refs/heads/"); can be
164 + given multiple times to specify more than one hierarchies.
165 When selecting which commits will receive bitmaps, prefer a
164 - commit at the tip of any reference that is a suffix of any value
165 - of this configuration over any other commits in the "selection
166 - window".
166 + commit at the tip of a reference that is contained in any of
167 + the configured hierarchies.
168 +
168 -Note that setting this configuration to `refs/foo` does not mean that
169 +Note that setting this configuration to `refs/foo/` does not mean that
170 the commits at the tips of `refs/foo/bar` and `refs/foo/baz` will
171 necessarily be selected. This is because commits are selected for
172 bitmaps from within a series of windows of variable length.
pack-bitmap.c
+12 -1
@@ -3328,15 +3328,26 @@ void for_each_preferred_bitmap_tip(struct repository *repo,
3328 {
3329 struct string_list_item *item;
3330 const struct string_list *preferred_tips;
3331 + struct strbuf buf = STRBUF_INIT;
3332
3333 preferred_tips = bitmap_preferred_tips(repo);
3334 if (!preferred_tips)
3335 return;
3336
3337 for_each_string_list_item(item, preferred_tips) {
3338 + const char *pattern = item->string;
3339 +
3340 + if (!ends_with(pattern, "/")) {
3341 + strbuf_reset(&buf);
3342 + strbuf_addf(&buf, "%s/", pattern);
3343 + pattern = buf.buf;
3344 + }
3345 +
3346 refs_for_each_ref_in(get_main_ref_store(repo),
3338 - item->string, cb, cb_data);
3347 + pattern, cb, cb_data);
3348 }
3349 +
3350 + strbuf_release(&buf);
3351 }
3352
3353 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
t/t5310-pack-bitmaps.sh
+41
@@ -466,6 +466,47 @@ test_bitmap_cases () {
466 )
467 '
468
469 + test_expect_success 'pack.preferBitmapTips interprets patterns as hierarchy' '
470 + git init repo &&
471 + test_when_finished "rm -fr repo" &&
472 + (
473 + cd repo &&
474 +
475 + # Create enough commits that not all will receive bitmap
476 + # coverage even if they are all at the tip of some reference.
477 + test_commit_bulk --message="%s" 103 &&
478 + git log --format="create refs/tags/%s/tag %H" HEAD >refs &&
479 + git update-ref --stdin <refs &&
480 +
481 + # Create the bitmap.
482 + git repack -adb &&
483 + test-tool bitmap list-commits | sort >commits-with-bitmap &&
484 +
485 + # Verify that we have at least one commit that did not
486 + # receive a bitmap.
487 + git rev-list HEAD >commits.raw &&
488 + sort <commits.raw >commits &&
489 + comm -13 commits-with-bitmap commits >commits-wo-bitmap &&
490 + test_file_not_empty commits-wo-bitmap &&
491 + commit_id=$(head commits-wo-bitmap) &&
492 + ref_without_bitmap=$(git for-each-ref --points-at="$commit_id" --format="%(refname)") &&
493 +
494 + # When passing the full refname we do not expect a
495 + # bitmap to be generated, as it should be interpreted
496 + # as if a slash was appended to the pattern.
497 + git -c pack.preferBitmapTips="$ref_without_bitmap" repack -adb &&
498 + test-tool bitmap list-commits >after &&
499 + test_grep ! "$commit_id" after &&
500 +
501 + # But if we pass the parent directory of the ref we
502 + # should see a bitmap.
503 + ref_namespace=$(dirname "$ref_without_bitmap") &&
504 + git -c pack.preferBitmapTips="$ref_namespace" repack -adb &&
505 + test-tool bitmap list-commits >after &&
506 + test_grep "$commit_id" after
507 + )
508 + '
509 +
510 test_expect_success 'complains about multiple pack bitmaps' '
511 rm -fr repo &&
512 git init repo &&
t/t5319-multi-pack-index.sh
+43
@@ -1345,4 +1345,47 @@ test_expect_success 'bitmapped packs are stored via the BTMP chunk' '
1345 )
1346 '
1347
1348 +test_expect_success 'pack.preferBitmapTips interprets patterns as hierarchy' '
1349 + git init repo &&
1350 + test_when_finished "rm -fr repo" &&
1351 + (
1352 + cd repo &&
1353 +
1354 + # Create enough commits that not all will receive bitmap
1355 + # coverage even if they are all at the tip of some reference.
1356 + test_commit_bulk --message="%s" 103 &&
1357 + git log --format="create refs/tags/%s %H" HEAD >refs &&
1358 + git update-ref --stdin <refs &&
1359 +
1360 + # Create the bitmap via the MIDX.
1361 + git repack -adb --write-midx &&
1362 + test-tool bitmap list-commits | sort >commits-with-bitmap &&
1363 +
1364 + # Verify that we have at least one commit that did not
1365 + # receive a bitmap.
1366 + git rev-list HEAD >commits.raw &&
1367 + sort <commits.raw >commits &&
1368 + comm -13 commits-with-bitmap commits >commits-wo-bitmap &&
1369 + test_file_not_empty commits-wo-bitmap &&
1370 + commit_id=$(head commits-wo-bitmap) &&
1371 + ref_without_bitmap=$(git for-each-ref --points-at="$commit_id" --format="%(refname)") &&
1372 +
1373 + # When passing the full refname we do not expect a bitmap to be
1374 + # generated, as it should be interpreted as if a slash was
1375 + # appended to the pattern.
1376 + rm .git/objects/pack/multi-pack-index* &&
1377 + git -c pack.preferBitmapTips="$ref_without_bitmap" repack -adb --write-midx &&
1378 + test-tool bitmap list-commits >after &&
1379 + test_grep ! "$commit_id" after &&
1380 +
1381 + # But if we pass the parent directory of the ref we should see
1382 + # a bitmap.
1383 + ref_namespace=$(dirname "$ref_without_bitmap") &&
1384 + rm .git/objects/pack/multi-pack-index* &&
1385 + git -c pack.preferBitmapTips="$ref_namespace" repack -adb --write-midx &&
1386 + test-tool bitmap list-commits >after &&
1387 + test_grep "$commit_id" after
1388 + )
1389 +'
1390 +
1391 test_done