aboutsummaryrefslogtreecommitdiffstats
path: root/builtin/repo.c
diff options
context:
space:
mode:
Diffstat (limited to 'builtin/repo.c')
-rw-r--r--builtin/repo.c99
1 files changed, 70 insertions, 29 deletions
diff --git a/builtin/repo.c b/builtin/repo.c
index 9d4749f79b..0dd41b1778 100644
--- a/builtin/repo.c
+++ b/builtin/repo.c
@@ -15,8 +15,8 @@
#include "utf8.h"
static const char *const repo_usage[] = {
- "git repo info [--format=(keyvalue|nul)] [-z] [<key>...]",
- "git repo structure [--format=(table|keyvalue|nul)]",
+ "git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]",
+ "git repo structure [--format=(table|keyvalue|nul) | -z]",
NULL
};
@@ -85,13 +85,29 @@ static get_value_fn *get_value_fn_for_key(const char *key)
return found ? found->get_value : NULL;
}
+static void print_field(enum output_format format, const char *key,
+ const char *value)
+{
+ switch (format) {
+ case FORMAT_KEYVALUE:
+ printf("%s=", key);
+ quote_c_style(value, NULL, stdout, 0);
+ putchar('\n');
+ break;
+ case FORMAT_NUL_TERMINATED:
+ printf("%s\n%s%c", key, value, '\0');
+ break;
+ default:
+ BUG("not a valid output format: %d", format);
+ }
+}
+
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;
@@ -105,28 +121,31 @@ static int print_fields(int argc, const char **argv,
}
strbuf_reset(&valbuf);
- strbuf_reset(&quotbuf);
-
get_value(repo, &valbuf);
-
- switch (format) {
- case FORMAT_KEYVALUE:
- quote_c_style(valbuf.buf, &quotbuf, 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);
- }
+ print_field(format, key, valbuf.buf);
}
strbuf_release(&valbuf);
- strbuf_release(&quotbuf);
return ret;
}
+static int print_all_fields(struct repository *repo,
+ enum output_format format)
+{
+ struct strbuf valbuf = STRBUF_INIT;
+
+ for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
+ const struct field *field = &repo_info_fields[i];
+
+ strbuf_reset(&valbuf);
+ field->get_value(repo, &valbuf);
+ print_field(format, field->key, valbuf.buf);
+ }
+
+ strbuf_release(&valbuf);
+ return 0;
+}
+
static int parse_format_cb(const struct option *opt,
const char *arg, int unset UNUSED)
{
@@ -150,6 +169,7 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
struct repository *repo)
{
enum output_format format = FORMAT_KEYVALUE;
+ int all_keys = 0;
struct option options[] = {
OPT_CALLBACK_F(0, "format", &format, N_("format"),
N_("output format"),
@@ -158,6 +178,7 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
N_("synonym for --format=nul"),
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
parse_format_cb),
+ OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")),
OPT_END()
};
@@ -165,7 +186,13 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix,
if (format != FORMAT_KEYVALUE && format != FORMAT_NUL_TERMINATED)
die(_("unsupported output format"));
- return print_fields(argc, argv, repo, format);
+ if (all_keys && argc)
+ die(_("--all and <key> cannot be used together"));
+
+ if (all_keys)
+ return print_all_fields(repo, format);
+ else
+ return print_fields(argc, argv, repo, format);
}
struct ref_stats {
@@ -292,14 +319,20 @@ static void stats_table_print_structure(const struct stats_table *table)
int name_col_width = utf8_strwidth(name_col_title);
int value_col_width = utf8_strwidth(value_col_title);
struct string_list_item *item;
+ struct strbuf buf = STRBUF_INIT;
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);
+ strbuf_addstr(&buf, "| ");
+ strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width, name_col_title);
+ strbuf_addstr(&buf, " | ");
+ strbuf_utf8_align(&buf, ALIGN_LEFT, value_col_width, value_col_title);
+ strbuf_addstr(&buf, " |");
+ printf("%s\n", buf.buf);
+
printf("| ");
for (int i = 0; i < name_col_width; i++)
putchar('-');
@@ -317,9 +350,16 @@ static void stats_table_print_structure(const struct stats_table *table)
value = entry->value;
}
- printf("| %-*s | %*s |\n", name_col_width, item->string,
- value_col_width, value);
+ strbuf_reset(&buf);
+ strbuf_addstr(&buf, "| ");
+ strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width, item->string);
+ strbuf_addstr(&buf, " | ");
+ strbuf_utf8_align(&buf, ALIGN_RIGHT, value_col_width, value);
+ strbuf_addstr(&buf, " |");
+ printf("%s\n", buf.buf);
}
+
+ strbuf_release(&buf);
}
static void stats_table_clear(struct stats_table *table)
@@ -366,16 +406,13 @@ struct count_references_data {
struct progress *progress;
};
-static int count_references(const char *refname,
- const char *referent UNUSED,
- const struct object_id *oid,
- int flags UNUSED, void *cb_data)
+static int count_references(const struct reference *ref, void *cb_data)
{
struct count_references_data *data = cb_data;
struct ref_stats *stats = data->stats;
size_t ref_count;
- switch (ref_kind_from_refname(refname)) {
+ switch (ref_kind_from_refname(ref->name)) {
case FILTER_REFS_BRANCHES:
stats->branches++;
break;
@@ -396,7 +433,7 @@ static int count_references(const char *refname,
* While iterating through references for counting, also add OIDs in
* preparation for the path walk.
*/
- add_pending_oid(data->revs, NULL, oid, 0);
+ add_pending_oid(data->revs, NULL, ref->oid, 0);
ref_count = get_total_reference_count(stats);
display_progress(data->progress, ref_count);
@@ -492,6 +529,10 @@ static int cmd_repo_structure(int argc, const char **argv, const char *prefix,
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_BOOL(0, "progress", &show_progress, N_("show progress")),
OPT_END()
};