Add the --format flag to git-repo-info. By using this flag, the users can choose the format for obtaining the data they requested. Given that this command can be used for generating input for other applications and for being read by end users, it requires at least two formats: one for being read by humans and other for being read by machines. Some other Git commands also have two output formats, notably git-config which was the inspiration for the two formats that were chosen here: - keyvalue, where the retrieved data is printed one per line, using = for delimiting the key and the value. This is the default format, targeted for end users. - nul, where the retrieved data is separated by NUL characters, using the newline character for delimiting the key and the value. This format is targeted for being read by machines. Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk> Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Justin Tobler <jltobler@gmail.com> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Mentored-by: Karthik Nayak <karthik.188@gmail.com> Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
151 lines
3.4 KiB
C
151 lines
3.4 KiB
C
#define USE_THE_REPOSITORY_VARIABLE
|
|
|
|
#include "builtin.h"
|
|
#include "environment.h"
|
|
#include "parse-options.h"
|
|
#include "quote.h"
|
|
#include "refs.h"
|
|
#include "strbuf.h"
|
|
#include "shallow.h"
|
|
|
|
static const char *const repo_usage[] = {
|
|
"git repo info [--format=(keyvalue|nul)] [<key>...]",
|
|
NULL
|
|
};
|
|
|
|
typedef int get_value_fn(struct repository *repo, struct strbuf *buf);
|
|
|
|
enum output_format {
|
|
FORMAT_KEYVALUE,
|
|
FORMAT_NUL_TERMINATED,
|
|
};
|
|
|
|
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_layout_shallow(struct repository *repo, struct strbuf *buf)
|
|
{
|
|
strbuf_addstr(buf,
|
|
is_repository_shallow(repo) ? "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 },
|
|
{ "layout.shallow", get_layout_shallow },
|
|
{ "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,
|
|
enum output_format format)
|
|
{
|
|
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);
|
|
|
|
switch (format) {
|
|
case FORMAT_KEYVALUE:
|
|
quote_c_style(valbuf.buf, "buf, NULL, 0);
|
|
printf("%s=%s\n", key, quotbuf.buf);
|
|
break;
|
|
case FORMAT_NUL_TERMINATED:
|
|
printf("%s\n%s%c", key, valbuf.buf, '\0');
|
|
break;
|
|
default:
|
|
BUG("not a valid output format: %d", format);
|
|
}
|
|
}
|
|
|
|
strbuf_release(&valbuf);
|
|
strbuf_release("buf);
|
|
return ret;
|
|
}
|
|
|
|
static int repo_info(int argc, const char **argv, const char *prefix,
|
|
struct repository *repo)
|
|
{
|
|
const char *format_str = "keyvalue";
|
|
enum output_format format;
|
|
struct option options[] = {
|
|
OPT_STRING(0, "format", &format_str, N_("format"),
|
|
N_("output format")),
|
|
OPT_END()
|
|
};
|
|
|
|
argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
|
|
|
|
if (!strcmp(format_str, "keyvalue"))
|
|
format = FORMAT_KEYVALUE;
|
|
else if (!strcmp(format_str, "nul"))
|
|
format = FORMAT_NUL_TERMINATED;
|
|
else
|
|
die(_("invalid format '%s'"), format_str);
|
|
|
|
return print_fields(argc, argv, repo, format);
|
|
}
|
|
|
|
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);
|
|
}
|