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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Oct 31, 2023 at 09:16 UTC
ff546ebb59d3a393e43d1ec7c97a08b5972a65ee
1 file changed
+28
-18
builtin/show-ref.c
+28
-18
@@ -20,7 +20,6 @@ static const char * const show_ref_usage[] = {
20
21
static int deref_tags, show_head, tags_only, heads_only, found_match, verify,
22
quiet, hash_only, abbrev, exclude_arg;
23
-static const char **pattern;
23
static const char *exclude_existing_arg;
24
25
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)
49
}
50
}
51
52
+struct show_ref_data {
53
+ const char **patterns;
54
+};
55
+
56
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)
58
{
59
+ struct show_ref_data *data = cbdata;
60
+
61
if (show_head && !strcmp(refname, "HEAD"))
62
goto match;
63
59
- if (pattern) {
64
+ if (data->patterns) {
65
int reflen = strlen(refname);
61
- const char **p = pattern, *m;
66
+ const char **p = data->patterns, *m;
67
while ((m = *p++) != NULL) {
68
int len = strlen(m);
69
if (len > reflen)
@@ -180,6 +185,9 @@ static const struct option show_ref_options[] = {
185
186
int cmd_show_ref(int argc, const char **argv, const char *prefix)
187
{
188
+ struct show_ref_data show_ref_data = {0};
189
+ const char **patterns;
190
+
191
git_config(git_default_config, NULL);
192
193
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)
196
if (exclude_arg)
197
return exclude_existing(exclude_existing_arg);
198
191
- pattern = argv;
192
- if (!*pattern)
193
- pattern = NULL;
199
+ patterns = argv;
200
+ if (!*patterns)
201
+ patterns = NULL;
202
203
if (verify) {
196
- if (!pattern)
204
+ if (!patterns)
205
die("--verify requires a reference");
198
- while (*pattern) {
206
+ while (*patterns) {
207
struct object_id oid;
208
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);
212
}
213
else if (!quiet)
206
- die("'%s' - not a valid ref", *pattern);
214
+ die("'%s' - not a valid ref", *patterns);
215
else
216
return 1;
209
- pattern++;
217
+ patterns++;
218
}
219
return 0;
220
}
221
222
+ show_ref_data.patterns = patterns;
223
+
224
if (show_head)
215
- head_ref(show_ref, NULL);
225
+ head_ref(show_ref, &show_ref_data);
226
if (heads_only || tags_only) {
227
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);
229
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);
231
} else {
222
- for_each_ref(show_ref, NULL);
232
+ for_each_ref(show_ref, &show_ref_data);
233
}
234
if (!found_match) {
235
if (verify && !quiet)