Skip to content

Commit c094f3f

Browse files
pcloudsgitster
authored andcommitted
dir.c: dump "UNTR" extension as json
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5e5cfc0 commit c094f3f

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

dir.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "varint.h"
2020
#include "ewah/ewok.h"
2121
#include "fsmonitor.h"
22+
#include "json-writer.h"
2223
#include "submodule-config.h"
2324

2425
/*
@@ -2826,7 +2827,42 @@ static void load_oid_stat(struct oid_stat *oid_stat, const unsigned char *data,
28262827
oid_stat->valid = 1;
28272828
}
28282829

2829-
struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz)
2830+
static void jw_object_oid_stat(struct json_writer *jw, const char *key,
2831+
const struct oid_stat *oid_stat)
2832+
{
2833+
jw_object_inline_begin_object(jw, key);
2834+
jw_object_bool(jw, "valid", oid_stat->valid);
2835+
jw_object_string(jw, "oid", oid_to_hex(&oid_stat->oid));
2836+
jw_object_stat_data(jw, "stat", &oid_stat->stat);
2837+
jw_end(jw);
2838+
}
2839+
2840+
static void jw_object_untracked_cache_dir(struct json_writer *jw,
2841+
const struct untracked_cache_dir *ucd)
2842+
{
2843+
int i;
2844+
2845+
jw_object_bool(jw, "valid", ucd->valid);
2846+
jw_object_bool(jw, "check-only", ucd->check_only);
2847+
jw_object_stat_data(jw, "stat", &ucd->stat_data);
2848+
jw_object_string(jw, "exclude-oid", oid_to_hex(&ucd->exclude_oid));
2849+
jw_object_inline_begin_array(jw, "untracked");
2850+
for (i = 0; i < ucd->untracked_nr; i++)
2851+
jw_array_string(jw, ucd->untracked[i]);
2852+
jw_end(jw);
2853+
2854+
jw_object_inline_begin_object(jw, "dirs");
2855+
for (i = 0; i < ucd->dirs_nr; i++) {
2856+
jw_object_inline_begin_object(jw, ucd->dirs[i]->name);
2857+
jw_object_untracked_cache_dir(jw, ucd->dirs[i]);
2858+
jw_end(jw);
2859+
}
2860+
jw_end(jw);
2861+
}
2862+
2863+
struct untracked_cache *read_untracked_extension(const void *data,
2864+
unsigned long sz,
2865+
struct json_writer *jw)
28302866
{
28312867
struct untracked_cache *uc;
28322868
struct read_data rd;
@@ -2864,6 +2900,17 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
28642900
uc->dir_flags = get_be32(next + ouc_offset(dir_flags));
28652901
exclude_per_dir = (const char *)next + exclude_per_dir_offset;
28662902
uc->exclude_per_dir = xstrdup(exclude_per_dir);
2903+
2904+
if (jw) {
2905+
jw_object_inline_begin_object(jw, "untracked-cache");
2906+
jw_object_intmax(jw, "ext-size", sz);
2907+
jw_object_string(jw, "ident", ident);
2908+
jw_object_oid_stat(jw, "info/exclude", &uc->ss_info_exclude);
2909+
jw_object_oid_stat(jw, "excludes-file", &uc->ss_excludes_file);
2910+
jw_object_intmax(jw, "flags", uc->dir_flags);
2911+
jw_object_string(jw, "excludes-per-dir", uc->exclude_per_dir);
2912+
}
2913+
28672914
/* NUL after exclude_per_dir is covered by sizeof(*ouc) */
28682915
next += exclude_per_dir_offset + strlen(exclude_per_dir) + 1;
28692916
if (next >= end)
@@ -2905,6 +2952,12 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
29052952
ewah_each_bit(rd.sha1_valid, read_oid, &rd);
29062953
next = rd.data;
29072954

2955+
if (jw) {
2956+
jw_object_inline_begin_object(jw, "root");
2957+
jw_object_untracked_cache_dir(jw, uc->root);
2958+
jw_end(jw);
2959+
}
2960+
29082961
done:
29092962
free(rd.ucd);
29102963
ewah_free(rd.valid);
@@ -2915,6 +2968,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
29152968
free_untracked_cache(uc);
29162969
uc = NULL;
29172970
}
2971+
jw_end_gently(jw);
29182972
return uc;
29192973
}
29202974

dir.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "cache.h"
77
#include "strbuf.h"
88

9+
struct json_writer;
10+
911
struct dir_entry {
1012
unsigned int len;
1113
char name[FLEX_ARRAY]; /* more */
@@ -362,7 +364,7 @@ void untracked_cache_remove_from_index(struct index_state *, const char *);
362364
void untracked_cache_add_to_index(struct index_state *, const char *);
363365

364366
void free_untracked_cache(struct untracked_cache *);
365-
struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz);
367+
struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz, struct json_writer *jw);
366368
void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);
367369
void add_untracked_cache(struct index_state *istate);
368370
void remove_untracked_cache(struct index_state *istate);

json-writer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ static inline void jw_object_inline_begin_array_gently(struct json_writer *jw,
121121
jw_object_inline_begin_array(jw, name);
122122
}
123123

124+
static inline void jw_array_inline_begin_object_gently(struct json_writer *jw)
125+
{
126+
if (jw)
127+
jw_array_inline_begin_object(jw);
128+
}
129+
124130
static inline void jw_end_gently(struct json_writer *jw)
125131
{
126132
if (jw)

read-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ static int read_index_extension(struct index_state *istate,
17081708
return -1;
17091709
break;
17101710
case CACHE_EXT_UNTRACKED:
1711-
istate->untracked = read_untracked_extension(data, sz);
1711+
istate->untracked = read_untracked_extension(data, sz, istate->jw);
17121712
break;
17131713
case CACHE_EXT_FSMONITOR:
17141714
read_fsmonitor_extension(istate, data, sz);

0 commit comments

Comments
 (0)