interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / builtin / repo.c

1006 lines · 27819 bytes Raw Blame History
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "builtin.h"
4#include "commit.h"
5#include "environment.h"
6#include "hash.h"
7#include "hex.h"
8#include "odb.h"
9#include "parse-options.h"
10#include "path.h"
11#include "path-walk.h"
12#include "progress.h"
13#include "quote.h"
14#include "ref-filter.h"
15#include "refs.h"
16#include "revision.h"
17#include "setup.h"
18#include "strbuf.h"
19#include "string-list.h"
20#include "shallow.h"
21#include "tree.h"
22#include "tree-walk.h"
23#include "utf8.h"
24
25#define REPO_INFO_USAGE \
26 "git repo info [--format=(lines|nul) | -z] [--all | <key>...]", \
27 "git repo info --keys [--format=(lines|nul) | -z]"
28
29#define REPO_STRUCTURE_USAGE \
30 "git repo structure [--format=(table|lines|nul) | -z]"
31
32static const char *const repo_usage[] = {
33 REPO_INFO_USAGE,
34 REPO_STRUCTURE_USAGE,
35 NULL,
36};
37
38static const char *const repo_info_usage[] = {
39 REPO_INFO_USAGE,
40 NULL,
41};
42
43static const char *const repo_structure_usage[] = {
44 REPO_STRUCTURE_USAGE,
45 NULL,
46};
47
48typedef int get_value_fn(struct repository *repo, struct strbuf *buf);
49
50enum output_format {
51 FORMAT_TABLE,
52 FORMAT_NEWLINE_TERMINATED,
53 FORMAT_NUL_TERMINATED,
54};
55
56struct repo_info_field {
57 const char *key;
58 get_value_fn *get_value;
59};
60
61static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf)
62{
63 strbuf_addstr(buf, is_bare_repository(the_repository) ? "true" : "false");
64 return 0;
65}
66
67static int get_layout_shallow(struct repository *repo, struct strbuf *buf)
68{
69 strbuf_addstr(buf,
70 is_repository_shallow(repo) ? "true" : "false");
71 return 0;
72}
73
74static int get_object_format(struct repository *repo, struct strbuf *buf)
75{
76 strbuf_addstr(buf, repo->hash_algo->name);
77 return 0;
78}
79
80static int get_path_commondir_absolute(struct repository *repo, struct strbuf *buf)
81{
82 const char *common_dir = repo_get_common_dir(repo);
83
84 if (!common_dir)
85 return error(_("unable to get common directory"));
86
87 format_path(buf, common_dir, repo->prefix, PATH_FORMAT_CANONICAL);
88 return 0;
89}
90
91static int get_path_commondir_relative(struct repository *repo, struct strbuf *buf)
92{
93 const char *common_dir = repo_get_common_dir(repo);
94
95 if (!common_dir)
96 return error(_("unable to get common directory"));
97
98 format_path(buf, common_dir, repo->prefix, PATH_FORMAT_RELATIVE);
99 return 0;
100}
101
102static int get_path_gitdir_absolute(struct repository *repo, struct strbuf *buf)
103{
104 const char *git_dir = repo_get_git_dir(repo);
105
106 if (!git_dir)
107 return error(_("unable to get git directory"));
108
109 format_path(buf, git_dir, repo->prefix, PATH_FORMAT_CANONICAL);
110 return 0;
111}
112
113static int get_path_gitdir_relative(struct repository *repo, struct strbuf *buf)
114{
115 const char *git_dir = repo_get_git_dir(repo);
116
117 if (!git_dir)
118 return error(_("unable to get git directory"));
119
120 format_path(buf, git_dir, repo->prefix, PATH_FORMAT_RELATIVE);
121 return 0;
122}
123
124static int get_references_format(struct repository *repo, struct strbuf *buf)
125{
126 strbuf_addstr(buf,
127 ref_storage_format_to_name(repo->ref_storage_format));
128 return 0;
129}
130
131static int get_object_storage_format(struct repository *repo, struct strbuf *buf)
132{
133 strbuf_addstr(buf,
134 object_storage_format_to_name(repo->object_storage_format));
135 if (repo->object_storage_payload)
136 strbuf_addf(buf, "://%s", repo->object_storage_payload);
137 return 0;
138}
139
140/* repo_info_field keys must be in lexicographical order */
141static const struct repo_info_field repo_info_field[] = {
142 { "layout.bare", get_layout_bare },
143 { "layout.shallow", get_layout_shallow },
144 { "object.format", get_object_format },
145 { "objects.storageFormat", get_object_storage_format },
146 { "path.commondir.absolute", get_path_commondir_absolute },
147 { "path.commondir.relative", get_path_commondir_relative },
148 { "path.gitdir.absolute", get_path_gitdir_absolute },
149 { "path.gitdir.relative", get_path_gitdir_relative },
150 { "references.storageFormat", get_references_format },
151};
152
153static int repo_info_field_cmp(const void *va, const void *vb)
154{
155 const struct repo_info_field *a = va;
156 const struct repo_info_field *b = vb;
157
158 return strcmp(a->key, b->key);
159}
160
161static const struct repo_info_field *get_repo_info_field(const char *key)
162{
163 const struct repo_info_field search_key = { key, NULL };
164 const struct repo_info_field *found = bsearch(&search_key,
165 repo_info_field,
166 ARRAY_SIZE(repo_info_field),
167 sizeof(*found),
168 repo_info_field_cmp);
169
170 return found;
171}
172
173static void print_field(enum output_format format, const char *key,
174 const char *value)
175{
176 switch (format) {
177 case FORMAT_NEWLINE_TERMINATED:
178 printf("%s=", key);
179 quote_c_style(value, NULL, stdout, 0);
180 putchar('\n');
181 break;
182 case FORMAT_NUL_TERMINATED:
183 printf("%s\n%s%c", key, value, '\0');
184 break;
185 default:
186 BUG("not a valid output format: %d", format);
187 }
188}
189
190static int print_fields(int argc, const char **argv,
191 struct repository *repo,
192 enum output_format format)
193{
194 int ret = 0;
195 struct strbuf valbuf = STRBUF_INIT;
196
197 for (int i = 0; i < argc; i++) {
198 const char *key = argv[i];
199 const struct repo_info_field *field = get_repo_info_field(key);
200
201 if (!field) {
202 ret = error(_("key '%s' not found"), key);
203 continue;
204 }
205
206 strbuf_reset(&valbuf);
207 field->get_value(repo, &valbuf);
208 print_field(format, key, valbuf.buf);
209 }
210
211 strbuf_release(&valbuf);
212 return ret;
213}
214
215static int print_all_fields(struct repository *repo,
216 enum output_format format)
217{
218 struct strbuf valbuf = STRBUF_INIT;
219
220 for (size_t i = 0; i < ARRAY_SIZE(repo_info_field); i++) {
221 const struct repo_info_field *field = &repo_info_field[i];
222
223 strbuf_reset(&valbuf);
224 field->get_value(repo, &valbuf);
225 print_field(format, field->key, valbuf.buf);
226 }
227
228 strbuf_release(&valbuf);
229 return 0;
230}
231
232static int print_keys(enum output_format format)
233{
234 char sep;
235
236 switch (format) {
237 case FORMAT_NEWLINE_TERMINATED:
238 sep = '\n';
239 break;
240 case FORMAT_NUL_TERMINATED:
241 sep = '\0';
242 break;
243 default:
244 die(_("--keys can only be used with --format=lines or --format=nul"));
245 }
246
247 for (size_t i = 0; i < ARRAY_SIZE(repo_info_field); i++) {
248 const struct repo_info_field *field = &repo_info_field[i];
249 printf("%s%c", field->key, sep);
250 }
251
252 return 0;
253}
254
255static int parse_format_cb(const struct option *opt,
256 const char *arg, int unset UNUSED)
257{
258 enum output_format *format = opt->value;
259
260 if (opt->short_name == 'z')
261 *format = FORMAT_NUL_TERMINATED;
262 else if (!strcmp(arg, "nul"))
263 *format = FORMAT_NUL_TERMINATED;
264 else if (!strcmp(arg, "lines"))
265 *format = FORMAT_NEWLINE_TERMINATED;
266 else if (!strcmp(arg, "table"))
267 *format = FORMAT_TABLE;
268 else
269 die(_("invalid format '%s'"), arg);
270
271 return 0;
272}
273
274static int cmd_repo_info(int argc, const char **argv, const char *prefix,
275 struct repository *repo)
276{
277 enum output_format format = FORMAT_NEWLINE_TERMINATED;
278 int all_keys = 0;
279 int show_keys = 0;
280 struct option options[] = {
281 OPT_CALLBACK_F(0, "format", &format, N_("format"),
282 N_("output format"),
283 PARSE_OPT_NONEG, parse_format_cb),
284 OPT_CALLBACK_F('z', NULL, &format, NULL,
285 N_("synonym for --format=nul"),
286 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
287 parse_format_cb),
288 OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")),
289 OPT_BOOL(0, "keys", &show_keys, N_("show keys")),
290 OPT_END()
291 };
292
293 argc = parse_options(argc, argv, prefix, options, repo_info_usage, 0);
294
295 if (show_keys && (all_keys || argc))
296 die(_("--keys cannot be used with a <key> or --all"));
297
298 if (show_keys)
299 return print_keys(format);
300
301 if (format != FORMAT_NEWLINE_TERMINATED && format != FORMAT_NUL_TERMINATED)
302 die(_("unsupported output format"));
303
304 if (all_keys && argc)
305 die(_("--all and <key> cannot be used together"));
306
307 if (all_keys)
308 return print_all_fields(repo, format);
309 else
310 return print_fields(argc, argv, repo, format);
311}
312
313struct object_data {
314 struct object_id oid;
315 size_t value;
316};
317
318struct largest_objects {
319 struct object_data tag_size;
320 struct object_data commit_size;
321 struct object_data tree_size;
322 struct object_data blob_size;
323
324 struct object_data parent_count;
325 struct object_data tree_entries;
326};
327
328struct ref_stats {
329 size_t branches;
330 size_t remotes;
331 size_t tags;
332 size_t others;
333};
334
335struct object_values {
336 size_t tags;
337 size_t commits;
338 size_t trees;
339 size_t blobs;
340};
341
342struct object_stats {
343 struct object_values type_counts;
344 struct object_values inflated_sizes;
345 struct object_values disk_sizes;
346 struct largest_objects largest;
347};
348
349struct repo_structure {
350 struct ref_stats refs;
351 struct object_stats objects;
352};
353
354struct stats_table {
355 struct string_list rows;
356 struct string_list annotations;
357
358 int name_col_width;
359 int value_col_width;
360 int unit_col_width;
361};
362
363/*
364 * Holds column data that gets stored for each row.
365 */
366struct stats_table_entry {
367 char *value;
368 const char *unit;
369 size_t index;
370 struct object_id *oid;
371};
372
373static void stats_table_vaddf(struct stats_table *table,
374 struct stats_table_entry *entry,
375 const char *format, va_list ap)
376{
377 struct strbuf buf = STRBUF_INIT;
378 struct string_list_item *item;
379 char *formatted_name;
380 int name_width;
381
382 strbuf_vaddf(&buf, format, ap);
383 formatted_name = strbuf_detach(&buf, NULL);
384 name_width = utf8_strwidth(formatted_name);
385
386 item = string_list_append_nodup(&table->rows, formatted_name);
387 item->util = entry;
388
389 if (name_width > table->name_col_width)
390 table->name_col_width = name_width;
391 if (!entry)
392 return;
393 if (entry->oid) {
394 entry->index = table->annotations.nr + 1;
395 strbuf_addf(&buf, "[%" PRIuMAX "] %s", (uintmax_t)entry->index,
396 oid_to_hex(entry->oid));
397 string_list_append_nodup(&table->annotations, strbuf_detach(&buf, NULL));
398 }
399 if (entry->value) {
400 int value_width = utf8_strwidth(entry->value);
401 if (value_width > table->value_col_width)
402 table->value_col_width = value_width;
403 }
404 if (entry->unit) {
405 int unit_width = utf8_strwidth(entry->unit);
406 if (unit_width > table->unit_col_width)
407 table->unit_col_width = unit_width;
408 }
409
410 strbuf_release(&buf);
411}
412
413static void stats_table_addf(struct stats_table *table, const char *format, ...)
414{
415 va_list ap;
416
417 va_start(ap, format);
418 stats_table_vaddf(table, NULL, format, ap);
419 va_end(ap);
420}
421
422static void stats_table_count_addf(struct stats_table *table, size_t value,
423 const char *format, ...)
424{
425 struct stats_table_entry *entry;
426 va_list ap;
427
428 CALLOC_ARRAY(entry, 1);
429 humanise_count(value, &entry->value, &entry->unit);
430
431 va_start(ap, format);
432 stats_table_vaddf(table, entry, format, ap);
433 va_end(ap);
434}
435
436static void stats_table_object_count_addf(struct stats_table *table,
437 struct object_id *oid, size_t value,
438 const char *format, ...)
439{
440 struct stats_table_entry *entry;
441 va_list ap;
442
443 CALLOC_ARRAY(entry, 1);
444 humanise_count(value, &entry->value, &entry->unit);
445
446 /*
447 * A NULL OID should not have a table annotation.
448 */
449 if (!is_null_oid(oid))
450 entry->oid = oid;
451
452 va_start(ap, format);
453 stats_table_vaddf(table, entry, format, ap);
454 va_end(ap);
455}
456
457static void stats_table_size_addf(struct stats_table *table, size_t value,
458 const char *format, ...)
459{
460 struct stats_table_entry *entry;
461 va_list ap;
462
463 CALLOC_ARRAY(entry, 1);
464 humanise_bytes(value, &entry->value, &entry->unit, HUMANISE_COMPACT);
465
466 va_start(ap, format);
467 stats_table_vaddf(table, entry, format, ap);
468 va_end(ap);
469}
470
471static void stats_table_object_size_addf(struct stats_table *table,
472 struct object_id *oid, size_t value,
473 const char *format, ...)
474{
475 struct stats_table_entry *entry;
476 va_list ap;
477
478 CALLOC_ARRAY(entry, 1);
479 humanise_bytes(value, &entry->value, &entry->unit, HUMANISE_COMPACT);
480
481 /*
482 * A NULL OID should not have a table annotation.
483 */
484 if (!is_null_oid(oid))
485 entry->oid = oid;
486
487 va_start(ap, format);
488 stats_table_vaddf(table, entry, format, ap);
489 va_end(ap);
490}
491
492static inline size_t get_total_reference_count(struct ref_stats *stats)
493{
494 return stats->branches + stats->remotes + stats->tags + stats->others;
495}
496
497static inline size_t get_total_object_values(struct object_values *values)
498{
499 return values->tags + values->commits + values->trees + values->blobs;
500}
501
502static void stats_table_setup_structure(struct stats_table *table,
503 struct repo_structure *stats)
504{
505 struct object_stats *objects = &stats->objects;
506 struct ref_stats *refs = &stats->refs;
507 size_t inflated_object_total;
508 size_t object_count_total;
509 size_t disk_object_total;
510 size_t ref_total;
511
512 ref_total = get_total_reference_count(refs);
513 stats_table_addf(table, "* %s", _("References"));
514 stats_table_count_addf(table, ref_total, " * %s", _("Count"));
515 stats_table_count_addf(table, refs->branches, " * %s", _("Branches"));
516 stats_table_count_addf(table, refs->tags, " * %s", _("Tags"));
517 stats_table_count_addf(table, refs->remotes, " * %s", _("Remotes"));
518 stats_table_count_addf(table, refs->others, " * %s", _("Others"));
519
520 object_count_total = get_total_object_values(&objects->type_counts);
521 stats_table_addf(table, "");
522 stats_table_addf(table, "* %s", _("Reachable objects"));
523 stats_table_count_addf(table, object_count_total, " * %s", _("Count"));
524 stats_table_count_addf(table, objects->type_counts.commits,
525 " * %s", _("Commits"));
526 stats_table_count_addf(table, objects->type_counts.trees,
527 " * %s", _("Trees"));
528 stats_table_count_addf(table, objects->type_counts.blobs,
529 " * %s", _("Blobs"));
530 stats_table_count_addf(table, objects->type_counts.tags,
531 " * %s", _("Tags"));
532
533 inflated_object_total = get_total_object_values(&objects->inflated_sizes);
534 stats_table_size_addf(table, inflated_object_total,
535 " * %s", _("Inflated size"));
536 stats_table_size_addf(table, objects->inflated_sizes.commits,
537 " * %s", _("Commits"));
538 stats_table_size_addf(table, objects->inflated_sizes.trees,
539 " * %s", _("Trees"));
540 stats_table_size_addf(table, objects->inflated_sizes.blobs,
541 " * %s", _("Blobs"));
542 stats_table_size_addf(table, objects->inflated_sizes.tags,
543 " * %s", _("Tags"));
544
545 disk_object_total = get_total_object_values(&objects->disk_sizes);
546 stats_table_size_addf(table, disk_object_total,
547 " * %s", _("Disk size"));
548 stats_table_size_addf(table, objects->disk_sizes.commits,
549 " * %s", _("Commits"));
550 stats_table_size_addf(table, objects->disk_sizes.trees,
551 " * %s", _("Trees"));
552 stats_table_size_addf(table, objects->disk_sizes.blobs,
553 " * %s", _("Blobs"));
554 stats_table_size_addf(table, objects->disk_sizes.tags,
555 " * %s", _("Tags"));
556
557 stats_table_addf(table, "");
558 stats_table_addf(table, "* %s", _("Largest objects"));
559 stats_table_addf(table, " * %s", _("Commits"));
560 stats_table_object_size_addf(table,
561 &objects->largest.commit_size.oid,
562 objects->largest.commit_size.value,
563 " * %s", _("Maximum size"));
564 stats_table_object_count_addf(table,
565 &objects->largest.parent_count.oid,
566 objects->largest.parent_count.value,
567 " * %s", _("Maximum parents"));
568 stats_table_addf(table, " * %s", _("Trees"));
569 stats_table_object_size_addf(table,
570 &objects->largest.tree_size.oid,
571 objects->largest.tree_size.value,
572 " * %s", _("Maximum size"));
573 stats_table_object_count_addf(table,
574 &objects->largest.tree_entries.oid,
575 objects->largest.tree_entries.value,
576 " * %s", _("Maximum entries"));
577 stats_table_addf(table, " * %s", _("Blobs"));
578 stats_table_object_size_addf(table,
579 &objects->largest.blob_size.oid,
580 objects->largest.blob_size.value,
581 " * %s", _("Maximum size"));
582 stats_table_addf(table, " * %s", _("Tags"));
583 stats_table_object_size_addf(table,
584 &objects->largest.tag_size.oid,
585 objects->largest.tag_size.value,
586 " * %s", _("Maximum size"));
587}
588
589#define INDEX_WIDTH 4
590
591static void stats_table_print_structure(const struct stats_table *table)
592{
593 const char *name_col_title = _("Repository structure");
594 const char *value_col_title = _("Value");
595 int title_name_width = utf8_strwidth(name_col_title);
596 int title_value_width = utf8_strwidth(value_col_title);
597 int name_col_width = table->name_col_width;
598 int value_col_width = table->value_col_width;
599 int unit_col_width = table->unit_col_width;
600 struct string_list_item *item;
601 struct strbuf buf = STRBUF_INIT;
602
603 if (title_name_width > name_col_width)
604 name_col_width = title_name_width;
605 if (title_value_width > value_col_width + unit_col_width + 1)
606 value_col_width = title_value_width - unit_col_width;
607
608 strbuf_addstr(&buf, "| ");
609 strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width + INDEX_WIDTH,
610 name_col_title);
611 strbuf_addstr(&buf, " | ");
612 strbuf_utf8_align(&buf, ALIGN_LEFT,
613 value_col_width + unit_col_width + 1, value_col_title);
614 strbuf_addstr(&buf, " |");
615 printf("%s\n", buf.buf);
616
617 printf("| ");
618 for (int i = 0; i < name_col_width + INDEX_WIDTH; i++)
619 putchar('-');
620 printf(" | ");
621 for (int i = 0; i < value_col_width + unit_col_width + 1; i++)
622 putchar('-');
623 printf(" |\n");
624
625 for_each_string_list_item(item, &table->rows) {
626 struct stats_table_entry *entry = item->util;
627 const char *value = "";
628 const char *unit = "";
629
630 if (entry) {
631 value = entry->value;
632 if (entry->unit)
633 unit = entry->unit;
634 }
635
636 strbuf_reset(&buf);
637 strbuf_addstr(&buf, "| ");
638 strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width, item->string);
639
640 if (entry && entry->oid)
641 strbuf_addf(&buf, " [%" PRIuMAX "]",
642 (uintmax_t)entry->index);
643 else
644 strbuf_addchars(&buf, ' ', INDEX_WIDTH);
645
646 strbuf_addstr(&buf, " | ");
647 strbuf_utf8_align(&buf, ALIGN_RIGHT, value_col_width, value);
648 strbuf_addch(&buf, ' ');
649 strbuf_utf8_align(&buf, ALIGN_LEFT, unit_col_width, unit);
650 strbuf_addstr(&buf, " |");
651 printf("%s\n", buf.buf);
652 }
653
654 if (table->annotations.nr) {
655 printf("\n");
656 for_each_string_list_item(item, &table->annotations)
657 printf("%s\n", item->string);
658 }
659
660 strbuf_release(&buf);
661}
662
663static void stats_table_clear(struct stats_table *table)
664{
665 struct stats_table_entry *entry;
666 struct string_list_item *item;
667
668 for_each_string_list_item(item, &table->rows) {
669 entry = item->util;
670 if (entry)
671 free(entry->value);
672 }
673
674 string_list_clear(&table->rows, 1);
675 string_list_clear(&table->annotations, 1);
676}
677
678static inline void print_keyvalue(const char *key, char key_delim, size_t value,
679 char value_delim)
680{
681 printf("%s%c%" PRIuMAX "%c", key, key_delim, (uintmax_t)value,
682 value_delim);
683}
684
685static void print_object_data(const char *key, char key_delim,
686 struct object_data *data, char value_delim)
687{
688 print_keyvalue(key, key_delim, data->value, value_delim);
689 printf("%s_oid%c%s%c", key, key_delim, oid_to_hex(&data->oid),
690 value_delim);
691}
692
693static void structure_keyvalue_print(struct repo_structure *stats,
694 char key_delim, char value_delim)
695{
696 print_keyvalue("references.branches.count", key_delim,
697 stats->refs.branches, value_delim);
698 print_keyvalue("references.tags.count", key_delim,
699 stats->refs.tags, value_delim);
700 print_keyvalue("references.remotes.count", key_delim,
701 stats->refs.remotes, value_delim);
702 print_keyvalue("references.others.count", key_delim,
703 stats->refs.others, value_delim);
704
705 print_keyvalue("objects.commits.count", key_delim,
706 stats->objects.type_counts.commits, value_delim);
707 print_keyvalue("objects.trees.count", key_delim,
708 stats->objects.type_counts.trees, value_delim);
709 print_keyvalue("objects.blobs.count", key_delim,
710 stats->objects.type_counts.blobs, value_delim);
711 print_keyvalue("objects.tags.count", key_delim,
712 stats->objects.type_counts.tags, value_delim);
713
714 print_keyvalue("objects.commits.inflated_size", key_delim,
715 stats->objects.inflated_sizes.commits, value_delim);
716 print_keyvalue("objects.trees.inflated_size", key_delim,
717 stats->objects.inflated_sizes.trees, value_delim);
718 print_keyvalue("objects.blobs.inflated_size", key_delim,
719 stats->objects.inflated_sizes.blobs, value_delim);
720 print_keyvalue("objects.tags.inflated_size", key_delim,
721 stats->objects.inflated_sizes.tags, value_delim);
722
723 print_keyvalue("objects.commits.disk_size", key_delim,
724 stats->objects.disk_sizes.commits, value_delim);
725 print_keyvalue("objects.trees.disk_size", key_delim,
726 stats->objects.disk_sizes.trees, value_delim);
727 print_keyvalue("objects.blobs.disk_size", key_delim,
728 stats->objects.disk_sizes.blobs, value_delim);
729 print_keyvalue("objects.tags.disk_size", key_delim,
730 stats->objects.disk_sizes.tags, value_delim);
731
732 print_object_data("objects.commits.max_size", key_delim,
733 &stats->objects.largest.commit_size, value_delim);
734 print_object_data("objects.trees.max_size", key_delim,
735 &stats->objects.largest.tree_size, value_delim);
736 print_object_data("objects.blobs.max_size", key_delim,
737 &stats->objects.largest.blob_size, value_delim);
738 print_object_data("objects.tags.max_size", key_delim,
739 &stats->objects.largest.tag_size, value_delim);
740
741 print_object_data("objects.commits.max_parents", key_delim,
742 &stats->objects.largest.parent_count, value_delim);
743 print_object_data("objects.trees.max_entries", key_delim,
744 &stats->objects.largest.tree_entries, value_delim);
745
746 fflush(stdout);
747}
748
749struct count_references_data {
750 struct ref_stats *stats;
751 struct rev_info *revs;
752 struct progress *progress;
753};
754
755static int count_references(const struct reference *ref, void *cb_data)
756{
757 struct count_references_data *data = cb_data;
758 struct ref_stats *stats = data->stats;
759 size_t ref_count;
760
761 switch (ref_kind_from_refname(ref->name)) {
762 case FILTER_REFS_BRANCHES:
763 stats->branches++;
764 break;
765 case FILTER_REFS_REMOTES:
766 stats->remotes++;
767 break;
768 case FILTER_REFS_TAGS:
769 stats->tags++;
770 break;
771 case FILTER_REFS_OTHERS:
772 stats->others++;
773 break;
774 default:
775 BUG("unexpected reference type");
776 }
777
778 /*
779 * While iterating through references for counting, also add OIDs in
780 * preparation for the path walk.
781 */
782 add_pending_oid(data->revs, NULL, ref->oid, 0);
783
784 ref_count = get_total_reference_count(stats);
785 display_progress(data->progress, ref_count);
786
787 return 0;
788}
789
790static void structure_count_references(struct ref_stats *stats,
791 struct rev_info *revs,
792 struct repository *repo,
793 int show_progress)
794{
795 struct count_references_data data = {
796 .stats = stats,
797 .revs = revs,
798 };
799
800 if (show_progress)
801 data.progress = start_delayed_progress(repo,
802 _("Counting references"), 0);
803
804 refs_for_each_ref(get_main_ref_store(repo), count_references, &data);
805 stop_progress(&data.progress);
806}
807
808struct count_objects_data {
809 struct object_database *odb;
810 struct object_stats *stats;
811 struct progress *progress;
812};
813
814static void check_largest(struct object_data *data, struct object_id *oid,
815 size_t value)
816{
817 if (value > data->value || is_null_oid(&data->oid)) {
818 oidcpy(&data->oid, oid);
819 data->value = value;
820 }
821}
822
823static size_t count_tree_entries(struct object *obj)
824{
825 struct tree *t = object_as_type(obj, OBJ_TREE, 0);
826 struct name_entry entry;
827 struct tree_desc desc;
828 size_t count = 0;
829
830 init_tree_desc(&desc, &t->object.oid, t->buffer, t->size);
831 while (tree_entry(&desc, &entry))
832 count++;
833
834 return count;
835}
836
837static int count_objects(const char *path UNUSED, struct oid_array *oids,
838 enum object_type type, void *cb_data)
839{
840 struct count_objects_data *data = cb_data;
841 struct object_stats *stats = data->stats;
842 size_t object_count;
843
844 for (size_t i = 0; i < oids->nr; i++) {
845 struct object_info oi = OBJECT_INFO_INIT;
846 unsigned long inflated;
847 size_t inflated_st = 0;
848 struct commit *commit;
849 struct object *obj;
850 void *content;
851 off_t disk;
852 int eaten;
853
854 oi.sizep = &inflated_st;
855 oi.disk_sizep = &disk;
856 oi.contentp = &content;
857
858 if (odb_read_object_info_extended(data->odb, &oids->oid[i], &oi,
859 OBJECT_INFO_SKIP_FETCH_OBJECT |
860 OBJECT_INFO_QUICK) < 0)
861 continue;
862 inflated = cast_size_t_to_ulong(inflated_st);
863
864 obj = parse_object_buffer(the_repository, &oids->oid[i], type,
865 inflated, content, &eaten);
866
867 switch (type) {
868 case OBJ_TAG:
869 stats->type_counts.tags++;
870 stats->inflated_sizes.tags += inflated;
871 stats->disk_sizes.tags += disk;
872 check_largest(&stats->largest.tag_size, &oids->oid[i],
873 inflated);
874 break;
875 case OBJ_COMMIT:
876 commit = object_as_type(obj, OBJ_COMMIT, 0);
877 stats->type_counts.commits++;
878 stats->inflated_sizes.commits += inflated;
879 stats->disk_sizes.commits += disk;
880 check_largest(&stats->largest.commit_size, &oids->oid[i],
881 inflated);
882 check_largest(&stats->largest.parent_count, &oids->oid[i],
883 commit_list_count(commit->parents));
884 break;
885 case OBJ_TREE:
886 stats->type_counts.trees++;
887 stats->inflated_sizes.trees += inflated;
888 stats->disk_sizes.trees += disk;
889 check_largest(&stats->largest.tree_size, &oids->oid[i],
890 inflated);
891 check_largest(&stats->largest.tree_entries, &oids->oid[i],
892 count_tree_entries(obj));
893 break;
894 case OBJ_BLOB:
895 stats->type_counts.blobs++;
896 stats->inflated_sizes.blobs += inflated;
897 stats->disk_sizes.blobs += disk;
898 check_largest(&stats->largest.blob_size, &oids->oid[i],
899 inflated);
900 break;
901 default:
902 BUG("invalid object type");
903 }
904
905 if (!eaten)
906 free(content);
907 }
908
909 object_count = get_total_object_values(&stats->type_counts);
910 display_progress(data->progress, object_count);
911
912 return 0;
913}
914
915static void structure_count_objects(struct object_stats *stats,
916 struct rev_info *revs,
917 struct repository *repo, int show_progress)
918{
919 struct path_walk_info info = PATH_WALK_INFO_INIT;
920 struct count_objects_data data = {
921 .odb = repo->objects,
922 .stats = stats,
923 };
924
925 info.revs = revs;
926 info.path_fn = count_objects;
927 info.path_fn_data = &data;
928
929 if (show_progress)
930 data.progress = start_delayed_progress(repo, _("Counting objects"), 0);
931
932 walk_objects_by_path(&info);
933 path_walk_info_clear(&info);
934 stop_progress(&data.progress);
935}
936
937static int cmd_repo_structure(int argc, const char **argv, const char *prefix,
938 struct repository *repo)
939{
940 struct stats_table table = {
941 .rows = STRING_LIST_INIT_DUP,
942 .annotations = STRING_LIST_INIT_DUP,
943 };
944 enum output_format format = FORMAT_TABLE;
945 struct repo_structure stats = { 0 };
946 struct rev_info revs;
947 int show_progress = -1;
948 struct option options[] = {
949 OPT_CALLBACK_F(0, "format", &format, N_("format"),
950 N_("output format"),
951 PARSE_OPT_NONEG, parse_format_cb),
952 OPT_CALLBACK_F('z', NULL, &format, NULL,
953 N_("synonym for --format=nul"),
954 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
955 parse_format_cb),
956 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
957 OPT_END()
958 };
959
960 argc = parse_options(argc, argv, prefix, options, repo_structure_usage, 0);
961 if (argc)
962 usage(_("too many arguments"));
963
964 repo_init_revisions(repo, &revs, prefix);
965
966 if (show_progress < 0)
967 show_progress = isatty(2);
968
969 structure_count_references(&stats.refs, &revs, repo, show_progress);
970 structure_count_objects(&stats.objects, &revs, repo, show_progress);
971
972 switch (format) {
973 case FORMAT_TABLE:
974 stats_table_setup_structure(&table, &stats);
975 stats_table_print_structure(&table);
976 break;
977 case FORMAT_NEWLINE_TERMINATED:
978 structure_keyvalue_print(&stats, '=', '\n');
979 break;
980 case FORMAT_NUL_TERMINATED:
981 structure_keyvalue_print(&stats, '\n', '\0');
982 break;
983 default:
984 BUG("invalid output format");
985 }
986
987 stats_table_clear(&table);
988 release_revisions(&revs);
989
990 return 0;
991}
992
993int cmd_repo(int argc, const char **argv, const char *prefix,
994 struct repository *repo)
995{
996 parse_opt_subcommand_fn *fn = NULL;
997 struct option options[] = {
998 OPT_SUBCOMMAND("info", &fn, cmd_repo_info),
999 OPT_SUBCOMMAND("structure", &fn, cmd_repo_structure),
1000 OPT_END()
1001 };
1002
1003 argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
1004
1005 return fn(argc, argv, prefix, repo);
1006}