Skip to content

Commit cdc6227

Browse files
committed
Merge branch 'lo/repo-info' into seen
* lo/repo-info: repo: add the --format flag repo: add the field layout.shallow repo: add the field layout.bare repo: add the field references.format repo: declare the repo command
2 parents df452c6 + 9741b7c commit cdc6227

File tree

11 files changed

+356
-0
lines changed

11 files changed

+356
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
/git-repack
141141
/git-replace
142142
/git-replay
143+
/git-repo
143144
/git-request-pull
144145
/git-rerere
145146
/git-reset

Documentation/git-repo.adoc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
git-repo(1)
2+
===========
3+
4+
NAME
5+
----
6+
git-repo - Retrieve information about the repository
7+
8+
SYNOPSIS
9+
--------
10+
[synopsis]
11+
git repo info [--format=(keyvalue|nul)] [<key>...]
12+
13+
DESCRIPTION
14+
-----------
15+
Retrieve information about the repository.
16+
17+
THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
18+
19+
COMMANDS
20+
--------
21+
`info [--format=(keyvalue|nul)] [<key>...]`::
22+
Retrieve metadata-related information about the current repository. Only
23+
the requested data will be returned based on their keys (see "INFO KEYS"
24+
section below).
25+
+
26+
The returned data is lexicographically sorted by the keys.
27+
+
28+
The output format can be chosen through the flag `--format`. Two formats are
29+
supported:
30+
+
31+
* `keyvalue`: output key-value pairs one per line using the `=` character as
32+
the delimiter between the key and the value. Values containing "unusual"
33+
characters are quoted as explained for the configuration variable
34+
`core.quotePath` (see linkgit:git-config[1]). This is the default.
35+
36+
* `nul`: similar to `keyvalue`, but using a newline character as the delimiter
37+
between the key and the value and using a null character after each value.
38+
This format is better suited for being parsed by another applications than
39+
`keyvalue`. Unlike in the `keyvalue` format, the values are never quoted.
40+
41+
INFO KEYS
42+
---------
43+
In order to obtain a set of values from `git repo info`, you should provide
44+
the keys that identify them. Here's a list of the available keys and the
45+
values that they return:
46+
47+
`layout.bare`::
48+
`true` if this is a bare repository, otherwise `false`.
49+
50+
`layout.shallow`::
51+
`true` if this is a shallow repository, otherwise `false`.
52+
53+
`references.format`::
54+
The reference storage format. The valid values are:
55+
+
56+
include::ref-storage-format.adoc[]
57+
58+
EXAMPLES
59+
--------
60+
61+
* Retrieves the reference format of the current repository:
62+
+
63+
------------
64+
git repo info references.format
65+
------------
66+
+
67+
68+
* Retrieves whether the current repository is bare and whether it is shallow
69+
using the `nul` format:
70+
+
71+
------------
72+
git repo info --format=nul layout.bare layout.shallow
73+
------------
74+
75+
SEE ALSO
76+
--------
77+
linkgit:git-rev-parse[1]
78+
79+
GIT
80+
---
81+
Part of the linkgit:git[1] suite

