1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#define USE_THE_REPOSITORY_VARIABLE
#include "builtin.h"
#include "environment.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
};
typedef int get_value_fn(struct repository *repo, struct strbuf *buf);
struct field {
const char *key;
get_value_fn *get_value;
};
static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf)
{
strbuf_addstr(buf, is_bare_repository() ? "true" : "false");
return 0;
}
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[] = {
{ "layout.bare", get_layout_bare },
{ "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("buf);
get_value(repo, &valbuf);
quote_c_style(valbuf.buf, "buf, NULL, 0);
printf("%s=%s\n", key, quotbuf.buf);
}
strbuf_release(&valbuf);
strbuf_release("buf);
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)
{
parse_opt_subcommand_fn *fn = NULL;
struct option options[] = {
OPT_SUBCOMMAND("info", &fn, repo_info),
OPT_END()
};
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
return fn(argc, argv, prefix, repo);
}
|