Skip to content

Commit a2371e3

Browse files
matheustavaresgitster
authored andcommitted
t/helper/test-config: facilitate addition of new cli options
test-config parses its arguments in an if-else chain, with one arm for each available subcommand. Every arm expects (and checks) that argv corresponds to something like "config <subcommand> [<subcommand args>]". This means that whenever we want to change the syntax to accommodate a new argument before <subcommand> (as we will do in the next patch), we also need to increment the indexes accessing argv everywhere in the if-else chain. This makes patches adding new options much noisier than they need to be, besides being error-prone. So let's skip the "config" argument in argv and argc to take the extra complexity out of such patches (as the following one). Signed-off-by: Matheus Tavares <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b77651b commit a2371e3

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

t/helper/test-config.c

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -84,33 +84,35 @@ int cmd__config(int argc, const char **argv)
8484
int i, val;
8585
const char *v;
8686
const struct string_list *strptr;
87-
struct config_set cs;
87+
struct config_set cs = { .hash_initialized = 0 };
8888
enum test_config_exit_code ret = TC_SUCCESS;
8989

90-
if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
91-
read_early_config(early_config_cb, (void *)argv[2]);
90+
argc--; /* skip over "config" */
91+
argv++;
92+
93+
if (argc == 0)
94+
goto print_usage_error;
95+
96+
if (argc == 2 && !strcmp(argv[0], "read_early_config")) {
97+
read_early_config(early_config_cb, (void *)argv[1]);
9298
return TC_SUCCESS;
9399
}
94100

95101
setup_git_directory();
96-
97102
git_configset_init(&cs);
98103

99-
if (argc < 2)
100-
goto print_usage_error;
101-
102-
if (argc == 3 && !strcmp(argv[1], "get_value")) {
103-
if (!git_config_get_value(argv[2], &v)) {
104+
if (argc == 2 && !strcmp(argv[0], "get_value")) {
105+
if (!git_config_get_value(argv[1], &v)) {
104106
if (!v)
105107
printf("(NULL)\n");
106108
else
107109
printf("%s\n", v);
108110
} else {
109-
printf("Value not found for \"%s\"\n", argv[2]);
111+
printf("Value not found for \"%s\"\n", argv[1]);
110112
ret = TC_VALUE_NOT_FOUND;
111113
}
112-
} else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
113-
strptr = git_config_get_value_multi(argv[2]);
114+
} else if (argc == 2 && !strcmp(argv[0], "get_value_multi")) {
115+
strptr = git_config_get_value_multi(argv[1]);
114116
if (strptr) {
115117
for (i = 0; i < strptr->nr; i++) {
116118
v = strptr->items[i].string;
@@ -120,58 +122,58 @@ int cmd__config(int argc, const char **argv)
120122
printf("%s\n", v);
121123
}
122124
} else {
123-
printf("Value not found for \"%s\"\n", argv[2]);
125+
printf("Value not found for \"%s\"\n", argv[1]);
124126
ret = TC_VALUE_NOT_FOUND;
125127
}
126-
} else if (argc == 3 && !strcmp(argv[1], "get_int")) {
127-
if (!git_config_get_int(argv[2], &val)) {
128+
} else if (argc == 2 && !strcmp(argv[0], "get_int")) {
129+
if (!git_config_get_int(argv[1], &val)) {
128130
printf("%d\n", val);
129131
} else {
130-
printf("Value not found for \"%s\"\n", argv[2]);
132+
printf("Value not found for \"%s\"\n", argv[1]);
131133
ret = TC_VALUE_NOT_FOUND;
132134
}
133-
} else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
134-
if (!git_config_get_bool(argv[2], &val)) {
135+
} else if (argc == 2 && !strcmp(argv[0], "get_bool")) {
136+
if (!git_config_get_bool(argv[1], &val)) {
135137
printf("%d\n", val);
136138
} else {
137-
printf("Value not found for \"%s\"\n", argv[2]);
139+
printf("Value not found for \"%s\"\n", argv[1]);
138140
ret = TC_VALUE_NOT_FOUND;
139141
}
140-
} else if (argc == 3 && !strcmp(argv[1], "get_string")) {
141-
if (!git_config_get_string_const(argv[2], &v)) {
142+
} else if (argc == 2 && !strcmp(argv[0], "get_string")) {
143+
if (!git_config_get_string_const(argv[1], &v)) {
142144
printf("%s\n", v);
143145
} else {
144-
printf("Value not found for \"%s\"\n", argv[2]);
146+
printf("Value not found for \"%s\"\n", argv[1]);
145147
ret = TC_VALUE_NOT_FOUND;
146148
}
147-
} else if (argc >= 3 && !strcmp(argv[1], "configset_get_value")) {
148-
for (i = 3; i < argc; i++) {
149+
} else if (argc >= 2 && !strcmp(argv[0], "configset_get_value")) {
150+
for (i = 2; i < argc; i++) {
149151
int err;
150152
if ((err = git_configset_add_file(&cs, argv[i]))) {
151153
fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
152154
ret = TC_CONFIG_FILE_ERROR;
153155
goto out;
154156
}
155157
}
156-
if (!git_configset_get_value(&cs, argv[2], &v)) {
158+
if (!git_configset_get_value(&cs, argv[1], &v)) {
157159
if (!v)
158160
printf("(NULL)\n");
159161
else
160162
printf("%s\n", v);
161163
} else {
162-
printf("Value not found for \"%s\"\n", argv[2]);
164+
printf("Value not found for \"%s\"\n", argv[1]);
163165
ret = TC_VALUE_NOT_FOUND;
164166
}
165-
} else if (argc >= 3 && !strcmp(argv[1], "configset_get_value_multi")) {
166-
for (i = 3; i < argc; i++) {
167+
} else if (argc >= 2 && !strcmp(argv[0], "configset_get_value_multi")) {
168+
for (i = 2; i < argc; i++) {
167169
int err;
168170
if ((err = git_configset_add_file(&cs, argv[i]))) {
169171
fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
170172
ret = TC_CONFIG_FILE_ERROR;
171173
goto out;
172174
}
173175
}
174-
strptr = git_configset_get_value_multi(&cs, argv[2]);
176+
strptr = git_configset_get_value_multi(&cs, argv[1]);
175177
if (strptr) {
176178
for (i = 0; i < strptr->nr; i++) {
177179
v = strptr->items[i].string;
@@ -181,10 +183,10 @@ int cmd__config(int argc, const char **argv)
181183
printf("%s\n", v);
182184
}
183185
} else {
184-
printf("Value not found for \"%s\"\n", argv[2]);
186+
printf("Value not found for \"%s\"\n", argv[1]);
185187
ret = TC_VALUE_NOT_FOUND;
186188
}
187-
} else if (!strcmp(argv[1], "iterate")) {
189+
} else if (!strcmp(argv[0], "iterate")) {
188190
git_config(iterate_cb, NULL);
189191
} else {
190192
print_usage_error:

0 commit comments

Comments
 (0)