builtin/show-ref: stop using global vars for `show_one()`

The `show_one()` function implicitly receives a bunch of options which are tracked via global variables. This makes it hard to see which subcommands of git-show-ref(1) actually make use of these options. Introduce a `show_one_options` structure that gets passed down to this function. This allows us to get rid of more global state and makes it more explicit which subcommands use those options. 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 b0f0be939841376fa0b8dab963ff439dae5226c3
1 file changed +40 -22
builtin/show-ref.c
+40 -22
@@ -18,10 +18,17 @@ static const char * const show_ref_usage[] = {
18 NULL
19 };
20
21 -static int deref_tags, show_head, tags_only, heads_only, verify,
22 - quiet, hash_only, abbrev;
21 +static int show_head, tags_only, heads_only, verify;
22
24 -static void show_one(const char *refname, const struct object_id *oid)
23 +struct show_one_options {
24 + int quiet;
25 + int hash_only;
26 + int abbrev;
27 + int deref_tags;
28 +};
29 +
30 +static void show_one(const struct show_one_options *opts,
31 + const char *refname, const struct object_id *oid)
32 {
33 const char *hex;
34 struct object_id peeled;
@@ -30,25 +37,26 @@ static void show_one(const char *refname, const struct object_id *oid)
37 die("git show-ref: bad ref %s (%s)", refname,
38 oid_to_hex(oid));
39
33 - if (quiet)
40 + if (opts->quiet)
41 return;
42
36 - hex = repo_find_unique_abbrev(the_repository, oid, abbrev);
37 - if (hash_only)
43 + hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev);
44 + if (opts->hash_only)
45 printf("%s\n", hex);
46 else
47 printf("%s %s\n", hex, refname);
48
42 - if (!deref_tags)
49 + if (!opts->deref_tags)
50 return;
51
52 if (!peel_iterated_oid(oid, &peeled)) {
46 - hex = repo_find_unique_abbrev(the_repository, &peeled, abbrev);
53 + hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev);
54 printf("%s %s^{}\n", hex, refname);
55 }
56 }
57
58 struct show_ref_data {
59 + const struct show_one_options *show_one_opts;
60 const char **patterns;
61 int found_match;
62 };
@@ -81,7 +89,7 @@ static int show_ref(const char *refname, const struct object_id *oid,
89 match:
90 data->found_match++;
91
84 - show_one(refname, oid);
92 + show_one(data->show_one_opts, refname, oid);
93
94 return 0;
95 }
@@ -153,7 +161,8 @@ static int cmd_show_ref__exclude_existing(const struct exclude_existing_options
161 return 0;
162 }
163
156 -static int cmd_show_ref__verify(const char **refs)
164 +static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
165 + const char **refs)
166 {
167 if (!refs || !*refs)
168 die("--verify requires a reference");
@@ -163,9 +172,9 @@ static int cmd_show_ref__verify(const char **refs)
172
173 if ((starts_with(*refs, "refs/") || !strcmp(*refs, "HEAD")) &&
174 !read_ref(*refs, &oid)) {
166 - show_one(*refs, &oid);
175 + show_one(show_one_opts, *refs, &oid);
176 }
168 - else if (!quiet)
177 + else if (!show_one_opts->quiet)
178 die("'%s' - not a valid ref", *refs);
179 else
180 return 1;
@@ -175,9 +184,12 @@ static int cmd_show_ref__verify(const char **refs)
184 return 0;
185 }
186
178 -static int cmd_show_ref__patterns(const char **patterns)
187 +static int cmd_show_ref__patterns(const struct show_one_options *show_one_opts,
188 + const char **patterns)
189 {
180 - struct show_ref_data show_ref_data = {0};
190 + struct show_ref_data show_ref_data = {
191 + .show_one_opts = show_one_opts,
192 + };
193
194 if (patterns && *patterns)
195 show_ref_data.patterns = patterns;
@@ -200,11 +212,16 @@ static int cmd_show_ref__patterns(const char **patterns)
212
213 static int hash_callback(const struct option *opt, const char *arg, int unset)
214 {
203 - hash_only = 1;
215 + struct show_one_options *opts = opt->value;
216 + struct option abbrev_opt = *opt;
217 +
218 + opts->hash_only = 1;
219 /* Use full length SHA1 if no argument */
220 if (!arg)
221 return 0;
207 - return parse_opt_abbrev_cb(opt, arg, unset);
222 +
223 + abbrev_opt.value = &opts->abbrev;
224 + return parse_opt_abbrev_cb(&abbrev_opt, arg, unset);
225 }
226
227 static int exclude_existing_callback(const struct option *opt, const char *arg,
@@ -220,6 +237,7 @@ static int exclude_existing_callback(const struct option *opt, const char *arg,
237 int cmd_show_ref(int argc, const char **argv, const char *prefix)
238 {
239 struct exclude_existing_options exclude_existing_opts = {0};
240 + struct show_one_options show_one_opts = {0};
241 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)")),
@@ -229,13 +247,13 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
247 N_("show the HEAD reference, even if it would be filtered out")),
248 OPT_BOOL(0, "head", &show_head,
249 N_("show the HEAD reference, even if it would be filtered out")),
232 - OPT_BOOL('d', "dereference", &deref_tags,
250 + OPT_BOOL('d', "dereference", &show_one_opts.deref_tags,
251 N_("dereference tags into object IDs")),
234 - OPT_CALLBACK_F('s', "hash", &abbrev, N_("n"),
252 + OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"),
253 N_("only show SHA1 hash using <n> digits"),
254 PARSE_OPT_OPTARG, &hash_callback),
237 - OPT__ABBREV(&abbrev),
238 - OPT__QUIET(&quiet,
255 + OPT__ABBREV(&show_one_opts.abbrev),
256 + OPT__QUIET(&show_one_opts.quiet,
257 N_("do not print results to stdout (useful with --verify)")),
258 OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts,
259 N_("pattern"), N_("show refs from stdin that aren't in local repository"),
@@ -251,7 +269,7 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
269 if (exclude_existing_opts.enabled)
270 return cmd_show_ref__exclude_existing(&exclude_existing_opts);
271 else if (verify)
254 - return cmd_show_ref__verify(argv);
272 + return cmd_show_ref__verify(&show_one_opts, argv);
273 else
256 - return cmd_show_ref__patterns(argv);
274 + return cmd_show_ref__patterns(&show_one_opts, argv);
275 }