Thread (178 messages) 178 messages, 10 authors, 2025-08-16

Re: [GSoC PATCH v5 2/5] repo: add the field references.format

From: Karthik Nayak <hidden>
Date: 2025-07-22 09:17:02

Lucas Seiki Oshiro [off-list ref] writes:
This commit is part of the series that introduces the new subcommand
git-repo-info.

The flag `--show-ref-format` from git-rev-parse is used for retrieving
the reference format (i.e. `files` or `reftable`). This way, it is
used for querying repository metadata, fitting in the purpose of
git-repo-info.

Then, add a new field `references.format` to the repo-info subcommand
containing that information.
Nit: I don't think we need the 'Then, ' here, perhaps 'Add ...'.
quoted hunk ↗ jump to hunk
Helped-by: Phillip Wood [off-list ref]
Helped-by: Junio C Hamano [off-list ref]
Helped-by: Justin Tobler [off-list ref]
Mentored-by: Karthik Nayak [off-list ref]
Mentored-by: Patrick Steinhardt [off-list ref]
Signed-off-by: Lucas Seiki Oshiro <redacted>
---
 Documentation/git-repo.adoc |  4 ++
 builtin/repo.c              | 75 ++++++++++++++++++++++++++++++++++++-
 t/meson.build               |  1 +
 t/t1900-repo.sh             | 50 +++++++++++++++++++++++++
 4 files changed, 128 insertions(+), 2 deletions(-)
 create mode 100755 t/t1900-repo.sh
diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc
index caee7d8aef..cf8483ec49 100644
--- a/Documentation/git-repo.adoc
+++ b/Documentation/git-repo.adoc
@@ -29,6 +29,10 @@ INFO KEYS
 The set of data that `git repo` can return is grouped into the following
 categories:

+`references`::
+Reference-related data:
+* `format`: the reference storage format
+
 SEE ALSO
 --------
 linkgit:git-rev-parse[1]
diff --git a/builtin/repo.c b/builtin/repo.c
index d4f01e35e2..5beae0f781 100644
--- a/builtin/repo.c
+++ b/builtin/repo.c
@@ -1,12 +1,83 @@
 #include "builtin.h"
 #include "parse-options.h"
+#include "refs.h"

-static int repo_info(int argc UNUSED, const char **argv UNUSED,
-		     const char *prefix UNUSED, struct repository *repo UNUSED)
+typedef const char *get_value_fn(struct repository *repo);
+
+struct field {
+	const char *key;
+	get_value_fn *add_field_callback;
+};
+
Shouldn't 'add_field_callback' be renamed, now that we don't add a field
but rather return a value?
+static const char *get_references_format(struct repository *repo)
+{
+	return ref_storage_format_to_name(repo->ref_storage_format);
+}
+
+/* repo_info_fields keys should be in lexicographical order */
+static const struct field repo_info_fields[] = {
+	{ "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_callback(const char *key)
 {
Nit: A callback generally is a function provided by when a 'fn A' calls
'fn B', providing a 'fn C' which 'fn A' provides.

Here perhaps we can simply rename this to 'get_value_fn_for_key' or
something?
+	const struct field search_key = { key, NULL };
+	const struct field *found = bsearch(&search_key, repo_info_fields,
+					    ARRAY_SIZE(repo_info_fields),
+					    sizeof(struct field),
+					    repo_info_fields_cmp);
+	return found ? found->add_field_callback : NULL;
+}
+
+static int qsort_strcmp(const void *va, const void *vb)
+{
+	const char *a = *(const char **)va;
+	const char *b = *(const char **)vb;
+
+	return strcmp(a, b);
+}
+
+static int print_fields(int argc, const char **argv, struct repository *repo)
+{
+	const char *last = "";
+
+	QSORT(argv, argc, qsort_strcmp);
+
+	for (int i = 0; i < argc; i++) {
+		get_value_fn *callback;
+		const char *key = argv[i];
+		const char *value;
+
+		if (!strcmp(key, last))
+			continue;
+
+		callback = get_value_callback(key);
+
+		if (!callback)
+			return error("key %s not found", key);
+
+		value = callback(repo);
+		printf("%s=%s\n", key, value);
I like this a lot, since we can simply modify this in the future for
different formats. Nice!

[snip]

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help