git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Allow cloning an empty repository

From
Sverre Rabbelier <srabbelier@gmail.com>
Date
Jan 23, 2009, 00:07 UTC
Message-ID
<1232669252-21881-1-git-send-email-srabbelier@gmail.com>

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 <srabbelier@gmail.com>
---
Thanks to Dscho for giving some pointers on how to do this.
 builtin-clone.c        |   17 +++++++++++++----
 t/t5701-clone-local.sh |   16 ++++++++++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/builtin-clone.c b/builtin-clone.c
index f7e5a7b..d5bba0e 100644
--- a/builtin-clone.c
+++ b/builtin-clone.c
@@ -522,14 +522,23 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 					     option_upload_pack);
 
 		refs = transport_get_remote_refs(transport);
-		transport_fetch_refs(transport, refs);
+		if(refs)
+			transport_fetch_refs(transport, refs);
 	}
 
-	clear_extra_refs();
+	if(refs) {
+		clear_extra_refs();
 
-	mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
+		mapped_refs = write_remote_refs(refs, &refspec, reflog_msg.buf);
 
-	head_points_at = locate_head(refs, mapped_refs, &remote_head);
+		head_points_at = locate_head(refs, mapped_refs, &remote_head);
+	}
+	else {
+		warning("You appear to have cloned an empty repository.");
+		head_points_at = NULL;
+		remote_head = NULL;
+		option_no_checkout = 1;
+	}
 
 	if (head_points_at) {
 		/* Local default branch link */
diff --git a/t/t5701-clone-local.sh b/t/t5701-clone-local.sh
index 8dfaaa4..fbd9bfa 100755
--- a/t/t5701-clone-local.sh
+++ b/t/t5701-clone-local.sh
@@ -116,4 +116,20 @@ test_expect_success 'bundle clone with nonexistent HEAD' '
 	test ! -e .git/refs/heads/master
 '
 
+test_expect_success 'clone empty repository' '
+	cd "$D" &&
+	mkdir empty &&
+	(cd empty && git init) &&
+	git clone empty empty-clone &&
+	test_tick &&
+	(cd empty-clone
+	 echo "content" >> foo &&
+	 git add foo &&
+	 git commit -m "Initial commit" &&
+	 git push origin master &&
+	 expected=$(git rev-parse master) &&
+	 actual=$(git --git-dir=../empty/.git rev-parse master) &&
+	 test $actual = $expected)
+'
+
 test_done
-- 
1.6.1.399.g0d272.dirty
Next: Junio C Hamano
Message 1 of 23 in “Allow cloning an empty repository”
  1. Allow cloning an empty repositorySverre Rabbelier, Jan 23, 2009
  2. Junio C HamanoJan 23, 2009
  3. Sverre RabbelierJan 23, 2009
  4. Johannes SchindelinJan 23, 2009
  5. Sverre RabbelierJan 23, 2009
  6. Johannes SchindelinJan 23, 2009
  7. Miklos VajnaJan 23, 2009
  8. Johannes SchindelinJan 23, 2009
  9. Jeff KingJan 23, 2009
  10. Sverre RabbelierJan 23, 2009
  11. Jeff KingJan 23, 2009
  12. Sverre RabbelierJan 23, 2009
  13. Johannes SchindelinJan 24, 2009
  14. Mike HommeyJan 23, 2009
  15. http fixes, was Re: [PATCH] Allow cloning an empty repositoryJohannes Schindelin, Jan 24, 2009
  16. Sverre RabbelierJan 24, 2009
  17. Miklos VajnaJan 23, 2009
  18. Johannes SchindelinJan 24, 2009
  19. Sverre RabbelierJan 24, 2009
  20. Miklos VajnaJan 25, 2009
  21. Sverre RabbelierJan 25, 2009
  22. Junio C HamanoJan 25, 2009
  23. Sverre RabbelierJan 25, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.