Skip to content

Commit fc6b5f5

Browse files
committed
Merge branch 'jk/rev-parse-cleanup'
Code clean-up. * jk/rev-parse-cleanup: rev-parse: simplify parsing of ref options rev-parse: add helper for parsing "--foo/--foo=" rev-parse: use skip_prefix when parsing options
2 parents c0f9c70 + ffddfc6 commit fc6b5f5

1 file changed

Lines changed: 57 additions & 52 deletions

File tree

builtin/rev-parse.c

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,34 @@ N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
536536
"\n"
537537
"Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
538538

539+
/*
540+
* Parse "opt" or "opt=<value>", setting value respectively to either
541+
* NULL or the string after "=".
542+
*/
543+
static int opt_with_value(const char *arg, const char *opt, const char **value)
544+
{
545+
if (skip_prefix(arg, opt, &arg)) {
546+
if (!*arg) {
547+
*value = NULL;
548+
return 1;
549+
}
550+
if (*arg++ == '=') {
551+
*value = arg;
552+
return 1;
553+
}
554+
}
555+
return 0;
556+
}
557+
558+
static void handle_ref_opt(const char *pattern, const char *prefix)
559+
{
560+
if (pattern)
561+
for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
562+
else
563+
for_each_ref_in(prefix, show_reference, NULL);
564+
clear_ref_exclusion(&ref_excludes);
565+
}
566+
539567
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
540568
{
541569
int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
@@ -675,14 +703,13 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
675703
flags |= GET_SHA1_QUIETLY;
676704
continue;
677705
}
678-
if (!strcmp(arg, "--short") ||
679-
starts_with(arg, "--short=")) {
706+
if (opt_with_value(arg, "--short", &arg)) {
680707
filter &= ~(DO_FLAGS|DO_NOREV);
681708
verify = 1;
682709
abbrev = DEFAULT_ABBREV;
683-
if (!arg[7])
710+
if (!arg)
684711
continue;
685-
abbrev = strtoul(arg + 8, NULL, 10);
712+
abbrev = strtoul(arg, NULL, 10);
686713
if (abbrev < MINIMUM_ABBREV)
687714
abbrev = MINIMUM_ABBREV;
688715
else if (40 <= abbrev)
@@ -705,73 +732,51 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
705732
symbolic = SHOW_SYMBOLIC_FULL;
706733
continue;
707734
}
708-
if (starts_with(arg, "--abbrev-ref") &&
709-
(!arg[12] || arg[12] == '=')) {
735+
if (opt_with_value(arg, "--abbrev-ref", &arg)) {
710736
abbrev_ref = 1;
711737
abbrev_ref_strict = warn_ambiguous_refs;
712-
if (arg[12] == '=') {
713-
if (!strcmp(arg + 13, "strict"))
738+
if (arg) {
739+
if (!strcmp(arg, "strict"))
714740
abbrev_ref_strict = 1;
715-
else if (!strcmp(arg + 13, "loose"))
741+
else if (!strcmp(arg, "loose"))
716742
abbrev_ref_strict = 0;
717743
else
718-
die("unknown mode for %s", arg);
744+
die("unknown mode for --abbrev-ref: %s",
745+
arg);
719746
}
720747
continue;
721748
}
722749
if (!strcmp(arg, "--all")) {
723750
for_each_ref(show_reference, NULL);
724751
continue;
725752
}
726-
if (starts_with(arg, "--disambiguate=")) {
727-
for_each_abbrev(arg + 15, show_abbrev, NULL);
753+
if (skip_prefix(arg, "--disambiguate=", &arg)) {
754+
for_each_abbrev(arg, show_abbrev, NULL);
728755
continue;
729756
}
730757
if (!strcmp(arg, "--bisect")) {
731758
for_each_ref_in("refs/bisect/bad", show_reference, NULL);
732759
for_each_ref_in("refs/bisect/good", anti_reference, NULL);
733760
continue;
734761
}
735-
if (starts_with(arg, "--branches=")) {
736-
for_each_glob_ref_in(show_reference, arg + 11,
737-
"refs/heads/", NULL);
738-
clear_ref_exclusion(&ref_excludes);
739-
continue;
740-
}
741-
if (!strcmp(arg, "--branches")) {
742-
for_each_branch_ref(show_reference, NULL);
743-
clear_ref_exclusion(&ref_excludes);
744-
continue;
745-
}
746-
if (starts_with(arg, "--tags=")) {
747-
for_each_glob_ref_in(show_reference, arg + 7,
748-
"refs/tags/", NULL);
749-
clear_ref_exclusion(&ref_excludes);
750-
continue;
751-
}
752-
if (!strcmp(arg, "--tags")) {
753-
for_each_tag_ref(show_reference, NULL);
754-
clear_ref_exclusion(&ref_excludes);
762+
if (opt_with_value(arg, "--branches", &arg)) {
763+
handle_ref_opt(arg, "refs/heads/");
755764
continue;
756765
}
757-
if (starts_with(arg, "--glob=")) {
758-
for_each_glob_ref(show_reference, arg + 7, NULL);
759-
clear_ref_exclusion(&ref_excludes);
766+
if (opt_with_value(arg, "--tags", &arg)) {
767+
handle_ref_opt(arg, "refs/tags/");
760768
continue;
761769
}
762-
if (starts_with(arg, "--remotes=")) {
763-
for_each_glob_ref_in(show_reference, arg + 10,
764-
"refs/remotes/", NULL);
765-
clear_ref_exclusion(&ref_excludes);
770+
if (skip_prefix(arg, "--glob=", &arg)) {
771+
handle_ref_opt(arg, NULL);
766772
continue;
767773
}
768-
if (!strcmp(arg, "--remotes")) {
769-
for_each_remote_ref(show_reference, NULL);
770-
clear_ref_exclusion(&ref_excludes);
774+
if (opt_with_value(arg, "--remotes", &arg)) {
775+
handle_ref_opt(arg, "refs/remotes/");
771776
continue;
772777
}
773-
if (starts_with(arg, "--exclude=")) {
774-
add_ref_exclusion(&ref_excludes, arg + 10);
778+
if (skip_prefix(arg, "--exclude=", &arg)) {
779+
add_ref_exclusion(&ref_excludes, arg);
775780
continue;
776781
}
777782
if (!strcmp(arg, "--show-toplevel")) {
@@ -872,20 +877,20 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
872877
}
873878
continue;
874879
}
875-
if (starts_with(arg, "--since=")) {
876-
show_datestring("--max-age=", arg+8);
880+
if (skip_prefix(arg, "--since=", &arg)) {
881+
show_datestring("--max-age=", arg);
877882
continue;
878883
}
879-
if (starts_with(arg, "--after=")) {
880-
show_datestring("--max-age=", arg+8);
884+
if (skip_prefix(arg, "--after=", &arg)) {
885+
show_datestring("--max-age=", arg);
881886
continue;
882887
}
883-
if (starts_with(arg, "--before=")) {
884-
show_datestring("--min-age=", arg+9);
888+
if (skip_prefix(arg, "--before=", &arg)) {
889+
show_datestring("--min-age=", arg);
885890
continue;
886891
}
887-
if (starts_with(arg, "--until=")) {
888-
show_datestring("--min-age=", arg+8);
892+
if (skip_prefix(arg, "--until=", &arg)) {
893+
show_datestring("--min-age=", arg);
889894
continue;
890895
}
891896
if (show_flag(arg) && verify)

0 commit comments

Comments
 (0)