diff options
| author | Junio C Hamano <gitster@pobox.com> | 2026-02-27 15:11:49 -0800 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2026-02-27 15:11:49 -0800 |
| commit | 341be27dfef8aba66f10be58aec1395075081e85 (patch) | |
| tree | 960e953bd1ab6699b0262d7517d27d0edee3c205 /builtin | |
| parent | 7b2bccb0d58d4f24705bf985de1f4612e4cf06e5 (diff) | |
| parent | 173c43be54f0039a9f6e13afaad3c953bb6b06dc (diff) | |
| download | git-341be27dfef8aba66f10be58aec1395075081e85.tar.xz | |
Merge branch 'lo/repo-info-keys'
"git repo info" learns "--keys" action to list known keys.
* lo/repo-info-keys:
repo: add new flag --keys to git-repo-info
repo: rename the output format "keyvalue" to "lines"
Diffstat (limited to 'builtin')
| -rw-r--r-- | builtin/repo.c | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/builtin/repo.c b/builtin/repo.c index 0ea045abc1..6a62a6020a 100644 --- a/builtin/repo.c +++ b/builtin/repo.c @@ -17,8 +17,9 @@ #include "utf8.h" static const char *const repo_usage[] = { - "git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]", - "git repo structure [--format=(table|keyvalue|nul) | -z]", + "git repo info [--format=(lines|nul) | -z] [--all | <key>...]", + "git repo info --keys [--format=(lines|nul) | -z]", + "git repo structure [--format=(table|lines|nul) | -z]", NULL }; @@ -26,7 +27,7 @@ typedef int get_value_fn(struct repository *repo, struct strbuf *buf); enum output_format { FORMAT_TABLE, - FORMAT_KEYVALUE, + FORMAT_NEWLINE_TERMINATED, FORMAT_NUL_TERMINATED, }; @@ -91,7 +92,7 @@ static void print_field(enum output_format format, const char *key, const char *value) { switch (format) { - case FORMAT_KEYVALUE: + case FORMAT_NEWLINE_TERMINATED: printf("%s=", key); quote_c_style(value, NULL, stdout, 0); putchar('\n'); @@ -148,6 +149,29 @@ static int print_all_fields(struct repository *repo, return 0; } +static int print_keys(enum output_format format) +{ + char sep; + + switch (format) { + case FORMAT_NEWLINE_TERMINATED: + sep = '\n'; + break; + case FORMAT_NUL_TERMINATED: + sep = '\0'; + break; + default: + die(_("--keys can only be used with --format=lines or --format=nul")); + } + + for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) { + const struct field *field = &repo_info_fields[i]; + printf("%s%c", field->key, sep); + } + + return 0; +} + static int parse_format_cb(const struct option *opt, const char *arg, int unset UNUSED) { @@ -157,8 +181,8 @@ static int parse_format_cb(const struct option *opt, *format = FORMAT_NUL_TERMINATED; else if (!strcmp(arg, "nul")) *format = FORMAT_NUL_TERMINATED; - else if (!strcmp(arg, "keyvalue")) - *format = FORMAT_KEYVALUE; + else if (!strcmp(arg, "lines")) + *format = FORMAT_NEWLINE_TERMINATED; else if (!strcmp(arg, "table")) *format = FORMAT_TABLE; else @@ -170,8 +194,9 @@ static int parse_format_cb(const struct option *opt, static int cmd_repo_info(int argc, const char **argv, const char *prefix, struct repository *repo) { - enum output_format format = FORMAT_KEYVALUE; + enum output_format format = FORMAT_NEWLINE_TERMINATED; int all_keys = 0; + int show_keys = 0; struct option options[] = { OPT_CALLBACK_F(0, "format", &format, N_("format"), N_("output format"), @@ -181,11 +206,19 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix, PARSE_OPT_NONEG | PARSE_OPT_NOARG, parse_format_cb), OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")), + OPT_BOOL(0, "keys", &show_keys, N_("show keys")), OPT_END() }; argc = parse_options(argc, argv, prefix, options, repo_usage, 0); - if (format != FORMAT_KEYVALUE && format != FORMAT_NUL_TERMINATED) + + if (show_keys && (all_keys || argc)) + die(_("--keys cannot be used with a <key> or --all")); + + if (show_keys) + return print_keys(format); + + if (format != FORMAT_NEWLINE_TERMINATED && format != FORMAT_NUL_TERMINATED) die(_("unsupported output format")); if (all_keys && argc) @@ -671,7 +704,7 @@ static int cmd_repo_structure(int argc, const char **argv, const char *prefix, stats_table_setup_structure(&table, &stats); stats_table_print_structure(&table); break; - case FORMAT_KEYVALUE: + case FORMAT_NEWLINE_TERMINATED: structure_keyvalue_print(&stats, '=', '\n'); break; case FORMAT_NUL_TERMINATED: |