Documentation/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ manpages = {
117117
'git-repack.adoc' : 1,
118118
'git-replace.adoc' : 1,
119119
'git-replay.adoc' : 1,
120+
'git-repo.adoc' : 1,
120121
'git-request-pull.adoc' : 1,
121122
'git-rerere.adoc' : 1,
122123
'git-reset.adoc' : 1,

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,7 @@ BUILTIN_OBJS += builtin/remote.o
13071307
BUILTIN_OBJS += builtin/repack.o
13081308
BUILTIN_OBJS += builtin/replace.o
13091309
BUILTIN_OBJS += builtin/replay.o
1310+
BUILTIN_OBJS += builtin/repo.o
13101311
BUILTIN_OBJS += builtin/rerere.o
13111312
BUILTIN_OBJS += builtin/reset.o
13121313
BUILTIN_OBJS += builtin/rev-list.o

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ int cmd_remote_ext(int argc, const char **argv, const char *prefix, struct repos
217217
int cmd_remote_fd(int argc, const char **argv, const char *prefix, struct repository *repo);
218218
int cmd_repack(int argc, const char **argv, const char *prefix, struct repository *repo);
219219
int cmd_replay(int argc, const char **argv, const char *prefix, struct repository *repo);
220+
int cmd_repo(int argc, const char **argv, const char *prefix, struct repository *repo);
220221
int cmd_rerere(int argc, const char **argv, const char *prefix, struct repository *repo);
221222
int cmd_reset(int argc, const char **argv, const char *prefix, struct repository *repo);
222223
int cmd_restore(int argc, const char **argv, const char *prefix, struct repository *repo);

builtin/repo.c

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
3+
#include "builtin.h"
4+
#include "environment.h"
5+
#include "parse-options.h"
6+
#include "quote.h"
7+
#include "refs.h"
8+
#include "strbuf.h"
9+
#include "shallow.h"
10+
11+
static const char *const repo_usage[] = {
12+
"git repo info [--format=(keyvalue|nul)] [<key>...]",
13+
NULL
14+
};
15+
16+
typedef int get_value_fn(struct repository *repo, struct strbuf *buf);
17+
18+
enum output_format {
19+
FORMAT_KEYVALUE,
20+
FORMAT_NUL_TERMINATED,
21+
};
22+
23+
struct field {
24+
const char *key;
25+
get_value_fn *get_value;
26+
};
27+
28+
static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf)
29+
{
30+
strbuf_addstr(buf, is_bare_repository() ? "true" : "false");
31+
return 0;
32+
}
33+
34+
static int get_layout_shallow(struct repository *repo, struct strbuf *buf)
35+
{
36+
strbuf_addstr(buf,
37+
is_repository_shallow(repo) ? "true" : "false");
38+
return 0;
39+
}
40+
41+
static int get_references_format(struct repository *repo, struct strbuf *buf)
42+
{
43+
strbuf_addstr(buf,
44+
ref_storage_format_to_name(repo->ref_storage_format));
45+
return 0;
46+
}
47+
48+
/* repo_info_fields keys should be in lexicographical order */
49+
static const struct field repo_info_fields[] = {
50+
{ "layout.bare", get_layout_bare },
51+
{ "layout.shallow", get_layout_shallow },
52+
{ "references.format", get_references_format },
53+
};
54+
55+
static int repo_info_fields_cmp(const void *va, const void *vb)
56+
{
57+
const struct field *a = va;
58+
const struct field *b = vb;
59+
60+
return strcmp(a->key, b->key);
61+
}
62+
63+
static get_value_fn *get_value_fn_for_key(const char *key)
64+
{
65+
const struct field search_key = { key, NULL };
66+
const struct field *found = bsearch(&search_key, repo_info_fields,
67+
ARRAY_SIZE(repo_info_fields),
68+
sizeof(*found),
69+
repo_info_fields_cmp);
70+
return found ? found->get_value : NULL;
71+
}
72+
73+
static int qsort_strcmp(const void *va, const void *vb)
74+
{
75+
const char *a = *(const char **)va;
76+
const char *b = *(const char **)vb;
77+
78+
return strcmp(a, b);
79+
}
80+
81+
static int print_fields(int argc, const char **argv,
82+
struct repository *repo,
83+
enum output_format format)
84+
{
85+
int ret = 0;
86+
const char *last = "";
87+
struct strbuf valbuf = STRBUF_INIT;
88+
struct strbuf quotbuf = STRBUF_INIT;
89+
90+
QSORT(argv, argc, qsort_strcmp);
91+
92+
for (int i = 0; i < argc; i++) {
93+
get_value_fn *get_value;
94+
const char *key = argv[i];
95+
96+
strbuf_reset(&valbuf);
97+
strbuf_reset(&quotbuf);
98+
99+
if (!strcmp(key, last))
100+
continue;
101+
102+
last = key;
103+
get_value = get_value_fn_for_key(key);
104+
105+
if (!get_value) {
106+
ret = error(_("key '%s' not found"), key);
107+
continue;
108+
}
109+
110+
get_value(repo, &valbuf);
111+
112+
switch (format) {
113+
case FORMAT_KEYVALUE:
114+
quote_c_style(valbuf.buf, &quotbuf, NULL, 0);
115+
printf("%s=%s\n", key, quotbuf.buf);
116+
break;
117+
case FORMAT_NUL_TERMINATED:
118+
printf("%s\n%s%c", key, valbuf.buf, '\0');
119+
break;
120+
default:
121+
BUG("%d: not a valid output format", format);
122+
}
123+
}
124+
125+
strbuf_release(&valbuf);
126+
strbuf_release(&quotbuf);
127+
return ret;
128+
}
129+
130+
static int repo_info(int argc, const char **argv, const char *prefix,
131+
struct repository *repo)
132+
{
133+
const char *format_str = "keyvalue";
134+
enum output_format format;
135+
struct option options[] = {
136+
OPT_STRING(0, "format", &format_str, N_("format"),
137+
N_("output format")),
138+
OPT_END()
139+
};
140+
141+
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
142+
143+
if (!strcmp(format_str, "keyvalue"))
144+
format = FORMAT_KEYVALUE;
145+
else if (!strcmp(format_str, "nul"))
146+
format = FORMAT_NUL_TERMINATED;
147+
else
148+
die(_("invalid format '%s'"), format_str);
149+
150+
return print_fields(argc, argv, repo, format);
151+
}
152+
153+
int cmd_repo(int argc, const char **argv, const char *prefix,
154+
struct repository *repo)
155+
{
156+
parse_opt_subcommand_fn *fn = NULL;
157+
struct option options[] = {
158+
OPT_SUBCOMMAND("info", &fn, repo_info),
159+
OPT_END()
160+
};
161+
162+
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
163+
164+
return fn(argc, argv, prefix, repo);
165+
}

command-list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ git-remote ancillarymanipulators complete
165165
git-repack ancillarymanipulators complete
166166
git-replace ancillarymanipulators complete
167167
git-replay plumbingmanipulators
168+
git-repo plumbinginterrogators
168169
git-request-pull foreignscminterface complete
169170
git-rerere ancillaryinterrogators
170171
git-reset mainporcelain history

git.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ static struct cmd_struct commands[] = {
612612
{ "repack", cmd_repack, RUN_SETUP },
613613
{ "replace", cmd_replace, RUN_SETUP },
614614
{ "replay", cmd_replay, RUN_SETUP },
615+
{ "repo", cmd_repo, RUN_SETUP },
615616
{ "rerere", cmd_rerere, RUN_SETUP },
616617
{ "reset", cmd_reset, RUN_SETUP },
617618
{ "restore", cmd_restore, RUN_SETUP | NEED_WORK_TREE },

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ builtin_sources = [
646646
'builtin/repack.c',
647647
'builtin/replace.c',
648648
'builtin/replay.c',
649+
'builtin/repo.c',
649650
'builtin/rerere.c',
650651
'builtin/reset.c',
651652
'builtin/rev-list.c',

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ integration_tests = [
232232
't1700-split-index.sh',
233233
't1701-racy-split-index.sh',
234234
't1800-hook.sh',
235+
't1900-repo.sh',
235236
't2000-conflict-when-checking-files-out.sh',
236237
't2002-checkout-cache-u.sh',
237238
't2003-checkout-cache-mkdir.sh',

0 commit comments

Comments
 (0)