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

[PATCH 8/8] dir.c: dump "UNTR" extension as json

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jun 19, 2019, 09:58 UTC
Message-ID
<20190619095858.30124-9-pclouds@gmail.com>
In-Reply-To
<20190619095858.30124-1-pclouds@gmail.com>

The big part of UNTR extension is dumped at the end instead of dumping as soon as we read it, because we actually "patch" some fields in untracked_cache_dir with EWAH bitmaps at the end.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 dir.c         | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 dir.h         |  4 +++-
 json-writer.h |  6 ++++++
 read-cache.c  |  2 +-
 4 files changed, 65 insertions(+), 3 deletions(-)
diff --git a/dir.c b/dir.c
index ba4a51c296..f389eee24a 100644
--- a/dir.c
+++ b/dir.c
@@ -19,6 +19,7 @@
 #include "varint.h"
 #include "ewah/ewok.h"
 #include "fsmonitor.h"
+#include "json-writer.h"
 #include "submodule-config.h"
 
 /*
@@ -2826,7 +2827,42 @@ static void load_oid_stat(struct oid_stat *oid_stat, const unsigned char *data,
 	oid_stat->valid = 1;
 }
 
-struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz)
+static void jw_object_oid_stat(struct json_writer *jw, const char *key,
+			       const struct oid_stat *oid_stat)
+{
+	jw_object_inline_begin_object(jw, key);
+	jw_object_bool(jw, "valid", oid_stat->valid);
+	jw_object_string(jw, "oid", oid_to_hex(&oid_stat->oid));
+	jw_object_stat_data(jw, "stat", &oid_stat->stat);
+	jw_end(jw);
+}
+
+static void jw_object_untracked_cache_dir(struct json_writer *jw,
+					  const struct untracked_cache_dir *ucd)
+{
+	int i;
+
+	jw_object_bool(jw, "valid", ucd->valid);
+	jw_object_bool(jw, "check-only", ucd->check_only);
+	jw_object_stat_data(jw, "stat", &ucd->stat_data);
+	jw_object_string(jw, "exclude-oid", oid_to_hex(&ucd->exclude_oid));
+	jw_object_inline_begin_array(jw, "untracked");
+	for (i = 0; i < ucd->untracked_nr; i++)
+		jw_array_string(jw, ucd->untracked[i]);
+	jw_end(jw);
+
+	jw_object_inline_begin_object(jw, "dirs");
+	for (i = 0; i < ucd->dirs_nr; i++) {
+		jw_object_inline_begin_object(jw, ucd->dirs[i]->name);
+		jw_object_untracked_cache_dir(jw, ucd->dirs[i]);
+		jw_end(jw);
+	}
+	jw_end(jw);
+}
+
+struct untracked_cache *read_untracked_extension(const void *data,
+						 unsigned long sz,
+						 struct json_writer *jw)
 {
 	struct untracked_cache *uc;
 	struct read_data rd;
@@ -2864,6 +2900,17 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
 	uc->dir_flags = get_be32(next + ouc_offset(dir_flags));
 	exclude_per_dir = (const char *)next + exclude_per_dir_offset;
 	uc->exclude_per_dir = xstrdup(exclude_per_dir);
+
+	if (jw) {
+		jw_object_inline_begin_object(jw, "untracked-cache");
+		jw_object_intmax(jw, "ext-size", sz);
+		jw_object_string(jw, "ident", ident);
+		jw_object_oid_stat(jw, "info/exclude", &uc->ss_info_exclude);
+		jw_object_oid_stat(jw, "excludes-file", &uc->ss_excludes_file);
+		jw_object_intmax(jw, "flags", uc->dir_flags);
+		jw_object_string(jw, "excludes-per-dir", uc->exclude_per_dir);
+	}
+
 	/* NUL after exclude_per_dir is covered by sizeof(*ouc) */
 	next += exclude_per_dir_offset + strlen(exclude_per_dir) + 1;
 	if (next >= end)
@@ -2905,6 +2952,12 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
 	ewah_each_bit(rd.sha1_valid, read_oid, &rd);
 	next = rd.data;
 
+	if (jw) {
+		jw_object_inline_begin_object(jw, "root");
+		jw_object_untracked_cache_dir(jw, uc->root);
+		jw_end(jw);
+	}
+
 done:
 	free(rd.ucd);
 	ewah_free(rd.valid);
@@ -2915,6 +2968,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
 		free_untracked_cache(uc);
 		uc = NULL;
 	}
+	jw_end_gently(jw);
 	return uc;
 }
 
