Re: [GSoC RFC PATCH v3 0/5] repo-info: add new command for retrieving repository info
From: Phillip Wood <hidden>
Date: 2025-07-08 10:12:01
Hi Lucas On 07/07/2025 00:19, Lucas Seiki Oshiro wrote:
Hi! This is the third version of the patchset introducing the new Git command called `repo-info`, which will return repository information currently stored under git-rev-parse using machine-readable formats. The main changes since v2 are: - Documentation for git-repo-info has been added. - The `--allow-empty` flag was removed. In this version, repo-info no longer returns a default set of data when called without parameters. However, an `--all` flag is planned for a future version. - The `plaintext` format was replaced by a null-terminated format, following the syntax of `git config --list -z`.
These all look like good changes. Looking through this series I do think
that it would be more readable and maintainable if you adopted the table
drive approach suggested by Junio. That way we would avoid the nested
switch statements and each piece of information only needs to be
retrieved once rather that having to retrieve it separately for the JSON
and plaintext output. Below is a sketch of might look. Each key is
handled by a callback and we have a table that maps key names to
callbacks. For the json output we use one json writer per category to
build output for each category.
Best Wishes
Phillip
enum format {
FORMAT_JSON,
FORMAT_PLAINTEXT,
};
enum category {
CATEGORY_LAYOUT,
};
static const char *category_name[] = {
[CATEGORY_LAYOUT] = "layout",
}
struct context {
struct json_writer *writer[ARRAY_SIZE(category_name)];
enum format format;
};
static struct ensure_writer(struct context ctx, enum category cat)
{
struct json_writer *writer = ctx.writer[cat];
if (!writer) {
writer = xmalloc(sizeof(*writer));
jw_init(writer);
jw_object_begin(writer, 1);
ctx->writer[cat] = writer;
}
return writer;
}
static void handle_layout_bare(struct context ctx)
{
int bare = ...;
if (ctx->format == FORMAT_JSON) {
struct json_writer *writer =
ensure_writer(ctx, CATEGORY_LAYOUT);
jw_object_bool(writer, "layout.bare", bare);
} else {
printf("%s\n%s\c", "layout.bare", bare ? "true", "false", '\0');
}
}
static int cmd_repo_info(int argc, const char **argv, sturct repo *r)
{
struct context = { 0 };
struct {
const char *key;
void (*cb)(struct *ctx);
} handler = {
{ "layout.bare", handle_layout_bare, },
};
/* parse options */
for (int i = 0; i < argc; i++) {
/* TODO use bsearch()? */
for (size_t j = 0; j < ARRAY_SIZE(handler); j++) {
if (!strcmp(argv[i], handler[j].key) {
handler[j].cb(&ctx);
break;
}
}
}
if (ctx->format == JSON) {
struct json_writer writer = JSON_WRITER_INIT;
jw_object_begin(&writer, 1);
for (size_t i = 0; i < ARRAY_SIZE(category); i++) {
if (ctx.writer[i]) {
jw_object_end(ctx.writer[i]);
jw_object_sub_obj(&writer,
category_name[i],
ctx.writer[i]);
}
}
jw_object_end(&writer);
fputs(writer.buf.buf);
}
context_release(&ctx);
return 0;
}