diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc index 319d30bd86e7f4..086ab922add16f 100644 --- a/Documentation/git-repo.adoc +++ b/Documentation/git-repo.adoc @@ -8,7 +8,7 @@ git-repo - Retrieve information about the repository SYNOPSIS -------- [synopsis] -git repo info [--format=(lines|nul) | -z] [--all | ...] +git repo info [--format=(lines|nul) | -z] [--path-format=(absolute|relative)] [--all | ...] git repo info --keys [--format=(lines|nul) | -z] git repo structure [--format=(table|lines|nul) | -z] @@ -56,6 +56,11 @@ supported: `nul`::: Similar to `lines`, but using a _NUL_ character after each value. +`--path-format=(absolute|relative)`::: + Controls formatting for keys in the `path` category. The default is + `absolute`. This option may be specified multiple times; the last one + specified takes effect. + `structure [--format=(table|lines|nul) | -z]`:: Retrieve statistics about the current repository structure. The following kinds of information are reported: @@ -64,6 +69,12 @@ supported: * Reachable object counts categorized by type * Total inflated size of reachable objects by type * Total disk size of reachable objects by type +* Largest inflated reachable object size by type +* Largest disk size of a reachable object by type +* Largest parent count among reachable commits +* Largest entry count among reachable trees +* Longest and deepest path among reachable blobs +* Deepest annotated tag chain + The output format can be chosen through the flag `--format`. Three formats are supported: @@ -76,6 +87,7 @@ supported: `lines`::: Each line of output contains a key-value pair for a repository stat. The '=' character is used to delimit between the key and the value. + Both aggregate metrics and per-type metrics are included. Values containing "unusual" characters are quoted as explained for the configuration variable `core.quotePath` (see linkgit:git-config[1]). @@ -90,9 +102,11 @@ supported: 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: +In order to obtain values from `git repo info`, provide either individual keys +or category names. A category returns all keys within that category. For +example, `layout` returns both `layout.bare` and `layout.shallow`. + +Here's a list of the available keys and the values that they return: `layout.bare`:: `true` if this is a bare repository, otherwise `false`. @@ -103,6 +117,42 @@ values that they return: `object.format`:: The object format (hash algorithm) used in the repository. +`path.common-dir`:: + The path to the common git directory. + +`path.config-file`:: + The path to the `config` file in the git directory. + +`path.git-dir`:: + The path to the git directory. + +`path.prefix`:: + The path of the current working directory relative to the top-level + directory. + +`path.grafts-file`:: + The path to the `info/grafts` file. + +`path.hooks-directory`:: + The path to the `hooks` directory. + +`path.index-file`:: + The path to the index file. + +`path.objects-directory`:: + The path to the objects directory. + +`path.superproject-working-tree`:: + The path to the superproject's working tree root, or an empty string + when the repository is not used as a submodule. + +`path.toplevel`:: + The path to the top-level working tree directory, or an empty string + for bare repositories. + +`path.working-tree`:: + Alias for `path.toplevel`. + `references.format`:: The reference storage format. The valid values are: + diff --git a/builtin/repo.c b/builtin/repo.c index 6a62a6020a5115..bcdb9210cb905e 100644 --- a/builtin/repo.c +++ b/builtin/repo.c @@ -1,10 +1,12 @@ #define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" +#include "abspath.h" #include "environment.h" #include "hex.h" #include "odb.h" #include "parse-options.h" +#include "path.h" #include "path-walk.h" #include "progress.h" #include "quote.h" @@ -14,16 +16,28 @@ #include "strbuf.h" #include "string-list.h" #include "shallow.h" +#include "submodule.h" #include "utf8.h" static const char *const repo_usage[] = { - "git repo info [--format=(lines|nul) | -z] [--all | ...]", + "git repo info [--format=(lines|nul) | -z] [--path-format=(absolute|relative)] [--all | ...]", "git repo info --keys [--format=(lines|nul) | -z]", "git repo structure [--format=(table|lines|nul) | -z]", NULL }; -typedef int get_value_fn(struct repository *repo, struct strbuf *buf); +enum path_format { + PATH_FORMAT_ABSOLUTE, + PATH_FORMAT_RELATIVE, +}; + +struct repo_info { + struct repository *repo; + const char *prefix; + enum path_format path_format; +}; + +typedef int get_value_fn(struct repo_info *info, struct strbuf *buf); enum output_format { FORMAT_TABLE, @@ -36,27 +50,128 @@ struct field { get_value_fn *get_value; }; -static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf) +static void repo_info_add_path(struct repo_info *info, + struct strbuf *buf, + const char *path) +{ + if (info->path_format == PATH_FORMAT_RELATIVE) { + char *cwd = xgetcwd(); + struct strbuf rel_path = STRBUF_INIT; + + strbuf_addstr(buf, relative_path(path, cwd, &rel_path)); + strbuf_release(&rel_path); + free(cwd); + return; + } + + strbuf_add_absolute_path(buf, path); +} + +static int get_layout_bare(struct repo_info *info 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) +static int get_layout_shallow(struct repo_info *info, struct strbuf *buf) { + struct repository *repo = info->repo; strbuf_addstr(buf, is_repository_shallow(repo) ? "true" : "false"); return 0; } -static int get_object_format(struct repository *repo, struct strbuf *buf) +static int get_object_format(struct repo_info *info, struct strbuf *buf) { + struct repository *repo = info->repo; strbuf_addstr(buf, repo->hash_algo->name); return 0; } -static int get_references_format(struct repository *repo, struct strbuf *buf) +static int get_path_common_dir(struct repo_info *info, struct strbuf *buf) +{ + repo_info_add_path(info, buf, repo_get_common_dir(info->repo)); + return 0; +} + +static int get_path_config_file(struct repo_info *info, struct strbuf *buf) +{ + struct strbuf path = STRBUF_INIT; + + repo_info_add_path(info, buf, repo_git_path_replace(info->repo, &path, "config")); + strbuf_release(&path); + return 0; +} + +static int get_path_git_dir(struct repo_info *info, struct strbuf *buf) +{ + repo_info_add_path(info, buf, repo_get_git_dir(info->repo)); + return 0; +} + +static int get_path_prefix(struct repo_info *info, struct strbuf *buf) +{ + strbuf_addstr(buf, info->prefix); + return 0; +} + +static int get_path_grafts_file(struct repo_info *info, struct strbuf *buf) +{ + repo_info_add_path(info, buf, repo_get_graft_file(info->repo)); + return 0; +} + +static int get_path_hooks_directory(struct repo_info *info, struct strbuf *buf) +{ + struct strbuf path = STRBUF_INIT; + + repo_info_add_path(info, buf, repo_git_path_replace(info->repo, &path, "hooks")); + strbuf_release(&path); + return 0; +} + +static int get_path_index_file(struct repo_info *info, struct strbuf *buf) +{ + repo_info_add_path(info, buf, repo_get_index_file(info->repo)); + return 0; +} + +static int get_path_objects_directory(struct repo_info *info, struct strbuf *buf) +{ + repo_info_add_path(info, buf, repo_get_object_directory(info->repo)); + return 0; +} + +static int get_path_superproject_working_tree(struct repo_info *info, + struct strbuf *buf) { + struct strbuf superproject = STRBUF_INIT; + + if (get_superproject_working_tree(&superproject)) + repo_info_add_path(info, buf, superproject.buf); + + strbuf_release(&superproject); + return 0; +} + +static int get_path_toplevel(struct repo_info *info, struct strbuf *buf) +{ + const char *work_tree = repo_get_work_tree(info->repo); + + if (work_tree) + repo_info_add_path(info, buf, work_tree); + + return 0; +} + +static int get_path_work_tree(struct repo_info *info, struct strbuf *buf) +{ + return get_path_toplevel(info, buf); +} + +static int get_references_format(struct repo_info *info, struct strbuf *buf) +{ + struct repository *repo = info->repo; strbuf_addstr(buf, ref_storage_format_to_name(repo->ref_storage_format)); return 0; @@ -67,6 +182,17 @@ static const struct field repo_info_fields[] = { { "layout.bare", get_layout_bare }, { "layout.shallow", get_layout_shallow }, { "object.format", get_object_format }, + { "path.common-dir", get_path_common_dir }, + { "path.config-file", get_path_config_file }, + { "path.git-dir", get_path_git_dir }, + { "path.grafts-file", get_path_grafts_file }, + { "path.hooks-directory", get_path_hooks_directory }, + { "path.index-file", get_path_index_file }, + { "path.objects-directory", get_path_objects_directory }, + { "path.prefix", get_path_prefix }, + { "path.superproject-working-tree", get_path_superproject_working_tree }, + { "path.toplevel", get_path_toplevel }, + { "path.working-tree", get_path_work_tree }, { "references.format", get_references_format }, }; @@ -88,6 +214,33 @@ static get_value_fn *get_value_fn_for_key(const char *key) return found ? found->get_value : NULL; } +static void print_field(enum output_format format, const char *key, + const char *value); + +static int print_category_fields(const char *category, + struct repo_info *info, + enum output_format format, + struct strbuf *valbuf) +{ + int found = 0; + size_t category_len = strlen(category); + + for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) { + const struct field *field = &repo_info_fields[i]; + + if (!starts_with(field->key, category) || + field->key[category_len] != '.') + continue; + + strbuf_reset(valbuf); + field->get_value(info, valbuf); + print_field(format, field->key, valbuf->buf); + found = 1; + } + + return found; +} + static void print_field(enum output_format format, const char *key, const char *value) { @@ -106,7 +259,7 @@ static void print_field(enum output_format format, const char *key, } static int print_fields(int argc, const char **argv, - struct repository *repo, + struct repo_info *info, enum output_format format) { int ret = 0; @@ -118,21 +271,22 @@ static int print_fields(int argc, const char **argv, get_value = get_value_fn_for_key(key); - if (!get_value) { - ret = error(_("key '%s' not found"), key); + if (get_value) { + strbuf_reset(&valbuf); + get_value(info, &valbuf); + print_field(format, key, valbuf.buf); continue; } - strbuf_reset(&valbuf); - get_value(repo, &valbuf); - print_field(format, key, valbuf.buf); + if (!print_category_fields(key, info, format, &valbuf)) + ret = error(_("key '%s' not found"), key); } strbuf_release(&valbuf); return ret; } -static int print_all_fields(struct repository *repo, +static int print_all_fields(struct repo_info *info, enum output_format format) { struct strbuf valbuf = STRBUF_INIT; @@ -141,7 +295,7 @@ static int print_all_fields(struct repository *repo, const struct field *field = &repo_info_fields[i]; strbuf_reset(&valbuf); - field->get_value(repo, &valbuf); + field->get_value(info, &valbuf); print_field(format, field->key, valbuf.buf); } @@ -191,10 +345,30 @@ static int parse_format_cb(const struct option *opt, return 0; } +static int parse_path_format_cb(const struct option *opt, + const char *arg, int unset UNUSED) +{ + enum path_format *path_format = opt->value; + + if (!strcmp(arg, "absolute")) + *path_format = PATH_FORMAT_ABSOLUTE; + else if (!strcmp(arg, "relative")) + *path_format = PATH_FORMAT_RELATIVE; + else + die(_("invalid path format '%s'"), arg); + + return 0; +} + static int cmd_repo_info(int argc, const char **argv, const char *prefix, struct repository *repo) { enum output_format format = FORMAT_NEWLINE_TERMINATED; + struct repo_info info = { + .repo = repo, + .prefix = prefix ? prefix : "", + .path_format = PATH_FORMAT_ABSOLUTE, + }; int all_keys = 0; int show_keys = 0; struct option options[] = { @@ -205,6 +379,9 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix, N_("synonym for --format=nul"), PARSE_OPT_NONEG | PARSE_OPT_NOARG, parse_format_cb), + OPT_CALLBACK_F(0, "path-format", &info.path_format, + N_("format"), N_("path output format"), + PARSE_OPT_NONEG, parse_path_format_cb), OPT_BOOL(0, "all", &all_keys, N_("print all keys/values")), OPT_BOOL(0, "keys", &show_keys, N_("show keys")), OPT_END() @@ -225,9 +402,9 @@ static int cmd_repo_info(int argc, const char **argv, const char *prefix, die(_("--all and cannot be used together")); if (all_keys) - return print_all_fields(repo, format); + return print_all_fields(&info, format); else - return print_fields(argc, argv, repo, format); + return print_fields(argc, argv, &info, format); } struct ref_stats { diff --git a/t/t1900-repo.sh b/t/t1900-repo.sh index a9eb07abe8aaea..6605394d1fc6e7 100755 --- a/t/t1900-repo.sh +++ b/t/t1900-repo.sh @@ -4,6 +4,40 @@ test_description='test git repo-info' . ./test-lib.sh +# git-repo-info keys. It must contain the same keys listed in the const +# repo_info_fields, in lexicographical order. +REPO_INFO_KEYS=' + layout.bare + layout.shallow + object.format + path.common-dir + path.config-file + path.git-dir + path.grafts-file + path.hooks-directory + path.index-file + path.objects-directory + path.prefix + path.superproject-working-tree + path.toplevel + path.working-tree + references.format +' + +REPO_INFO_PATH_KEYS=' + path.common-dir + path.config-file + path.git-dir + path.grafts-file + path.hooks-directory + path.index-file + path.objects-directory + path.prefix + path.superproject-working-tree + path.toplevel + path.working-tree +' + # Test whether a key-value pair is correctly returned # # Usage: test_repo_info