pathspec: create parse_element_magic helper

Factor out the logic responsible for the magic in a pathspec element into its own function. Also avoid calling into the parsing functions when `PATHSPEC_LITERAL_PATH` is specified since it causes magic to be ignored and all paths to be treated as literals. 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 1b6112c527be607f56a674301b413af16aa186c6
1 file changed +20 -17
pathspec.c
+20 -17
@@ -245,6 +245,19 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)
245 return pos;
246 }
247
248 +static const char *parse_element_magic(unsigned *magic, int *prefix_len,
249 + const char *elem)
250 +{
251 + if (elem[0] != ':' || get_literal_global())
252 + return elem; /* nothing to do */
253 + else if (elem[1] == '(')
254 + /* longhand */
255 + return parse_long_magic(magic, prefix_len, elem);
256 + else
257 + /* shorthand */
258 + return parse_short_magic(magic, elem);
259 +}
260 +
261 /*
262 * Take an element of a pathspec and check for magic signatures.
263 * Append the result to the prefix. Return the magic bitmap.
@@ -267,26 +280,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
280 char *match;
281 int i, pathspec_prefix = -1;
282
270 - if (elt[0] != ':' || get_literal_global() ||
271 - (flags & PATHSPEC_LITERAL_PATH)) {
272 - ; /* nothing to do */
273 - } else if (elt[1] == '(') {
274 - /* longhand */
275 - copyfrom = parse_long_magic(&element_magic,
276 - &pathspec_prefix,
277 - elt);
278 - } else {
279 - /* shorthand */
280 - copyfrom = parse_short_magic(&element_magic, elt);
281 - }
282 -
283 - magic |= element_magic;
284 -
283 /* PATHSPEC_LITERAL_PATH ignores magic */
286 - if (flags & PATHSPEC_LITERAL_PATH)
284 + if (flags & PATHSPEC_LITERAL_PATH) {
285 magic = PATHSPEC_LITERAL;
288 - else
286 + } else {
287 + copyfrom = parse_element_magic(&element_magic,
288 + &pathspec_prefix,
289 + elt);
290 + magic |= element_magic;
291 magic |= get_global_magic(element_magic);
292 + }
293
294 if (pathspec_prefix >= 0 &&
295 (prefixlen || (prefix && *prefix)))