git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: Nicolas Pitre <nico@fluxnic.net>
Cc: git@vger.kernel.org, "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 13/17] index-pack: encode appended trees using v4 format in pack v4
Date: Sat, 21 Sep 2013 20:57:59 +0700	[thread overview]
Message-ID: <1379771883-10278-14-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1379771883-10278-1-git-send-email-pclouds@gmail.com>


Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/index-pack.c | 43 ++++++++++++++++++++++++++++++++++++++++---
 packv4-create.c      | 20 ++++++++++++++++++++
 packv4-create.h      |  2 ++
 3 files changed, 62 insertions(+), 3 deletions(-)

diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index db885b1..caec388 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -12,6 +12,7 @@
 #include "streaming.h"
 #include "thread-utils.h"
 #include "packv4-parse.h"
+#include "packv4-create.h"
 #include "varint.h"
 #include "tree-walk.h"
 
@@ -76,6 +77,7 @@ static struct delta_entry *deltas;
 static struct thread_local nothread_data;
 static unsigned char *sha1_table;
 static struct packv4_dict *name_dict, *path_dict;
+static struct packv4_tables pv4_tables;
 static int nr_objects;
 static int nr_deltas;
 static int nr_resolved_deltas;
@@ -1717,18 +1719,48 @@ static int write_compressed(struct sha1file *f, void *in, unsigned int size)
 	return size;
 }
 
+static void initialize_packv4_tables(void)
+{
+	static int initialized;
+	int i, nr = nr_objects_final;
+	if (initialized)
+		return;
+	pv4_tables.commit_ident_table = pv4_dict_to_dict_table(name_dict);
+	pv4_tables.tree_path_table = pv4_dict_to_dict_table(path_dict);
+	pv4_tables.all_objs_nr = nr;
+	pv4_tables.all_objs = xmalloc(nr * sizeof(struct pack_idx_entry));
+	/* for pv4_encode_tree() pv4_tables[].offset is not needed */
+	for (i = 0; i < nr; i++)
+		hashcpy(pv4_tables.all_objs[i].sha1, sha1_table + i * 20);
+	initialized = 1;
+}
+
 static struct object_entry *append_obj_to_pack(struct sha1file *f,
 			       const unsigned char *sha1, void *buf,
 			       unsigned long size, enum object_type type)
 {
 	struct object_entry *obj = &objects[nr_objects++];
+	void *v4_data = NULL;
 	unsigned char header[10];
+	unsigned long v4_size;
+	enum object_type real_type = type;
 	int n;
 
 	if (packv4) {
 		if (nr_objects > nr_objects_final)
 			die(_("too many objects"));
-		/* TODO: convert OBJ_TREE to OBJ_PV4_TREE using pv4_encode_tree */
+
+		if (type == OBJ_TREE) {
+			initialize_packv4_tables();
+			v4_size = size;
+			v4_data = pv4_encode_tree(&pv4_tables, buf, &v4_size,
+						  NULL, 0, NULL);
+			if (v4_data)
+				type = OBJ_PV4_TREE;
+		}
+
+		/* TODO: convert OBJ_COMMIT to OBJ_PV4_COMMIT using pv4_encode_commit */
+
 		n = pv4_encode_object_header(type, size, header);
 	} else
 		n = encode_in_pack_object_header(type, size, header);
@@ -1737,12 +1769,17 @@ static struct object_entry *append_obj_to_pack(struct sha1file *f,
 	obj[0].size = size;
 	obj[0].hdr_size = n;
 	obj[0].type = type;
-	obj[0].real_type = type;
+	obj[0].real_type = real_type;
 	obj[1].idx.offset = obj[0].idx.offset + n;
-	obj[1].idx.offset += write_compressed(f, buf, size);
+	if (type != real_type) { /* must be v4 representation */
+		sha1write(f, v4_data, v4_size);
+		obj[1].idx.offset += v4_size;
+	} else
+		obj[1].idx.offset += write_compressed(f, buf, size);
 	obj[0].idx.crc32 = crc32_end(f);
 	sha1flush(f);
 	hashcpy(obj->idx.sha1, sha1);
+	free(v4_data);
 	return obj;
 }
 
diff --git a/packv4-create.c b/packv4-create.c
index 3acd10f..14be867 100644
--- a/packv4-create.c
+++ b/packv4-create.c
@@ -15,6 +15,7 @@
 #include "pack-revindex.h"
 #include "progress.h"
 #include "varint.h"
+#include "packv4-parse.h"
 #include "packv4-create.h"
 
 
@@ -144,6 +145,25 @@ void sort_dict_entries_by_hits(struct dict_table *t)
 	rehash_entries(t);
 }
 
