mirror of
https://github.com/git/git.git
synced 2026-03-04 22:47:35 +01:00
Merge branch 'lo/repo-leftover-bits' into next
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
This commit is contained in:
@@ -668,6 +668,19 @@ For C programs:
|
||||
unsigned other_field:1;
|
||||
unsigned field_with_longer_name:1;
|
||||
|
||||
- Array names should be named in the singular form if the individual items are
|
||||
subject of use. E.g.:
|
||||
|
||||
char *dog[] = ...;
|
||||
walk_dog(dog[0]);
|
||||
walk_dog(dog[1]);
|
||||
|
||||
Cases where the array is employed as a whole rather than as its unit parts,
|
||||
the plural forms is preferable. E.g:
|
||||
|
||||
char *dogs[] = ...;
|
||||
walk_all_dogs(dogs);
|
||||
|
||||
For Perl programs:
|
||||
|
||||
- Most of the C guidelines above apply.
|
||||
|
||||
@@ -33,14 +33,14 @@ supported:
|
||||
+
|
||||
|
||||
`lines`:::
|
||||
output key-value pairs one per line using the `=` character as
|
||||
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 `lines`, but using a newline character as the delimiter
|
||||
between the key and the value and using a NUL character after each value.
|
||||
Similar to `lines`, 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
|
||||
`lines`. Unlike in the `lines` format, the values are never quoted.
|
||||
+
|
||||
@@ -80,7 +80,7 @@ supported:
|
||||
configuration variable `core.quotePath` (see linkgit:git-config[1]).
|
||||
|
||||
`nul`:::
|
||||
Similar to `lines`, but uses a NUL character to delimit between
|
||||
Similar to `lines`, but uses a _NUL_ character to delimit between
|
||||
key-value pairs instead of a newline. Also uses a newline character as
|
||||
the delimiter between the key and value instead of '='. Unlike the
|
||||
`lines` format, values containing "unusual" characters are never
|
||||
|
||||
@@ -31,7 +31,7 @@ enum output_format {
|
||||
FORMAT_NUL_TERMINATED,
|
||||
};
|
||||
|
||||
struct field {
|
||||
struct repo_info_field {
|
||||
const char *key;
|
||||
get_value_fn *get_value;
|
||||
};
|
||||
@@ -62,30 +62,32 @@ static int get_references_format(struct repository *repo, struct strbuf *buf)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* repo_info_fields keys must be in lexicographical order */
|
||||
static const struct field repo_info_fields[] = {
|
||||
/* repo_info_field keys must be in lexicographical order */
|
||||
static const struct repo_info_field repo_info_field[] = {
|
||||
{ "layout.bare", get_layout_bare },
|
||||
{ "layout.shallow", get_layout_shallow },
|
||||
{ "object.format", get_object_format },
|
||||
{ "references.format", get_references_format },
|
||||
};
|
||||
|
||||
static int repo_info_fields_cmp(const void *va, const void *vb)
|
||||
static int repo_info_field_cmp(const void *va, const void *vb)
|
||||
{
|
||||
const struct field *a = va;
|
||||
const struct field *b = vb;
|
||||
const struct repo_info_field *a = va;
|
||||
const struct repo_info_field *b = vb;
|
||||
|
||||
return strcmp(a->key, b->key);
|
||||
}
|
||||
|
||||
static get_value_fn *get_value_fn_for_key(const char *key)
|
||||
static const struct repo_info_field *get_repo_info_field(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;
|
||||
const struct repo_info_field search_key = { key, NULL };
|
||||
const struct repo_info_field *found = bsearch(&search_key,
|
||||
repo_info_field,
|
||||
ARRAY_SIZE(repo_info_field),
|
||||
sizeof(*found),
|
||||
repo_info_field_cmp);
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
static void print_field(enum output_format format, const char *key,
|
||||
@@ -113,18 +115,16 @@ static int print_fields(int argc, const char **argv,
|
||||
struct strbuf valbuf = STRBUF_INIT;
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
get_value_fn *get_value;
|
||||
const char *key = argv[i];
|
||||
const struct repo_info_field *field = get_repo_info_field(key);
|
||||
|
||||
get_value = get_value_fn_for_key(key);
|
||||
|
||||
if (!get_value) {
|
||||
if (!field) {
|
||||
ret = error(_("key '%s' not found"), key);
|
||||
continue;
|
||||
}
|
||||
|
||||
strbuf_reset(&valbuf);
|
||||
get_value(repo, &valbuf);
|
||||
field->get_value(repo, &valbuf);
|
||||
print_field(format, key, valbuf.buf);
|
||||
}
|
||||
|
||||
@@ -137,8 +137,8 @@ static int print_all_fields(struct repository *repo,
|
||||
{
|
||||
struct strbuf valbuf = STRBUF_INIT;
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
|
||||
const struct field *field = &repo_info_fields[i];
|
||||
for (size_t i = 0; i < ARRAY_SIZE(repo_info_field); i++) {
|
||||
const struct repo_info_field *field = &repo_info_field[i];
|
||||
|
||||
strbuf_reset(&valbuf);
|
||||
field->get_value(repo, &valbuf);
|
||||
@@ -164,8 +164,8 @@ static int print_keys(enum output_format format)
|
||||
die(_("--keys can only be used with --format=lines or --format=nul"));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(repo_info_fields); i++) {
|
||||
const struct field *field = &repo_info_fields[i];
|
||||
for (size_t i = 0; i < ARRAY_SIZE(repo_info_field); i++) {
|
||||
const struct repo_info_field *field = &repo_info_field[i];
|
||||
printf("%s%c", field->key, sep);
|
||||
}
|
||||
|
||||
|
||||
@@ -243,7 +243,7 @@ integration_tests = [
|
||||
't1700-split-index.sh',
|
||||
't1701-racy-split-index.sh',
|
||||
't1800-hook.sh',
|
||||
't1900-repo.sh',
|
||||
't1900-repo-info.sh',
|
||||
't1901-repo-structure.sh',
|
||||
't2000-conflict-when-checking-files-out.sh',
|
||||
't2002-checkout-cache-u.sh',
|
||||
|
||||
@@ -145,18 +145,18 @@ test_expect_success SHA1 'lines and nul format' '
|
||||
test_cmp expect out &&
|
||||
test_line_count = 0 err &&
|
||||
|
||||
# Replace key and value delimiters for nul format.
|
||||
tr "\n=" "\0\n" <expect >expect_nul &&
|
||||
git repo structure --format=nul >out 2>err &&
|
||||
tr "\012\000" "=\012" <out >actual &&
|
||||
|
||||
test_cmp expect_nul out &&
|
||||
test_cmp expect actual &&
|
||||
test_line_count = 0 err &&
|
||||
|
||||
# "-z", as a synonym to "--format=nul", participates in the
|
||||
# usual "last one wins" rule.
|
||||
git repo structure --format=table -z >out 2>err &&
|
||||
tr "\012\000" "=\012" <out >actual &&
|
||||
|
||||
test_cmp expect_nul out &&
|
||||
test_cmp expect actual &&
|
||||
test_line_count = 0 err
|
||||
)
|
||||
'
|
||||
|
||||
Reference in New Issue
Block a user