builtin/show-ref: refactor options for patterns subcommand

The patterns subcommand is the last command that still uses global variables to track its options. Convert it to use a structure instead with the same motivation as preceding commits. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Oct 31, 2023 at 09:16 UTC ee26f1e29a7fb2d26946015a7cf946d6d112e432
1 file changed +22 -13
builtin/show-ref.c
+22 -13
@@ -18,8 +18,6 @@ static const char * const show_ref_usage[] = {
18 NULL
19 };
20
21 -static int show_head, tags_only, heads_only, verify;
22 -
21 struct show_one_options {
22 int quiet;
23 int hash_only;
@@ -59,6 +57,7 @@ struct show_ref_data {
57 const struct show_one_options *show_one_opts;
58 const char **patterns;
59 int found_match;
60 + int show_head;
61 };
62
63 static int show_ref(const char *refname, const struct object_id *oid,
@@ -66,7 +65,7 @@ static int show_ref(const char *refname, const struct object_id *oid,
65 {
66 struct show_ref_data *data = cbdata;
67
69 - if (show_head && !strcmp(refname, "HEAD"))
68 + if (data->show_head && !strcmp(refname, "HEAD"))
69 goto match;
70
71 if (data->patterns) {
@@ -184,22 +183,30 @@ static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
183 return 0;
184 }
185
187 -static int cmd_show_ref__patterns(const struct show_one_options *show_one_opts,
186 +struct patterns_options {
187 + int show_head;
188 + int heads_only;
189 + int tags_only;
190 +};
191 +
192 +static int cmd_show_ref__patterns(const struct patterns_options *opts,
193 + const struct show_one_options *show_one_opts,
194 const char **patterns)
195 {
196 struct show_ref_data show_ref_data = {
197 .show_one_opts = show_one_opts,
198 + .show_head = opts->show_head,
199 };
200
201 if (patterns && *patterns)
202 show_ref_data.patterns = patterns;
203
197 - if (show_head)
204 + if (opts->show_head)
205 head_ref(show_ref, &show_ref_data);
199 - if (heads_only || tags_only) {
200 - if (heads_only)
206 + if (opts->heads_only || opts->tags_only) {
207 + if (opts->heads_only)
208 for_each_fullref_in("refs/heads/", show_ref, &show_ref_data);
202 - if (tags_only)
209 + if (opts->tags_only)
210 for_each_fullref_in("refs/tags/", show_ref, &show_ref_data);
211 } else {
212 for_each_ref(show_ref, &show_ref_data);
@@ -237,15 +244,17 @@ static int exclude_existing_callback(const struct option *opt, const char *arg,
244 int cmd_show_ref(int argc, const char **argv, const char *prefix)
245 {
246 struct exclude_existing_options exclude_existing_opts = {0};
247 + struct patterns_options patterns_opts = {0};
248 struct show_one_options show_one_opts = {0};
249 + int verify = 0;
250 const struct option show_ref_options[] = {
242 - OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")),
243 - OPT_BOOL(0, "heads", &heads_only, N_("only show heads (can be combined with tags)")),
251 + OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with heads)")),
252 + OPT_BOOL(0, "heads", &patterns_opts.heads_only, N_("only show heads (can be combined with tags)")),
253 OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
254 "requires exact ref path")),
246 - OPT_HIDDEN_BOOL('h', NULL, &show_head,
255 + OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head,
256 N_("show the HEAD reference, even if it would be filtered out")),
248 - OPT_BOOL(0, "head", &show_head,
257 + OPT_BOOL(0, "head", &patterns_opts.show_head,
258 N_("show the HEAD reference, even if it would be filtered out")),
259 OPT_BOOL('d', "dereference", &show_one_opts.deref_tags,
260 N_("dereference tags into object IDs")),
@@ -271,5 +280,5 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
280 else if (verify)
281 return cmd_show_ref__verify(&show_one_opts, argv);
282 else
274 - return cmd_show_ref__patterns(&show_one_opts, argv);
283 + return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv);
284 }