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

Re: [PATCH] add--interactive: handle initial commit better

From
Jeff King <peff@peff.net>
Date
Feb 13, 2008, 11:25 UTC
Message-ID
<20080213112504.GA26627@coredump.intra.peff.net>
In-Reply-To
<20080213105051.GA26522@coredump.intra.peff.net>
On Wed, Feb 13, 2008 at 05:50:51AM -0500, Jeff King wrote:
> And here it is. I think this is a more sane approach in general than the
> last patch. The only ugly thing is the empty tree hack, but that can be
> addressed with a patch to allow referencing the empty tree without it
> existing in the object db.

And this patch hard-codes the empty tree. I _think_ this shouldn't cause us any problems, as it just reuses the existing pretend_sha1_file infrastructure. However that infrastructure is very rarely used.

This patch also contains the matching fix to git-add--interactive (on top of my last patch). They should probably just be squashed together, but I can submit a cleaned up 2-patch series if people agree that this is a good idea.

-- >8 -- hard-code the empty tree object

Now any commands may reference the empty tree object by its sha1 (4b825dc642cb6eb9a060e54bf8d69288fbee4904). This is useful for showing some diffs, especially for initial commits.

Signed-off-by: Jeff King <peff@peff.net>
---
 git-add--interactive.perl |   13 ++-----------
 sha1_file.c               |   11 +++++++++++
 2 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index bae631e..a0a81f1 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -91,17 +91,8 @@ my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
 	}
 }
 
-{
-	my $sha1;
-	sub get_empty_tree {
-		if (!$sha1) {
-			local $ENV{GIT_INDEX_FILE} = "$GIT_DIR/empty_index";
-			$sha1 = run_cmd_pipe(qw(git write-tree));
-			chomp $sha1;
-			unlink($ENV{GIT_INDEX_FILE});
-		}
-		return $sha1;
-	}
+sub get_empty_tree {
+	return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
 }
 
 # Returns list of hashes, contents of each of which are:
diff --git a/sha1_file.c b/sha1_file.c
index 4179949..1a6c7c8 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1845,6 +1845,15 @@ static struct cached_object {
 } *cached_objects;
 static int cached_object_nr, cached_object_alloc;
 
+static struct cached_object empty_tree = {
+	/* empty tree sha1: 4b825dc642cb6eb9a060e54bf8d69288fbee4904 */
+	"\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60"
+	"\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04",
+	OBJ_TREE,
+	"",
+	0
+};
+
 static struct cached_object *find_cached_object(const unsigned char *sha1)
 {
 	int i;
@@ -1854,6 +1863,8 @@ static struct cached_object *find_cached_object(const unsigned char *sha1)
 		if (!hashcmp(co->sha1, sha1))
 			return co;
 	}
+	if (!hashcmp(sha1, empty_tree.sha1))
+		return &empty_tree;
 	return NULL;
 }
 
-- 
1.5.4.1.1297.g3482b-dirty
Previous: Jeff KingNext: Johannes Schindelin
Message 6 of 13 in “[Bug] git add -i fails in multiple ways prior to first commit.”
  1. Rhodes, KateFeb 12, 2008
  2. Jeff KingFeb 13, 2008
  3. Jakub NarebskiFeb 13, 2008
  4. Jeff KingFeb 13, 2008
  5. add--interactive: handle initial commit betterJeff King, Feb 13, 2008
  6. Jeff KingFeb 13, 2008
  7. Johannes SchindelinFeb 13, 2008
  8. Jakub NarebskiFeb 13, 2008
  9. Jeff KingFeb 13, 2008
  10. Johannes SchindelinFeb 13, 2008
  11. Johannes SchindelinFeb 13, 2008
  12. Junio C HamanoFeb 13, 2008
  13. Jeff KingFeb 13, 2008

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.