Skip to content

Commit d3245bb

Browse files
committed
Merge branch 'lo/repo-info-step-2' into seen
* lo/repo-info-step-2: repo: add the field objects.format repo: add the flag -z as an alias for --format=nul
2 parents 4bd4949 + e39214d commit d3245bb

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
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+
`objects.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: 19 additions & 5 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_objects_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+
{ "objects.format", get_objects_format},
5259
{ "references.format", get_references_format },
5360
};
5461

@@ -115,20 +122,27 @@ static int print_fields(int argc, const char **argv,
115122
static int repo_info(int argc, const char **argv, const char *prefix,
116123
struct repository *repo)
117124
{
118-
const char *format_str = "keyvalue";
125+
const char *format_str = NULL;
119126
enum output_format format;
127+
int format_nul = 0;
120128
struct option options[] = {
121129
OPT_STRING(0, "format", &format_str, N_("format"),
122130
N_("output format")),
131+
OPT_BOOL('z', NULL, &format_nul, N_("alias for --format=nul")),
123132
OPT_END()
124133
};
125134

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

128-
if (!strcmp(format_str, "keyvalue"))
129-
format = FORMAT_KEYVALUE;
130-
else if (!strcmp(format_str, "nul"))
137+
die_for_incompatible_opt2(!!format_nul, "-z",
138+
!!format_str, "--format");
139+
140+
format_str = format_str ? format_str : "keyvalue";
141+
142+
if (format_nul || !strcmp(format_str, "nul"))
131143
format = FORMAT_NUL_TERMINATED;
144+
else if (!strcmp(format_str, "keyvalue"))
145+
format = FORMAT_KEYVALUE;
132146
else
133147
die(_("invalid format '%s'"), format_str);
134148

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 'objects.format = sha1 is retrieved correctly' \
67+
'git init --object-format=sha1' 'sha1' 'objects.format' 'sha1'
68+
69+
test_repo_info 'objects.format = sha256 is retrieved correctly' \
70+
'git init --object-format=sha256' 'sha256' 'objects.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 fails when using --format and -z' '
108+
echo "fatal: options ${SQ}-z${SQ} and ${SQ}--format${SQ} cannot be used together" >expected &&
109+
test_must_fail git repo info -z --format=keyvalue 2>actual &&
110+
test_cmp expected actual
111+
'
112+
95113
test_done

0 commit comments

Comments
 (0)