git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 7/8] cache-tree.c: dump "TREE" extension as json
Date: Wed, 19 Jun 2019 16:58:57 +0700	[thread overview]
Message-ID: <20190619095858.30124-8-pclouds@gmail.com> (raw)
In-Reply-To: <20190619095858.30124-1-pclouds@gmail.com>

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 cache-tree.c | 41 ++++++++++++++++++++++++++++++++++++-----
 cache-tree.h |  5 ++++-
 read-cache.c |  2 +-
 3 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/cache-tree.c b/cache-tree.c
index b13bfaf71e..fc44016fe8 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -3,6 +3,7 @@
 #include "tree.h"
 #include "tree-walk.h"
 #include "cache-tree.h"
+#include "json-writer.h"
 #include "object-store.h"
 #include "replace-object.h"
 
@@ -492,7 +493,8 @@ void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
 	write_one(sb, root, "", 0);
 }
 
-static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
+static struct cache_tree *read_one(const char **buffer, unsigned long *size_p,
+				   struct json_writer *jw)
 {
 	const char *buf = *buffer;
 	unsigned long size = *size_p;
@@ -546,6 +548,15 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
 			*buffer, subtree_nr);
 #endif
 
+	if (jw) {
+		if (it->entry_count >= 0) {
+			jw_object_string(jw, "oid", oid_to_hex(&it->oid));
+			jw_object_intmax(jw, "entry_count", it->entry_count);
+		} else {
+			jw_object_null(jw, "oid");
+		}
+		jw_object_inline_begin_array(jw, "subdirs");
+	}
 	/*
 	 * Just a heuristic -- we do not add directories that often but
 	 * we do not want to have to extend it immediately when we do,
@@ -559,12 +570,18 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
 		struct cache_tree_sub *subtree;
 		const char *name = buf;
 
-		sub = read_one(&buf, &size);
+		if (jw) {
+			jw_array_inline_begin_object(jw);
+			jw_object_string(jw, "name", name);
+		}
+		sub = read_one(&buf, &size, jw);
+		jw_end_gently(jw);
 		if (!sub)
 			goto free_return;
 		subtree = cache_tree_sub(it, name);
 		subtree->cache_tree = sub;
 	}
+	jw_end_gently(jw);
 	if (subtree_nr != it->subtree_nr)
 		die("cache-tree: internal error");
 	*buffer = buf;
@@ -576,11 +593,25 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
 	return NULL;
 }
 
-struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
+struct cache_tree *cache_tree_read(const char *buffer, unsigned long size,
+				   struct json_writer *jw)
 {
+	struct cache_tree *ret;
+
+	if (jw) {
+		jw_object_inline_begin_object(jw, "cache-tree");
+		jw_object_intmax(jw, "ext-size", size);
+		jw_object_inline_begin_object(jw, "root");
+	}
 	if (buffer[0])
-		return NULL; /* not the whole tree */
-	return read_one(&buffer, &size);
+		ret = NULL; /* not the whole tree */
+	else
+		ret = read_one(&buffer, &size, jw);
+	if (jw) {
+		jw_end(jw);	/* root */
+		jw_end(jw);	/* cache-tree */
+	}
+	return ret;
 }
 
 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
diff --git a/cache-tree.h b/cache-tree.h
index 757bbc48bc..fc3c73284b 100644
--- a/cache-tree.h
+++ b/cache-tree.h
@@ -6,6 +6,8 @@
 #include "tree-walk.h"
 
 struct cache_tree;
+struct json_writer;
+
 struct cache_tree_sub {
 	struct cache_tree *cache_tree;
 	int count;		/* internally used by update_one() */
@@ -28,7 +30,8 @@ void cache_tree_invalidate_path(struct index_state *, const char *);
 struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *);
 
 void cache_tree_write(struct strbuf *, struct cache_tree *root);
-struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
+struct cache_tree *cache_tree_read(const char *buffer, unsigned long size,
+				   struct json_writer *jw);
 
 int cache_tree_fully_valid(struct cache_tree *);
 int cache_tree_update(struct index_state *, int);
diff --git a/read-cache.c b/read-cache.c
index 200834e77e..289705b816 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -1698,7 +1698,7 @@ static int read_index_extension(struct index_state *istate,
 {
 	switch (CACHE_EXT(ext)) {
 	case CACHE_EXT_TREE:
-		istate->cache_tree = cache_tree_read(data, sz);
+		istate->cache_tree = cache_tree_read(data, sz, istate->jw);
 		break;
 	case CACHE_EXT_RESOLVE_UNDO:
 		istate->resolve_undo = resolve_undo_read(data, sz, istate->jw);
-- 
2.22.0.rc0.322.g2b0371e29a


  parent reply	other threads:[~2019-06-19  9:59 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-19  9:58 [PATCH 0/8] Add 'ls-files --json' to dump the index in json Nguyễn Thái Ngọc Duy
2019-06-19  9:58 ` [PATCH 1/8] ls-files: add --json to dump the index Nguyễn Thái Ngọc Duy
2019-06-19 10:30   ` Ævar Arnfjörð Bjarmason
2019-06-19 13:03   ` Derrick Stolee
2019-06-21 13:04     ` Johannes Schindelin
2019-06-24 12:50     ` Duy Nguyen
2019-06-19  9:58 ` [PATCH 2/8] split-index.c: dump "link" extension as json Nguyễn Thái Ngọc Duy
2019-06-19  9:58 ` [PATCH 3/8] fsmonitor.c: dump "FSMN" " Nguyễn Thái Ngọc Duy
2019-06-19  9:58 ` [PATCH 4/8] resolve-undo.c: dump "REUC" " Nguyễn Thái Ngọc Duy
2019-06-19 13:16   ` Derrick Stolee
2019-06-19  9:58 ` [PATCH 5/8] read-cache.c: dump "EOIE" " Nguyễn Thái Ngọc Duy
2019-06-19  9:58 ` [PATCH 6/8] read-cache.c: dump "IEOT" " Nguyễn Thái Ngọc Duy
2019-06-19 13:18   ` Derrick Stolee
2019-06-19 13:24     ` Duy Nguyen
2019-06-19 14:26       ` Derrick Stolee
2019-06-19  9:58 ` Nguyễn Thái Ngọc Duy [this message]
2019-06-19  9:58 ` [PATCH 8/8] dir.c: dump "UNTR" " Nguyễn Thái Ngọc Duy
2019-06-19 11:58 ` [PATCH 0/8] Add 'ls-files --json' to dump the index in json Derrick Stolee
2019-06-19 12:42   ` Duy Nguyen
2019-06-19 12:48     ` Derrick Stolee
2019-06-19 19:17 ` Jeff King
2019-06-21  8:37   ` Duy Nguyen
2019-06-21 20:48     ` Jeff King
2019-06-21 13:16   ` Johannes Schindelin
2019-06-21 13:49     ` Duy Nguyen
2019-06-21 15:10       ` Junio C Hamano
2019-06-21 20:52         ` Jeff King
2019-06-24  9:35           ` Johannes Schindelin
2019-06-24  9:33       ` Johannes Schindelin
2019-06-24  9:35         ` Duy Nguyen
2019-06-21 20:51     ` Jeff King
2019-06-24  9:52       ` Johannes Schindelin
2019-06-20  4:00 ` Junio C Hamano
2019-06-20 19:12 ` Jeff Hostetler
2019-06-21 23:30 ` brian m. carlson
2019-06-22  2:54   ` Duy Nguyen

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20190619095858.30124-8-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).