clone: use remote branch if it matches default HEAD

Usually clone tries to use the same local HEAD as the remote (unless the user has given --branch explicitly). Even if the remote HEAD is detached or unborn, we can detect those situations with modern versions of Git. If the remote is too old to support the "unborn" extension (or it has been disabled via config), then we can't know the name of the remote's unborn HEAD, and we fall back whatever the local default branch name is configured to be. But that leads to one weird corner case. It's rare because it needs a number of factors: - the remote has an unborn HEAD - the remote is too old to support "unborn", or has disabled it - the remote has another branch "foo" - the local default branch name is "foo" In that case you end up with a local clone on an unborn "foo" branch, disconnected completely from the remote's "foo". This is rare in practice, but the result is quite confusing. When choosing "foo", we can double check whether the remote has such a name, and if so, start our local "foo" at the same spot, rather than making it unborn. Note that this causes a test failure in t5605, which is cloning from a bundle that doesn't contain HEAD (so it behaves like a remote that doesn't support "unborn"), but has a single "main" branch. That test expects that we end up in the weird "unborn main" case, where we don't actually check out the remote branch of the same name. Even though we have to update the test, this seems like an argument in favor of this patch: checking out main is what I'd expect from such a bundle. So this patch updates the test for the new behavior and adds an adjacent one that checks what the original was going for: if there's no HEAD and the bundle _doesn't_ have a branch that matches our local default name, then we end up with nothing checked out. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 7, 2022 at 19:59 UTC cc8fcd1e1ac6ae2a7463b295ccb48e18c73f924a
3 files changed +48 -6
builtin/clone.c
+14 -3
@@ -1290,8 +1290,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1290 option_no_checkout = 1;
1291 }
1292
1293 - our_head_points_at = NULL;
1294 -
1293 if (transport_ls_refs_options.unborn_head_target &&
1294 skip_prefix(transport_ls_refs_options.unborn_head_target,
1295 "refs/heads/", &branch)) {
@@ -1303,7 +1301,20 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1301 ref = ref_free;
1302 }
1303
1306 - if (!option_bare)
1304 + /*
1305 + * We may have selected a local default branch name "foo",
1306 + * and even though the remote's HEAD does not point there,
1307 + * it may still have a "foo" branch. If so, set it up so
1308 + * that we can follow the usual checkout code later.
1309 + *
1310 + * Note that for an empty repo we'll already have set
1311 + * option_no_checkout above, which would work against us here.
1312 + * But for an empty repo, find_remote_branch() can never find
1313 + * a match.
1314 + */
1315 + our_head_points_at = find_remote_branch(mapped_refs, branch);
1316 +
1317 + if (!option_bare && !our_head_points_at)
1318 install_branch_config(0, branch, remote_name, ref);
1319 free(ref_free);
1320 }
t/t5605-clone-local.sh
+13 -3
@@ -21,7 +21,9 @@ test_expect_success 'preparing origin repository' '
21 git bundle create b2.bundle main &&
22 mkdir dir &&
23 cp b1.bundle dir/b3 &&
24 - cp b1.bundle b4
24 + cp b1.bundle b4 &&
25 + git branch not-main main &&
26 + git bundle create b5.bundle not-main
27 '
28
29 test_expect_success 'local clone without .git suffix' '
@@ -83,11 +85,19 @@ test_expect_success 'bundle clone from b4.bundle that does not exist' '
85 test_must_fail git clone b4.bundle bb
86 '
87
86 -test_expect_success 'bundle clone with nonexistent HEAD' '
88 +test_expect_success 'bundle clone with nonexistent HEAD (match default)' '
89 git clone b2.bundle b2 &&
90 (cd b2 &&
91 git fetch &&
90 - test_must_fail git rev-parse --verify refs/heads/main)
92 + git rev-parse --verify refs/heads/main)
93 +'
94 +
95 +test_expect_success 'bundle clone with nonexistent HEAD (no match default)' '
96 + git clone b5.bundle b5 &&
97 + (cd b5 &&
98 + git fetch &&
99 + test_must_fail git rev-parse --verify refs/heads/main &&
100 + test_must_fail git rev-parse --verify refs/heads/not-main)
101 '
102
103 test_expect_success 'clone empty repository' '
t/t5702-protocol-v2.sh
+21
@@ -288,6 +288,27 @@ test_expect_success 'bare clone propagates unborn HEAD from non-empty repo' '
288 ! grep "warning:" stderr
289 '
290
291 +test_expect_success 'defaulted HEAD uses remote branch if available' '
292 + test_when_finished "rm -rf file_unborn_parent file_unborn_child" &&
293 +
294 + git init file_unborn_parent &&
295 + (
296 + cd file_unborn_parent &&
297 + git config lsrefs.unborn ignore &&
298 + git checkout -b branchwithstuff &&
299 + test_commit --no-tag stuff &&
300 + git symbolic-ref HEAD refs/heads/mydefaultbranch
301 + ) &&
302 +
303 + GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
304 + git -c init.defaultBranch=branchwithstuff -c protocol.version=2 \
305 + clone "file://$(pwd)/file_unborn_parent" \
306 + file_unborn_child 2>stderr &&
307 + grep "refs/heads/branchwithstuff" file_unborn_child/.git/HEAD &&
308 + test_path_is_file file_unborn_child/stuff.t &&
309 + ! grep "warning:" stderr
310 +'
311 +
312 test_expect_success 'fetch with file:// using protocol v2' '
313 test_when_finished "rm -f log" &&
314