Skip to content

Commit cc8fcd1

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3d8314f commit cc8fcd1

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

builtin/clone.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,8 +1290,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12901290
option_no_checkout = 1;
12911291
}
12921292

1293-
our_head_points_at = NULL;
1294-
12951293
if (transport_ls_refs_options.unborn_head_target &&
12961294
skip_prefix(transport_ls_refs_options.unborn_head_target,
12971295
"refs/heads/", &branch)) {
@@ -1303,7 +1301,20 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13031301
ref = ref_free;
13041302
}
13051303

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)
13071318
install_branch_config(0, branch, remote_name, ref);
13081319
free(ref_free);
13091320
}

t/t5605-clone-local.sh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ test_expect_success 'preparing origin repository' '
2121
git bundle create b2.bundle main &&
2222
mkdir dir &&
2323
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
2527
'
2628

2729
test_expect_success 'local clone without .git suffix' '
@@ -83,11 +85,19 @@ test_expect_success 'bundle clone from b4.bundle that does not exist' '
8385
test_must_fail git clone b4.bundle bb
8486
'
8587

86-
test_expect_success 'bundle clone with nonexistent HEAD' '
88+
test_expect_success 'bundle clone with nonexistent HEAD (match default)' '
8789
git clone b2.bundle b2 &&
8890
(cd b2 &&
8991
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)
91101
'
92102

93103
test_expect_success 'clone empty repository' '

t/t5702-protocol-v2.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,27 @@ test_expect_success 'bare clone propagates unborn HEAD from non-empty repo' '
288288
! grep "warning:" stderr
289289
'
290290

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+
291312
test_expect_success 'fetch with file:// using protocol v2' '
292313
test_when_finished "rm -f log" &&
293314

0 commit comments

Comments
 (0)