Skip to content

Commit ffddfc6

Browse files
peffgitster
authored andcommitted
rev-parse: simplify parsing of ref options
All of these options do the same thing "--foo" iterates over the "foo" refs, and "--foo=<glob>" does the same with a glob. We can factor this into its own function to avoid repeating ourselves. There are two subtleties to note: - the original called for_each_branch_ref(), etc, in the non-glob case. Now we will call for_each_ref_in("refs/heads/") which is exactly what for_each_branch_ref() did under the hood. - for --glob, we'll call for_each_glob_ref_in() with a NULL "prefix" argument. Which is exactly what for_each_glob_ref() was doing already. So both cases should behave identically, and it seems reasonable to assume that this will remain the same. The functions we are calling now are the more-generic ones, and the ones we are dropping are just convenience wrappers. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9d16ca6 commit ffddfc6

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

builtin/rev-parse.c

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,15 @@ static int opt_with_value(const char *arg, const char *opt, const char **value)
554554
return 0;
555555
}
556556

557+
static void handle_ref_opt(const char *pattern, const char *prefix)
558+
{
559+
if (pattern)
560+
for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
561+
else
562+
for_each_ref_in(prefix, show_reference, NULL);
563+
clear_ref_exclusion(&ref_excludes);
564+
}
565+
557566
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
558567
{
559568
int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
@@ -746,42 +755,20 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
746755
for_each_ref_in("refs/bisect/good", anti_reference, NULL);
747756
continue;
748757
}
749-
if (skip_prefix(arg, "--branches=", &arg)) {
750-
for_each_glob_ref_in(show_reference, arg,
751-
"refs/heads/", NULL);
752-
clear_ref_exclusion(&ref_excludes);
753-
continue;
754-
}
755-
if (!strcmp(arg, "--branches")) {
756-
for_each_branch_ref(show_reference, NULL);
757-
clear_ref_exclusion(&ref_excludes);
758-
continue;
759-
}
760-
if (skip_prefix(arg, "--tags=", &arg)) {
761-
for_each_glob_ref_in(show_reference, arg,
762-
"refs/tags/", NULL);
763-
clear_ref_exclusion(&ref_excludes);
758+
if (opt_with_value(arg, "--branches", &arg)) {
759+
handle_ref_opt(arg, "refs/heads/");
764760
continue;
765761
}
766-
if (!strcmp(arg, "--tags")) {
767-
for_each_tag_ref(show_reference, NULL);
768-
clear_ref_exclusion(&ref_excludes);
762+
if (opt_with_value(arg, "--tags", &arg)) {
763+
handle_ref_opt(arg, "refs/tags/");
769764
continue;
770765
}
771766
if (skip_prefix(arg, "--glob=", &arg)) {
772-
for_each_glob_ref(show_reference, arg, NULL);
773-
clear_ref_exclusion(&ref_excludes);
774-
continue;
775-
}
776-
if (skip_prefix(arg, "--remotes=", &arg)) {
777-
for_each_glob_ref_in(show_reference, arg,
778-
"refs/remotes/", NULL);
779-
clear_ref_exclusion(&ref_excludes);
767+
handle_ref_opt(arg, NULL);
780768
continue;
781769
}
782-
if (!strcmp(arg, "--remotes")) {
783-
for_each_remote_ref(show_reference, NULL);
784-
clear_ref_exclusion(&ref_excludes);
770+
if (opt_with_value(arg, "--remotes", &arg)) {
771+
handle_ref_opt(arg, "refs/remotes/");
785772
continue;
786773
}
787774
if (skip_prefix(arg, "--exclude=", &arg)) {

0 commit comments

Comments
 (0)