Skip to content

Commit b0f0be9

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

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

builtin/show-ref.c

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

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;
2322

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)
2532
{
2633
const char *hex;
2734
struct object_id peeled;
@@ -30,25 +37,26 @@ static void show_one(const char *refname, const struct object_id *oid)
3037
die("git show-ref: bad ref %s (%s)", refname,
3138
oid_to_hex(oid));
3239

33-
if (quiet)
40+
if (opts->quiet)
3441
return;
3542

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)
3845
printf("%s\n", hex);
3946
else
4047
printf("%s %s\n", hex, refname);
4148

42-
if (!deref_tags)
49+
if (!opts->deref_tags)
4350
return;
4451

4552
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);
4754
printf("%s %s^{}\n", hex, refname);
4855
}
4956
}
5057

5158
struct show_ref_data {
59+
const struct show_one_options *show_one_opts;
5260
const char **patterns;
5361
int found_match;
5462
};
@@ -81,7 +89,7 @@ static int show_ref(const char *refname, const struct object_id *oid,
8189
match:
8290
data->found_match++;
8391

84-
show_one(refname, oid);
92+
show_one(data->show_one_opts, refname, oid);
8593

8694
return 0;
8795
}
@@ -153,7 +161,8 @@ static int cmd_show_ref__exclude_existing(const struct exclude_existing_options
153161
return 0;
154162
}
155163

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)
157166
{
158167
if (!refs || !*refs)
159168
die("--verify requires a reference");
@@ -163,9 +172,9 @@ static int cmd_show_ref__verify(const char **refs)
163172

164173
if ((starts_with(*refs, "refs/") || !strcmp(*refs, "HEAD")) &&
165174
!read_ref(*refs, &oid)) {
166-
show_one(*refs, &oid);
175+
show_one(show_one_opts, *refs, &oid);
167176
}
168-
else if (!quiet)
177+
else if (!show_one_opts->quiet)
169178
die("'%s' - not a valid ref", *refs);
170179
else
171180
return 1;
@@ -175,9 +184,12 @@ static int cmd_show_ref__verify(const char **refs)
175184
return 0;
176185
}
177186

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)
179189
{
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+
};
181193

182194
if (patterns && *patterns)
183195
show_ref_data.patterns = patterns;
@@ -200,11 +212,16 @@ static int cmd_show_ref__patterns(const char **patterns)
200212

201213
static int hash_callback(const struct option *opt, const char *arg, int unset)
202214
{
203-
hash_only = 1;
215+
struct show_one_options *opts = opt->value;
216+
struct option abbrev_opt = *opt;
217+
218+
opts->hash_only = 1;
204219
/* Use full length SHA1 if no argument */
205220
if (!arg)
206221
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);
208225
}
209226

210227
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,
220237
int cmd_show_ref(int argc, const char **argv, const char *prefix)
221238
{
222239
struct exclude_existing_options exclude_existing_opts = {0};
240+
struct show_one_options show_one_opts = {0};
223241
const struct option show_ref_options[] = {
224242
OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")),
225243
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)
229247
N_("show the HEAD reference, even if it would be filtered out")),
230248
OPT_BOOL(0, "head", &show_head,
231249
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,
233251
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"),
235253
N_("only show SHA1 hash using <n> digits"),
236254
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,
239257
N_("do not print results to stdout (useful with --verify)")),
240258
OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts,
241259
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)
251269
if (exclude_existing_opts.enabled)
252270
return cmd_show_ref__exclude_existing(&exclude_existing_opts);
253271
else if (verify)
254-
return cmd_show_ref__verify(argv);
272+
return cmd_show_ref__verify(&show_one_opts, argv);
255273
else
256-
return cmd_show_ref__patterns(argv);
274+
return cmd_show_ref__patterns(&show_one_opts, argv);
257275
}

0 commit comments

Comments
 (0)