unbundle: extend object verification for fetches

The existing fetch.fsckObjects and transfer.fsckObjects configurations were not fully applied to bundle-involved fetches, including direct bundle fetches and bundle-uri enabled fetches. Furthermore, there was no object verification support for unbundle. This commit extends object verification support in `bundle.c:unbundle` by adding the `VERIFY_BUNDLE_FSCK` option to `verify_bundle_flags`. When this option is enabled, we append the `--fsck-objects` flag to `git-index-pack`. The `VERIFY_BUNDLE_FSCK` option is now used by bundle-involved fetches, where we use `fetch-pack.c:fetch_pack_fsck_objects` to determine whether to enable this option for `bundle.c:unbundle`, specifically in: - `transport.c:fetch_refs_from_bundle` for direct bundle fetches. - `bundle-uri.c:unbundle_from_file` for bundle-uri enabled fetches. This addition ensures a consistent logic for object verification during fetches. Tests have been added to confirm functionality in the scenarios mentioned above. Reviewed-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Xing Xin <xingxin.xx@bytedance.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Xing Xin committed Jun 19, 2024 at 04:07 UTC 63d903ff52594eb52289abb89db1a4bca7b0f946
6 files changed +76 -3
bundle-uri.c
+2 -1
@@ -9,6 +9,7 @@
9 #include "hashmap.h"
10 #include "pkt-line.h"
11 #include "config.h"
12 +#include "fetch-pack.h"
13 #include "remote.h"
14
15 static struct {
@@ -373,7 +374,7 @@ static int unbundle_from_file(struct repository *r, const char *file)
374 * the prerequisite commits.
375 */
376 if ((result = unbundle(r, &header, bundle_fd, NULL,
376 - VERIFY_BUNDLE_QUIET)))
377 + VERIFY_BUNDLE_QUIET | (fetch_pack_fsck_objects() ? VERIFY_BUNDLE_FSCK : 0))))
378 return 1;
379
380 /*
bundle.c
+3
@@ -625,6 +625,9 @@ int unbundle(struct repository *r, struct bundle_header *header,
625 if (header->filter.choice)
626 strvec_push(&ip.args, "--promisor=from-bundle");
627
628 + if (flags & VERIFY_BUNDLE_FSCK)
629 + strvec_push(&ip.args, "--fsck-objects");
630 +
631 if (extra_index_pack_args) {
632 strvec_pushv(&ip.args, extra_index_pack_args->v);
633 strvec_clear(extra_index_pack_args);
bundle.h
+1
@@ -33,6 +33,7 @@ int create_bundle(struct repository *r, const char *path,
33 enum verify_bundle_flags {
34 VERIFY_BUNDLE_VERBOSE = (1 << 0),
35 VERIFY_BUNDLE_QUIET = (1 << 1),
36 + VERIFY_BUNDLE_FSCK = (1 << 2),
37 };
38
39 int verify_bundle(struct repository *r, struct bundle_header *header,
t/t5558-clone-bundle-uri.sh
+33 -1
@@ -36,7 +36,22 @@ test_expect_success 'create bundle' '
36 sed -e "/^$/q" -e "s/$commit_a /$commit_b /" \
37 <A.bundle >bad-header.bundle &&
38 convert_bundle_to_pack \
39 - <A.bundle >>bad-header.bundle
39 + <A.bundle >>bad-header.bundle &&
40 +
41 + tree_b=$(git rev-parse B^{tree}) &&
42 + cat >data <<-EOF &&
43 + tree $tree_b
44 + parent $commit_b
45 + author A U Thor
46 + committer A U Thor
47 +
48 + commit: this is a commit with bad emails
49 +
50 + EOF
51 + bad_commit=$(git hash-object --literally -t commit -w --stdin <data) &&
52 + git branch bad $bad_commit &&
53 + git bundle create bad-object.bundle bad &&
54 + git update-ref -d refs/heads/bad
55 )
56 '
57
@@ -58,6 +73,23 @@ test_expect_success 'clone with bundle that has bad header' '
73 test_grep ! "refs/bundles/" refs
74 '
75
76 +test_expect_success 'clone with bundle that has bad object' '
77 + # Unbundle succeeds if no fsckObjects configured.
78 + git clone --bundle-uri="clone-from/bad-object.bundle" \
79 + clone-from clone-bad-object-no-fsck &&
80 + git -C clone-bad-object-no-fsck for-each-ref --format="%(refname)" >refs &&
81 + grep "refs/bundles/" refs >actual &&
82 + test_write_lines refs/bundles/bad >expect &&
83 + test_cmp expect actual &&
84 +
85 + # Unbundle fails with fsckObjects set true, but clone can still proceed.
86 + git -c fetch.fsckObjects=true clone --bundle-uri="clone-from/bad-object.bundle" \
87 + clone-from clone-bad-object-fsck 2>err &&
88 + test_grep "missingEmail" err &&
89 + git -C clone-bad-object-fsck for-each-ref --format="%(refname)" >refs &&
90 + test_grep ! "refs/bundles/" refs
91 +'
92 +
93 test_expect_success 'clone with path bundle and non-default hash' '
94 test_when_finished "rm -rf clone-path-non-default-hash" &&
95 GIT_DEFAULT_HASH=sha256 git clone --bundle-uri="clone-from/B.bundle" \
t/t5607-clone-bundle.sh
+35
@@ -138,6 +138,41 @@ test_expect_success 'fetch SHA-1 from bundle' '
138 git fetch --no-tags foo/tip.bundle "$(cat hash)"
139 '
140
141 +test_expect_success 'clone bundle with different fsckObjects configurations' '
142 + test_create_repo bundle-fsck &&
143 + (
144 + cd bundle-fsck &&
145 + test_commit A &&
146 + commit_a=$(git rev-parse A) &&
147 + tree_a=$(git rev-parse A^{tree}) &&
148 + cat >data <<-EOF &&
149 + tree $tree_a
150 + parent $commit_a
151 + author A U Thor
152 + committer A U Thor
153 +
154 + commit: this is a commit with bad emails
155 +
156 + EOF
157 + bad_commit=$(git hash-object --literally -t commit -w --stdin <data) &&
158 + git branch bad $bad_commit &&
159 + git bundle create bad.bundle bad
160 + ) &&
161 +
162 + git clone bundle-fsck/bad.bundle bundle-no-fsck &&
163 +
164 + git -c fetch.fsckObjects=false -c transfer.fsckObjects=true \
165 + clone bundle-fsck/bad.bundle bundle-fetch-no-fsck &&
166 +
167 + test_must_fail git -c fetch.fsckObjects=true \
168 + clone bundle-fsck/bad.bundle bundle-fetch-fsck 2>err &&
169 + test_grep "missingEmail" err &&
170 +
171 + test_must_fail git -c transfer.fsckObjects=true \
172 + clone bundle-fsck/bad.bundle bundle-transfer-fsck 2>err &&
173 + test_grep "missingEmail" err
174 +'
175 +
176 test_expect_success 'git bundle uses expected default format' '
177 git bundle create bundle HEAD^.. &&
178 cat >expect <<-EOF &&
transport.c
+2 -1
@@ -184,7 +184,8 @@ static int fetch_refs_from_bundle(struct transport *transport,
184 if (!data->get_refs_from_bundle_called)
185 get_refs_from_bundle_inner(transport);
186 ret = unbundle(the_repository, &data->header, data->fd,
187 - &extra_index_pack_args, 0);
187 + &extra_index_pack_args,
188 + fetch_pack_fsck_objects() ? VERIFY_BUNDLE_FSCK : 0);
189 transport->hash_algo = data->header.hash_algo;
190 return ret;
191 }