Commit

314b468c68de5a61f5d3b35ada15ee9cd6ba875d

builtin/multi-pack-index: refuse unknown sources with "--object-dir="

Users can tell git-multi-pack-index(1) to access multi-pack indices that
are stored in a different object directory via the "--object-dir="
option. This allows them to for example write or verify a multi-pack
index other than the one located in the main object directory in case a
repository has alternates with multiple multi-pack indices.

But while the documentation explicitly points out that the specified
object directory must be an alternate of the current repository, we
never verify that property. Instead, starting with 017db7bb14 (midx:
load multi-pack indices via their source, 2025-08-11), we now construct
an ad-hoc source and link it to the main object directory.

Besides contradicting the documentation, it's dubious that this really
ought to work in the first place: creating a multi-pack index (and
potentially a bitmap) for a completely foreign object directory is of
questionable value, as bitmap commit selection operates on the invoking
repository's refs. Furthermore, this is the only remaining caller
outside of our test helpers that constructs an ad-hoc source and links
it to the database, and we want to get rid of this mechanism as part of
this series.

Stop constructing the ad-hoc source and instead refuse the operation.
While this results in a change in behaviour, this restriction has been
documented as such ever since f57a739691 (midx: avoid opening multiple
MIDXs when writing, 2021-09-01).

Note that this change requires us to adapt one test chain in t5319, as
it creates an object directory that is not connected to any repository
and then uses it via "--object-dir=". The setup itself already documents
this and does the necessary gymnastics to link the object directory to a
temporary repository, but subsequent tests don't. Adapt those tests to
retain and reuse the temporary repository.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt <ps@pks.im> · 2026-09-11 05:51 UTC · committed by Junio C Hamano

Browse files
2 files changed, +6 −6
modified builtin/multi-pack-index.c
@@ -90,7 +90,8 @@ static struct odb_source_files *handle_object_dir_option(struct repository *repo
90 90 {
91 91 struct odb_source *source = odb_find_source(repo->objects, opts.object_dir);
92 92 if (!source)
93 - source = odb_add_to_alternates_memory(repo->objects, opts.object_dir);
93 + die(_("object directory is not an alternate of the current repository: '%s'"),
94 + opts.object_dir);
94 95 return odb_source_files_downcast(source);
95 96 }
96 97
modified t/t5319-multi-pack-index.sh
@@ -698,10 +698,9 @@ test_expect_success 'force some 64-bit offsets with pack-objects' '
698 698 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
699 699 # objects64 is not a real repository, but can serve as an alternate
700 700 # anyway so we can write a MIDX into it
701 - git init repo &&
702 - test_when_finished "rm -fr repo" &&
701 + git init repo64 &&
703 702 (
704 - cd repo &&
703 + cd repo64 &&
705 704 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
706 705 midx64=$(git multi-pack-index --object-dir=../objects64 write)
707 706 ) &&
@@ -709,7 +708,7 @@ test_expect_success 'force some 64-bit offsets with pack-objects' '
709 708 '
710 709
711 710 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
712 - git multi-pack-index verify --object-dir=objects64
711 + git -C repo64 multi-pack-index verify --object-dir=../objects64
713 712 '
714 713
715 714 NUM_OBJECTS=63
@@ -721,7 +720,7 @@ MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
721 720
722 721 test_expect_success 'verify incorrect 64-bit offset' '
723 722 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
724 - "incorrect object offset"
723 + "incorrect object offset" "git -C repo64 multi-pack-index verify --object-dir=../objects64"
725 724 '
726 725
727 726 test_expect_success 'setup expire tests' '