rev-parse: add helper for parsing "--foo/--foo="
We can't just use a bare skip_prefix() for these cases, because we need to match both the "--foo" form and the "--foo=<value>" form (and tell the difference between the two in the caller). We can wrap this in a simple helper which has two obvious callsites, and will gain some more in the next patch. Note that the error output for abbrev-ref changes slightly, as we don't keep our original "arg" pointer. However, the new output should hopefully be more clear: [before] fatal: unknown mode for --abbrev-ref=foo [after] fatal: unknown mode for --abbrev-ref: foo Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
ef87cc79df
commit
9d16ca65bb
@ -535,6 +535,25 @@ 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;
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
@ -671,14 +690,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)
|
||||||
@ -701,17 +719,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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user