Skip to content

Commit 86ac751

Browse files
SRabbeliergitster
authored andcommitted
Allow cloning an empty repository
Cloning an empty repository manually (that is, doing 'git init' and then doing all configuration by hand) can be a lot of work. Save the user this work by allowing the cloning of empty repositories. Signed-off-by: Sverre Rabbelier <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9a01387 commit 86ac751

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

builtin-clone.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,23 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
522522
option_upload_pack);
523523

524524
refs = transport_get_remote_refs(transport);
525-
transport_fetch_refs(transport, refs);
525+
if(refs)
526+
transport_fetch_refs(transport, refs);
526527
}
527528

528-
clear_extra_refs();
529+
if (refs) {
530+
clear_extra_refs();
529531

530-
mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
532+
mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
531533

532-
head_points_at = locate_head(refs, mapped_refs, &remote_head);
534+
head_points_at = locate_head(refs, mapped_refs, &remote_head);
535+
}
536+
else {
537+
warning("You appear to have cloned an empty repository.");
538+
head_points_at = NULL;
539+
remote_head = NULL;
540+
option_no_checkout = 1;
541+
}
533542

534543
if (head_points_at) {
535544
/* Local default branch link */

t/t5701-clone-local.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,20 @@ test_expect_success 'bundle clone with nonexistent HEAD' '
116116
test ! -e .git/refs/heads/master
117117
'
118118

119+
test_expect_success 'clone empty repository' '
120+
cd "$D" &&
121+
mkdir empty &&
122+
(cd empty && git init) &&
123+
git clone empty empty-clone &&
124+
test_tick &&
125+
(cd empty-clone
126+
echo "content" >> foo &&
127+
git add foo &&
128+
git commit -m "Initial commit" &&
129+
git push origin master &&
130+
expected=$(git rev-parse master) &&
131+
actual=$(git --git-dir=../empty/.git rev-parse master) &&
132+
test $actual = $expected)
133+
'
134+
119135
test_done

0 commit comments

Comments
 (0)