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>
Jeff King committed
Mar 15, 2017 at 16:06 UTC
9d16ca65bbe45d4a28cbb37c3299b544c81ff2e8
1 file changed
+28
-10
builtin/rev-parse.c
+28
-10
@@ -535,6 +535,25 @@ N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
535
"\n"
536
"Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
537
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
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
558
{
559
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)
690
flags |= GET_SHA1_QUIETLY;
691
continue;
692
}
674
- if (!strcmp(arg, "--short") ||
675
- starts_with(arg, "--short=")) {
693
+ if (opt_with_value(arg, "--short", &arg)) {
694
filter &= ~(DO_FLAGS|DO_NOREV);
695
verify = 1;
696
abbrev = DEFAULT_ABBREV;
679
- if (!arg[7])
697
+ if (!arg)
698
continue;
681
- abbrev = strtoul(arg + 8, NULL, 10);
699
+ abbrev = strtoul(arg, NULL, 10);
700
if (abbrev < MINIMUM_ABBREV)
701
abbrev = MINIMUM_ABBREV;
702
else if (40 <= abbrev)
@@ -701,17 +719,17 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
719
symbolic = SHOW_SYMBOLIC_FULL;
720
continue;
721
}
704
- if (starts_with(arg, "--abbrev-ref") &&
705
- (!arg[12] || arg[12] == '=')) {
722
+ if (opt_with_value(arg, "--abbrev-ref", &arg)) {
723
abbrev_ref = 1;
724
abbrev_ref_strict = warn_ambiguous_refs;
708
- if (arg[12] == '=') {
709
- if (!strcmp(arg + 13, "strict"))
725
+ if (arg) {
726
+ if (!strcmp(arg, "strict"))
727
abbrev_ref_strict = 1;
711
- else if (!strcmp(arg + 13, "loose"))
728
+ else if (!strcmp(arg, "loose"))
729
abbrev_ref_strict = 0;
730
else
714
- die("unknown mode for %s", arg);
731
+ die("unknown mode for --abbrev-ref: %s",
732
+ arg);
733
}
734
continue;
735
}