pathspec: create parse_long_magic function
authorBrandon Williams <bmwill@google.com>
Wed, 4 Jan 2017 18:04:07 +0000 (10:04 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 9 Jan 2017 02:04:18 +0000 (18:04 -0800)
Factor out the logic responsible for parsing long magic into its own
function. As well as hoist the prefix check logic outside of the inner
loop as there isn't anything that needs to be done after matching
"prefix:".

Signed-off-by: Brandon Williams <bmwill@google.com>
Reviewed-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
pathspec.c
index 1b090184801a3b09623f480f5b78d48a7a9a225e..f6356bde16fcd3d85dfbb906b042c7b40a4487a6 100644 (file)
@@ -156,6 +156,60 @@ static int get_global_magic(int element_magic)
        return global_magic;
 }
 
+/*
+ * Parse the pathspec element looking for long magic
+ *
+ * saves all magic in 'magic'
+ * if prefix magic is used, save the prefix length in 'prefix_len'
+ * returns the position in 'elem' after all magic has been parsed
+ */
+static const char *parse_long_magic(unsigned *magic, int *prefix_len,
+                                   const char *elem)
+{
+       const char *pos;
+       const char *nextat;
+
+       for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
+               size_t len = strcspn(pos, ",)");
+               int i;
+
+               if (pos[len] == ',')
+                       nextat = pos + len + 1; /* handle ',' */
+               else
+                       nextat = pos + len; /* handle ')' and '\0' */
+
+               if (!len)
+                       continue;
+
+               if (starts_with(pos, "prefix:")) {
+                       char *endptr;
+                       *prefix_len = strtol(pos + 7, &endptr, 10);
+                       if (endptr - pos != len)
+                               die(_("invalid parameter for pathspec magic 'prefix'"));
+                       continue;
+               }
+
+               for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
+                       if (strlen(pathspec_magic[i].name) == len &&
+                           !strncmp(pathspec_magic[i].name, pos, len)) {
+                               *magic |= pathspec_magic[i].bit;
+                               break;
+                       }
+               }
+
+               if (ARRAY_SIZE(pathspec_magic) <= i)
+                       die(_("Invalid pathspec magic '%.*s' in '%s'"),
+                           (int) len, pos, elem);
+       }
+
+       if (*pos != ')')
+               die(_("Missing ')' at the end of pathspec magic in '%s'"),
+                   elem);
+       pos++;
+
+       return pos;
+}
+
 /*
  * Parse the pathspec element looking for short magic
  *
@@ -218,41 +272,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
                ; /* nothing to do */
        } else if (elt[1] == '(') {
                /* longhand */
-               const char *nextat;
-               for (copyfrom = elt + 2;
-                    *copyfrom && *copyfrom != ')';
-                    copyfrom = nextat) {
-                       size_t len = strcspn(copyfrom, ",)");
-                       if (copyfrom[len] == ',')
-                               nextat = copyfrom + len + 1;
-                       else
-                               /* handle ')' and '\0' */
-                               nextat = copyfrom + len;
-                       if (!len)
-                               continue;
-                       for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
-                               if (strlen(pathspec_magic[i].name) == len &&
-                                   !strncmp(pathspec_magic[i].name, copyfrom, len)) {
-                                       element_magic |= pathspec_magic[i].bit;
-                                       break;
-                               }
-                               if (starts_with(copyfrom, "prefix:")) {
-                                       char *endptr;
-                                       pathspec_prefix = strtol(copyfrom + 7,
-                                                                &endptr, 10);
-                                       if (endptr - copyfrom != len)
-                                               die(_("invalid parameter for pathspec magic 'prefix'"));
-                                       /* "i" would be wrong, but it does not matter */
-                                       break;
-                               }
-                       }
-                       if (ARRAY_SIZE(pathspec_magic) <= i)
-                               die(_("Invalid pathspec magic '%.*s' in '%s'"),
-                                   (int) len, copyfrom, elt);
-               }
-               if (*copyfrom != ')')
-                       die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
-               copyfrom++;
+               copyfrom = parse_long_magic(&element_magic,
+                                           &pathspec_prefix,
+                                           elt);
        } else {
                /* shorthand */
                copyfrom = parse_short_magic(&element_magic, elt);