Skip to content

Commit 8f71209

Browse files
committed
Merge branch 'jk/rev-parse-cleanup' into maint
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 a9508a1 + ffddfc6 commit 8f71209

File tree

1 file changed

+57
-52
lines changed

1 file changed

+57
-52
lines changed

builtin/rev-parse.c

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

538+
/*
539+
* Parse "opt" or "opt=<value>", setting value respectively to either
540+
* NULL or the string after "=".
541+
*/
542+
static int opt_with_value(const char *arg, const char *opt, const char **value)
543+
{
544+
if (skip_prefix(arg, opt, &arg)) {
545+
if (!*arg) {
546+
*value = NULL;
547+
return 1;
548+
}
549+
if (*arg++ == '=') {
550+
*value = arg;
551+
return 1;
552+
}
553+
}
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+
538566
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
539567
{
540568
int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
@@ -671,14 +699,13 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
671699
flags |= GET_SHA1_QUIETLY;
672700
continue;
673701
}
674-
if (!strcmp(arg, "--short") ||
675-
starts_with(arg, "--short=")) {
702+
if (opt_with_value(arg, "--short", &arg)) {
676703
filter &= ~(DO_FLAGS|DO_NOREV);
677704
verify = 1;
678705
abbrev = DEFAULT_ABBREV;
679-
if (!arg[7])
706+
if (!arg)
680707
continue;
681-
abbrev = strtoul(arg + 8, NULL, 10);
708+
abbrev = strtoul(arg, NULL, 10);
682709
if (abbrev < MINIMUM_ABBREV)
683710
abbrev = MINIMUM_ABBREV;
684711
else if (40 <= abbrev)
@@ -701,73 +728,51 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
701728
symbolic = SHOW_SYMBOLIC_FULL;
702729
continue;
703730
}
704-
if (starts_with(arg, "--abbrev-ref") &&
705-
(!arg[12] || arg[12] == '=')) {
731+
if (opt_with_value(arg, "--abbrev-ref", &arg)) {
706732
abbrev_ref = 1;
707733
abbrev_ref_strict = warn_ambiguous_refs;
708-
if (arg[12] == '=') {
709-
if (!strcmp(arg + 13, "strict"))
734+
if (arg) {
735+
if (!strcmp(arg, "strict"))
710736
abbrev_ref_strict = 1;
711-
else if (!strcmp(arg + 13, "loose"))
737+
else if (!strcmp(arg, "loose"))
712738
abbrev_ref_strict = 0;
713739
else
714-
die("unknown mode for %s", arg);
740+
die("unknown mode for --abbrev-ref: %s",
741+
arg);
715742
}
716743
continue;
717744
}
718745
if (!strcmp(arg, "--all")) {
719746
for_each_ref(show_reference, NULL);
720747
continue;
721748
}
722-
if (starts_with(arg, "--disambiguate=")) {
723-
for_each_abbrev(arg + 15, show_abbrev, NULL);
749+
if (skip_prefix(arg, "--disambiguate=", &arg)) {
750+
for_each_abbrev(arg, show_abbrev, NULL);
724751
continue;
725752
}
726753
if (!strcmp(arg, "--bisect")) {
727754
for_each_ref_in("refs/bisect/bad", show_reference, NULL);
728755
for_each_ref_in("refs/bisect/good", anti_reference, NULL);
729756
continue;
730757
}
731-
if (starts_with(arg, "--branches=")) {
732-
for_each_glob_ref_in(show_reference, arg + 11,
733-
"refs/heads/", NULL);
734-
clear_ref_exclusion(&ref_excludes);
735-
continue;
736-
}
737-
if (!strcmp(arg, "--branches")) {
738-
for_each_branch_ref(show_reference, NULL);
739-
clear_ref_exclusion(&ref_excludes);
740-
continue;
741-
}
742-
if (starts_with(arg, "--tags=")) {
743-
for_each_glob_ref_in(show_reference, arg + 7,
744-
"refs/tags/", NULL);
745-
clear_ref_exclusion(&ref_excludes);
746-
continue;
747-
}
748-
if (!strcmp(arg, "--tags")) {
749-
for_each_tag_ref(show_reference, NULL);
750-
clear_ref_exclusion(&ref_excludes);
758+
if (opt_with_value(arg, "--branches", &arg)) {
759+
handle_ref_opt(arg, "refs/heads/");
751760
continue;
752761
}
753-
if (starts_with(arg, "--glob=")) {
754-
for_each_glob_ref(show_reference, arg + 7, NULL);
755-
clear_ref_exclusion(&ref_excludes);
762+
if (opt_with_value(arg, "--tags", &arg)) {
763+
handle_ref_opt(arg, "refs/tags/");
756764
continue;
757765
}
758-
if (starts_with(arg, "--remotes=")) {
759-
for_each_glob_ref_in(show_reference, arg + 10,
760-
"refs/remotes/", NULL);
761-
clear_ref_exclusion(&ref_excludes);
766+
if (skip_prefix(arg, "--glob=", &arg)) {
767+
handle_ref_opt(arg, NULL);
762768
continue;
763769
}
764-
if (!strcmp(arg, "--remotes")) {
765-
for_each_remote_ref(show_reference, NULL);
766-
clear_ref_exclusion(&ref_excludes);
770+
if (opt_with_value(arg, "--remotes", &arg)) {
771+
handle_ref_opt(arg, "refs/remotes/");
767772
continue;
768773
}
769-
if (starts_with(arg, "--exclude=")) {
770-
add_ref_exclusion(&ref_excludes, arg + 10);
774+
if (skip_prefix(arg, "--exclude=", &arg)) {
775+
add_ref_exclusion(&ref_excludes, arg);
771776
continue;
772777
}
773778
if (!strcmp(arg, "--show-toplevel")) {
@@ -849,20 +854,20 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
849854
}
850855
continue;
851856
}
852-
if (starts_with(arg, "--since=")) {
853-
show_datestring("--max-age=", arg+8);
857+
if (skip_prefix(arg, "--since=", &arg)) {
858+
show_datestring("--max-age=", arg);
854859
continue;
855860
}
856-
if (starts_with(arg, "--after=")) {
857-
show_datestring("--max-age=", arg+8);
861+
if (skip_prefix(arg, "--after=", &arg)) {
862+
show_datestring("--max-age=", arg);
858863
continue;
859864
}
860-
if (starts_with(arg, "--before=")) {
861-
show_datestring("--min-age=", arg+9);
865+
if (skip_prefix(arg, "--before=", &arg)) {
866+
show_datestring("--min-age=", arg);
862867
continue;
863868
}
864-
if (starts_with(arg, "--until=")) {
865-
show_datestring("--min-age=", arg+8);
869+
if (skip_prefix(arg, "--until=", &arg)) {
870+
show_datestring("--min-age=", arg);
866871
continue;
867872
}
868873
if (show_flag(arg) && verify)

0 commit comments

Comments
 (0)