[PATCH 1/2] fast-import: no longer compute the SHA1 data in store_object(). Instead, do it in the caller through the new sha1_object() and parse_object_data() functions.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is necessary if we want to send deflated data to store_object() without
keeping the original data in memory.

Signed-off-by: Sam Hocevar <sam@xxxxxxx>
---
 fast-import.c |   58 +++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/fast-import.c b/fast-import.c
index 3748ddf..6419d00 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1022,29 +1022,35 @@ static size_t encode_header(
 	return n;
 }
 
-static int store_object(
+static void sha1_object(
 	enum object_type type,
 	struct strbuf *dat,
-	struct last_object *last,
-	unsigned char *sha1out,
-	uintmax_t mark)
+	unsigned char *sha1out)
 {
-	void *out, *delta;
-	struct object_entry *e;
 	unsigned char hdr[96];
-	unsigned char sha1[20];
-	unsigned long hdrlen, deltalen;
+	unsigned long hdrlen;
 	git_SHA_CTX c;
-	z_stream s;
 
 	hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
 		(unsigned long)dat->len) + 1;
 	git_SHA1_Init(&c);
 	git_SHA1_Update(&c, hdr, hdrlen);
 	git_SHA1_Update(&c, dat->buf, dat->len);
-	git_SHA1_Final(sha1, &c);
-	if (sha1out)
-		hashcpy(sha1out, sha1);
+	git_SHA1_Final(sha1out, &c);
+}
+
+static int store_object(
+	enum object_type type,
+	struct strbuf *dat,
+	struct last_object *last,
+	unsigned char *sha1,
+	uintmax_t mark)
+{
+	void *out, *delta;
+	struct object_entry *e;
+	unsigned char hdr[96];
+	unsigned long hdrlen, deltalen;
+	z_stream s;
 
 	e = insert_object(sha1);
 	if (mark)
@@ -1336,6 +1342,7 @@ static void store_tree(struct tree_entry *root)
 	}
 
 	mktree(t, 1, &new_tree);
+	sha1_object(OBJ_TREE, &new_tree, root->versions[1].sha1);
 	store_object(OBJ_TREE, &new_tree, &lo, root->versions[1].sha1, 0);
 
 	t->delta_depth = lo.depth;
@@ -1702,7 +1709,12 @@ static void parse_mark(void)
 		next_mark = 0;
 }
 
-static void parse_data(struct strbuf *sb)
+/* This actually parses a "data" command, with the addition that if sha1out
+ * is not NULL, it will also compute the sha1 on the fly. */
+static void parse_object_data(
+	enum object_type type,
+	struct strbuf *sb,
+	unsigned char *sha1out)
 {
 	strbuf_reset(sb);
 
@@ -1724,6 +1736,9 @@ static void parse_data(struct strbuf *sb)
 			strbuf_addch(sb, '\n');
 		}
 		free(term);
+
+		if(sha1out)
+			sha1_object(type, sb, sha1out);
 	}
 	else {
 		size_t n = 0, length;
@@ -1737,11 +1752,19 @@ static void parse_data(struct strbuf *sb)
 					(unsigned long)(length - n));
 			n += s;
 		}
+
+		if(sha1out)
+			sha1_object(type, sb, sha1out);
 	}
 
 	skip_optional_lf();
 }
 
+static void parse_data(struct strbuf *sb)
+{
+	parse_object_data(OBJ_NONE, sb, NULL);
+}
+
 static int validate_raw_date(const char *src, char *result, int maxlen)
 {
 	const char *orig_src = src;
@@ -1805,12 +1828,13 @@ static char *parse_ident(const char *buf)
 
 static void parse_new_blob(void)
 {
+	unsigned char sha1[20];
 	static struct strbuf buf = STRBUF_INIT;
 
 	read_next_command();
 	parse_mark();
-	parse_data(&buf);
-	store_object(OBJ_BLOB, &buf, &last_blob, NULL, next_mark);
+	parse_object_data(OBJ_BLOB, &buf, sha1);
+	store_object(OBJ_BLOB, &buf, &last_blob, sha1, next_mark);
 }
 
 static void unload_one_branch(void)
@@ -1928,7 +1952,7 @@ static void file_change_m(struct branch *b)
 			p = uq.buf;
 		}
 		read_next_command();
-		parse_data(&buf);
+		parse_object_data(OBJ_BLOB, &buf, sha1);
 		store_object(OBJ_BLOB, &buf, &last_blob, sha1, 0);
 	} else if (oe) {
 		if (oe->type != OBJ_BLOB)
@@ -2211,6 +2235,7 @@ static void parse_new_commit(void)
 	free(author);
 	free(committer);
 
+	sha1_object(OBJ_COMMIT, &new_data, b->sha1);
 	if (!store_object(OBJ_COMMIT, &new_data, NULL, b->sha1, next_mark))
 		b->pack_id = pack_id;
 	b->last_commit = object_count_by_type[OBJ_COMMIT];
@@ -2291,6 +2316,7 @@ static void parse_new_tag(void)
 	strbuf_addbuf(&new_data, &msg);
 	free(tagger);
 
+	sha1_object(OBJ_TAG, &new_data, t->sha1);
 	if (store_object(OBJ_TAG, &new_data, NULL, t->sha1, 0))
 		t->pack_id = MAX_PACK_ID;
 	else
-- 
1.6.2
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux