summaryrefslogtreecommitdiff
path: root/builtin/repo.c
diff options
context:
space:
mode:
authorLucas Seiki Oshiro <lucasseikioshiro@gmail.com>2025-08-16 19:46:00 -0300
committerJunio C Hamano <gitster@pobox.com>2025-08-17 09:13:40 -0700
commit9adb8a7fd132f6033db1f04f17f0687bf2ac84e2 (patch)
treea6d7bc8a95829dfbd27dcaebd04577d2cc775d2b /builtin/repo.c
parentab94bb80002a85b31124f9ece8ba3843f93f063c (diff)
repo: add the field references.format
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. Add a new field `references.format` to the repo-info subcommand containing that information. Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk> Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Justin Tobler <jltobler@gmail.com> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Mentored-by: Karthik Nayak <karthik.188@gmail.com> Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/repo.c')
-rw-r--r--builtin/repo.c74
1 files changed, 72 insertions, 2 deletions
diff --git a/builtin/repo.c b/builtin/repo.c
index fd2a9b4216..73d4e27a16 100644
--- a/builtin/repo.c
+++ b/builtin/repo.c
@@ -1,17 +1,87 @@
#include "builtin.h"
#include "parse-options.h"
+#include "quote.h"
+#include "refs.h"
+#include "strbuf.h"
static const char *const repo_usage[] = {
"git repo info [<key>...]",
NULL
};
-static int repo_info(int argc UNUSED, const char **argv UNUSED,
- const char *prefix UNUSED, struct repository *repo UNUSED)
+typedef int get_value_fn(struct repository *repo, struct strbuf *buf);
+
+struct field {
+ const char *key;
+ get_value_fn *get_value;
+};
+
+static int get_references_format(struct repository *repo, struct strbuf *buf)
{
+ strbuf_addstr(buf,
+ ref_storage_format_to_name(repo->ref_storage_format));
return 0;
}
+/* repo_info_fields keys must 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_fn_for_key(const char *key)
+{
+ const struct field search_key = { key, NULL };
+ const struct field *found = bsearch(&search_key, repo_info_fields,
+ ARRAY_SIZE(repo_info_fields),
+ sizeof(*found),
+ repo_info_fields_cmp);
+ return found ? found->get_value : NULL;
+}
+
+static int print_fields(int argc, const char **argv, struct repository *repo)
+{
+ 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;
+ const char *key = argv[i];
+
+ get_value = get_value_fn_for_key(key);
+
+ if (!get_value) {
+ ret = error(_("key '%s' not found"), key);
+ continue;
+ }
+
+ strbuf_reset(&valbuf);
+ strbuf_reset(&quotbuf);
+
+ get_value(repo, &valbuf);
+ quote_c_style(valbuf.buf, &quotbuf, NULL, 0);
+ printf("%s=%s\n", key, quotbuf.buf);
+ }
+
+ strbuf_release(&valbuf);
+ strbuf_release(&quotbuf);
+ return ret;
+}
+
+static int repo_info(int argc, const char **argv, const char *prefix UNUSED,
+ struct repository *repo)
+{
+ return print_fields(argc - 1, argv + 1, repo);
+}
+
int cmd_repo(int argc, const char **argv, const char *prefix,
struct repository *repo)
{