1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
#define USE_THE_REPOSITORY_VARIABLE
#include "builtin.h"
#include "environment.h"
#include "parse-options.h"
#include "quote.h"
#include "ref-filter.h"
#include "refs.h"
#include "strbuf.h"
#include "string-list.h"
#include "shallow.h"
#include "utf8.h"
static const char *const repo_usage[] = {
"git repo info [--format=(keyvalue|nul)] [-z] [<key>...]",
"git repo structure",
NULL
};
typedef int get_value_fn(struct repository *repo, struct strbuf *buf);
enum output_format {
FORMAT_KEYVALUE,
FORMAT_NUL_TERMINATED,
};
struct field {
const char *key;
get_value_fn *get_value;
};
static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf)
{
strbuf_addstr(buf, is_bare_repository() ? "true" : "false");
return 0;
}
static int get_layout_shallow(struct repository *repo, struct strbuf *buf)
{
strbuf_addstr(buf,
is_repository_shallow(repo) ? "true" : "false");
return 0;
}
static int get_object_format(struct repository *repo, struct strbuf *buf)
{
strbuf_addstr(buf, repo->hash_algo->name);
return 0;
}
static int get_references_format(struct repository *repo, struct strbuf *buf)
{
strbuf_addstr(buf,
ref_storage_format_to_name(repo->ref_storage_format));
return 0;
}
/* repo_info_fields keys must be in lexicographical order */
static const struct field repo_info_fields[] = {
{ "layout.bare", get_layout_bare },
{ "layout.shallow", get_layout_shallow },
{ "object.format", get_object_format },
{ "references.format", get_references_format },
};
static int repo_info_fields_cmp(const void *va, const void *vb)
{
const struct field *a = va;
const struct field *b = vb;
return strcmp(a->key, b->key);
}
static get_value_fn *get_value_fn_for_key(const char *key)
{
const struct field search_key = { key, NULL };
const struct field *found = bsearch(&search_key, repo_info_fields,
ARRAY_SIZE(repo_info_fields),
sizeof(*found),
repo_info_fields_cmp);
return found ? found->get_value : NULL;
}
static int print_fields(int argc, const char **argv,
struct repository *repo,
enum output_format format)
{
int ret = 0;
struct strbuf valbuf = STRBUF_INIT;
struct strbuf quotbuf = STRBUF_INIT;
for (int i = 0; i < argc; i++) {
get_value_fn *get_value;
const char *key = argv[i];
get_value = get_value_fn_for_key(key);
if (!get_value) {
ret = error(_("key '%s' not found"), key);
continue;
}
strbuf_reset(&valbuf);
strbuf_reset("buf);
get_value(repo, &valbuf);
switch (format) {
case FORMAT_KEYVALUE:
quote_c_style(valbuf.buf, "buf, NULL, 0);
printf("%s=%s\n", key, quotbuf.buf);
break;
case FORMAT_NUL_TERMINATED:
printf("%s\n%s%c", key, valbuf.buf, '\0');
break;
default:
BUG("not a valid output format: %d", format);
}
}
strbuf_release(&valbuf);
strbuf_release("buf);
return ret;
}
static int parse_format_cb(const struct option *opt,
const char *arg, int unset UNUSED)
{
enum output_format *format = opt->value;
if (opt->short_name == 'z')
*format = FORMAT_NUL_TERMINATED;
else if (!strcmp(arg, "nul"))
*format = FORMAT_NUL_TERMINATED;
else if (!strcmp(arg, "keyvalue"))
*format = FORMAT_KEYVALUE;
else
die(_("invalid format '%s'"), arg);
return 0;
}
static int cmd_repo_info(int argc, const char **argv, const char *prefix,
struct repository *repo)
{
enum output_format format = FORMAT_KEYVALUE;
struct option options[] = {
OPT_CALLBACK_F(0, "format", &format, N_("format"),
N_("output format"),
PARSE_OPT_NONEG, parse_format_cb),
OPT_CALLBACK_F('z', NULL, &format, NULL,
N_("synonym for --format=nul"),
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
parse_format_cb),
OPT_END()
};
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
return print_fields(argc, argv, repo, format);
}
struct ref_stats {
size_t branches;
size_t remotes;
size_t tags;
size_t others;
};
struct stats_table {
struct string_list rows;
int name_col_width;
int value_col_width;
};
/*
* Holds column data that gets stored for each row.
*/
struct stats_table_entry {
char *value;
};
static void stats_table_vaddf(struct stats_table *table,
struct stats_table_entry *entry,
const char *format, va_list ap)
{
struct strbuf buf = STRBUF_INIT;
struct string_list_item *item;
char *formatted_name;
int name_width;
strbuf_vaddf(&buf, format, ap);
formatted_name = strbuf_detach(&buf, NULL);
name_width = utf8_strwidth(formatted_name);
item = string_list_append_nodup(&table->rows, formatted_name);
item->util = entry;
if (name_width > table->name_col_width)
table->name_col_width = name_width;
if (entry) {
int value_width = utf8_strwidth(entry->value);
if (value_width > table->value_col_width)
table->value_col_width = value_width;
}
}
static void stats_table_addf(struct stats_table *table, const char *format, ...)
{
va_list ap;
va_start(ap, format);
stats_table_vaddf(table, NULL, format, ap);
va_end(ap);
}
static void stats_table_count_addf(struct stats_table *table, size_t value,
const char *format, ...)
{
struct stats_table_entry *entry;
va_list ap;
CALLOC_ARRAY(entry, 1);
entry->value = xstrfmt("%" PRIuMAX, (uintmax_t)value);
va_start(ap, format);
stats_table_vaddf(table, entry, format, ap);
va_end(ap);
}
static inline size_t get_total_reference_count(struct ref_stats *stats)
{
return stats->branches + stats->remotes + stats->tags + stats->others;
}
static void stats_table_setup_structure(struct stats_table *table,
struct ref_stats *refs)
{
size_t ref_total;
ref_total = get_total_reference_count(refs);
stats_table_addf(table, "* %s", _("References"));
stats_table_count_addf(table, ref_total, " * %s", _("Count"));
stats_table_count_addf(table, refs->branches, " * %s", _("Branches"));
stats_table_count_addf(table, refs->tags, " * %s", _("Tags"));
stats_table_count_addf(table, refs->remotes, " * %s", _("Remotes"));
stats_table_count_addf(table, refs->others, " * %s", _("Others"));
}
static void stats_table_print_structure(const struct stats_table *table)
{
const char *name_col_title = _("Repository structure");
const char *value_col_title = _("Value");
int name_col_width = utf8_strwidth(name_col_title);
int value_col_width = utf8_strwidth(value_col_title);
struct string_list_item *item;
if (table->name_col_width > name_col_width)
name_col_width = table->name_col_width;
if (table->value_col_width > value_col_width)
value_col_width = table->value_col_width;
printf("| %-*s | %-*s |\n", name_col_width, name_col_title,
value_col_width, value_col_title);
printf("| ");
for (int i = 0; i < name_col_width; i++)
putchar('-');
printf(" | ");
for (int i = 0; i < value_col_width; i++)
putchar('-');
printf(" |\n");
for_each_string_list_item(item, &table->rows) {
struct stats_table_entry *entry = item->util;
const char *value = "";
if (entry) {
struct stats_table_entry *entry = item->util;
value = entry->value;
}
printf("| %-*s | %*s |\n", name_col_width, item->string,
value_col_width, value);
}
}
static void stats_table_clear(struct stats_table *table)
{
struct stats_table_entry *entry;
struct string_list_item *item;
for_each_string_list_item(item, &table->rows) {
entry = item->util;
if (entry)
free(entry->value);
}
string_list_clear(&table->rows, 1);
}
static int count_references(const char *refname,
const char *referent UNUSED,
const struct object_id *oid UNUSED,
int flags UNUSED, void *cb_data)
{
struct ref_stats *stats = cb_data;
switch (ref_kind_from_refname(refname)) {
case FILTER_REFS_BRANCHES:
stats->branches++;
break;
case FILTER_REFS_REMOTES:
stats->remotes++;
break;
case FILTER_REFS_TAGS:
stats->tags++;
break;
case FILTER_REFS_OTHERS:
stats->others++;
break;
default:
BUG("unexpected reference type");
}
return 0;
}
static void structure_count_references(struct ref_stats *stats,
struct repository *repo)
{
refs_for_each_ref(get_main_ref_store(repo), count_references, &stats);
}
static int cmd_repo_structure(int argc, const char **argv, const char *prefix,
struct repository *repo)
{
struct stats_table table = {
.rows = STRING_LIST_INIT_DUP,
};
struct ref_stats stats = { 0 };
struct option options[] = { 0 };
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
if (argc)
usage(_("too many arguments"));
structure_count_references(&stats, repo);
stats_table_setup_structure(&table, &stats);
stats_table_print_structure(&table);
stats_table_clear(&table);
return 0;
}
int cmd_repo(int argc, const char **argv, const char *prefix,
struct repository *repo)
{
parse_opt_subcommand_fn *fn = NULL;
struct option options[] = {
OPT_SUBCOMMAND("info", &fn, cmd_repo_info),
OPT_SUBCOMMAND("structure", &fn, cmd_repo_structure),
OPT_END()
};
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
return fn(argc, argv, prefix, repo);
}
|