pathspec: create parse_long_magic function
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>
This commit is contained in:
parent
b4bebdce83
commit
8881fde013
92
pathspec.c
92
pathspec.c
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user