Skip to content

Commit ebb45da

Browse files
committed
Merge branch 'lo/repo-info'
A new subcommand "git repo" gives users a way to grab various repository characteristics. * 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 eed447d + a81224d commit ebb45da

File tree

11 files changed

+337
-0
lines changed

11 files changed

+337
-0
lines changed

.gitignore

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

Documentation/git-repo.adoc

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 values are returned in the same order in which their respective keys were
27+
requested.
28+
+
29+
The output format can be chosen through the flag `--format`. Two formats are
30+
supported:
31+
+
32+
`keyvalue`:::
33+
output key-value pairs one per line using the `=` character as
34+
the delimiter between the key and the value. Values containing "unusual"
35+
characters are quoted as explained for the configuration variable
36+
`core.quotePath` (see linkgit:git-config[1]). This is the default.
37+
38+
`nul`:::
39+
similar to `keyvalue`, but using a newline character as the delimiter
40+
between the key and the value and using a NUL character after each value.
41+
This format is better suited for being parsed by another applications than
42+
`keyvalue`. Unlike in the `keyvalue` format, the values are never quoted.
43+
44+
INFO KEYS
45+
---------
46+
In order to obtain a set of values from `git repo info`, you should provide
47+
the keys that identify them. Here's a list of the available keys and the
48+
values that they return:
49+
50+
`layout.bare`::
51+
`true` if this is a bare repository, otherwise `false`.
52+
53+
`layout.shallow`::
54+
`true` if this is a shallow repository, otherwise `false`.
55+
56+
`references.format`::
57+
The reference storage format. The valid values are:
58+
+
59+
include::ref-storage-format.adoc[]
60+
61+
EXAMPLES
62+
--------
63+
64+
* Retrieves the reference format of the current repository:
65+
+
66+
------------
67+
git repo info references.format
68+
------------
69+
+
70+
71+
* Retrieves whether the current repository is bare and whether it is shallow
72+
using the `nul` format:
73+
+
74+
------------
75+
git repo info --format=nul layout.bare layout.shallow
76+
------------
77+
78+
SEE ALSO
79+
--------
80+
linkgit:git-rev-parse[1]
81+
82+
GIT
83+
---
84+
Part of the linkgit:git[1] suite

Documentation/meson.build

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

Makefile

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

builtin.h

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

builtin/repo.c

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 must 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 print_fields(int argc, const char **argv,
74+
struct repository *repo,
75+
enum output_format format)
76+
{
77+
int ret = 0;
78+
struct strbuf valbuf = STRBUF_INIT;
79+
struct strbuf quotbuf = STRBUF_INIT;
80+
81+
for (int i = 0; i < argc; i++) {
82+
get_value_fn *get_value;
83+
const char *key = argv[i];
84+
85+
get_value = get_value_fn_for_key(key);
86+
87+
if (!get_value) {
88+
ret = error(_("key '%s' not found"), key);
89+
continue;
90+
}
91+
92+
strbuf_reset(&valbuf);
93+
strbuf_reset(&quotbuf);
94+
95+
get_value(repo, &valbuf);
96+
97+
switch (format) {
98+
case FORMAT_KEYVALUE:
99+
quote_c_style(valbuf.buf, &quotbuf, NULL, 0);
100+
printf("%s=%s\n", key, quotbuf.buf);
101+
break;
102+
case FORMAT_NUL_TERMINATED:
103+
printf("%s\n%s%c", key, valbuf.buf, '\0');
104+
break;
105+
default:
106+
BUG("not a valid output format: %d", format);
107+
}
108+
}
109+
110+
strbuf_release(&valbuf);
111+
strbuf_release(&quotbuf);
112+
return ret;
113+
}
114+
115+
static int repo_info(int argc, const char **argv, const char *prefix,
116+
struct repository *repo)
117+
{
118+
const char *format_str = "keyvalue";
119+
enum output_format format;
120+
struct option options[] = {
121+
OPT_STRING(0, "format", &format_str, N_("format"),
122+
N_("output format")),
123+
OPT_END()
124+
};
125+
126+
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
127+
128+
if (!strcmp(format_str, "keyvalue"))
129+
format = FORMAT_KEYVALUE;
130+
else if (!strcmp(format_str, "nul"))
131+
format = FORMAT_NUL_TERMINATED;
132+
else
133+
die(_("invalid format '%s'"), format_str);
134+
135+
return print_fields(argc, argv, repo, format);
136+
}
137+
138+
int cmd_repo(int argc, const char **argv, const char *prefix,
139+
struct repository *repo)
140+
{
141+
parse_opt_subcommand_fn *fn = NULL;
142+
struct option options[] = {
143+
OPT_SUBCOMMAND("info", &fn, repo_info),
144+
OPT_END()
145+
};
146+
147+
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
148+
149+
return fn(argc, argv, prefix, repo);
150+
}

command-list.txt

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

git.c

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

meson.build

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

t/meson.build

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

0 commit comments

Comments
 (0)