@matheusbsbs / matheus-bagliano / commits / 0cc1b475bb
Fork 0

clone: use configured default branch name when appropriate

When cloning a repository without any branches, Git chooses a default branch name for the as-yet unborn branch. As part of the implicit initialization of the local repository, Git just learned to respect `init.defaultBranch` to choose a different initial branch name. We now really want that branch name to be used as a fall-back. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 24, 2020 at 14:46 UTC 0cc1b475bbcc530c5429c741fbcfdc2730d27223
3 files changed +24 -5
Documentation/config/init.txt
+2 -2
@@ -3,5 +3,5 @@ init.templateDir::
3 (See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)
4
5 init.defaultBranch::
6 - Allows overriding the default branch name when initializing
7 - a new repository.
6 + Allows overriding the default branch name e.g. when initializing
7 + a new repository or when cloning an empty repository.
builtin/clone.c
+7 -3
@@ -1264,9 +1264,13 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1264 remote_head_points_at = NULL;
1265 remote_head = NULL;
1266 option_no_checkout = 1;
1267 - if (!option_bare)
1268 - install_branch_config(0, "master", option_origin,
1269 - "refs/heads/master");
1267 + if (!option_bare) {
1268 + const char *branch = git_default_branch_name();
1269 + char *ref = xstrfmt("refs/heads/%s", branch);
1270 +
1271 + install_branch_config(0, branch, option_origin, ref);
1272 + free(ref);
1273 + }
1274 }
1275
1276 write_refspec_config(src_ref_prefix, our_head_points_at,
t/t5606-clone-options.sh
+15
@@ -35,4 +35,19 @@ test_expect_success 'redirected clone -v does show progress' '
35
36 '
37
38 +test_expect_success 'chooses correct default initial branch name' '
39 + git init --bare empty &&
40 + git -c init.defaultBranch=up clone empty whats-up &&
41 + test refs/heads/up = $(git -C whats-up symbolic-ref HEAD) &&
42 + test refs/heads/up = $(git -C whats-up config branch.up.merge)
43 +'
44 +
45 +test_expect_success 'guesses initial branch name correctly' '
46 + git init --initial-branch=guess initial-branch &&
47 + test_commit -C initial-branch no-spoilers &&
48 + git -C initial-branch branch abc guess &&
49 + git clone initial-branch is-it &&
50 + test refs/heads/guess = $(git -C is-it symbolic-ref HEAD)
51 +'
52 +
53 test_done