|
| 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("buf); |
| 94 | + |
| 95 | + get_value(repo, &valbuf); |
| 96 | + |
| 97 | + switch (format) { |
| 98 | + case FORMAT_KEYVALUE: |
| 99 | + quote_c_style(valbuf.buf, "buf, 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("buf); |
| 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 | +} |
0 commit comments