Skip to content

Commit ff546eb

Browse files
pks-tgitster
authored andcommitted
builtin/show-ref: convert pattern to a local variable
The `pattern` variable is a global variable that tracks either the reference names (not patterns!) for the `--verify` mode or the patterns for the non-verify mode. This is a bit confusing due to the slightly different meanings. Convert the variable to be local. While this does not yet fix the double meaning of the variable, this change allows us to address it in a subsequent patch more easily by explicitly splitting up the different subcommands of git-show-ref(1). Note that we introduce a `struct show_ref_data` to pass the patterns to `show_ref()`. While this is overengineered now, we will extend this structure in a subsequent patch. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2e8e77c commit ff546eb

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

builtin/show-ref.c

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ static const char * const show_ref_usage[] = {
2020

2121
static int deref_tags, show_head, tags_only, heads_only, found_match, verify,
2222
quiet, hash_only, abbrev, exclude_arg;
23-
static const char **pattern;
2423
static const char *exclude_existing_arg;
2524

2625
static void show_one(const char *refname, const struct object_id *oid)
@@ -50,15 +49,21 @@ static void show_one(const char *refname, const struct object_id *oid)
5049
}
5150
}
5251

52+
struct show_ref_data {
53+
const char **patterns;
54+
};
55+
5356
static int show_ref(const char *refname, const struct object_id *oid,
54-
int flag UNUSED, void *cbdata UNUSED)
57+
int flag UNUSED, void *cbdata)
5558
{
59+
struct show_ref_data *data = cbdata;
60+
5661
if (show_head && !strcmp(refname, "HEAD"))
5762
goto match;
5863

59-
if (pattern) {
64+
if (data->patterns) {
6065
int reflen = strlen(refname);
61-
const char **p = pattern, *m;
66+
const char **p = data->patterns, *m;
6267
while ((m = *p++) != NULL) {
6368
int len = strlen(m);
6469
if (len > reflen)
@@ -180,6 +185,9 @@ static const struct option show_ref_options[] = {
180185

181186
int cmd_show_ref(int argc, const char **argv, const char *prefix)
182187
{
188+
struct show_ref_data show_ref_data = {0};
189+
const char **patterns;
190+
183191
git_config(git_default_config, NULL);
184192

185193
argc = parse_options(argc, argv, prefix, show_ref_options,
@@ -188,38 +196,40 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
188196
if (exclude_arg)
189197
return exclude_existing(exclude_existing_arg);
190198

191-
pattern = argv;
192-
if (!*pattern)
193-
pattern = NULL;
199+
patterns = argv;
200+
if (!*patterns)
201+
patterns = NULL;
194202

195203
if (verify) {
196-
if (!pattern)
204+
if (!patterns)
197205
die("--verify requires a reference");
198-
while (*pattern) {
206+
while (*patterns) {
199207
struct object_id oid;
200208

201-
if ((starts_with(*pattern, "refs/") || !strcmp(*pattern, "HEAD")) &&
202-
!read_ref(*pattern, &oid)) {
203-
show_one(*pattern, &oid);
209+
if ((starts_with(*patterns, "refs/") || !strcmp(*patterns, "HEAD")) &&
210+
!read_ref(*patterns, &oid)) {
211+
show_one(*patterns, &oid);
204212
}
205213
else if (!quiet)
206-
die("'%s' - not a valid ref", *pattern);
214+
die("'%s' - not a valid ref", *patterns);
207215
else
208216
return 1;
209-
pattern++;
217+
patterns++;
210218
}
211219
return 0;
212220
}
213221

222+
show_ref_data.patterns = patterns;
223+
214224
if (show_head)
215-
head_ref(show_ref, NULL);
225+
head_ref(show_ref, &show_ref_data);
216226
if (heads_only || tags_only) {
217227
if (heads_only)
218-
for_each_fullref_in("refs/heads/", show_ref, NULL);
228+
for_each_fullref_in("refs/heads/", show_ref, &show_ref_data);
219229
if (tags_only)
220-
for_each_fullref_in("refs/tags/", show_ref, NULL);
230+
for_each_fullref_in("refs/tags/", show_ref, &show_ref_data);
221231
} else {
222-
for_each_ref(show_ref, NULL);
232+
for_each_ref(show_ref, &show_ref_data);
223233
}
224234
if (!found_match) {
225235
if (verify && !quiet)

0 commit comments

Comments
 (0)