aboutsummaryrefslogtreecommitdiffstats
path: root/builtin/repo.c
diff options
context:
space:
mode:
Diffstat (limited to 'builtin/repo.c')
-rw-r--r--builtin/repo.c46
1 files changed, 40 insertions, 6 deletions
diff --git a/builtin/repo.c b/builtin/repo.c
index 3c9140593b..8c6e7f42ab 100644
--- a/builtin/repo.c
+++ b/builtin/repo.c
@@ -9,12 +9,17 @@
#include "shallow.h"
static const char *const repo_usage[] = {
- "git repo info [<key>...]",
+ "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;
@@ -65,7 +70,9 @@ static get_value_fn *get_value_fn_for_key(const char *key)
return found ? found->get_value : NULL;
}
-static int print_fields(int argc, const char **argv, struct repository *repo)
+static int print_fields(int argc, const char **argv,
+ struct repository *repo,
+ enum output_format format)
{
int ret = 0;
struct strbuf valbuf = STRBUF_INIT;
@@ -86,8 +93,18 @@ static int print_fields(int argc, const char **argv, struct repository *repo)
strbuf_reset(&quotbuf);
get_value(repo, &valbuf);
- quote_c_style(valbuf.buf, &quotbuf, NULL, 0);
- printf("%s=%s\n", key, quotbuf.buf);
+
+ switch (format) {
+ case FORMAT_KEYVALUE:
+ quote_c_style(valbuf.buf, &quotbuf, 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);
@@ -95,10 +112,27 @@ static int print_fields(int argc, const char **argv, struct repository *repo)
return ret;
}
-static int repo_info(int argc, const char **argv, const char *prefix UNUSED,
+static int repo_info(int argc, const char **argv, const char *prefix,
struct repository *repo)
{
- return print_fields(argc - 1, argv + 1, 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,