SYNOPSIS
--------
[synopsis]
-git repo info [<key>...]
+git repo info [--format=(keyvalue|nul)] [<key>...]
DESCRIPTION
-----------
COMMANDS
--------
-`info [<key>...]`::
+`info [--format=(keyvalue|nul)] [<key>...]`::
Retrieve metadata-related information about the current repository. Only
the requested data will be returned based on their keys (see "INFO KEYS"
section below).
The values are returned in the same order in which their respective keys were
requested.
+
-The output format consists of key-value pairs one per line using the `=`
-character as the delimiter between the key and the value. Values containing
-"unusual" characters are quoted as explained for the configuration variable
-`core.quotePath` (see linkgit:git-config[1]).
+The output format can be chosen through the flag `--format`. Two formats are
+supported:
++
+`keyvalue`:::
+ output key-value pairs one per line using the `=` character as
+ the delimiter between the key and the value. Values containing "unusual"
+ characters are quoted as explained for the configuration variable
+ `core.quotePath` (see linkgit:git-config[1]). This is the default.
+
+`nul`:::
+ similar to `keyvalue`, but using a newline character as the delimiter
+ between the key and the value and using a NUL character after each value.
+ This format is better suited for being parsed by another applications than
+ `keyvalue`. Unlike in the `keyvalue` format, the values are never quoted.
INFO KEYS
---------
-
In order to obtain a set of values from `git repo info`, you should provide
the keys that identify them. Here's a list of the available keys and the
values that they return:
+
include::ref-storage-format.adoc[]
+EXAMPLES
+--------
+
+* Retrieves the reference format of the current repository:
++
+------------
+git repo info references.format
+------------
++
+
+* Retrieves whether the current repository is bare and whether it is shallow
+using the `nul` format:
++
+------------
+git repo info --format=nul layout.bare layout.shallow
+------------
+
SEE ALSO
--------
linkgit:git-rev-parse[1]
#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;
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;
strbuf_reset("buf);
get_value(repo, &valbuf);
- quote_c_style(valbuf.buf, "buf, NULL, 0);
- printf("%s=%s\n", key, quotbuf.buf);
+
+ 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);
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,
eval "$init_command $repo_name"
'
- test_expect_success "$label" '
- echo "$key=$expected_value" >expect &&
- git -C $repo_name repo info "$key" >actual &&
+ test_expect_success "keyvalue: $label" '
+ echo "$key=$expected_value" > expect &&
+ git -C "$repo_name" repo info "$key" >actual &&
test_cmp expect actual
'
+
+ test_expect_success "nul: $label" '
+ printf "%s\n%s\0" "$key" "$expected_value" >expect &&
+ git -C "$repo_name" repo info --format=nul "$key" >actual &&
+ test_cmp_bin expect actual
+ '
}
test_repo_info 'ref format files is retrieved correctly' \
test_cmp expect actual
'
+test_expect_success 'git-repo-info aborts when requesting an invalid format' '
+ echo "fatal: invalid format ${SQ}foo${SQ}" >expect &&
+ test_must_fail git repo info --format=foo 2>actual &&
+ test_cmp expect actual
+'
+
test_done