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

Re: [PATCH 2/3] merge-recursive: make empty tree a known object

From
Junio C Hamano <junkio@cox.net>
Date
Dec 10, 2006, 22:28 UTC
Message-ID
<7vpsar2xom.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<7vpsar4fcu.fsf@assigned-by-dhcp.cox.net>
Junio C Hamano <junkio@cox.net> writes:
Show 9 quoted lines
> Linus Torvalds <torvalds@osdl.org> writes:
>
>> So you could make "read_sha1_file()" just have a special case for known 
>> objects at the end. If the pack entry fails, the loose file case fails, 
>> then rather than returning NULL at the end, you could have a list of known 
>> fixed objects..
>
> That is fine by me.  We would benefit from an empty blob and an
> empty tree.

I think this might be fragile in the presense of older implementation of git.

A new git may operate fine without having "well known" objects in the repository, and would happily write result that points at them; an older git would find the resulting repository corrupt.

We somehow need to force "well known" objects to be instantiated in the object database when something else points at them. I am not absolutely sure if keeping has_sha1_file() unaware of this magic is enough.

-- >8 -- Implement "well known" built-in objects.

Make read_sha1_file() aware of a handful "well known" objects and use built-in copy when they are not found in the object database.

Note that we do not hook this into has_sha1_file(), so it is possible that has_sha1_file() says you do not have an empty tree object in your repository but read_sha1_file() successfully gives you an empty tree object back.

Otherwise write_sha1_file() would not create "well known" objects in the repository, leaving the repository corrupt for older implementations of git.

---
diff --git a/cache.h b/cache.h
index f2ec5c8..7ee40e8 100644
--- a/cache.h
+++ b/cache.h
@@ -241,6 +241,10 @@ int adjust_shared_perm(const char *path);
 int safe_create_leading_directories(char *path);
 char *enter_repo(char *path, int strict);
 
+/* Well known object names */
+extern unsigned const char EMPTY_TREE_NAME[];
+extern unsigned const char EMPTY_BLOB_NAME[];
+
 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
 extern int sha1_object_info(const unsigned char *, char *, unsigned long *);
 extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size);
diff --git a/merge-recursive.c b/merge-recursive.c
index 32e186c..1c56fd3 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1234,11 +1234,8 @@ static int merge(struct commit *h1,
 	merged_common_ancestors = pop_commit(&ca);
 	if (merged_common_ancestors == NULL) {
 		/* if there is no common ancestor, make an empty tree */
-		struct tree *tree = xcalloc(1, sizeof(struct tree));
-
-		tree->object.parsed = 1;
-		tree->object.type = OBJ_TREE;
-		write_sha1_file(NULL, 0, tree_type, tree->object.sha1);
+		struct tree *tree = lookup_tree(EMPTY_TREE_NAME);
+		parse_tree(tree);
 		merged_common_ancestors = make_virtual_commit(tree, "ancestor");
 	}
 
diff --git a/sha1_file.c b/sha1_file.c
index 63f416b..c6efe88 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1315,11 +1315,32 @@ static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned lo
 	return unpack_entry(&e, type, size);
 }
 
-void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
+unsigned const char EMPTY_TREE_NAME[20] = {
+	0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9,	0xa0, 0x60,
+	0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04,
+};
+
+unsigned const char EMPTY_BLOB_NAME[20] = {
+	0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1, 0xd6, 0x43, 0x4b, 0x8b,
+	0x29, 0xae, 0x77, 0x5a, 0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91,
+};
+
+static struct well_known_objects {
+	char *type;
+	unsigned const char *sha1;
+	unsigned long size;
+	const char *data;
+} well_known_objects[] = {
+	{ "tree", EMPTY_TREE_NAME, 0UL, "" },
+	{ "blob", EMPTY_BLOB_NAME, 0UL, "" },
+};
+
+void *read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
 {
 	unsigned long mapsize;
 	void *map, *buf;
 	struct pack_entry e;
+	int i;
 
 	if (find_pack_entry(sha1, &e, NULL))
 		return read_packed_sha1(sha1, type, size);
@@ -1332,6 +1353,17 @@ void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size
 	reprepare_packed_git();
 	if (find_pack_entry(sha1, &e, NULL))
 		return read_packed_sha1(sha1, type, size);
+
+	/* "well known" objects */
+	for (i = 0; i < ARRAY_SIZE(well_known_objects); i++) {
+		if (!hashcmp(well_known_objects[i].sha1, sha1)) {
+			*size = well_known_objects[i].size;
+			buf = xmalloc(*size);
+			strcpy(type, well_known_objects[i].type);
+			memcpy(buf, well_known_objects[i].data, *size);
+			return buf;
+		}
+	}
 	return NULL;
 }
 
Previous: Linus TorvaldsNext: Johannes Schindelin
Message 11 of 37 in “Make sure the empty tree exists when needed in merge-recursive.”
  1. 1/1 Make sure the empty tree exists when needed in merge-recursive.Shawn O. Pearce, Dec 7, 2006
  2. 1/3 diff_tree_sha1(): avoid rereading trees if possibleJohannes Schindelin, Dec 9, 2006
  3. Junio C HamanoDec 10, 2006
  4. Johannes SchindelinDec 10, 2006
  5. 2/3 merge-recursive: make empty tree a known objectJohannes Schindelin, Dec 9, 2006
  6. Linus TorvaldsDec 10, 2006
  7. Junio C HamanoDec 10, 2006
  8. Linus TorvaldsDec 10, 2006
  9. Junio C HamanoDec 10, 2006
  10. Linus TorvaldsDec 10, 2006
  11. Junio C HamanoDec 10, 2006
  12. Johannes SchindelinDec 10, 2006
  13. 3/3 add test case for recursive mergeJohannes Schindelin, Dec 9, 2006
  14. Johannes SchindelinDec 10, 2006
  15. Junio C HamanoDec 10, 2006
  16. Johannes SchindelinDec 10, 2006
  17. t6024: fix timing problemJohannes Schindelin, Dec 12, 2006
  18. Junio C HamanoDec 12, 2006
  19. Johannes SchindelinDec 12, 2006
  20. merge-recursive: add/add really is modify/modify with an empty baseJohannes Schindelin, Dec 13, 2006
  21. Junio C HamanoDec 13, 2006
  22. StGit repo & gitweb, was Re: [PATCH] merge-recursive: add/add really is modify/modify with an empty baseJohannes Schindelin, Dec 13, 2006
  23. Jakub NarebskiDec 13, 2006
  24. Catalin MarinasDec 13, 2006
  25. Robin RosenbergDec 13, 2006
  26. Johannes SchindelinDec 13, 2006
  27. Jakub NarebskiDec 13, 2006
  28. Petr BaudisDec 19, 2006
  29. Jakub NarebskiDec 19, 2006
  30. Catalin MarinasDec 13, 2006
  31. Junio C HamanoDec 13, 2006
  32. Johannes SchindelinDec 13, 2006
  33. Catalin MarinasDec 14, 2006
  34. Shawn PearceDec 14, 2006
  35. Shawn PearceDec 14, 2006
  36. Johannes SchindelinDec 14, 2006
  37. Catalin MarinasDec 14, 2006

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.