Hi! This v6 contains small fixes pointed in v5. The main changes were: - The functions that retrieve the values now add a string to a strbuf instead of returning the string - The null-terminated format is now identified as "nul" - Two test cases were added, covering invalid input values in the CLI In v5, Patrick and Junio were discussing about quoting the values in the key=value format using `quote_c_style` [1]. Given that it wouldn't affect this patchset and it's a simple change that can be done later, I didn't change it and I'll leave for further discussion when I start to deal with paths. Thanks! [1] https://lore.kernel.org/git/aIHRCz_qswp7RgSy@xxxxxx/ Range-diff versus v5: 1: 396cfc256a ! 1: bc6f19ba8e repo: declare the repo command @@ Commit message Create a new Git command called `repo`. `git repo` will be the main command for obtaining the information about a repository (such as - metadata and metrics), returning them in a machine readable format - following the syntax "field<LF>value<NUL>". + metadata and metrics). Also declare a subcommand for `repo` called `info`. `git repo info` will bring the functionality of retrieving repository-related @@ Documentation/git-repo.adoc (new) + +COMMANDS +-------- -+info [<key>...]:: ++`info [<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). 2: 28f4c21a96 ! 2: b1c0627af8 repo: add the field references.format @@ Commit message used for querying repository metadata, fitting in the purpose of git-repo-info. - Then, add a new field `references.format` to the repo-info subcommand + Add a new field `references.format` to the repo-info subcommand containing that information. Helped-by: Phillip Wood <phillip.wood@xxxxxxxxxxxxx> @@ builtin/repo.c #include "builtin.h" #include "parse-options.h" +#include "refs.h" ++#include "strbuf.h" -static int repo_info(int argc UNUSED, const char **argv UNUSED, - const char *prefix UNUSED, struct repository *repo UNUSED) -+typedef const char *get_value_fn(struct repository *repo); ++typedef int get_value_fn(struct repository *repo, struct strbuf *buf); + +struct field { + const char *key; -+ get_value_fn *add_field_callback; ++ get_value_fn *get_value; +}; + -+static const char *get_references_format(struct repository *repo) ++static int get_references_format(struct repository *repo, struct strbuf *buf) +{ -+ return ref_storage_format_to_name(repo->ref_storage_format); ++ strbuf_addstr(buf, ++ ref_storage_format_to_name(repo->ref_storage_format)); ++ return 0; +} + +/* repo_info_fields keys should be in lexicographical order */ @@ builtin/repo.c + return strcmp(a->key, b->key); +} + -+static get_value_fn *get_value_callback(const char *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(struct field), ++ sizeof(*found), + repo_info_fields_cmp); -+ return found ? found->add_field_callback : NULL; ++ return found ? found->get_value : NULL; +} + +static int qsort_strcmp(const void *va, const void *vb) -+{ + { + const char *a = *(const char **)va; + const char *b = *(const char **)vb; + @@ builtin/repo.c + QSORT(argv, argc, qsort_strcmp); + + for (int i = 0; i < argc; i++) { -+ get_value_fn *callback; ++ get_value_fn *get_value; + const char *key = argv[i]; -+ const char *value; ++ struct strbuf value; ++ strbuf_init(&value, 64); + + if (!strcmp(key, last)) + continue; + -+ callback = get_value_callback(key); ++ get_value = get_value_fn_for_key(key); + -+ if (!callback) -+ return error("key %s not found", key); ++ if (!get_value) ++ die(_("key '%s' not found"), key); + -+ value = callback(repo); -+ printf("%s=%s\n", key, value); ++ get_value(repo, &value); ++ printf("%s=%s\n", key, value.buf); + last = key; ++ strbuf_release(&value); + } + return 0; @@ t/t1900-repo.sh (new) +test_repo_info 'ref format reftable is retrieved correctly' ' + git init --ref-format=reftable repo' 'references.format' 'reftable' + ++test_expect_success 'git-repo-info aborts if an invalid key is requested' ' ++ test_when_finished "rm -rf expected err" && ++ echo "fatal: key '\'foo\'' not found" >expected && ++ test_must_fail git repo info foo 2>err && ++ test_cmp expected err ++' ++ +test_expect_success "only one value is returned if the same key is requested twice" ' + test_when_finished "rm -f expected_key expected_value actual_key actual_value output" && + echo "references.format" >expected_key && 3: b8001ae87e ! 3: d002401587 repo: add field layout.bare @@ builtin/repo.c +#define USE_THE_REPOSITORY_VARIABLE + #include "builtin.h" ++#include "environment.h" #include "parse-options.h" #include "refs.h" -+#include "environment.h" - - typedef const char *get_value_fn(struct repository *repo); - + #include "strbuf.h" @@ builtin/repo.c: struct field { - get_value_fn *add_field_callback; + get_value_fn *get_value; }; -+static const char *get_layout_bare(struct repository *repo UNUSED) ++static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf) +{ -+ return is_bare_repository() ? "true" : "false"; ++ strbuf_addstr(buf, ++ is_bare_repository() ? "true" : "false"); ++ return 0; +} + - static const char *get_references_format(struct repository *repo) + static int get_references_format(struct repository *repo, struct strbuf *buf) { - return ref_storage_format_to_name(repo->ref_storage_format); -@@ builtin/repo.c: static const char *get_references_format(struct repository *repo) + strbuf_addstr(buf, +@@ builtin/repo.c: static int get_references_format(struct repository *repo, struct strbuf *buf) /* repo_info_fields keys should be in lexicographical order */ static const struct field repo_info_fields[] = { @@ t/t1900-repo.sh: test_repo_info 'ref format files is retrieved correctly' ' +test_repo_info 'bare repository = true is retrieved correctly' ' + git init --bare repo' 'layout.bare' 'true' + - test_expect_success "only one value is returned if the same key is requested twice" ' - test_when_finished "rm -f expected_key expected_value actual_key actual_value output" && - echo "references.format" >expected_key && + test_expect_success 'git-repo-info aborts if an invalid key is requested' ' + test_when_finished "rm -rf expected err" && + echo "fatal: key '\'foo\'' not found" >expected && 4: bceba54e8b ! 4: 6eaac3f9c3 repo: add field layout.shallow @@ builtin/repo.c @@ #include "parse-options.h" #include "refs.h" - #include "environment.h" + #include "strbuf.h" +#include "shallow.h" - typedef const char *get_value_fn(struct repository *repo); + typedef int get_value_fn(struct repository *repo, struct strbuf *buf); -@@ builtin/repo.c: static const char *get_layout_bare(struct repository *repo UNUSED) - return is_bare_repository() ? "true" : "false"; +@@ builtin/repo.c: static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf) + return 0; } -+static const char *get_layout_shallow(struct repository *repo) ++static int get_layout_shallow(struct repository *repo, struct strbuf *buf) +{ -+ return is_repository_shallow(repo) ? "true" : "false"; ++ strbuf_addstr(buf, ++ is_repository_shallow(repo) ? "true" : "false"); ++ return 0; +} + - static const char *get_references_format(struct repository *repo) + static int get_references_format(struct repository *repo, struct strbuf *buf) { - return ref_storage_format_to_name(repo->ref_storage_format); -@@ builtin/repo.c: static const char *get_references_format(struct repository *repo) + strbuf_addstr(buf, +@@ builtin/repo.c: static int get_references_format(struct repository *repo, struct strbuf *buf) /* repo_info_fields keys should be in lexicographical order */ static const struct field repo_info_fields[] = { { "layout.bare", get_layout_bare }, @@ t/t1900-repo.sh: test_repo_info 'bare repository = false is retrieved correctly' + rm -rf remote + ' 'layout.shallow' 'true' + - test_expect_success "only one value is returned if the same key is requested twice" ' - test_when_finished "rm -f expected_key expected_value actual_key actual_value output" && - echo "references.format" >expected_key && + test_expect_success 'git-repo-info aborts if an invalid key is requested' ' + test_when_finished "rm -rf expected err" && + echo "fatal: key '\'foo\'' not found" >expected && @@ t/t1900-repo.sh: test_expect_success "only one value is returned if the same key is requested twi test_cmp expected_value actual_value ' 5: f4a2b0a04e ! 5: 69c7554bf3 repo: add the --format flag @@ Commit message 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 another + 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 @@ Documentation/git-repo.adoc: THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHAN COMMANDS -------- --info [<key>...]:: -+info [--format=<format>] [<key>...]:: +-`info [<key>...]`:: ++`info [--format=<format>] [<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). @@ Documentation/git-repo.adoc: THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHAN +* `keyvalue`: output key-value pairs one per line using the `=` character as +the delimiter between the key and the value. This is the default. + -+* `null`: similar to `keyvalue`, but using a newline character as the delimiter ++* `nul`: similar to `keyvalue`, but using a newline character as the delimiter +between the key and the value and using a null character after each value. +This format is better suited for being parsed by another applications than +`keyvalue`. @@ Documentation/git-repo.adoc: THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHAN ## builtin/repo.c ## @@ - typedef const char *get_value_fn(struct repository *repo); + typedef int get_value_fn(struct repository *repo, struct strbuf *buf); +enum output_format { + FORMAT_KEYVALUE, -+ FORMAT_NULL_TERMINATED, ++ FORMAT_NUL_TERMINATED, +}; + struct field { const char *key; - get_value_fn *add_field_callback; + get_value_fn *get_value; @@ builtin/repo.c: static int qsort_strcmp(const void *va, const void *vb) return strcmp(a, b); } @@ builtin/repo.c: static int qsort_strcmp(const void *va, const void *vb) + kv_sep = '='; + field_sep = '\n'; + break; -+ case FORMAT_NULL_TERMINATED: ++ case FORMAT_NUL_TERMINATED: + kv_sep = '\n'; + field_sep = '\0'; + break; @@ builtin/repo.c: static int qsort_strcmp(const void *va, const void *vb) QSORT(argv, argc, qsort_strcmp); @@ builtin/repo.c: static int print_fields(int argc, const char **argv, struct repository *repo) - return error("key %s not found", key); + die(_("key '%s' not found"), key); - value = callback(repo); -- printf("%s=%s\n", key, value); -+ printf("%s%c%s%c", key, kv_sep, value, field_sep); + get_value(repo, &value); +- printf("%s=%s\n", key, value.buf); ++ printf("%s%c%s%c", key, kv_sep, value.buf, field_sep); last = key; + strbuf_release(&value); } - +@@ builtin/repo.c: static int print_fields(int argc, const char **argv, struct repository *repo) return 0; } @@ builtin/repo.c: static int print_fields(int argc, const char **argv, struct repo + + if (!strcmp(format_str, "keyvalue")) + format = FORMAT_KEYVALUE; -+ else if (!strcmp(format_str, "null")) -+ format = FORMAT_NULL_TERMINATED; ++ else if (!strcmp(format_str, "nul")) ++ format = FORMAT_NUL_TERMINATED; + else -+ die("invalid format %s", format_str); ++ die(_("invalid format '%s'"), format_str); + + return print_fields(argc, argv, repo, format); } @@ t/t1900-repo.sh: test_repo_info () { + test_when_finished "rm -rf repo" && + eval "$init_command" && + echo "$expected_value" | lf_to_nul >expected && -+ git -C repo repo info --format=null "$key" >output && ++ git -C repo repo info --format=nul "$key" >output && + tail -n 1 output >actual && + test_cmp expected actual + ' @@ t/t1900-repo.sh: test_repo_info () { cut -d "=" -f 2 <output >actual && test_cmp expected actual ' +@@ t/t1900-repo.sh: test_expect_success 'output is returned correctly when two keys are requested' ' + test_cmp expect actual + ' + ++test_expect_success 'git-repo-info aborts when requesting an invalid format' ' ++ test_when_finished "rm -f err expected" && ++ echo "fatal: invalid format '\'foo\''" >expected && ++ test_must_fail git repo info --format=foo 2>err && ++ test_cmp expected err ++' ++ + test_done Lucas Seiki Oshiro (5): repo: declare the repo command repo: add the field references.format repo: add field layout.bare repo: add field layout.shallow repo: add the --format flag .gitignore | 1 + Documentation/git-repo.adoc | 58 +++++++++++++ Documentation/meson.build | 1 + Makefile | 1 + builtin.h | 1 + builtin/repo.c | 165 ++++++++++++++++++++++++++++++++++++ command-list.txt | 1 + git.c | 1 + meson.build | 1 + t/meson.build | 1 + t/t1900-repo.sh | 100 ++++++++++++++++++++++ 11 files changed, 331 insertions(+) create mode 100644 Documentation/git-repo.adoc create mode 100644 builtin/repo.c create mode 100755 t/t1900-repo.sh -- 2.39.5 (Apple Git-154)