Skip to content

Commit 5e1c06a

Browse files
committed
Merge branch 'lo/repo-leftover-bits' into jch
Clean-up the code around "git repo info" command. * lo/repo-leftover-bits: Documentation/git-repo: capitalize format descriptions Documentation/git-repo: replace 'NUL' with '_NUL_' t1901: adjust nul format output instead of expected value t1900: rename t1900-repo to t1900-repo-info repo: rename struct field to repo_info_field repo: replace get_value_fn_for_key by get_repo_info_field repo: rename repo_info_fields to repo_info_field CodingGuidelines: instruct to name arrays in singular
2 parents f826b16 + 8b97dc3 commit 5e1c06a

6 files changed

Lines changed: 44 additions & 31 deletions

File tree

Documentation/CodingGuidelines

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,19 @@ For C programs:
668668
unsigned other_field:1;
669669
unsigned field_with_longer_name:1;
670670

671+
- Array names should be named in the singular form if the individual items are
672+
subject of use. E.g.:
673+
674+
char *dog[] = ...;
675+
walk_dog(dog[0]);
676+
walk_dog(dog[1]);
677+
678+
Cases where the array is employed as a whole rather than as its unit parts,
679+
the plural forms is preferable. E.g:
680+
681+
char *dogs[] = ...;
682+
walk_all_dogs(dogs);
683+
671684
For Perl programs:
672685

673686
- Most of the C guidelines above apply.

Documentation/git-repo.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ supported:
3333
+
3434

3535
`lines`:::
36-
output key-value pairs one per line using the `=` character as
36+
Output key-value pairs one per line using the `=` character as
3737
the delimiter between the key and the value. Values containing "unusual"
3838
characters are quoted as explained for the configuration variable
3939
`core.quotePath` (see linkgit:git-config[1]). This is the default.
4040

4141
`nul`:::
42-
similar to `lines`, but using a newline character as the delimiter
43-
between the key and the value and using a NUL character after each value.
42+
Similar to `lines`, but using a newline character as the delimiter
43+
between the key and the value and using a _NUL_ character after each value.
4444
This format is better suited for being parsed by another applications than
4545
`lines`. Unlike in the `lines` format, the values are never quoted.
4646
+
@@ -80,7 +80,7 @@ supported:
8080
configuration variable `core.quotePath` (see linkgit:git-config[1]).
8181

8282
`nul`:::
83-
Similar to `lines`, but uses a NUL character to delimit between
83+
Similar to `lines`, but uses a _NUL_ character to delimit between
8484
key-value pairs instead of a newline. Also uses a newline character as
8585
the delimiter between the key and value instead of '='. Unlike the
8686
`lines` format, values containing "unusual" characters are never

builtin/repo.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum output_format {
3131
FORMAT_NUL_TERMINATED,
3232
};
3333

34-
struct field {
34+
struct repo_info_field {
3535
const char *key;
3636
get_value_fn *get_value;
3737
};
@@ -62,30 +62,32 @@ static int get_references_format(struct repository *repo, struct strbuf *buf)
6262
return 0;
6363
}
6464

