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>
Brandon Williams committed
Jan 4, 2017 at 10:04 UTC
5d8f084a5d63501270d6cc0ff2ce04358ca704d6
1 file changed
+13
-20
pathspec.c
+13
-20
@@ -74,13 +74,12 @@ static struct pathspec_magic {
74
{ PATHSPEC_EXCLUDE, '!', "exclude" },
75
};
76
77
-static void prefix_short_magic(struct strbuf *sb, int prefixlen,
78
- unsigned short_magic)
77
+static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
78
{
79
int i;
80
strbuf_addstr(sb, ":(");
81
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
83
- if (short_magic & pathspec_magic[i].bit) {
82
+ if (magic & pathspec_magic[i].bit) {
83
if (sb->buf[sb->len - 1] != '(')
84
strbuf_addch(sb, ',');
85
strbuf_addstr(sb, pathspec_magic[i].name);
@@ -109,8 +108,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
108
static int glob_global = -1;
109
static int noglob_global = -1;
110
static int icase_global = -1;
112
- unsigned magic = 0, short_magic = 0, global_magic = 0;
113
- const char *copyfrom = elt, *long_magic_end = NULL;
111
+ unsigned magic = 0, element_magic = 0, global_magic = 0;
112
+ const char *copyfrom = elt;
113
char *match;
114
int i, pathspec_prefix = -1;
115
@@ -164,7 +163,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
163
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
164
if (strlen(pathspec_magic[i].name) == len &&
165
!strncmp(pathspec_magic[i].name, copyfrom, len)) {
167
- magic |= pathspec_magic[i].bit;
166
+ element_magic |= pathspec_magic[i].bit;
167
break;
168
}
169
if (starts_with(copyfrom, "prefix:")) {
@@ -183,7 +182,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
182
}
183
if (*copyfrom != ')')
184
die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
186
- long_magic_end = copyfrom;
185
copyfrom++;
186
} else {
187
/* shorthand */
@@ -196,7 +194,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
194
break;
195
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
196
if (pathspec_magic[i].mnemonic == ch) {
199
- short_magic |= pathspec_magic[i].bit;
197
+ element_magic |= pathspec_magic[i].bit;
198
break;
199
}
200
if (ARRAY_SIZE(pathspec_magic) <= i)
@@ -207,7 +205,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
205
copyfrom++;
206
}
207
210
- magic |= short_magic;
208
+ magic |= element_magic;
209
210
/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
211
if (noglob_global && !(magic & PATHSPEC_GLOB))
@@ -242,18 +240,13 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
240
* Prefix the pathspec (keep all magic) and assign to
241
* original. Useful for passing to another command.
242
*/
245
- if (flags & PATHSPEC_PREFIX_ORIGIN) {
243
+ if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
244
+ prefixlen && !literal_global) {
245
struct strbuf sb = STRBUF_INIT;
247
- if (prefixlen && !literal_global) {
248
- /* Preserve the actual prefix length of each pattern */
249
- if (short_magic)
250
- prefix_short_magic(&sb, prefixlen, short_magic);
251
- else if (long_magic_end) {
252
- strbuf_add(&sb, elt, long_magic_end - elt);
253
- strbuf_addf(&sb, ",prefix:%d)", prefixlen);
254
- } else
255
- strbuf_addf(&sb, ":(prefix:%d)", prefixlen);
256
- }
246
+
247
+ /* Preserve the actual prefix length of each pattern */
248
+ prefix_magic(&sb, prefixlen, element_magic);
249
+
250
strbuf_addstr(&sb, match);
251
item->original = strbuf_detach(&sb, NULL);
252
} else {