A bundle list, either advertised by a server or given to "git clone
--bundle-uri", may contain relative URIs. They are resolved with
relative_url(), which calls die() when there are more "../" components
than the base can absorb, so one bad entry aborts the whole clone.
Resolve them with relative_url_gently() instead. When that fails,
bundle_list_update() reports the problem as an error, drops the bundle,
which has no URI to use, and stops parsing the list. Dropping it keeps
the clone from complaining a second time that the bundle has no URI. A
"uri" key without a value is handled in the same way.
What happens then depends on where the list came from. For a list
advertised by a server, the bundles before the bad entry are still
used and the ones after it are not. A list given to "--bundle-uri" is
read from a file and rejected as a whole. In both cases the clone
carries on and fetches what is missing from the remote.
This resolves the TODO in t5750, so update the test to match.
Signed-off-by: Aniket Gupta <anik…@gmail.com>
---
bundle-uri.c | 29 ++++++++++++++++++++++++++---
t/t5558-clone-bundle-uri.sh | 16 ++++++++++++++++
t/t5750-bundle-uri-parse.sh | 24 ++++++++++++++++--------
3 files changed, 58 insertions(+), 11 deletions(-)
diff --git a/bundle-uri.c b/bundle-uri.c
index 41c817eefb..4cb5014968 100644
--- a/bundle-uri.c
+++ b/bundle-uri.c
@@ -134,6 +134,19 @@ void print_bundle_list(FILE *fp, struct bundle_list *list)
for_all_bundles_in_list(list, summarize_bundle, fp);
}
+/*
+ * Remove a bundle from the list when its URI cannot be used, so that the
+ * list does not keep an entry without a URI.
+ */
+static int drop_bundle(struct bundle_list *list,
+ struct remote_bundle_info *bundle)
+{
+ hashmap_remove(&list->bundles, &bundle->ent, NULL);
+ clear_remote_bundle_info(bundle, NULL);
+ free(bundle);
+ return -1;
+}
+
/**
* Given a key-value pair, update the state of the given bundle list.
* Returns 0 if the key-value pair is understood. Returns -1 if the key
@@ -214,11 +227,21 @@ static int bundle_list_update(const char *key, const char *value,
strbuf_release(&id);
if (!strcmp(subkey, "uri")) {
- if (!value)
- return config_error_nonbool(key);
+ struct strbuf err = STRBUF_INIT;
+
+ if (!value) {
+ config_error_nonbool(key);
+ return drop_bundle(list, bundle);
+ }
if (bundle->uri)
return -1;
- bundle->uri = relative_url(list->baseURI, value, NULL);
+ bundle->uri = relative_url_gently(list->baseURI, value, NULL,
+ &err);
+ if (!bundle->uri) {
+ error("%s", err.buf);
+ strbuf_release(&err);
+ return drop_bundle(list, bundle);
+ }
return 0;
}
diff --git a/t/t5558-clone-bundle-uri.sh b/t/t5558-clone-bundle-uri.sh
index e86ed7d30c..e7a3e17775 100755
--- a/t/t5558-clone-bundle-uri.sh
+++ b/t/t5558-clone-bundle-uri.sh
@@ -1351,6 +1351,22 @@ test_expect_success 'bundles advertised with empty URI' '
test_grep ! "expected packfile" err
'
+test_expect_success 'bundles advertised with unresolvable relative URI' '
+ git clone --no-local --mirror clone-from \
+ "$HTTPD_DOCUMENT_ROOT_PATH/bad-relative-uri.git" &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/bad-relative-uri.git" config uploadpack.advertiseBundleURIs true &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/bad-relative-uri.git" config bundle.version 1 &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/bad-relative-uri.git" config bundle.mode all &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/bad-relative-uri.git" config bundle.bundle-1.uri ../../../../../../../../bundle.bdl &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/bad-relative-uri.git" config bundle.bundle-1.creationToken 1 &&
+
+ git -c transfer.bundleURI=true clone \
+ "$HTTPD_URL/smart/bad-relative-uri.git" target-bad-relative-uri 2>err &&
+ test_grep "cannot strip one component off url" err &&
+ test_grep ! "has no uri" err &&
+ git -C target-bad-relative-uri fsck
+'
+
# Do not add tests here unless they use the HTTP server, as they will
# not run unless the HTTP dependencies exist.
diff --git a/t/t5750-bundle-uri-parse.sh b/t/t5750-bundle-uri-parse.sh
index a91c1b428d..f035fc99a7 100755
--- a/t/t5750-bundle-uri-parse.sh
+++ b/t/t5750-bundle-uri-parse.sh
@@ -68,17 +68,16 @@ test_expect_success 'bundle_uri_parse_line(): relative URIs and parent paths' '
uri = <uri>/bundle.bdl
[bundle "two"]
uri = bundle.bdl
- [bundle "three"]
- uri = <uri>/../bundle.bdl
EOF
- # TODO: We would prefer if parsing a bundle list would not cause
- # a die() and instead would give a warning and allow the rest of
- # a Git command to continue. This test_must_fail is necessary for
- # now until the interface for relative_url() allows for reporting
- # an error instead of die()ing.
+ cat >err.expect <<-\EOF &&
+ error: cannot strip one component off url '\''<uri>'\''
+ error: bad line: '\''bundle.three.uri=../../bundle.bdl'\''
+ EOF
+
test_must_fail test-tool bundle-uri parse-key-values in >actual 2>err &&
- test_grep "fatal: cannot strip one component off url" err
+ test_cmp err.expect err &&
+ test_cmp_config_output expect actual
'
test_expect_success 'bundle_uri_parse_line() parsing edge cases: empty key or value' '
@@ -328,4 +327,13 @@ test_expect_success 'parse config format edge cases: keys without a value' '
done
'
+test_expect_success 'parse config format edge cases: a uri without a value drops the bundle' '
+ printf "[bundle]\n\tversion = 1\n\tmode = all\n[bundle \"one\"]\n\turi\n" >input &&
+
+ test_must_fail test-tool bundle-uri parse-config input >actual 2>err &&
+ test_grep "missing value for .bundle.one.uri." err &&
+ test_grep "mode = all" actual &&
+ test_grep ! "bundle .one." actual
+'
+
test_done
--
2.54.0 (Apple Git-157)