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

[BUG] (minor) Cloning an empty repository

From
Matthieu Moy <matthieu.moy@imag.fr>
Date
Aug 31, 2007, 10:03 UTC
Message-ID
<vpqk5rc10mx.fsf@bauges.imag.fr>
Hi,

Git fails to clone an empty repository (no commit yet), but claims it does:

/tmp$ mkdir empty /tmp$ cd empty /tmp/empty$ git init Initialized empty Git repository in .git/ /tmp/empty$ cd ../ /tmp$ git clone empty my-clone Initialized empty Git repository in /tmp/my-clone/.git/

# All seems correct.

/tmp$ cd my-clone cd: no such file or directory: my-clone

# Gosh, git lied when telling me it initialized # /tmp/my-clone/.git/ ? ;-)

A quick look at the code tells me that the directory was actually initialized, but later, at git-clone.sh:282,

			# objects directory should not be empty because
			# we are cloning!
			test -f "$repo/$sample_file" || exit

and the exit is trapped to "cleanup()" which rm -fr the directory. Unfortunately, just commenting-out this line isn't sufficient, it leaves me with a half-initialized repository (.git/config/ doesn't contain the "origin" remote).

That's a detail, but it would be better if either git could actually do the clone, or give me a clean error message like "Can not clone empty repository".

If no one does anything better, the following patch can be a very small step forward.

>From fdbb61bb9ac26627c4786b58f38426bde621cdcb Mon Sep 17 00:00:00 2001
From: Matthieu Moy <Matthieu.Moy@imag.fr>
Date: Fri, 31 Aug 2007 12:00:09 +0200
Subject: [PATCH] Slightly more informative error message when cloning an empty repository.
---
 git-clone.sh |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/git-clone.sh b/git-clone.sh
index 18003ab..1aad510 100755
--- a/git-clone.sh
+++ b/git-clone.sh
@@ -278,7 +278,11 @@ yes)
 				      find objects -type f -print | sed -e 1q)
 			# objects directory should not be empty because
 			# we are cloning!
-			test -f "$repo/$sample_file" || exit
+			if test \! -f "$repo/$sample_file"
+			then
+				echo "No objects in repository. Cleaning up ..." >&2
+				exit
+			fi
 			if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
 			then
 				rm -f "$GIT_DIR/objects/sample"
-- 
1.5.3.rc6.49.gd50b


PS: In case someone's interested, my use-case is the following:

I have ~/archives/git/ which contains git tree-less repositories
(normally bare repository, but sometimes simply normal repositories in
which I didn't checkout), and I have a working tree somewhere else in
my $HOME (or on my /local, faster but not backed-up). I set my commit
hook to "git push --all", so it's a kind of centralized system inside
my machine. Just to separate the stuff that matters (repositories),
and the others (working tree).

To start a new project, I can either create the repository with the
working tree, and clone it to ~/archives/git/some-project, but it's
more conveinient to create ~/archives/git/some-project and then clone
it somewhere else, since it sets up the "origin" remote for me, and
push/pull work out of the box.

-- 
Matthieu
Next: Petr Baudis
Message 1 of 2 in “[BUG] (minor) Cloning an empty repository”
  1. Matthieu MoyAug 31, 2007
  2. Petr BaudisAug 31, 2007

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.