From: Lucas Seiki Oshiro Date: Fri, 01 Aug 2025 13:11:05 GMT Subject: [GSoC PATCH v7 0/5] repo: add new command for retrieving repository info Message-ID: <20250801131111.8115-1-lucasseikioshiro@gmail.com> In-Reply-To: <20250610152117.14826-1-lucasseikioshiro@gmail.com> Hi! These are the changes of this 7th version of `git repo`: - The tests were refactored (thanks Eric for your careful revision!) - The documentation has been improved: now it is a little more descriptive about the keys. I'm also including more information about the values - The documentation now contains examples - If an invalid key is requested, the command fails, however, now it returns all the valid fields that were requested - Now, I'm using `quote_c_style` in the key=value format Thanks! Range-diff versus v6: 1: bc6f19ba8e ! 1: b4f063b177 repo: declare the repo command @@ Commit message will bring the functionality of retrieving repository-related information currently returned by `rev-parse`. - Add the required tests, documentation and build changes to enable - usage of this subcommand. + Add the required documentation and build changes to enable usage of + this subcommand. Helped-by: Phillip Wood Helped-by: Junio C Hamano Helped-by: Justin Tobler + Helped-by: Eric Sunshine Mentored-by: Karthik Nayak Mentored-by: Patrick Steinhardt Signed-off-by: Lucas Seiki Oshiro @@ Documentation/git-repo.adoc (new) + +NAME +---- -+git-repo - Retrieve information about a repository ++git-repo - Retrieve information about the repository + +SYNOPSIS +-------- @@ Documentation/git-repo.adoc (new) + +DESCRIPTION +----------- -+This command retrieve repository level information. ++Retrieve information about the repository. + +THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE. + @@ Documentation/git-repo.adoc (new) + the requested data will be returned based on their keys (see "INFO KEYS" + section below). + -+INFO KEYS -+--------- -+ -+The set of data that `git repo` can return is grouped into the following -+categories: -+ +SEE ALSO +-------- +linkgit:git-rev-parse[1] @@ builtin/repo.c (new) +#include "builtin.h" +#include "parse-options.h" + ++static const char *const repo_usage[] = { ++ "git repo info [...]", ++ NULL ++}; ++ +static int repo_info(int argc UNUSED, const char **argv UNUSED, + const char *prefix UNUSED, struct repository *repo UNUSED) +{ @@ builtin/repo.c (new) + struct repository *repo) +{ + parse_opt_subcommand_fn *fn = NULL; -+ const char *const repo_usage[] = { -+ "git repo info [...]", -+ NULL -+ }; + struct option options[] = { + OPT_SUBCOMMAND("info", &fn, repo_info), + OPT_END() 2: 2b0e91f94d ! 2: 56cb05ecb2 repo: add the field references.format @@ Commit message Helped-by: Phillip Wood Helped-by: Junio C Hamano Helped-by: Justin Tobler + Helped-by: Eric Sunshine Mentored-by: Karthik Nayak Mentored-by: Patrick Steinhardt Signed-off-by: Lucas Seiki Oshiro ## Documentation/git-repo.adoc ## -@@ Documentation/git-repo.adoc: INFO KEYS - The set of data that `git repo` can return is grouped into the following - categories: +@@ Documentation/git-repo.adoc: COMMANDS + 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 returned data is lexicographically sorted by the keys. ++ ++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: ++ ++`references.format`:: ++The reference storage format. The valid values are: +++ ++include::ref-storage-format.adoc[] -+`references`:: -+Reference-related data: -+* `format`: the reference storage format -+ SEE ALSO -------- - linkgit:git-rev-parse[1] ## builtin/repo.c ## @@ #include "builtin.h" #include "parse-options.h" ++#include "quote.h" +#include "refs.h" +#include "strbuf.h" + static const char *const repo_usage[] = { + "git repo info [...]", + NULL + }; + -static int repo_info(int argc UNUSED, const char **argv UNUSED, - const char *prefix UNUSED, struct repository *repo UNUSED) +typedef int get_value_fn(struct repository *repo, struct strbuf *buf); @@ builtin/repo.c +}; + +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; -+} -+ + return 0; + } + +/* repo_info_fields keys should be in lexicographical order */ +static const struct field repo_info_fields[] = { + { "references.format", get_references_format }, @@ builtin/repo.c +} + +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 + +static int print_fields(int argc, const char **argv, struct repository *repo) +{ ++ int ret = 0; + const char *last = ""; ++ struct strbuf sb = STRBUF_INIT; + + QSORT(argv, argc, qsort_strcmp); + + for (int i = 0; i < argc; i++) { + get_value_fn *get_value; + const char *key = argv[i]; -+ struct strbuf value; ++ char *value; + + if (!strcmp(key, last)) + continue; + -+ strbuf_init(&value, 64); + get_value = get_value_fn_for_key(key); + + if (!get_value) { -+ strbuf_release(&value); -+ return error(_("key '%s' not found"), key); ++ ret = error(_("key '%s' not found"), key); ++ continue; + } + -+ get_value(repo, &value); -+ printf("%s=%s\n", key, value.buf); ++ strbuf_reset(&sb); ++ get_value(repo, &sb); ++ ++ value = strbuf_detach(&sb, NULL); ++ quote_c_style(value, &sb, NULL, 0); ++ free(value); ++ ++ printf("%s=%s\n", key, sb.buf); + last = key; -+ strbuf_release(&value); + } + - return 0; - } - ++ strbuf_release(&sb); ++ return ret; ++} ++ +static int repo_info(int argc, const char **argv, const char *prefix UNUSED, + struct repository *repo) +{ @@ t/t1900-repo.sh (new) + +. ./test-lib.sh + -+# Test if a field is correctly returned in the null-terminated format ++# Test whether a key-value pair is correctly returned +# +# Usage: test_repo_info