git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <junkio@cox.net>
To: Linus Torvalds <torvalds@osdl.org>
Cc: git@vger.kernel.org, "Shawn O. Pearce" <spearce@spearce.org>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>
Subject: Re: [PATCH 2/3] merge-recursive: make empty tree a known object
Date: Sun, 10 Dec 2006 14:28:09 -0800	[thread overview]
Message-ID: <7vpsar2xom.fsf@assigned-by-dhcp.cox.net> (raw)
In-Reply-To: <7vpsar4fcu.fsf@assigned-by-dhcp.cox.net> (Junio C. Hamano's message of "Sun, 10 Dec 2006 13:21:05 -0800")

Junio C Hamano <junkio@cox.net> writes:

> 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;
 }
 

  parent reply	other threads:[~2006-12-10 22:28 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-07 10:17 [PATCH 1/1] Make sure the empty tree exists when needed in merge-recursive Shawn O. Pearce
2006-12-09 23:55 ` [PATCH 1/3] diff_tree_sha1(): avoid rereading trees if possible Johannes Schindelin
2006-12-10  1:47   ` Junio C Hamano
2006-12-10 22:49     ` Johannes Schindelin
2006-12-09 23:56 ` [PATCH 2/3] merge-recursive: make empty tree a known object Johannes Schindelin
2006-12-10 18:37   ` Linus Torvalds
2006-12-10 21:21     ` Junio C Hamano
2006-12-10 21:31       ` Linus Torvalds
2006-12-10 22:33         ` Junio C Hamano
2006-12-10 22:54           ` Linus Torvalds
2006-12-10 22:28       ` Junio C Hamano [this message]
2006-12-10 23:16         ` Johannes Schindelin
2006-12-09 23:56 ` [PATCH 3/3] add test case for recursive merge Johannes Schindelin
2006-12-10  0:18   ` Johannes Schindelin
2006-12-10  3:10     ` Junio C Hamano
2006-12-10 22:51       ` Johannes Schindelin
2006-12-12 22:49       ` [PATCH] t6024: fix timing problem Johannes Schindelin
2006-12-12 23:23         ` Junio C Hamano
2006-12-12 23:59           ` Johannes Schindelin
2006-12-13  3:05           ` [PATCH] merge-recursive: add/add really is modify/modify with an empty base Johannes Schindelin
2006-12-13  6:33             ` Junio C Hamano
2006-12-13 11:46               ` StGit repo & gitweb, was " Johannes Schindelin
2006-12-13 11:56                 ` Jakub Narebski
2006-12-13 22:09                 ` Catalin Marinas
2006-12-13 23:06                   ` Robin Rosenberg
2006-12-13 23:50                   ` Johannes Schindelin
2006-12-13 23:57                     ` Jakub Narebski
2006-12-19 18:50                 ` Petr Baudis
2006-12-19 19:39                   ` Jakub Narebski
2006-12-13 22:01               ` Catalin Marinas
2006-12-13 22:26                 ` Junio C Hamano
2006-12-13 23:48                 ` Johannes Schindelin
2006-12-14 11:31                   ` Catalin Marinas
2006-12-14 11:41                     ` Shawn Pearce
2006-12-14 12:00                     ` Shawn Pearce
2006-12-14 13:44                     ` Johannes Schindelin
2006-12-14 14:15                       ` Catalin Marinas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7vpsar2xom.fsf@assigned-by-dhcp.cox.net \
    --to=junkio@cox.net \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=spearce@spearce.org \
    --cc=torvalds@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).