+struct dict_table *pv4_dict_to_dict_table(struct packv4_dict *pv4dict)
+{
+	struct dict_table *dict;
+	int i;
+
+	dict = create_dict_table();
+	for (i = 0; i < pv4dict->nb_entries; i++) {
+		const unsigned char *mode_bytes;
+		const char *str;
+		int mode, str_len;
+		mode_bytes = pv4dict->data + pv4dict->offsets[i];
+		mode = (mode_bytes[0] << 8) | mode_bytes[1];
+		str = (const char *)mode_bytes + 2;
+		str_len = pv4dict->offsets[i+1] - pv4dict->offsets[i] - 2;
+		str_len--;  /* for NUL, dict_add_entry will add one back */
+		dict_add_entry(dict, mode, str, str_len);
+	}
+	return dict;
+}
 /*
  * Parse the author/committer line from a canonical commit object.
  * The 'from' argument points right after the "author " or "committer "
diff --git a/packv4-create.h b/packv4-create.h
index 4ac4d71..e0d4b1f 100644
--- a/packv4-create.h
+++ b/packv4-create.h
@@ -8,11 +8,13 @@ struct packv4_tables {
 	struct dict_table *tree_path_table;
 };
 
+struct packv4_dict;
 struct dict_table;
 struct sha1file;
 
 struct dict_table *create_dict_table(void);
 int dict_add_entry(struct dict_table *t, int val, const char *str, int str_len);
+struct dict_table *pv4_dict_to_dict_table(struct packv4_dict *dict);
 void destroy_dict_table(struct dict_table *t);
 void dict_dump(struct packv4_tables *v4);
 
-- 
1.8.2.83.gc99314b

  parent reply	other threads:[~2013-09-21 13:56 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-21 13:57 [PATCH 00/17] np/pack-v4 updates Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` [PATCH 01/17] fixup! index-pack: record all delta bases in v4 (tree and ref-delta) Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` [PATCH 02/17] fixup! packv4-parse.c: add tree offset caching Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` [PATCH 03/17] fixup! pack-objects: support writing pack v4 Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` [PATCH 04/17] fixup! pack-objects: recognize v4 as pack source Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` [PATCH 05/17] fixup! index-pack: support completing thin packs v4 Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` [PATCH 06/17] fixup! pack v4: move packv4-create.c to libgit.a Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` [PATCH 07/17] fixup! index-pack, pack-objects: allow creating .idx v2 with .pack v4 Nguyễn Thái Ngọc Duy
2013-09-22  1:39   ` Nicolas Pitre
2013-09-21 13:57 ` [PATCH 08/17] fixup! pack v4: code to obtain a SHA1 from a sha1ref Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` [PATCH 09/17] fixup! pack-objects: add --version to specify written pack version Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` [PATCH 10/17] test-lib.sh: add --packv4 for running the tests with pack v4 as default Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` [PATCH 11/17] packv4-parse: accept ref-delta as base of pv4-tree Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` [PATCH 12/17] pack-objects: do not add type OBJ_NONE to objects[] in pack v4 Nguyễn Thái Ngọc Duy
2013-09-21 13:57 ` Nguyễn Thái Ngọc Duy [this message]
2013-09-21 13:58 ` [PATCH 14/17] t5302: disable sealth corruption tests when run with --packv4 Nguyễn Thái Ngọc Duy
2013-09-22  2:13   ` Eric Sunshine
2013-09-21 13:58 ` [PATCH 15/17] t5300: avoid testing ofs-delta " Nguyễn Thái Ngọc Duy
2013-09-21 16:46   ` Nicolas Pitre
2013-09-22  1:48     ` Duy Nguyen
2013-09-22  2:13       ` Nicolas Pitre
2013-09-21 13:58 ` [PATCH 16/17] pack-objects: disable pack size limit feature on pack v4 Nguyễn Thái Ngọc Duy
2013-09-21 13:58 ` [PATCH 17/17] t5303: adapt the tests to run with --packv4 Nguyễn Thái Ngọc Duy
2013-09-21 16:07 ` [PATCH 00/17] np/pack-v4 updates Nicolas Pitre
2013-10-15 21:45   ` Junio C Hamano

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=1379771883-10278-14-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=nico@fluxnic.net \
    /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).