From abe5080a5dd14049ea49b90e06a4ce9759fc0d3a Mon Sep 17 00:00:00 2001 From: Jeff Hostetler Date: Fri, 24 May 2024 12:06:59 -0400 Subject: [PATCH] survey: checkpoint add blob pretty print Signed-off-by: Jeff Hostetler --- builtin/survey.c | 97 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 14 deletions(-) diff --git a/builtin/survey.c b/builtin/survey.c index 7bf13c04cea990..e72f6089099c40 100644 --- a/builtin/survey.c +++ b/builtin/survey.c @@ -358,8 +358,9 @@ struct large_item { /* * For blobs and trees the name field is the pathname of the - * file or directory. Root trees will have a zero-length - * name. The name field is not currenly used for commits. + * file or directory (as reported by the treewalk). Root trees + * are reported with a zero-length name, but we'll fix them up. + * The name field is not currenly used for commits. */ struct strbuf *name; @@ -387,13 +388,15 @@ struct large_item_vec { const struct large_item_vec_labels *labels_json; const struct large_item_vec_labels *labels_pretty; uint64_t nr_items; + enum object_type type; struct large_item items[FLEX_ARRAY]; /* nr_items */ }; static struct large_item_vec *alloc_large_item_vec( const struct large_item_vec_labels *labels_json, const struct large_item_vec_labels *labels_pretty, - uint64_t nr_items) + uint64_t nr_items, + enum object_type type) { struct large_item_vec *vec; size_t flex_len = nr_items * sizeof(struct large_item); @@ -406,6 +409,7 @@ static struct large_item_vec *alloc_large_item_vec( vec->labels_json = labels_json; vec->labels_pretty = labels_pretty; vec->nr_items = nr_items; + vec->type = type; for (k = 0; k < nr_items; k++) { struct strbuf *p = xcalloc(1, sizeof(struct strbuf)); @@ -470,6 +474,15 @@ static void maybe_insert_large_item(struct large_item_vec *vec, strbuf_reset(pbuf_temp); if (name && *name) strbuf_addstr(pbuf_temp, name); + else if (vec->type == OBJ_TREE) { + /* + * NEEDSWORK: Would it be better to wait and create + * a name of the form "^{tree}" after the + * treewalk is finished? + */ + strbuf_addf(pbuf_temp, "%s^{tree}", + oid_to_hex(containing_commit_oid)); + } /* push items[k..] down one and insert data for this item here */ @@ -654,7 +667,8 @@ static void alloc_commit_by_parents(void) if (survey_opts.show_largest_commits_by_nr_parents) survey_stats.commits.vec_largest_by_nr_parents = alloc_large_item_vec(&json, &pretty, - survey_opts.show_largest_commits_by_nr_parents); + survey_opts.show_largest_commits_by_nr_parents, + OBJ_COMMIT); } static void alloc_commit_by_size(void) { @@ -670,7 +684,8 @@ static void alloc_commit_by_size(void) { if (survey_opts.show_largest_commits_by_size_bytes) survey_stats.commits.vec_largest_by_size_bytes = alloc_large_item_vec(&json, &pretty, - survey_opts.show_largest_commits_by_size_bytes); + survey_opts.show_largest_commits_by_size_bytes, + OBJ_COMMIT); }; static void alloc_tree_by_entries(void) @@ -687,7 +702,8 @@ static void alloc_tree_by_entries(void) if (survey_opts.show_largest_trees_by_nr_entries) survey_stats.trees.vec_largest_by_nr_entries = alloc_large_item_vec(&json, &pretty, - survey_opts.show_largest_trees_by_nr_entries); + survey_opts.show_largest_trees_by_nr_entries, + OBJ_TREE); } static void alloc_tree_by_size(void) @@ -704,7 +720,8 @@ static void alloc_tree_by_size(void) if (survey_opts.show_largest_trees_by_size_bytes) survey_stats.trees.vec_largest_by_size_bytes = alloc_large_item_vec(&json, &pretty, - survey_opts.show_largest_trees_by_size_bytes); + survey_opts.show_largest_trees_by_size_bytes, + OBJ_TREE); } static void alloc_blob_by_size(void) @@ -721,7 +738,8 @@ static void alloc_blob_by_size(void) if (survey_opts.show_largest_blobs_by_size_bytes) survey_stats.blobs.vec_largest_by_size_bytes = alloc_large_item_vec(&json, &pretty, - survey_opts.show_largest_blobs_by_size_bytes); + survey_opts.show_largest_blobs_by_size_bytes, + OBJ_BLOB); } static void do_load_refs(struct ref_array *ref_array) @@ -1788,6 +1806,7 @@ static void fmt_pbin_row(struct strbuf *buf, static void fmt_large_item_hdr(struct strbuf *buf, int indent, + int name_length, int name_rev_length, const char *item_hdr_label) { @@ -1797,6 +1816,8 @@ static void fmt_large_item_hdr(struct strbuf *buf, strbuf_addchars(buf, ' ', indent); strbuf_addf(buf, "%-*s | %14s", column0, "OID", item_hdr_label); + if (name_length) + strbuf_addf(buf, " | %-*s", name_length, "Name"); strbuf_addf(buf, " | %-*s", name_rev_length, "Name Rev"); strbuf_addch(buf, '\n'); @@ -1804,6 +1825,7 @@ static void fmt_large_item_hdr(struct strbuf *buf, static void fmt_large_item_hr(struct strbuf *buf, int indent, + int name_length, int name_rev_length) { int column0 = the_hash_algo->hexsz; @@ -1814,6 +1836,10 @@ static void fmt_large_item_hr(struct strbuf *buf, strbuf_addchars(buf, '-', column0); strbuf_addstr(buf, "-+-"); strbuf_addchars(buf, '-', 14); + if (name_length) { + strbuf_addstr(buf, "-+-"); + strbuf_addchars(buf, '-', name_length); + } strbuf_addstr(buf, "-+-"); strbuf_addchars(buf, '-', name_rev_length); @@ -1822,6 +1848,8 @@ static void fmt_large_item_hr(struct strbuf *buf, static void fmt_large_item_row(struct strbuf *buf, int indent, + int name_length, + int name_rev_length, struct large_item *pitem) { int column0 = the_hash_algo->hexsz; @@ -1832,7 +1860,10 @@ static void fmt_large_item_row(struct strbuf *buf, strbuf_addf(buf, "%-*s | %14"PRIu64, column0, oid_to_hex(&pitem->oid), pitem->size); - strbuf_addf(buf, " | %-s", pitem->name_rev->buf); + if (name_length) + strbuf_addf(buf, " | %-*s", name_length, + (pitem->name ? pitem->name->buf: "")); + strbuf_addf(buf, " | %-*s", name_rev_length, pitem->name_rev->buf); strbuf_addch(buf, '\n'); } @@ -1841,23 +1872,34 @@ static void fmt_large_item_vec(struct strbuf *buf, int indent, struct large_item_vec *pvec) { + int name_length = 0; int name_rev_length = 10; int k; + if (pvec->type != OBJ_COMMIT) { + /* Add "Name" column for trees and blobs. */ + for (k = 0; k < pvec->nr_items; k++) + if (pvec->items[k].name && pvec->items[k].name->len > name_length) + name_length = pvec->items[k].name->len; + if (name_length) + if (name_length < 4) /* strlen("Name") */ + name_length = 4; + } + for (k = 0; k < pvec->nr_items; k++) if (pvec->items[k].name_rev->len > name_rev_length) name_rev_length = pvec->items[k].name_rev->len; strbuf_addch(buf, '\n'); fmt_txt_line(buf, indent, pvec->labels_pretty->dimension); - fmt_large_item_hr(buf, indent, name_rev_length); - fmt_large_item_hdr(buf, indent, name_rev_length, pvec->labels_pretty->item); - fmt_large_item_hr(buf, indent, name_rev_length); + fmt_large_item_hr(buf, indent, name_length, name_rev_length); + fmt_large_item_hdr(buf, indent, name_length, name_rev_length, pvec->labels_pretty->item); + fmt_large_item_hr(buf, indent, name_length, name_rev_length); for (k = 0; k < pvec->nr_items; k++) - fmt_large_item_row(buf, indent, &pvec->items[k]); + fmt_large_item_row(buf, indent, name_length, name_rev_length, &pvec->items[k]); - fmt_large_item_hr(buf, indent, name_rev_length); + fmt_large_item_hr(buf, indent, name_length, name_rev_length); } static void fmt_commit_row(struct strbuf *buf, @@ -2234,7 +2276,12 @@ static void pretty_print_trees(int indent) static void pretty_print_blobs(int indent) { + struct survey_stats_blobs *psb = &survey_stats.blobs; + struct survey_stats_base_object *base = &psb->base; struct strbuf buf = STRBUF_INIT; + int indent1 = indent + 4; + int indent2 = indent + 8; + int indent3 = indent + 12; int k; const char *intro[] = { @@ -2252,6 +2299,28 @@ static void pretty_print_blobs(int indent) while (intro[k]) fmt_txt_line(&buf, indent, intro[k++]); + fmt_blob_row(&buf, indent1, "Total Number of Blobs", base->cnt_seen, NULL); + + strbuf_addch(&buf, '\n'); + fmt_txt_line(&buf, indent1, "Blob Count by Storage Location"); + if (base->cnt_missing) + fmt_commit_row(&buf, indent2, "Missing", base->cnt_missing, NULL); + if (base->cnt_cached) + fmt_commit_row(&buf, indent2, "Cached", base->cnt_cached, NULL); + if (base->cnt_loose) + fmt_commit_row(&buf, indent2, "Loose", base->cnt_loose, NULL); + if (base->cnt_packed) + fmt_commit_row(&buf, indent2, "Packed", base->cnt_packed, NULL); + if (base->cnt_dbcached) + fmt_commit_row(&buf, indent2, "DBCached", base->cnt_dbcached, NULL); + + strbuf_addch(&buf, '\n'); + fmt_commit_row(&buf, indent1, "Total Size of Blobs", base->sum_size, NULL); + fmt_commit_row(&buf, indent1, "Total Disk Size of Blobs", base->sum_disk_size, NULL); + + fmt_hbin(&buf, indent1, "Blob Histogram by Size in Bytes", base->size_hbin); + fmt_large_item_vec(&buf, indent1, psb->vec_largest_by_size_bytes); + /* * NEEDSWORK: TODO */