Skip to content

Commit ac162a6

Browse files
committed
Merge branch 'jk/clone-unborn-head-in-bare'
"git clone" from a repository whose HEAD is unborn into a bare repository didn't follow the branch name the other side used, which is corrected. * jk/clone-unborn-head-in-bare: clone: handle unborn branch in bare repos
2 parents 4a6fd7d + 6b58df5 commit ac162a6

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

builtin/clone.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12291229
our_head_points_at = remote_head_points_at;
12301230
}
12311231
else {
1232+
const char *branch;
1233+
char *ref;
1234+
12321235
if (option_branch)
12331236
die(_("Remote branch %s not found in upstream %s"),
12341237
option_branch, remote_name);
@@ -1239,24 +1242,22 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12391242
remote_head_points_at = NULL;
12401243
remote_head = NULL;
12411244
option_no_checkout = 1;
1242-
if (!option_bare) {
1243-
const char *branch;
1244-
char *ref;
1245-
1246-
if (transport_ls_refs_options.unborn_head_target &&
1247-
skip_prefix(transport_ls_refs_options.unborn_head_target,
1248-
"refs/heads/", &branch)) {
1249-
ref = transport_ls_refs_options.unborn_head_target;
1250-
transport_ls_refs_options.unborn_head_target = NULL;
1251-
create_symref("HEAD", ref, reflog_msg.buf);
1252-
} else {
1253-
branch = git_default_branch_name(0);
1254-
ref = xstrfmt("refs/heads/%s", branch);
1255-
}
12561245

1257-
install_branch_config(0, branch, remote_name, ref);
1258-
free(ref);
1246+
if (transport_ls_refs_options.unborn_head_target &&
1247+
skip_prefix(transport_ls_refs_options.unborn_head_target,
1248+
"refs/heads/", &branch)) {
1249+
ref = transport_ls_refs_options.unborn_head_target;
1250+
transport_ls_refs_options.unborn_head_target = NULL;
1251+
create_symref("HEAD", ref, reflog_msg.buf);
1252+
} else {
1253+
branch = git_default_branch_name(0);
1254+
ref = xstrfmt("refs/heads/%s", branch);
12591255
}
1256+
1257+
if (!option_bare)
1258+
install_branch_config(0, branch, remote_name, ref);
1259+
1260+
free(ref);
12601261
}
12611262

12621263
write_refspec_config(src_ref_prefix, our_head_points_at,

t/t5702-protocol-v2.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,19 @@ test_expect_success '...but not if explicitly forbidden by config' '
237237
! grep "refs/heads/mydefaultbranch" file_empty_child/.git/HEAD
238238
'
239239

240+
test_expect_success 'bare clone propagates empty default branch' '
241+
test_when_finished "rm -rf file_empty_parent file_empty_child.git" &&
242+
243+
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
244+
git -c init.defaultBranch=mydefaultbranch init file_empty_parent &&
245+
246+
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
247+
git -c init.defaultBranch=main -c protocol.version=2 \
248+
clone --bare \
249+
"file://$(pwd)/file_empty_parent" file_empty_child.git &&
250+
grep "refs/heads/mydefaultbranch" file_empty_child.git/HEAD
251+
'
252+
240253
test_expect_success 'fetch with file:// using protocol v2' '
241254
test_when_finished "rm -f log" &&
242255

0 commit comments

Comments
 (0)