Merge branch 'lo/repo-info'

A new subcommand "git repo" gives users a way to grab various
repository characteristics.

* lo/repo-info:
  repo: add the --format flag
  repo: add the field layout.shallow
  repo: add the field layout.bare
  repo: add the field references.format
  repo: declare the repo command
main
Junio C Hamano 2025-08-25 14:22:03 -07:00
commit ebb45da976
11 changed files with 337 additions and 0 deletions

1
.gitignore vendored
View File

@ -139,6 +139,7 @@
/git-repack
/git-replace
/git-replay
/git-repo
/git-request-pull
/git-rerere
/git-reset

View File

@ -0,0 +1,84 @@
git-repo(1)
===========

NAME
----
git-repo - Retrieve information about the repository

SYNOPSIS
--------
[synopsis]
git repo info [--format=(keyvalue|nul)] [<key>...]

DESCRIPTION
-----------
Retrieve information about the repository.

THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

COMMANDS
--------
`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 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:

`layout.bare`::
`true` if this is a bare repository, otherwise `false`.

`layout.shallow`::
`true` if this is a shallow repository, otherwise `false`.

`references.format`::
The reference storage format. The valid values are:
+
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]

GIT
---
Part of the linkgit:git[1] suite

View File

@ -116,6 +116,7 @@ manpages = {
'git-repack.adoc' : 1,
'git-replace.adoc' : 1,
'git-replay.adoc' : 1,
'git-repo.adoc' : 1,
'git-request-pull.adoc' : 1,
'git-rerere.adoc' : 1,
'git-reset.adoc' : 1,

View File

@ -1306,6 +1306,7 @@ BUILTIN_OBJS += builtin/remote.o
BUILTIN_OBJS += builtin/repack.o
BUILTIN_OBJS += builtin/replace.o
BUILTIN_OBJS += builtin/replay.o
BUILTIN_OBJS += builtin/repo.o
BUILTIN_OBJS += builtin/rerere.o
BUILTIN_OBJS += builtin/reset.o
BUILTIN_OBJS += builtin/rev-list.o

View File

@ -216,6 +216,7 @@ int cmd_remote_ext(int argc, const char **argv, const char *prefix, struct repos
int cmd_remote_fd(int argc, const char **argv, const char *prefix, struct repository *repo);
int cmd_repack(int argc, const char **argv, const char *prefix, struct repository *repo);
int cmd_replay(int argc, const char **argv, const char *prefix, struct repository *repo);
int cmd_repo(int argc, const char **argv, const char *prefix, struct repository *repo);
int cmd_rerere(int argc, const char **argv, const char *prefix, struct repository *repo);
int cmd_reset(int argc, const char **argv, const char *prefix, struct repository *repo);
int cmd_restore(int argc, const char **argv, const char *prefix, struct repository *repo);

150
builtin/repo.c Normal file
View File

@ -0,0 +1,150 @@
#define USE_THE_REPOSITORY_VARIABLE

#include "builtin.h"
#include "environment.h"
#include "parse-options.h"
#include "quote.h"
#include "refs.h"
#include "strbuf.h"
#include "shallow.h"

static const char *const repo_usage[] = {
"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;
};

static int get_layout_bare(struct repository *repo 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)
{
strbuf_addstr(buf,
is_repository_shallow(repo) ? "true" : "false");
return 0;
}

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;
}

/* repo_info_fields keys must be in lexicographical order */
static const struct field repo_info_fields[] = {
{ "layout.bare", get_layout_bare },
{ "layout.shallow", get_layout_shallow },
{ "references.format", get_references_format },
};

static int repo_info_fields_cmp(const void *va, const void *vb)
{
const struct field *a = va;
const struct field *b = vb;

return strcmp(a->key, b->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(*found),
repo_info_fields_cmp);
return found ? found->get_value : NULL;
}

static int print_fields(int argc, const char **argv,
struct repository *repo,
enum output_format format)
{
int ret = 0;
struct strbuf valbuf = STRBUF_INIT;
struct strbuf quotbuf = STRBUF_INIT;

for (int i = 0; i < argc; i++) {
get_value_fn *get_value;
const char *key = argv[i];

get_value = get_value_fn_for_key(key);

if (!get_value) {
ret = error(_("key '%s' not found"), key);
continue;
}

strbuf_reset(&valbuf);
strbuf_reset(&quotbuf);

get_value(repo, &valbuf);

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);
strbuf_release(&quotbuf);
return ret;
}

static int repo_info(int argc, const char **argv, const char *prefix,
struct repository *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,
struct repository *repo)
{
parse_opt_subcommand_fn *fn = NULL;
struct option options[] = {
OPT_SUBCOMMAND("info", &fn, repo_info),
OPT_END()
};

argc = parse_options(argc, argv, prefix, options, repo_usage, 0);

return fn(argc, argv, prefix, repo);
}

View File

@ -164,6 +164,7 @@ git-remote ancillarymanipulators complete
git-repack ancillarymanipulators complete
git-replace ancillarymanipulators complete
git-replay plumbingmanipulators
git-repo plumbinginterrogators
git-request-pull foreignscminterface complete
git-rerere ancillaryinterrogators
git-reset mainporcelain history

1
git.c
View File

@ -611,6 +611,7 @@ static struct cmd_struct commands[] = {
{ "repack", cmd_repack, RUN_SETUP },
{ "replace", cmd_replace, RUN_SETUP },
{ "replay", cmd_replay, RUN_SETUP },
{ "repo", cmd_repo, RUN_SETUP },
{ "rerere", cmd_rerere, RUN_SETUP },
{ "reset", cmd_reset, RUN_SETUP },
{ "restore", cmd_restore, RUN_SETUP | NEED_WORK_TREE },

View File

@ -645,6 +645,7 @@ builtin_sources = [
'builtin/repack.c',
'builtin/replace.c',
'builtin/replay.c',
'builtin/repo.c',
'builtin/rerere.c',
'builtin/reset.c',
'builtin/rev-list.c',

View File

@ -233,6 +233,7 @@ integration_tests = [
't1700-split-index.sh',
't1701-racy-split-index.sh',
't1800-hook.sh',
't1900-repo.sh',
't2000-conflict-when-checking-files-out.sh',
't2002-checkout-cache-u.sh',
't2003-checkout-cache-mkdir.sh',

95
t/t1900-repo.sh Executable file
View File

@ -0,0 +1,95 @@
#!/bin/sh

test_description='test git repo-info'

. ./test-lib.sh

# Test whether a key-value pair is correctly returned
#
# Usage: test_repo_info <label> <init command> <repo_name> <key> <expected value>
#
# Arguments:
# label: the label of the test
# init_command: a command which creates a repository
# repo_name: the name of the repository that will be created in init_command
# key: the key of the field that is being tested
# expected_value: the value that the field should contain
test_repo_info () {
label=$1
init_command=$2
repo_name=$3
key=$4
expected_value=$5

test_expect_success "setup: $label" '
eval "$init_command $repo_name"
'

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' \
'git init --ref-format=files' 'format-files' 'references.format' 'files'

test_repo_info 'ref format reftable is retrieved correctly' \
'git init --ref-format=reftable' 'format-reftable' 'references.format' 'reftable'

test_repo_info 'bare repository = false is retrieved correctly' \
'git init' 'nonbare' 'layout.bare' 'false'

test_repo_info 'bare repository = true is retrieved correctly' \
'git init --bare' 'bare' 'layout.bare' 'true'

test_repo_info 'shallow repository = false is retrieved correctly' \
'git init' 'nonshallow' 'layout.shallow' 'false'

test_expect_success 'setup remote' '
git init remote &&
echo x >remote/x &&
git -C remote add x &&
git -C remote commit -m x
'

test_repo_info 'shallow repository = true is retrieved correctly' \
'git clone --depth 1 "file://$PWD/remote"' 'shallow' 'layout.shallow' 'true'

test_expect_success 'values returned in order requested' '
cat >expect <<-\EOF &&
layout.bare=false
references.format=files
layout.bare=false
EOF
git init --ref-format=files ordered &&
git -C ordered repo info layout.bare references.format layout.bare >actual &&
test_cmp expect actual
'

test_expect_success 'git-repo-info fails if an invalid key is requested' '
echo "error: key ${SQ}foo${SQ} not found" >expect &&
test_must_fail git repo info foo 2>actual &&
test_cmp expect actual
'

test_expect_success 'git-repo-info outputs data even if there is an invalid field' '
echo "references.format=$(test_detect_ref_format)" >expect &&
test_must_fail git repo info foo references.format bar >actual &&
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