Skip to content

Commit 13d1e86

Browse files
committed
Merge branch 'lo/repo-info-step-2'
"repo info" learns a short-hand option "-z" that is the same as "--format=nul", and learns to report the objects format used in the repository. * lo/repo-info-step-2: repo: add the field objects.format repo: add the flag -z as an alias for --format=nul
2 parents 7d00521 + c2e3713 commit 13d1e86

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

Documentation/git-repo.adoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-repo - Retrieve information about the repository
88
SYNOPSIS
99
--------
1010
[synopsis]
11-
git repo info [--format=(keyvalue|nul)] [<key>...]
11+
git repo info [--format=(keyvalue|nul)] [-z] [<key>...]
1212

1313
DESCRIPTION
1414
-----------
@@ -18,7 +18,7 @@ THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
1818

1919
COMMANDS
2020
--------
21-
`info [--format=(keyvalue|nul)] [<key>...]`::
21+
`info [--format=(keyvalue|nul)] [-z] [<key>...]`::
2222
Retrieve metadata-related information about the current repository. Only
2323
the requested data will be returned based on their keys (see "INFO KEYS"
2424
section below).
@@ -40,6 +40,8 @@ supported:
4040
between the key and the value and using a NUL character after each value.
4141
This format is better suited for being parsed by another applications than
4242
`keyvalue`. Unlike in the `keyvalue` format, the values are never quoted.
43+
+
44+
`-z` is an alias for `--format=nul`.
4345

4446
INFO KEYS
4547
---------
@@ -53,6 +55,9 @@ values that they return:
5355
`layout.shallow`::
5456
`true` if this is a shallow repository, otherwise `false`.
5557

58+
`object.format`::
59+
The object format (hash algorithm) used in the repository.
60+
5661
`references.format`::
5762
The reference storage format. The valid values are:
5863
+

builtin/repo.c

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "shallow.h"
1010

1111
static const char *const repo_usage[] = {
12-
"git repo info [--format=(keyvalue|nul)] [<key>...]",
12+
"git repo info [--format=(keyvalue|nul)] [-z] [<key>...]",
1313
NULL
1414
};
1515

@@ -38,6 +38,12 @@ static int get_layout_shallow(struct repository *repo, struct strbuf *buf)
3838
return 0;
3939
}
4040

41+
static int get_object_format(struct repository *repo, struct strbuf *buf)
42+
{
43+
strbuf_addstr(buf, repo->hash_algo->name);
44+
return 0;
45+
}
46+
4147
static int get_references_format(struct repository *repo, struct strbuf *buf)
4248
{
4349
strbuf_addstr(buf,
@@ -49,6 +55,7 @@ static int get_references_format(struct repository *repo, struct strbuf *buf)
4955
static const struct field repo_info_fields[] = {
5056
{ "layout.bare", get_layout_bare },
5157
{ "layout.shallow", get_layout_shallow },
58+
{ "object.format", get_object_format },
5259
{ "references.format", get_references_format },
5360
};
5461

@@ -112,26 +119,40 @@ static int print_fields(int argc, const char **argv,
112119
return ret;
113120
}
114121

122+
static int parse_format_cb(const struct option *opt,
123+
const char *arg, int unset UNUSED)
124+
{
125+
enum output_format *format = opt->value;
126+
127+
if (opt->short_name == 'z')
128+
*format = FORMAT_NUL_TERMINATED;
129+
else if (!strcmp(arg, "nul"))
130+
*format = FORMAT_NUL_TERMINATED;
131+
else if (!strcmp(arg, "keyvalue"))
132+
*format = FORMAT_KEYVALUE;
133+
else
134+
die(_("invalid format '%s'"), arg);
135+
136+
return 0;
137+
}
138+
115139
static int repo_info(int argc, const char **argv, const char *prefix,
116140
struct repository *repo)
117141
{
118-
const char *format_str = "keyvalue";
119-
enum output_format format;
142+
enum output_format format = FORMAT_KEYVALUE;
120143
struct option options[] = {
121-
OPT_STRING(0, "format", &format_str, N_("format"),
122-
N_("output format")),
144+
OPT_CALLBACK_F(0, "format", &format, N_("format"),
145+
N_("output format"),
146+
PARSE_OPT_NONEG, parse_format_cb),
147+
OPT_CALLBACK_F('z', NULL, &format, NULL,
148+
N_("synonym for --format=nul"),
149+
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
150+
parse_format_cb),
123151
OPT_END()
124152
};
125153

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

128-
if (!strcmp(format_str, "keyvalue"))
129-
format = FORMAT_KEYVALUE;
130-
else if (!strcmp(format_str, "nul"))
131-
format = FORMAT_NUL_TERMINATED;
132-
else
133-
die(_("invalid format '%s'"), format_str);
134-
135156
return print_fields(argc, argv, repo, format);
136157
}
137158

t/t1900-repo.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ test_expect_success 'setup remote' '
6363
test_repo_info 'shallow repository = true is retrieved correctly' \
6464
'git clone --depth 1 "file://$PWD/remote"' 'shallow' 'layout.shallow' 'true'
6565

66+
test_repo_info 'object.format = sha1 is retrieved correctly' \
67+
'git init --object-format=sha1' 'sha1' 'object.format' 'sha1'
68+
69+
test_repo_info 'object.format = sha256 is retrieved correctly' \
70+
'git init --object-format=sha256' 'sha256' 'object.format' 'sha256'
71+
6672
test_expect_success 'values returned in order requested' '
6773
cat >expect <<-\EOF &&
6874
layout.bare=false
@@ -92,4 +98,16 @@ test_expect_success 'git-repo-info aborts when requesting an invalid format' '
9298
test_cmp expect actual
9399
'
94100

101+
test_expect_success '-z uses nul-terminated format' '
102+
printf "layout.bare\nfalse\0layout.shallow\nfalse\0" >expected &&
103+
git repo info -z layout.bare layout.shallow >actual &&
104+
test_cmp expected actual
105+
'
106+
107+
test_expect_success 'git repo info uses the last requested format' '
108+
echo "layout.bare=false" >expected &&
109+
git repo info --format=nul -z --format=keyvalue layout.bare >actual &&
110+
test_cmp expected actual
111+
'
112+
95113
test_done

0 commit comments

Comments
 (0)