Skip to content

Commit b14cbae

Browse files
pks-tgitster
authored andcommitted
builtin/show-ref: split up different subcommands
While not immediately obvious, git-show-ref(1) actually implements three different subcommands: - `git show-ref <patterns>` can be used to list references that match a specific pattern. - `git show-ref --verify <refs>` can be used to list references. These are _not_ patterns. - `git show-ref --exclude-existing` can be used as a filter that reads references from standard input, performing some conversions on each of them. Let's make this more explicit in the code by splitting up the three subcommands into separate functions. This also allows us to address the confusingly named `patterns` variable, which may hold either patterns or reference names. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ff546eb commit b14cbae

File tree

1 file changed

+54
-47
lines changed

1 file changed

+54
-47
lines changed

builtin/show-ref.c

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static int add_existing(const char *refname,
104104
* (4) ignore if refname is a ref that exists in the local repository;
105105
* (5) otherwise output the line.
106106
*/
107-
static int exclude_existing(const char *match)
107+
static int cmd_show_ref__exclude_existing(const char *match)
108108
{
109109
static struct string_list existing_refs = STRING_LIST_INIT_DUP;
110110
char buf[1024];
@@ -142,6 +142,54 @@ static int exclude_existing(const char *match)
142142
return 0;
143143
}
144144

145+
static int cmd_show_ref__verify(const char **refs)
146+
{
147+
if (!refs || !*refs)
148+
die("--verify requires a reference");
149+
150+
while (*refs) {
151+
struct object_id oid;
152+
153+
if ((starts_with(*refs, "refs/") || !strcmp(*refs, "HEAD")) &&
154+
!read_ref(*refs, &oid)) {
155+
show_one(*refs, &oid);
156+
}
157+
else if (!quiet)
158+
die("'%s' - not a valid ref", *refs);
159+
else
160+
return 1;
161+
refs++;
162+
}
163+
164+
return 0;
165+
}
166+
167+
static int cmd_show_ref__patterns(const char **patterns)
168+
{
169+
struct show_ref_data show_ref_data = {0};
170+
171+
if (patterns && *patterns)
172+
show_ref_data.patterns = patterns;
173+
174+
if (show_head)
175+
head_ref(show_ref, &show_ref_data);
176+
if (heads_only || tags_only) {
177+
if (heads_only)
178+
for_each_fullref_in("refs/heads/", show_ref, &show_ref_data);
179+
if (tags_only)
180+
for_each_fullref_in("refs/tags/", show_ref, &show_ref_data);
181+
} else {
182+
for_each_ref(show_ref, &show_ref_data);
183+
}
184+
if (!found_match) {
185+
if (verify && !quiet)
186+
die("No match");
187+
return 1;
188+
}
189+
190+
return 0;
191+
}
192+
145193
static int hash_callback(const struct option *opt, const char *arg, int unset)
146194
{
147195
hash_only = 1;
@@ -185,56 +233,15 @@ static const struct option show_ref_options[] = {
185233

186234
int cmd_show_ref(int argc, const char **argv, const char *prefix)
187235
{
188-
struct show_ref_data show_ref_data = {0};
189-
const char **patterns;
190-
191236
git_config(git_default_config, NULL);
192237

193238
argc = parse_options(argc, argv, prefix, show_ref_options,
194239
show_ref_usage, 0);
195240

196241
if (exclude_arg)
197-
return exclude_existing(exclude_existing_arg);
198-
199-
patterns = argv;
200-
if (!*patterns)
201-
patterns = NULL;
202-
203-
if (verify) {
204-
if (!patterns)
205-
die("--verify requires a reference");
206-
while (*patterns) {
207-
struct object_id oid;
208-
209-
if ((starts_with(*patterns, "refs/") || !strcmp(*patterns, "HEAD")) &&
210-
!read_ref(*patterns, &oid)) {
211-
show_one(*patterns, &oid);
212-
}
213-
else if (!quiet)
214-
die("'%s' - not a valid ref", *patterns);
215-
else
216-
return 1;
217-
patterns++;
218-
}
219-
return 0;
220-
}
221-
222-
show_ref_data.patterns = patterns;
223-
224-
if (show_head)
225-
head_ref(show_ref, &show_ref_data);
226-
if (heads_only || tags_only) {
227-
if (heads_only)
228-
for_each_fullref_in("refs/heads/", show_ref, &show_ref_data);
229-
if (tags_only)
230-
for_each_fullref_in("refs/tags/", show_ref, &show_ref_data);
231-
} else {
232-
for_each_ref(show_ref, &show_ref_data);
233-
}
234-
if (!found_match) {
235-
if (verify && !quiet)
236-
die("No match");
237-
return 1;
238-
}
239-
return 0;
242+
return cmd_show_ref__exclude_existing(exclude_existing_arg);
243+
else if (verify)
244+
return cmd_show_ref__verify(argv);
245+
else
246+
return cmd_show_ref__patterns(argv);
240247
}

0 commit comments

Comments
 (0)