clone: support unusual remote ref configurations

When cloning a branchless and tagless but not refless remote using protocol v0 or v1, Git calls transport_fetch_refs() with an empty ref list. This makes the clone fail with the message "remote transport reported error". Git should have refrained from calling transport_fetch_refs(), just like it does in the case that the remote is refless. Therefore, teach Git to do this. In protocol v2, this does not happen because the client passes ref-prefix arguments that filter out non-branches and non-tags in the ref advertisement, making the remote appear empty. Note that this bug concerns logic in builtin/clone.c and only affects cloning, not fetching. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Jan 24, 2022 at 10:09 UTC dccea605b6c30262a6a44bde8d10b0a20c331d87
2 files changed +21 -6
builtin/clone.c
+6 -6
@@ -862,7 +862,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
862 const struct ref *refs, *remote_head;
863 struct ref *remote_head_points_at = NULL;
864 const struct ref *our_head_points_at;
865 - struct ref *mapped_refs;
865 + struct ref *mapped_refs = NULL;
866 const struct ref *ref;
867 struct strbuf key = STRBUF_INIT;
868 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
@@ -1184,7 +1184,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1184
1185 refs = transport_get_remote_refs(transport, &transport_ls_refs_options);
1186
1187 - if (refs) {
1187 + if (refs)
1188 + mapped_refs = wanted_peer_refs(refs, &remote->fetch);
1189 +
1190 + if (mapped_refs) {
1191 int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
1192
1193 /*
@@ -1193,8 +1196,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1196 */
1197 initialize_repository_version(hash_algo, 1);
1198 repo_set_hash_algo(the_repository, hash_algo);
1196 -
1197 - mapped_refs = wanted_peer_refs(refs, &remote->fetch);
1199 /*
1200 * transport_get_remote_refs() may return refs with null sha-1
1201 * in mapped_refs (see struct transport->get_refs_list
@@ -1240,7 +1241,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1241 option_branch, remote_name);
1242
1243 warning(_("You appear to have cloned an empty repository."));
1243 - mapped_refs = NULL;
1244 our_head_points_at = NULL;
1245 remote_head_points_at = NULL;
1246 remote_head = NULL;
@@ -1271,7 +1271,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1271
1272 if (is_local)
1273 clone_local(path, git_dir);
1274 - else if (refs && complete_refs_before_fetch) {
1274 + else if (mapped_refs && complete_refs_before_fetch) {
1275 if (transport_fetch_refs(transport, mapped_refs))
1276 die(_("remote transport reported error"));
1277 }
t/t5700-protocol-v1.sh
+15
@@ -149,6 +149,21 @@ test_expect_success 'push with file:// using protocol v1' '
149 grep "push< version 1" log
150 '
151
152 +test_expect_success 'cloning branchless tagless but not refless remote' '
153 + rm -rf server client &&
154 +
155 + git -c init.defaultbranch=main init server &&
156 + echo foo >server/foo.txt &&
157 + git -C server add foo.txt &&
158 + git -C server commit -m "message" &&
159 + git -C server update-ref refs/notbranch/alsonottag HEAD &&
160 + git -C server checkout --detach &&
161 + git -C server branch -D main &&
162 + git -C server symbolic-ref HEAD refs/heads/nonexistentbranch &&
163 +
164 + git -c protocol.version=1 clone "file://$(pwd)/server" client
165 +'
166 +
167 # Test protocol v1 with 'ssh://' transport
168 #
169 test_expect_success 'setup ssh wrapper' '