Re: [GSoC PATCH v5 5/5] repo: add the --format flag
From: Patrick Steinhardt <hidden>
Date: 2025-07-24 06:22:20
On Mon, Jul 21, 2025 at 09:28:35PM -0300, Lucas Seiki Oshiro wrote:
quoted hunk ↗ jump to hunk
diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc index 375b956d3f..5bdc3831a7 100644 --- a/Documentation/git-repo.adoc +++ b/Documentation/git-repo.adoc@@ -18,10 +18,21 @@ THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE. COMMANDS -------- -info [<key>...]:: +info [--format=<format>] [<key>...]:: Retrieve metadata-related information about the current repository. Only the requested data will be returned based on their keys (see "INFO KEYS" section below). ++ +The output format can be chosen through the flag `--format`. Two formats are +supported: ++ +* `keyvalue`: output key-value pairs one per line using the `=` character as +the delimiter between the key and the value. This is the default. + +* `null`: similar to `keyvalue`, but using a newline character as the delimiter
Our manual typically says "NUL"-terminated format in other places, like for example in git-update-ref(1). So let's rather name the format `nul`.
quoted hunk ↗ jump to hunk
diff --git a/builtin/repo.c b/builtin/repo.c index 490fa9dd49..10d02bb3ea 100644 --- a/builtin/repo.c +++ b/builtin/repo.c@@ -8,6 +8,11 @@ typedef const char *get_value_fn(struct repository *repo); +enum output_format { + FORMAT_KEYVALUE, + FORMAT_NULL_TERMINATED,
Likewise, this should be `FORMAT_NUL_TERMINATED`.
quoted hunk ↗ jump to hunk
@@ -61,9 +66,24 @@ static int qsort_strcmp(const void *va, const void *vb) return strcmp(a, b); } -static int print_fields(int argc, const char **argv, struct repository *repo) +static int print_fields(int argc, const char **argv, + struct repository *repo, + enum output_format format) { const char *last = ""; + char kv_sep; + char field_sep; + + switch (format) { + case FORMAT_KEYVALUE: + kv_sep = '='; + field_sep = '\n'; + break; + case FORMAT_NULL_TERMINATED: + kv_sep = '\n'; + field_sep = '\0'; + break; + } QSORT(argv, argc, qsort_strcmp);
Makes sense.
quoted hunk ↗ jump to hunk
@@ -81,17 +101,38 @@ static int print_fields(int argc, const char **argv, struct repository *repo) return error("key %s not found", key); value = callback(repo); - printf("%s=%s\n", key, value); + printf("%s%c%s%c", key, kv_sep, value, field_sep); last = key;
This here is where we can introduce the quoting. Something like:
if (format == FORMAT_KEYVALUE) {
strbuf_reset("ed_value);
quote_c_style(value, "ed_value, NULL, 0);
}
printf("%s%c%s%c", key, kv_sep,
format == FORMAT_KEYVALUE ? quoted_value.buf : value,
field_sep);
}
return 0;
}
-static int repo_info(int argc, const char **argv, const char *prefix UNUSED,
+static int repo_info(int argc, const char **argv, const char *prefix,
struct repository *repo)
{
- return print_fields(argc - 1, argv + 1, repo);
+ const char *format_str = "keyvalue";
+ enum output_format format;
+ const char *const repo_info_usage[] = {
+ "git repo info [<key>...]",
+ NULL
+ };
+ struct option options[] = {
+ OPT_STRING(0, "format", &format_str, N_("format"),
+ N_("output format")),
+ OPT_END()
+ };
+
+ argc = parse_options(argc, argv, prefix, options, repo_info_usage, 0);
+
+ if (!strcmp(format_str, "keyvalue"))
+ format = FORMAT_KEYVALUE;
+ else if (!strcmp(format_str, "null"))
+ format = FORMAT_NULL_TERMINATED;
+ else
+ die("invalid format %s", format_str);
Nit: this should be translated and quote the format string:
die(_("invalid format: '%s'"), format_str);
Patrick