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
This commit is contained in:
Junio C Hamano 2017-03-21 15:07:16 -07:00
commit fc6b5f523a

View file

@ -536,6 +536,34 @@ N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
"\n" "\n"
"Run \"git rev-parse --parseopt -h\" for more information on the first usage."); "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
/*
* Parse "opt" or "opt=<value>", setting value respectively to either
* NULL or the string after "=".
*/
static int opt_with_value(const char *arg, const char *opt, const char **value)
{
if (skip_prefix(arg, opt, &arg)) {
if (!*arg) {
*value = NULL;
return 1;
}
if (*arg++ == '=') {
*value = arg;
return 1;
}
}
return 0;
}
static void handle_ref_opt(const char *pattern, const char *prefix)
{
if (pattern)
for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
else
for_each_ref_in(prefix, show_reference, NULL);
clear_ref_exclusion(&ref_excludes);
}
int cmd_rev_parse(int argc, const char **argv, const char *prefix) int cmd_rev_parse(int argc, const char **argv, const char *prefix)
{ {
int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0; 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)
flags |= GET_SHA1_QUIETLY; flags |= GET_SHA1_QUIETLY;
continue; continue;
} }
if (!strcmp(arg, "--short") || if (opt_with_value(arg, "--short", &arg)) {
starts_with(arg, "--short=")) {
filter &= ~(DO_FLAGS|DO_NOREV); filter &= ~(DO_FLAGS|DO_NOREV);
verify = 1; verify = 1;
abbrev = DEFAULT_ABBREV; abbrev = DEFAULT_ABBREV;
if (!arg[7]) if (!arg)
continue; continue;
abbrev = strtoul(arg + 8, NULL, 10); abbrev = strtoul(arg, NULL, 10);
if (abbrev < MINIMUM_ABBREV) if (abbrev < MINIMUM_ABBREV)
abbrev = MINIMUM_ABBREV; abbrev = MINIMUM_ABBREV;
else if (40 <= abbrev) else if (40 <= abbrev)
@ -705,17 +732,17 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
symbolic = SHOW_SYMBOLIC_FULL; symbolic = SHOW_SYMBOLIC_FULL;
continue; continue;
} }
if (starts_with(arg, "--abbrev-ref") && if (opt_with_value(arg, "--abbrev-ref", &arg)) {
(!arg[12] || arg[12] == '=')) {
abbrev_ref = 1; abbrev_ref = 1;
abbrev_ref_strict = warn_ambiguous_refs; abbrev_ref_strict = warn_ambiguous_refs;
if (arg[12] == '=') { if (arg) {
if (!strcmp(arg + 13, "strict")) if (!strcmp(arg, "strict"))
abbrev_ref_strict = 1; abbrev_ref_strict = 1;
else if (!strcmp(arg + 13, "loose")) else if (!strcmp(arg, "loose"))
abbrev_ref_strict = 0; abbrev_ref_strict = 0;
else else
die("unknown mode for %s", arg); die("unknown mode for --abbrev-ref: %s",
arg);
} }
continue; continue;
} }
@ -723,8 +750,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
for_each_ref(show_reference, NULL); for_each_ref(show_reference, NULL);
continue; continue;
} }
if (starts_with(arg, "--disambiguate=")) { if (skip_prefix(arg, "--disambiguate=", &arg)) {
for_each_abbrev(arg + 15, show_abbrev, NULL); for_each_abbrev(arg, show_abbrev, NULL);
continue; continue;
} }
if (!strcmp(arg, "--bisect")) { if (!strcmp(arg, "--bisect")) {
@ -732,46 +759,24 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
for_each_ref_in("refs/bisect/good", anti_reference, NULL); for_each_ref_in("refs/bisect/good", anti_reference, NULL);
continue; continue;
} }
if (starts_with(arg, "--branches=")) { if (opt_with_value(arg, "--branches", &arg)) {
for_each_glob_ref_in(show_reference, arg + 11, handle_ref_opt(arg, "refs/heads/");
"refs/heads/", NULL);
clear_ref_exclusion(&ref_excludes);
continue; continue;
} }
if (!strcmp(arg, "--branches")) { if (opt_with_value(arg, "--tags", &arg)) {
for_each_branch_ref(show_reference, NULL); handle_ref_opt(arg, "refs/tags/");
clear_ref_exclusion(&ref_excludes);
continue; continue;
} }
if (starts_with(arg, "--tags=")) { if (skip_prefix(arg, "--glob=", &arg)) {
for_each_glob_ref_in(show_reference, arg + 7, handle_ref_opt(arg, NULL);
"refs/tags/", NULL);
clear_ref_exclusion(&ref_excludes);
continue; continue;
} }
if (!strcmp(arg, "--tags")) { if (opt_with_value(arg, "--remotes", &arg)) {
for_each_tag_ref(show_reference, NULL); handle_ref_opt(arg, "refs/remotes/");
clear_ref_exclusion(&ref_excludes);
continue; continue;
} }
if (starts_with(arg, "--glob=")) { if (skip_prefix(arg, "--exclude=", &arg)) {
for_each_glob_ref(show_reference, arg + 7, NULL); add_ref_exclusion(&ref_excludes, arg);
clear_ref_exclusion(&ref_excludes);
continue;
}
if (starts_with(arg, "--remotes=")) {
for_each_glob_ref_in(show_reference, arg + 10,
"refs/remotes/", NULL);
clear_ref_exclusion(&ref_excludes);
continue;
}
if (!strcmp(arg, "--remotes")) {
for_each_remote_ref(show_reference, NULL);
clear_ref_exclusion(&ref_excludes);
continue;
}
if (starts_with(arg, "--exclude=")) {
add_ref_exclusion(&ref_excludes, arg + 10);
continue; continue;
} }
if (!strcmp(arg, "--show-toplevel")) { if (!strcmp(arg, "--show-toplevel")) {
@ -872,20 +877,20 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
} }
continue; continue;
} }
if (starts_with(arg, "--since=")) { if (skip_prefix(arg, "--since=", &arg)) {
show_datestring("--max-age=", arg+8); show_datestring("--max-age=", arg);
continue; continue;
} }
if (starts_with(arg, "--after=")) { if (skip_prefix(arg, "--after=", &arg)) {
show_datestring("--max-age=", arg+8); show_datestring("--max-age=", arg);
continue; continue;
} }
if (starts_with(arg, "--before=")) { if (skip_prefix(arg, "--before=", &arg)) {
show_datestring("--min-age=", arg+9); show_datestring("--min-age=", arg);
continue; continue;
} }
if (starts_with(arg, "--until=")) { if (skip_prefix(arg, "--until=", &arg)) {
show_datestring("--min-age=", arg+8); show_datestring("--min-age=", arg);
continue; continue;
} }
if (show_flag(arg) && verify) if (show_flag(arg) && verify)