Skip to content

Commit 9adb8a7

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 ab94bb8 commit 9adb8a7

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
lines changed

Documentation/git-repo.adoc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ 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 values are returned in the same order in which their respective keys were
27+
requested.
28+
+
29+
The output format consists of key-value pairs one per line using the `=`
30+
character as the delimiter between the key and the value. Values containing
31+
"unusual" characters are quoted as explained for the configuration variable
32+
`core.quotePath` (see linkgit:git-config[1]).
33+
34+
INFO KEYS
35+
---------
36+
37+
In order to obtain a set of values from `git repo info`, you should provide
38+
the keys that identify them. Here's a list of the available keys and the
39+
values that they return:
40+
41+
`references.format`::
42+
The reference storage format. The valid values are:
43+
+
44+
include::ref-storage-format.adoc[]
2545

2646
SEE ALSO
2747
--------

builtin/repo.c

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,87 @@
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 must 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 print_fields(int argc, const char **argv, struct repository *repo)
50+
{
51+
int ret = 0;
52+
struct strbuf valbuf = STRBUF_INIT;
53+
struct strbuf quotbuf = STRBUF_INIT;
54+
55+
for (int i = 0; i < argc; i++) {
56+
get_value_fn *get_value;
57+
const char *key = argv[i];
58+
59+
get_value = get_value_fn_for_key(key);
60+
61+
if (!get_value) {
62+
ret = error(_("key '%s' not found"), key);
63+
continue;
64+
}
65+
66+
strbuf_reset(&valbuf);
67+
strbuf_reset(&quotbuf);
68+
69+
get_value(repo, &valbuf);
70+
quote_c_style(valbuf.buf, &quotbuf, NULL, 0);
71+
printf("%s=%s\n", key, quotbuf.buf);
72+
}
73+
74+
strbuf_release(&valbuf);
75+
strbuf_release(&quotbuf);
76+
return ret;
77+
}
78+
79+
static int repo_info(int argc, const char **argv, const char *prefix UNUSED,
80+
struct repository *repo)
81+
{
82+
return print_fields(argc - 1, argv + 1, repo);
83+
}
84+
1585
int cmd_repo(int argc, const char **argv, const char *prefix,
1686
struct repository *repo)
1787
{

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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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> <repo_name> <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 "setup: $label" '
25+
eval "$init_command $repo_name"
26+
'
27+
28+
test_expect_success "$label" '
29+
echo "$key=$expected_value" >expect &&
30+
git -C $repo_name repo info "$key" >actual &&
31+
test_cmp expect actual
32+
'
33+
}
34+
35+
test_repo_info 'ref format files is retrieved correctly' \
36+
'git init --ref-format=files' 'format-files' 'references.format' 'files'
37+
38+
test_repo_info 'ref format reftable is retrieved correctly' \
39+
'git init --ref-format=reftable' 'format-reftable' 'references.format' 'reftable'
40+
41+
test_expect_success 'git-repo-info fails if an invalid key is requested' '
42+
echo "error: key ${SQ}foo${SQ} not found" >expect &&
43+
test_must_fail git repo info foo 2>actual &&
44+
test_cmp expect actual
45+
'
46+
47+
test_expect_success 'git-repo-info outputs data even if there is an invalid field' '
48+
echo "references.format=$(test_detect_ref_format)" >expect &&
49+
test_must_fail git repo info foo references.format bar >actual &&
50+
test_cmp expect actual
51+
'
52+
53+
test_done

0 commit comments

Comments
 (0)