Skip to content

Commit 9eb06c9

Browse files
lucasoshirogitster
authored andcommitted
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 <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Justin Tobler <[email protected]> Helped-by: Eric Sunshine <[email protected]> Mentored-by: Karthik Nayak <[email protected]> Mentored-by: Patrick Steinhardt <[email protected]> Signed-off-by: Lucas Seiki Oshiro <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3166c58 commit 9eb06c9

File tree

4 files changed

+164
-2
lines changed

4 files changed

+164
-2
lines changed

Documentation/git-repo.adoc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ COMMANDS
2222
Retrieve metadata-related information about the current repository. Only
2323
the requested data will be returned based on their keys (see "INFO KEYS"
2424
section below).
25+
+
26+
The returned data is lexicographically sorted by the keys.
27+
+
28+
The output format consists of key-value pairs one per line using the `=`
29+
character as the delimiter between the key and the value. Values containing
30+
"unusual" characters are quoted as explained for the configuration variable
31+
`core.quotePath` (see linkgit:git-config[1]). This is the default.
32+
33+
INFO KEYS
34+
---------
35+
36+
In order to obtain a set of values from `git repo info`, you should provide
37+
the keys that identify them. Here's a list of the available keys and the
38+
values that they return:
39+
40+
`references.format`::
41+
The reference storage format. The valid values are:
42+
+
43+
include::ref-storage-format.adoc[]
2544

2645
SEE ALSO
2746
--------

builtin/repo.c

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,102 @@
11
#include "builtin.h"
22
#include "parse-options.h"
3+
#include "quote.h"
4+
#include "refs.h"
5+
#include "strbuf.h"
36

47
static const char *const repo_usage[] = {
58
"git repo info [<key>...]",
69
NULL
710
};
811

9-
static int repo_info(int argc UNUSED, const char **argv UNUSED,
10-
const char *prefix UNUSED, struct repository *repo UNUSED)
12+
typedef int get_value_fn(struct repository *repo, struct strbuf *buf);
13+
14+
struct field {
15+
const char *key;
16+
get_value_fn *get_value;
17+
};
18+
19+
static int get_references_format(struct repository *repo, struct strbuf *buf)
1120
{
21+
strbuf_addstr(buf,
22+
ref_storage_format_to_name(repo->ref_storage_format));
1223
return 0;
1324
}
1425

26+
/* repo_info_fields keys should be in lexicographical order */
27+
static const struct field repo_info_fields[] = {
28+
{ "references.format", get_references_format },
29+
};
30+
31+
static int repo_info_fields_cmp(const void *va, const void *vb)
32+
{
33+
const struct field *a = va;
34+
const struct field *b = vb;
35+
36+
return strcmp(a->key, b->key);
37+
}
38+
39+
static get_value_fn *get_value_fn_for_key(const char *key)
40+
{
41+
const struct field search_key = { key, NULL };
42+
const struct field *found = bsearch(&search_key, repo_info_fields,
43+
ARRAY_SIZE(repo_info_fields),
44+
sizeof(*found),
45+
repo_info_fields_cmp);
46+
return found ? found->get_value : NULL;
47+
}
48+
49+
static int qsort_strcmp(const void *va, const void *vb)
50+
{
51+
const char *a = *(const char **)va;
52+
const char *b = *(const char **)vb;
53+
54+
return strcmp(a, b);
55+
}
56+
57+
static int print_fields(int argc, const char **argv, struct repository *repo)
58+
{
59+
int ret = 0;
60+
const char *last = "";
61+
struct strbuf valbuf = STRBUF_INIT;
62+
struct strbuf quotbuf = STRBUF_INIT;
63+
64+
QSORT(argv, argc, qsort_strcmp);
65+
66+
for (int i = 0; i < argc; i++) {
67+
get_value_fn *get_value;
68+
const char *key = argv[i];
69+
70+
strbuf_reset(&valbuf);
71+
strbuf_reset(&quotbuf);
72+
73+
if (!strcmp(key, last))
74+
continue;
75+
76+
last = key;
77+
get_value = get_value_fn_for_key(key);
78+
79+
if (!get_value) {
80+
ret = error(_("key '%s' not found"), key);
81+
continue;
82+
}
83+
84+
get_value(repo, &valbuf);
85+
quote_c_style(valbuf.buf, &quotbuf, NULL, 0);
86+
printf("%s=%s\n", key, quotbuf.buf);
87+
}
88+
89+
strbuf_release(&valbuf);
90+
strbuf_release(&quotbuf);
91+
return ret;
92+
}
93+
94+
static int repo_info(int argc, const char **argv, const char *prefix UNUSED,
95+
struct repository *repo)
96+
{
97+
return print_fields(argc - 1, argv + 1, repo);
98+
}
99+
15100
int cmd_repo(int argc, const char **argv, const char *prefix,
16101
struct repository *repo)
17102
{

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ integration_tests = [
246246
't1700-split-index.sh',
247247
't1701-racy-split-index.sh',
248248
't1800-hook.sh',
249+
't1900-repo.sh',
249250
't2000-conflict-when-checking-files-out.sh',
250251
't2002-checkout-cache-u.sh',
251252
't2003-checkout-cache-mkdir.sh',

t/t1900-repo.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/sh
2+
3+
test_description='test git repo-info'
4+
5+
. ./test-lib.sh
6+
7+
# Test whether a key-value pair is correctly returned
8+
#
9+
# Usage: test_repo_info <label> <init command> <key> <expected value>
10+
#
11+
# Arguments:
12+
# label: the label of the test
13+
# init_command: a command which creates a repository
14+
# repo_name: the name of the repository that will be created in init_command
15+
# key: the key of the field that is being tested
16+
# expected_value: the value that the field should contain
17+
test_repo_info () {
18+
label=$1
19+
init_command=$2
20+
repo_name=$3
21+
key=$4
22+
expected_value=$5
23+
24+
test_expect_success "$label" '
25+
eval "$init_command $repo_name" &&
26+
echo "$key=$expected_value" >expected &&
27+
git -C $repo_name repo info "$key" >actual &&
28+
test_cmp expected actual
29+
'
30+
}
31+
32+
test_repo_info 'ref format files is retrieved correctly' '
33+
git init --ref-format=files' 'format-files' 'references.format' 'files'
34+
35+
test_repo_info 'ref format reftable is retrieved correctly' '
36+
git init --ref-format=reftable' 'format-reftable' 'references.format' 'reftable'
37+
38+
test_expect_success 'git-repo-info fails if an invalid key is requested' '
39+
echo "error: key '\'foo\'' not found" >expected_err &&
40+
test_must_fail git repo info foo 2>actual_err &&
41+
test_cmp expected_err actual_err
42+
'
43+
44+
test_expect_success 'git-repo-info outputs data even if there is an invalid field' '
45+
echo "references.format=$(test_detect_ref_format)" >expected &&
46+
test_must_fail git repo info foo references.format bar >actual &&
47+
test_cmp expected actual
48+
'
49+
50+
test_expect_success 'only one value is returned if the same key is requested twice' '
51+
val=$(git rev-parse --show-ref-format) &&
52+
echo "references.format=$val" >expect &&
53+
git repo info references.format references.format >actual &&
54+
test_cmp expect actual
55+
'
56+
57+
test_done

0 commit comments

Comments
 (0)