|
1 | 1 | #include "builtin.h" |
2 | 2 | #include "parse-options.h" |
| 3 | +#include "quote.h" |
| 4 | +#include "refs.h" |
| 5 | +#include "strbuf.h" |
3 | 6 |
|
4 | 7 | static const char *const repo_usage[] = { |
5 | 8 | "git repo info [<key>...]", |
6 | 9 | NULL |
7 | 10 | }; |
8 | 11 |
|
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) |
11 | 20 | { |
| 21 | + strbuf_addstr(buf, |
| 22 | + ref_storage_format_to_name(repo->ref_storage_format)); |
12 | 23 | return 0; |
13 | 24 | } |
14 | 25 |
|
| 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("buf); |
| 68 | + |
| 69 | + get_value(repo, &valbuf); |
| 70 | + quote_c_style(valbuf.buf, "buf, NULL, 0); |
| 71 | + printf("%s=%s\n", key, quotbuf.buf); |
| 72 | + } |
| 73 | + |
| 74 | + strbuf_release(&valbuf); |
| 75 | + strbuf_release("buf); |
| 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 | + |
15 | 85 | int cmd_repo(int argc, const char **argv, const char *prefix, |
16 | 86 | struct repository *repo) |
17 | 87 | { |
|
0 commit comments