diff --git a/dir.h b/dir.h
index 680079bbe3..80efdd05c4 100644
--- a/dir.h
+++ b/dir.h
@@ -6,6 +6,8 @@
 #include "cache.h"
 #include "strbuf.h"
 
+struct json_writer;
+
 struct dir_entry {
 	unsigned int len;
 	char name[FLEX_ARRAY]; /* more */
@@ -362,7 +364,7 @@ void untracked_cache_remove_from_index(struct index_state *, const char *);
 void untracked_cache_add_to_index(struct index_state *, const char *);
 
 void free_untracked_cache(struct untracked_cache *);
-struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz);
+struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz, struct json_writer *jw);
 void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);
 void add_untracked_cache(struct index_state *istate);
 void remove_untracked_cache(struct index_state *istate);
diff --git a/json-writer.h b/json-writer.h
index 3c173647d3..f778e019a2 100644
--- a/json-writer.h
+++ b/json-writer.h
@@ -121,6 +121,12 @@ static inline void jw_object_inline_begin_array_gently(struct json_writer *jw,
 		jw_object_inline_begin_array(jw, name);
 }
 
+static inline void jw_array_inline_begin_object_gently(struct json_writer *jw)
+{
+	if (jw)
+		jw_array_inline_begin_object(jw);
+}
+
 static inline void jw_end_gently(struct json_writer *jw)
 {
 	if (jw)
diff --git a/read-cache.c b/read-cache.c
index 289705b816..d7d9ce7260 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -1708,7 +1708,7 @@ static int read_index_extension(struct index_state *istate,
 			return -1;
 		break;
 	case CACHE_EXT_UNTRACKED:
-		istate->untracked = read_untracked_extension(data, sz);
+		istate->untracked = read_untracked_extension(data, sz, istate->jw);
 		break;
 	case CACHE_EXT_FSMONITOR:
 		read_fsmonitor_extension(istate, data, sz);
-- 
2.22.0.rc0.322.g2b0371e29a
Previous: Nguyễn Thái Ngọc DuyNext: Derrick Stolee
Message 17 of 36 in “Add 'ls-files --json' to dump the index in json”
  1. 0/8 Add 'ls-files --json' to dump the index in jsonNguyễn Thái Ngọc Duy, Jun 19, 2019
  2. 1/8 ls-files: add --json to dump the indexNguyễn Thái Ngọc Duy, Jun 19, 2019
  3. Ævar Arnfjörð BjarmasonJun 19, 2019
  4. Derrick StoleeJun 19, 2019
  5. Johannes SchindelinJun 21, 2019
  6. Duy NguyenJun 24, 2019
  7. 2/8 split-index.c: dump "link" extension as jsonNguyễn Thái Ngọc Duy, Jun 19, 2019
  8. 3/8 fsmonitor.c: dump "FSMN" extension as jsonNguyễn Thái Ngọc Duy, Jun 19, 2019
  9. 4/8 resolve-undo.c: dump "REUC" extension as jsonNguyễn Thái Ngọc Duy, Jun 19, 2019
  10. Derrick StoleeJun 19, 2019
  11. 5/8 read-cache.c: dump "EOIE" extension as jsonNguyễn Thái Ngọc Duy, Jun 19, 2019
  12. 6/8 read-cache.c: dump "IEOT" extension as jsonNguyễn Thái Ngọc Duy, Jun 19, 2019
  13. Derrick StoleeJun 19, 2019
  14. Duy NguyenJun 19, 2019
  15. Derrick StoleeJun 19, 2019
  16. 7/8 cache-tree.c: dump "TREE" extension as jsonNguyễn Thái Ngọc Duy, Jun 19, 2019
  17. 8/8 dir.c: dump "UNTR" extension as jsonNguyễn Thái Ngọc Duy, Jun 19, 2019
  18. Derrick StoleeJun 19, 2019
  19. Duy NguyenJun 19, 2019
  20. Derrick StoleeJun 19, 2019
  21. Jeff KingJun 19, 2019
  22. Duy NguyenJun 21, 2019
  23. Jeff KingJun 21, 2019
  24. Johannes SchindelinJun 21, 2019
  25. Duy NguyenJun 21, 2019
  26. Junio C HamanoJun 21, 2019
  27. Jeff KingJun 21, 2019
  28. Johannes SchindelinJun 24, 2019
  29. Johannes SchindelinJun 24, 2019
  30. Duy NguyenJun 24, 2019
  31. Jeff KingJun 21, 2019
  32. Johannes SchindelinJun 24, 2019
  33. Junio C HamanoJun 20, 2019
  34. Jeff HostetlerJun 20, 2019
  35. brian m. carlsonJun 21, 2019
  36. Duy NguyenJun 22, 2019

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.