]> git.ipfire.org Git - thirdparty/git.git/commitdiff
repo: add the field references.format
authorLucas Seiki Oshiro <lucasseikioshiro@gmail.com>
Sat, 16 Aug 2025 22:46:00 +0000 (19:46 -0300)
committerJunio C Hamano <gitster@pobox.com>
Sun, 17 Aug 2025 16:13:40 +0000 (09:13 -0700)
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>
Documentation/git-repo.adoc
builtin/repo.c
t/meson.build
t/t1900-repo.sh [new file with mode: 0755]

index 68c706f5a0dc02b6eec4942ccaf18635e77f989c..2779a6d995ca2308d5cb172bb5fa40183164f020 100644 (file)
@@ -22,6 +22,26 @@ COMMANDS
        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 values are returned in the same order in which their respective keys were
+requested.
++
+The output format consists of key-value pairs one per line using the `=`
+character as the delimiter between the key and the value. Values containing
+"unusual" characters are quoted as explained for the configuration variable
+`core.quotePath` (see linkgit:git-config[1]).
+
+INFO KEYS
+---------
+
+In order to obtain a set of values from `git repo info`, you should provide
+the keys that identify them. Here's a list of the available keys and the
+values that they return:
+
+`references.format`::
+       The reference storage format. The valid values are:
++
+include::ref-storage-format.adoc[]
 
 SEE ALSO
 --------
index fd2a9b42167d281b487894529714e5d54572f0f4..73d4e27a1686b3c4161de14516bf8973c4814551 100644 (file)
@@ -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)
 {
index d052fc3e23d2ec78199d987d21394148ff0f890b..9773130feb92f99f5caabf6ab07b48be4e4cc7f0 100644 (file)
@@ -246,6 +246,7 @@ integration_tests = [
   't1700-split-index.sh',
   't1701-racy-split-index.sh',
   't1800-hook.sh',
+  't1900-repo.sh',
   't2000-conflict-when-checking-files-out.sh',
   't2002-checkout-cache-u.sh',
   't2003-checkout-cache-mkdir.sh',
diff --git a/t/t1900-repo.sh b/t/t1900-repo.sh
new file mode 100755 (executable)
index 0000000..be8a4b2
--- /dev/null
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+test_description='test git repo-info'
+
+. ./test-lib.sh
+
+# Test whether a key-value pair is correctly returned
+#
+# Usage: test_repo_info <label> <init command> <repo_name> <key> <expected value>
+#
+# Arguments:
+#   label: the label of the test
+#   init_command: a command which creates a repository
+#   repo_name: the name of the repository that will be created in init_command
+#   key: the key of the field that is being tested
+#   expected_value: the value that the field should contain
+test_repo_info () {
+       label=$1
+       init_command=$2
+       repo_name=$3
+       key=$4
+       expected_value=$5
+
+       test_expect_success "setup: $label" '
+               eval "$init_command $repo_name"
+       '
+
+       test_expect_success "$label" '
+               echo "$key=$expected_value" >expect &&
+               git -C $repo_name repo info "$key" >actual &&
+               test_cmp expect actual
+       '
+}
+
+test_repo_info 'ref format files is retrieved correctly' \
+       'git init --ref-format=files' 'format-files' 'references.format' 'files'
+
+test_repo_info 'ref format reftable is retrieved correctly' \
+       'git init --ref-format=reftable' 'format-reftable' 'references.format' 'reftable'
+
+test_expect_success 'git-repo-info fails if an invalid key is requested' '
+       echo "error: key ${SQ}foo${SQ} not found" >expect &&
+       test_must_fail git repo info foo 2>actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'git-repo-info outputs data even if there is an invalid field' '
+       echo "references.format=$(test_detect_ref_format)" >expect &&
+       test_must_fail git repo info foo references.format bar >actual &&
+       test_cmp expect actual
+'
+
+test_done