summaryrefslogtreecommitdiff
path: root/builtin/clone.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2022-07-07 23:59:35 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-07-08 03:57:54 (GMT)
commitcc8fcd1e1ac6ae2a7463b295ccb48e18c73f924a (patch)
tree31788c7aa588fecf4f1f5eda5f4219d7f0646bfc /builtin/clone.c
parent3d8314f8d1e68d92f6c91d50e5a510db61355774 (diff)
downloadgit-cc8fcd1e1ac6ae2a7463b295ccb48e18c73f924a.zip
git-cc8fcd1e1ac6ae2a7463b295ccb48e18c73f924a.tar.gz
git-cc8fcd1e1ac6ae2a7463b295ccb48e18c73f924a.tar.bz2
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>
Diffstat (limited to 'builtin/clone.c')
-rw-r--r--builtin/clone.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/builtin/clone.c b/builtin/clone.c
index 9288fe2..eafbd3d 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -1290,8 +1290,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
option_no_checkout = 1;
}
- our_head_points_at = NULL;
-
if (transport_ls_refs_options.unborn_head_target &&
skip_prefix(transport_ls_refs_options.unborn_head_target,
"refs/heads/", &branch)) {
@@ -1303,7 +1301,20 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
ref = ref_free;
}
- if (!option_bare)
+ /*
+ * We may have selected a local default branch name "foo",
+ * and even though the remote's HEAD does not point there,
+ * it may still have a "foo" branch. If so, set it up so
+ * that we can follow the usual checkout code later.
+ *
+ * Note that for an empty repo we'll already have set
+ * option_no_checkout above, which would work against us here.
+ * But for an empty repo, find_remote_branch() can never find
+ * a match.
+ */
+ our_head_points_at = find_remote_branch(mapped_refs, branch);
+
+ if (!option_bare && !our_head_points_at)
install_branch_config(0, branch, remote_name, ref);
free(ref_free);
}