Skip to content

Commit ee26f1e

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b0f0be9 commit ee26f1e

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

builtin/show-ref.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ static const char * const show_ref_usage[] = {
1818
NULL
1919
};
2020

21-
static int show_head, tags_only, heads_only, verify;
22-
2321
struct show_one_options {
2422
int quiet;
2523
int hash_only;
@@ -59,14 +57,15 @@ struct show_ref_data {
5957
const struct show_one_options *show_one_opts;
6058
const char **patterns;
6159
int found_match;
60+
int show_head;
6261
};
6362

6463
static int show_ref(const char *refname, const struct object_id *oid,
6564
int flag UNUSED, void *cbdata)
6665
{
6766
struct show_ref_data *data = cbdata;
6867

69-
if (show_head && !strcmp(refname, "HEAD"))
68+
if (data->show_head && !strcmp(refname, "HEAD"))
7069
goto match;
7170

7271
if (data->patterns) {
@@ -184,22 +183,30 @@ static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
184183
return 0;
185184
}
186185

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,
188194
const char **patterns)
189195
{
190196
struct show_ref_data show_ref_data = {
191197
.show_one_opts = show_one_opts,
198+
.show_head = opts->show_head,
192199
};
193200

194201
if (patterns && *patterns)
195202
show_ref_data.patterns = patterns;
196203

197-
if (show_head)
204+
if (opts->show_head)
198205
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)
201208
for_each_fullref_in("refs/heads/", show_ref, &show_ref_data);
202-
if (tags_only)
209+
if (opts->tags_only)
203210
for_each_fullref_in("refs/tags/", show_ref, &show_ref_data);
204211
} else {
205212
for_each_ref(show_ref, &show_ref_data);
@@ -237,15 +244,17 @@ static int exclude_existing_callback(const struct option *opt, const char *arg,
237244
int cmd_show_ref(int argc, const char **argv, const char *prefix)
238245
{
239246
struct exclude_existing_options exclude_existing_opts = {0};
247+
struct patterns_options patterns_opts = {0};
240248
struct show_one_options show_one_opts = {0};
249+
int verify = 0;
241250
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)")),
244253
OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
245254
"requires exact ref path")),
246-
OPT_HIDDEN_BOOL('h', NULL, &show_head,
255+
OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head,
247256
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,
249258
N_("show the HEAD reference, even if it would be filtered out")),
250259
OPT_BOOL('d', "dereference", &show_one_opts.deref_tags,
251260
N_("dereference tags into object IDs")),
@@ -271,5 +280,5 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
271280
else if (verify)
272281
return cmd_show_ref__verify(&show_one_opts, argv);
273282
else
274-
return cmd_show_ref__patterns(&show_one_opts, argv);
283+
return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv);
275284
}

0 commit comments

Comments
 (0)