65-
/* repo_info_fields keys must be in lexicographical order */
66-
static const struct field repo_info_fields[] = {
65+
/* repo_info_field keys must be in lexicographical order */
66+
static const struct repo_info_field repo_info_field[] = {
6767
{ "layout.bare", get_layout_bare },
6868
{ "layout.shallow", get_layout_shallow },
6969
{ "object.format", get_object_format },
7070
{ "references.format", get_references_format },
7171
};
7272

73-
static int repo_info_fields_cmp(const void *va, const void *vb)
73+
static int repo_info_field_cmp(const void *va, const void *vb)
7474
{
75-
const struct field *a = va;
76-
const struct field *b = vb;
75+
const struct repo_info_field *a = va;
76+
const struct repo_info_field *b = vb;
7777

7878
return strcmp(a->key, b->key);
7979
}
8080

81-
static get_value_fn *get_value_fn_for_key(const char *key)
81+
static const struct repo_info_field *get_repo_info_field(const char *key)
8282
{
83-
const struct field search_key = { key, NULL };
84-
const struct field *found = bsearch(&search_key, repo_info_fields,
85-
ARRAY_SIZE(repo_info_fields),
86-
sizeof(*found),
87-
repo_info_fields_cmp);
88-
return found ? found->get_value : NULL;
83+
const struct repo_info_field search_key = { key, NULL };
84+
const struct repo_info_field *found = bsearch(&search_key,
85+
repo_info_field,
86+
ARRAY_SIZE(repo_info_field),
87+
sizeof(*found),
88+
repo_info_field_cmp);
89+
90+
return found;
8991
}
9092

9193
static void print_field(enum output_format format, const char *key,
@@ -113,18 +115,16 @@ static int print_fields(int argc, const char **argv,
113115
struct strbuf valbuf = STRBUF_INIT;
114116

115117
for (int i = 0; i < argc; i++) {
116-
get_value_fn *get_value;
117118
const char *key = argv[i];
119+
const struct repo_info_field *field = get_repo_info_field(key);
118120

119-
get_value = get_value_fn_for_key(key);
120-
121-
if (!get_value) {
121+
if (!field) {
122122
ret = error(_("key '%s' not found"), key);
123123
continue;
124124
}
125125

126126
strbuf_reset(&valbuf);
127-
get_value(repo, &valbuf);
127+
field->get_value(repo, &valbuf);
128128
print_field(format, key, valbuf.buf);
129129
}
130130

@@ -137,8 +137,8 @@ static int print_all_fields(struct repository *repo,
137137
{
138138
struct strbuf valbuf = STRBUF_INIT;
139139

140-
for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
141-
const struct field *field = &repo_info_fields[i];
140+
for (size_t i = 0; i < ARRAY_SIZE(repo_info_field); i++) {
141+
const struct repo_info_field *field = &repo_info_field[i];
142142

143143
strbuf_reset(&valbuf);
144144
field->get_value(repo, &valbuf);
@@ -164,8 +164,8 @@ static int print_keys(enum output_format format)
164164
die(_("--keys can only be used with --format=lines or --format=nul"));
165165
}
166166

167-
for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
168-
const struct field *field = &repo_info_fields[i];
167+
for (size_t i = 0; i < ARRAY_SIZE(repo_info_field); i++) {
168+
const struct repo_info_field *field = &repo_info_field[i];
169169
printf("%s%c", field->key, sep);
170170
}
171171

t/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ integration_tests = [
243243
't1700-split-index.sh',
244244
't1701-racy-split-index.sh',
245245
't1800-hook.sh',
246-
't1900-repo.sh',
246+
't1900-repo-info.sh',
247247
't1901-repo-structure.sh',
248248
't2000-conflict-when-checking-files-out.sh',
249249
't2002-checkout-cache-u.sh',
File renamed without changes.

t/t1901-repo-structure.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,18 @@ test_expect_success SHA1 'lines and nul format' '
145145
test_cmp expect out &&
146146
test_line_count = 0 err &&
147147
148-
# Replace key and value delimiters for nul format.
149-
tr "\n=" "\0\n" <expect >expect_nul &&
150148
git repo structure --format=nul >out 2>err &&
149+
tr "\012\000" "=\012" <out >actual &&
151150
152-
test_cmp expect_nul out &&
151+
test_cmp expect actual &&
153152
test_line_count = 0 err &&
154153
155154
# "-z", as a synonym to "--format=nul", participates in the
156155
# usual "last one wins" rule.
157156
git repo structure --format=table -z >out 2>err &&
157+
tr "\012\000" "=\012" <out >actual &&
158158
159-
test_cmp expect_nul out &&
159+
test_cmp expect actual &&
160160
test_line_count = 0 err
161161
)
162162
'

0 commit comments

Comments
 (0)