pathspec: simpler logic to prefix original pathspec elements
The logic used to prefix an original pathspec element with 'prefix' magic is more general purpose and can be used for more than just short magic. Remove the extra code paths and rename 'prefix_short_magic' to 'prefix_magic' to better indicate that it can be used in more general situations. Also, slightly change the logic which decides when to prefix the original element in order to prevent a pathspec of "." from getting converted to "" (empty string). 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
2aee5849c9
commit
5d8f084a5d
33
pathspec.c
33
pathspec.c
@ -74,13 +74,12 @@ static struct pathspec_magic {
|
|||||||
{ PATHSPEC_EXCLUDE, '!', "exclude" },
|
{ PATHSPEC_EXCLUDE, '!', "exclude" },
|
||||||
};
|
};
|
||||||
|
|
||||||
static void prefix_short_magic(struct strbuf *sb, int prefixlen,
|
static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
|
||||||
unsigned short_magic)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
strbuf_addstr(sb, ":(");
|
strbuf_addstr(sb, ":(");
|
||||||
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
|
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
|
||||||
if (short_magic & pathspec_magic[i].bit) {
|
if (magic & pathspec_magic[i].bit) {
|
||||||
if (sb->buf[sb->len - 1] != '(')
|
if (sb->buf[sb->len - 1] != '(')
|
||||||
strbuf_addch(sb, ',');
|
strbuf_addch(sb, ',');
|
||||||
strbuf_addstr(sb, pathspec_magic[i].name);
|
strbuf_addstr(sb, pathspec_magic[i].name);
|
||||||
@ -109,8 +108,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
|
|||||||
static int glob_global = -1;
|
static int glob_global = -1;
|
||||||
static int noglob_global = -1;
|
static int noglob_global = -1;
|
||||||
static int icase_global = -1;
|
static int icase_global = -1;
|
||||||
unsigned magic = 0, short_magic = 0, global_magic = 0;
|
unsigned magic = 0, element_magic = 0, global_magic = 0;
|
||||||
const char *copyfrom = elt, *long_magic_end = NULL;
|
const char *copyfrom = elt;
|
||||||
char *match;
|
char *match;
|
||||||
int i, pathspec_prefix = -1;
|
int i, pathspec_prefix = -1;
|
||||||
|
|
||||||
@ -164,7 +163,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
|
|||||||
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
|
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
|
||||||
if (strlen(pathspec_magic[i].name) == len &&
|
if (strlen(pathspec_magic[i].name) == len &&
|
||||||
!strncmp(pathspec_magic[i].name, copyfrom, len)) {
|
!strncmp(pathspec_magic[i].name, copyfrom, len)) {
|
||||||
magic |= pathspec_magic[i].bit;
|
element_magic |= pathspec_magic[i].bit;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (starts_with(copyfrom, "prefix:")) {
|
if (starts_with(copyfrom, "prefix:")) {
|
||||||
@ -183,7 +182,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
|
|||||||
}
|
}
|
||||||
if (*copyfrom != ')')
|
if (*copyfrom != ')')
|
||||||
die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
|
die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
|
||||||
long_magic_end = copyfrom;
|
|
||||||
copyfrom++;
|
copyfrom++;
|
||||||
} else {
|
} else {
|
||||||
/* shorthand */
|
/* shorthand */
|
||||||
@ -196,7 +194,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
|
|||||||
break;
|
break;
|
||||||
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
|
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
|
||||||
if (pathspec_magic[i].mnemonic == ch) {
|
if (pathspec_magic[i].mnemonic == ch) {
|
||||||
short_magic |= pathspec_magic[i].bit;
|
element_magic |= pathspec_magic[i].bit;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (ARRAY_SIZE(pathspec_magic) <= i)
|
if (ARRAY_SIZE(pathspec_magic) <= i)
|
||||||
@ -207,7 +205,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
|
|||||||
copyfrom++;
|
copyfrom++;
|
||||||
}
|
}
|
||||||
|
|
||||||
magic |= short_magic;
|
magic |= element_magic;
|
||||||
|
|
||||||
/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
|
/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
|
||||||
if (noglob_global && !(magic & PATHSPEC_GLOB))
|
if (noglob_global && !(magic & PATHSPEC_GLOB))
|
||||||
@ -242,18 +240,13 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
|
|||||||
* Prefix the pathspec (keep all magic) and assign to
|
* Prefix the pathspec (keep all magic) and assign to
|
||||||
* original. Useful for passing to another command.
|
* original. Useful for passing to another command.
|
||||||
*/
|
*/
|
||||||
if (flags & PATHSPEC_PREFIX_ORIGIN) {
|
if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
|
||||||
|
prefixlen && !literal_global) {
|
||||||
struct strbuf sb = STRBUF_INIT;
|
struct strbuf sb = STRBUF_INIT;
|
||||||
if (prefixlen && !literal_global) {
|
|
||||||
/* Preserve the actual prefix length of each pattern */
|
/* Preserve the actual prefix length of each pattern */
|
||||||
if (short_magic)
|
prefix_magic(&sb, prefixlen, element_magic);
|
||||||
prefix_short_magic(&sb, prefixlen, short_magic);
|
|
||||||
else if (long_magic_end) {
|
|
||||||
strbuf_add(&sb, elt, long_magic_end - elt);
|
|
||||||
strbuf_addf(&sb, ",prefix:%d)", prefixlen);
|
|
||||||
} else
|
|
||||||
strbuf_addf(&sb, ":(prefix:%d)", prefixlen);
|
|
||||||
}
|
|
||||||
strbuf_addstr(&sb, match);
|
strbuf_addstr(&sb, match);
|
||||||
item->original = strbuf_detach(&sb, NULL);
|
item->original = strbuf_detach(&sb, NULL);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user