Skip to content

Commit dccea60

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e9d7761 commit dccea60

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

builtin/clone.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
862862
const struct ref *refs, *remote_head;
863863
struct ref *remote_head_points_at = NULL;
864864
const struct ref *our_head_points_at;
865-
struct ref *mapped_refs;
865+
struct ref *mapped_refs = NULL;
866866
const struct ref *ref;
867867
struct strbuf key = STRBUF_INIT;
868868
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)
11841184

11851185
refs = transport_get_remote_refs(transport, &transport_ls_refs_options);
11861186

1187-
if (refs) {
1187+
if (refs)
1188+
mapped_refs = wanted_peer_refs(refs, &remote->fetch);
1189+
1190+
if (mapped_refs) {
11881191
int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
11891192

11901193
/*
@@ -1193,8 +1196,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11931196
*/
11941197
initialize_repository_version(hash_algo, 1);
11951198
repo_set_hash_algo(the_repository, hash_algo);
1196-
1197-
mapped_refs = wanted_peer_refs(refs, &remote->fetch);
11981199
/*
11991200
* transport_get_remote_refs() may return refs with null sha-1
12001201
* in mapped_refs (see struct transport->get_refs_list
@@ -1240,7 +1241,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12401241
option_branch, remote_name);
12411242

12421243
warning(_("You appear to have cloned an empty repository."));
1243-
mapped_refs = NULL;
12441244
our_head_points_at = NULL;
12451245
remote_head_points_at = NULL;
12461246
remote_head = NULL;
@@ -1271,7 +1271,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12711271

12721272
if (is_local)
12731273
clone_local(path, git_dir);
1274-
else if (refs && complete_refs_before_fetch) {
1274+
else if (mapped_refs && complete_refs_before_fetch) {
12751275
if (transport_fetch_refs(transport, mapped_refs))
12761276
die(_("remote transport reported error"));
12771277
}

t/t5700-protocol-v1.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ test_expect_success 'push with file:// using protocol v1' '
149149
grep "push< version 1" log
150150
'
151151

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+
152167
# Test protocol v1 with 'ssh://' transport
153168
#
154169
test_expect_success 'setup ssh wrapper' '

0 commit comments

Comments
 (0)