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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 15, 2017 at 16:08 UTC ffddfc6328fd3c8b0e8ee69144ec2dda5c766364
1 file changed +16 -29
builtin/rev-parse.c
+16 -29
@@ -554,6 +554,15 @@ static int opt_with_value(const char *arg, const char *opt, const char **value)
554 return 0;
555 }
556
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 +
566 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
567 {
568 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)
755 for_each_ref_in("refs/bisect/good", anti_reference, NULL);
756 continue;
757 }
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/");
760 continue;
761 }
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/");
764 continue;
765 }
766 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);
768 continue;
769 }
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/");
772 continue;
773 }
774 if (skip_prefix(arg, "--exclude=", &arg)) {