bundle: default to SHA1 when reading bundle headers

We hit a segfault when trying to open a bundle via `git bundle list-heads` when running outside of a repository. This is caused by c8aed5e8da (repository: stop setting SHA1 as the default object hash, 2024-05-07), which stopped setting the default object hash so that `the_hash_algo` is a `NULL` pointer when running outside of any repo. This is only a symptom of a deeper issue though. Bundles default to the SHA1 object format unless they advertise an "@object-format=" header. Consequently, it has been wrong in the first place to use the object format used by the current repository when parsing bundles. The consequence is that trying to open a bundle that uses a different object hash than the current repository will fail: $ git bundle list-heads sha1.bundle error: unrecognized header: ee4b540943284700a32591ad09f7e15bdeb2a10c HEAD (45) Fix the bug by defaulting to the SHA1 object hash. We already handle the "@object-format=" header as expected, so we don't need to adapt this part. Helped-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 13, 2024 at 11:18 UTC 96a9a3e42e85874ba5edfcf86d91f7d8c05d5f94
2 files changed +31 -1
bundle.c
+6 -1
@@ -89,7 +89,12 @@ int read_bundle_header_fd(int fd, struct bundle_header *header,
89 goto abort;
90 }
91
92 - header->hash_algo = the_hash_algo;
92 + /*
93 + * The default hash format for bundles is SHA1, unless told otherwise
94 + * by an "object-format=" capability, which is being handled in
95 + * `parse_capability()`.
96 + */
97 + header->hash_algo = &hash_algos[GIT_HASH_SHA1];
98
99 /* The bundle header ends with an empty line */
100 while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
t/t6020-bundle-misc.sh
+25
@@ -659,4 +659,29 @@ test_expect_success 'unbundle outside of a repository' '
659 test_cmp expect err
660 '
661
662 +test_expect_success 'list-heads outside of a repository' '
663 + git bundle create some.bundle HEAD &&
664 + cat >expect <<-EOF &&
665 + $(git rev-parse HEAD) HEAD
666 + EOF
667 + nongit git bundle list-heads "$(pwd)/some.bundle" >actual &&
668 + test_cmp expect actual
669 +'
670 +
671 +for hash in sha1 sha256
672 +do
673 + test_expect_success "list-heads with bundle using $hash" '
674 + test_when_finished "rm -rf hash" &&
675 + git init --object-format=$hash hash &&
676 + test_commit -C hash initial &&
677 + git -C hash bundle create hash.bundle HEAD &&
678 +
679 + cat >expect <<-EOF &&
680 + $(git -C hash rev-parse HEAD) HEAD
681 + EOF
682 + git bundle list-heads hash/hash.bundle >actual &&
683 + test_cmp expect actual
684 + '
685 +done
686 +
687 test_done