Coverage Report

Created: 2026-01-09 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/pathspec.c
Line
Count
Source
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "abspath.h"
5
#include "parse.h"
6
#include "dir.h"
7
#include "environment.h"
8
#include "gettext.h"
9
#include "pathspec.h"
10
#include "attr.h"
11
#include "read-cache.h"
12
#include "repository.h"
13
#include "setup.h"
14
#include "strvec.h"
15
#include "symlinks.h"
16
#include "quote.h"
17
#include "wildmatch.h"
18
19
/*
20
 * Finds which of the given pathspecs match items in the index.
21
 *
22
 * For each pathspec, sets the corresponding entry in the seen[] array
23
 * (which should be specs items long, i.e. the same size as pathspec)
24
 * to the nature of the "closest" (i.e. most specific) match found for
25
 * that pathspec in the index, if it was a closer type of match than
26
 * the existing entry.  As an optimization, matching is skipped
27
 * altogether if seen[] already only contains non-zero entries.
28
 *
29
 * If seen[] has not already been written to, it may make sense
30
 * to use find_pathspecs_matching_against_index() instead.
31
 */
32
void add_pathspec_matches_against_index(const struct pathspec *pathspec,
33
          struct index_state *istate,
34
          char *seen,
35
          enum ps_skip_worktree_action sw_action)
36
0
{
37
0
  int num_unmatched = 0;
38
39
  /*
40
   * Since we are walking the index as if we were walking the directory,
41
   * we have to mark the matched pathspec as seen; otherwise we will
42
   * mistakenly think that the user gave a pathspec that did not match
43
   * anything.
44
   */
45
0
  for (int i = 0; i < pathspec->nr; i++)
46
0
    if (!seen[i])
47
0
      num_unmatched++;
48
0
  if (!num_unmatched)
49
0
    return;
50
0
  for (unsigned int i = 0; i < istate->cache_nr; i++) {
51
0
    const struct cache_entry *ce = istate->cache[i];
52
0
    if (sw_action == PS_IGNORE_SKIP_WORKTREE &&
53
0
        (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)))
54
0
      continue;
55
0
    ce_path_match(istate, ce, pathspec, seen);
56
0
  }
57
0
}
58
59
/*
60
 * Finds which of the given pathspecs match items in the index.
61
 *
62
 * This is a one-shot wrapper around add_pathspec_matches_against_index()
63
 * which allocates, populates, and returns a seen[] array indicating the
64
 * nature of the "closest" (i.e. most specific) matches which each of the
65
 * given pathspecs achieves against all items in the index.
66
 */
67
char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
68
              struct index_state *istate,
69
              enum ps_skip_worktree_action sw_action)
70
0
{
71
0
  char *seen = xcalloc(pathspec->nr, 1);
72
0
  add_pathspec_matches_against_index(pathspec, istate, seen, sw_action);
73
0
  return seen;
74
0
}
75
76
char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec)
77
0
{
78
0
  struct index_state *istate = the_repository->index;
79
0
  char *seen = xcalloc(pathspec->nr, 1);
80
0
  unsigned int i;
81
82
0
  for (i = 0; i < istate->cache_nr; i++) {
83
0
    struct cache_entry *ce = istate->cache[i];
84
0
    if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))
85
0
        ce_path_match(istate, ce, pathspec, seen);
86
0
  }
87
88
0
  return seen;
89
0
}
90
91
/*
92
 * Magic pathspec
93
 *
94
 * Possible future magic semantics include stuff like:
95
 *
96
 *  { PATHSPEC_RECURSIVE, '*', "recursive" },
97
 *  { PATHSPEC_REGEXP, '\0', "regexp" },
98
 *
99
 */
100
101
static struct pathspec_magic {
102
  unsigned bit;
103
  char mnemonic; /* this cannot be ':'! */
104
  const char *name;
105
} pathspec_magic[] = {
106
  { PATHSPEC_FROMTOP,  '/', "top" },
107
  { PATHSPEC_LITERAL, '\0', "literal" },
108
  { PATHSPEC_GLOB,    '\0', "glob" },
109
  { PATHSPEC_ICASE,   '\0', "icase" },
110
  { PATHSPEC_EXCLUDE,  '!', "exclude" },
111
  { PATHSPEC_ATTR,    '\0', "attr" },
112
};
113
114
static void prefix_magic(struct strbuf *sb, int prefixlen,
115
       unsigned magic, const char *element)
116
0
{
117
  /* No magic was found in element, just add prefix magic */
118
0
  if (!magic) {
119
0
    strbuf_addf(sb, ":(prefix:%d)", prefixlen);
120
0
    return;
121
0
  }
122
123
  /*
124
   * At this point, we know that parse_element_magic() was able
125
   * to extract some pathspec magic from element. So we know
126
   * element is correctly formatted in either shorthand or
127
   * longhand form
128
   */
129
0
  if (element[1] != '(') {
130
    /* Process an element in shorthand form (e.g. ":!/<match>") */
131
0
    strbuf_addstr(sb, ":(");
132
0
    for (unsigned int i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
133
0
      if ((magic & pathspec_magic[i].bit) &&
134
0
          pathspec_magic[i].mnemonic) {
135
0
        if (sb->buf[sb->len - 1] != '(')
136
0
          strbuf_addch(sb, ',');
137
0
        strbuf_addstr(sb, pathspec_magic[i].name);
138
0
      }
139
0
    }
140
0
  } else {
141
    /* For the longhand form, we copy everything up to the final ')' */
142
0
    size_t len = strchr(element, ')') - element;
143
0
    strbuf_add(sb, element, len);
144
0
  }
145
0
  strbuf_addf(sb, ",prefix:%d)", prefixlen);
146
0
}
147
148
static size_t strcspn_escaped(const char *s, const char *stop)
149
0
{
150
0
  const char *i;
151
152
0
  for (i = s; *i; i++) {
153
    /* skip the escaped character */
154
0
    if (i[0] == '\\' && i[1]) {
155
0
      i++;
156
0
      continue;
157
0
    }
158
159
0
    if (strchr(stop, *i))
160
0
      break;
161
0
  }
162
0
  return i - s;
163
0
}
164
165
static inline int invalid_value_char(const char ch)
166
0
{
167
0
  if (isalnum(ch) || strchr(",-_", ch))
168
0
    return 0;
169
0
  return -1;
170
0
}
171
172
static char *attr_value_unescape(const char *value)
173
0
{
174
0
  const char *src;
175
0
  char *dst, *ret;
176
177
0
  ret = xmallocz(strlen(value));
178
0
  for (src = value, dst = ret; *src; src++, dst++) {
179
0
    if (*src == '\\') {
180
0
      if (!src[1])
181
0
        die(_("Escape character '\\' not allowed as "
182
0
              "last character in attr value"));
183
0
      src++;
184
0
    }
185
0
    if (invalid_value_char(*src))
186
0
      die("cannot use '%c' for value matching", *src);
187
0
    *dst = *src;
188
0
  }
189
0
  *dst = '\0';
190
0
  return ret;
191
0
}
192
193
static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
194
0
{
195
0
  struct string_list_item *si;
196
0
  struct string_list list = STRING_LIST_INIT_DUP;
197
198
0
  if (item->attr_check || item->attr_match)
199
0
    die(_("Only one 'attr:' specification is allowed."));
200
201
0
  if (!value || !*value)
202
0
    die(_("attr spec must not be empty"));
203
204
0
  string_list_split_f(&list, value, " ", -1, STRING_LIST_SPLIT_NONEMPTY);
205
206
0
  item->attr_check = attr_check_alloc();
207
0
  CALLOC_ARRAY(item->attr_match, list.nr);
208
209
0
  for_each_string_list_item(si, &list) {
210
0
    size_t attr_len;
211
0
    char *attr_name;
212
0
    const struct git_attr *a;
213
214
0
    int j = item->attr_match_nr++;
215
0
    const char *attr = si->string;
216
0
    struct attr_match *am = &item->attr_match[j];
217
218
0
    switch (*attr) {
219
0
    case '!':
220
0
      am->match_mode = MATCH_UNSPECIFIED;
221
0
      attr++;
222
0
      attr_len = strlen(attr);
223
0
      break;
224
0
    case '-':
225
0
      am->match_mode = MATCH_UNSET;
226
0
      attr++;
227
0
      attr_len = strlen(attr);
228
0
      break;
229
0
    default:
230
0
      attr_len = strcspn(attr, "=");
231
0
      if (attr[attr_len] != '=')
232
0
        am->match_mode = MATCH_SET;
233
0
      else {
234
0
        const char *v = &attr[attr_len + 1];
235
0
        am->match_mode = MATCH_VALUE;
236
0
        am->value = attr_value_unescape(v);
237
0
      }
238
0
      break;
239
0
    }
240
241
0
    attr_name = xmemdupz(attr, attr_len);
242
0
    a = git_attr(attr_name);
243
0
    if (!a)
244
0
      die(_("invalid attribute name %s"), attr_name);
245
246
0
    attr_check_append(item->attr_check, a);
247
248
0
    free(attr_name);
249
0
  }
250
251
0
  if (item->attr_check->nr != item->attr_match_nr)
252
0
    BUG("should have same number of entries");
253
254
0
  string_list_clear(&list, 0);
255
0
}
256
257
static inline int get_literal_global(void)
258
0
{
259
0
  static int literal = -1;
260
261
0
  if (literal < 0)
262
0
    literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
263
264
0
  return literal;
265
0
}
266
267
static inline int get_glob_global(void)
268
0
{
269
0
  static int glob = -1;
270
271
0
  if (glob < 0)
272
0
    glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
273
274
0
  return glob;
275
0
}
276
277
static inline int get_noglob_global(void)
278
0
{
279
0
  static int noglob = -1;
280
281
0
  if (noglob < 0)
282
0
    noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
283
284
0
  return noglob;
285
0
}
286
287
static inline int get_icase_global(void)
288
0
{
289
0
  static int icase = -1;
290
291
0
  if (icase < 0)
292
0
    icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
293
294
0
  return icase;
295
0
}
296
297
static int get_global_magic(int element_magic)
298
0
{
299
0
  int global_magic = 0;
300
301
0
  if (get_literal_global())
302
0
    global_magic |= PATHSPEC_LITERAL;
303
304
  /* --glob-pathspec is overridden by :(literal) */
305
0
  if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
306
0
    global_magic |= PATHSPEC_GLOB;
307
308
0
  if (get_glob_global() && get_noglob_global())
309
0
    die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
310
311
0
  if (get_icase_global())
312
0
    global_magic |= PATHSPEC_ICASE;
313
314
0
  if ((global_magic & PATHSPEC_LITERAL) &&
315
0
      (global_magic & ~PATHSPEC_LITERAL))
316
0
    die(_("global 'literal' pathspec setting is incompatible "
317
0
          "with all other global pathspec settings"));
318
319
  /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
320
0
  if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
321
0
    global_magic |= PATHSPEC_LITERAL;
322
323
0
  return global_magic;
324
0
}
325
326
/*
327
 * Parse the pathspec element looking for long magic
328
 *
329
 * saves all magic in 'magic'
330
 * if prefix magic is used, save the prefix length in 'prefix_len'
331
 * returns the position in 'elem' after all magic has been parsed
332
 */
333
static const char *parse_long_magic(unsigned *magic, int *prefix_len,
334
            struct pathspec_item *item,
335
            const char *elem)
336
0
{
337
0
  const char *pos;
338
0
  const char *nextat;
339
340
0
  for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
341
0
    size_t len = strcspn_escaped(pos, ",)");
342
0
    unsigned int i;
343
344
0
    if (pos[len] == ',')
345
0
      nextat = pos + len + 1; /* handle ',' */
346
0
    else
347
0
      nextat = pos + len; /* handle ')' and '\0' */
348
349
0
    if (!len)
350
0
      continue;
351
352
0
    if (starts_with(pos, "prefix:")) {
353
0
      char *endptr;
354
0
      *prefix_len = strtol(pos + 7, &endptr, 10);
355
0
      if ((size_t)(endptr - pos) != len)
356
0
        die(_("invalid parameter for pathspec magic 'prefix'"));
357
0
      continue;
358
0
    }
359
360
0
    if (starts_with(pos, "attr:")) {
361
0
      char *attr_body = xmemdupz(pos + 5, len - 5);
362
0
      parse_pathspec_attr_match(item, attr_body);
363
0
      *magic |= PATHSPEC_ATTR;
364
0
      free(attr_body);
365
0
      continue;
366
0
    }
367
368
0
    for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
369
0
      if (strlen(pathspec_magic[i].name) == len &&
370
0
          !strncmp(pathspec_magic[i].name, pos, len)) {
371
0
        *magic |= pathspec_magic[i].bit;
372
0
        break;
373
0
      }
374
0
    }
375
376
0
    if (ARRAY_SIZE(pathspec_magic) <= i)
377
0
      die(_("Invalid pathspec magic '%.*s' in '%s'"),
378
0
          (int) len, pos, elem);
379
0
  }
380
381
0
  if (*pos != ')')
382
0
    die(_("Missing ')' at the end of pathspec magic in '%s'"),
383
0
        elem);
384
0
  pos++;
385
386
0
  return pos;
387
0
}
388
389
/*
390
 * Parse the pathspec element looking for short magic
391
 *
392
 * saves all magic in 'magic'
393
 * returns the position in 'elem' after all magic has been parsed
394
 */
395
static const char *parse_short_magic(unsigned *magic, const char *elem)
396
0
{
397
0
  const char *pos;
398
399
0
  for (pos = elem + 1; *pos && *pos != ':'; pos++) {
400
0
    char ch = *pos;
401
0
    unsigned int i;
402
403
    /* Special case alias for '!' */
404
0
    if (ch == '^') {
405
0
      *magic |= PATHSPEC_EXCLUDE;
406
0
      continue;
407
0
    }
408
409
0
    if (!is_pathspec_magic(ch))
410
0
      break;
411
412
0
    for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
413
0
      if (pathspec_magic[i].mnemonic == ch) {
414
0
        *magic |= pathspec_magic[i].bit;
415
0
        break;
416
0
      }
417
0
    }
418
419
0
    if (ARRAY_SIZE(pathspec_magic) <= i)
420
0
      die(_("Unimplemented pathspec magic '%c' in '%s'"),
421
0
          ch, elem);
422
0
  }
423
424
0
  if (*pos == ':')
425
0
    pos++;
426
427
0
  return pos;
428
0
}
429
430
static const char *parse_element_magic(unsigned *magic, int *prefix_len,
431
               struct pathspec_item *item,
432
               const char *elem)
433
0
{
434
0
  if (elem[0] != ':' || get_literal_global())
435
0
    return elem; /* nothing to do */
436
0
  else if (elem[1] == '(')
437
    /* longhand */
438
0
    return parse_long_magic(magic, prefix_len, item, elem);
439
0
  else
440
    /* shorthand */
441
0
    return parse_short_magic(magic, elem);
442
0
}
443
444
/*
445
 * Perform the initialization of a pathspec_item based on a pathspec element.
446
 */
447
static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
448
             const char *prefix, int prefixlen,
449
             const char *elt)
450
0
{
451
0
  unsigned magic = 0, element_magic = 0;
452
0
  const char *copyfrom = elt;
453
0
  char *match;
454
0
  int pathspec_prefix = -1;
455
456
0
  item->attr_check = NULL;
457
0
  item->attr_match = NULL;
458
0
  item->attr_match_nr = 0;
459
460
  /* PATHSPEC_LITERAL_PATH ignores magic */
461
0
  if (flags & PATHSPEC_LITERAL_PATH) {
462
0
    magic = PATHSPEC_LITERAL;
463
0
  } else {
464
0
    copyfrom = parse_element_magic(&element_magic,
465
0
                 &pathspec_prefix,
466
0
                 item,
467
0
                 elt);
468
0
    magic |= element_magic;
469
0
    magic |= get_global_magic(element_magic);
470
0
  }
471
472
0
  item->magic = magic;
473
474
0
  if (pathspec_prefix >= 0 &&
475
0
      (prefixlen || (prefix && *prefix)))
476
0
    BUG("'prefix' magic is supposed to be used at worktree's root");
477
478
0
  if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
479
0
    die(_("%s: 'literal' and 'glob' are incompatible"), elt);
480
481
  /* Create match string which will be used for pathspec matching */
482
0
  if (pathspec_prefix >= 0) {
483
0
    match = xstrdup(copyfrom);
484
0
    prefixlen = pathspec_prefix;
485
0
  } else if (magic & PATHSPEC_FROMTOP) {
486
0
    match = xstrdup(copyfrom);
487
0
    prefixlen = 0;
488
0
  } else {
489
0
    match = prefix_path_gently(prefix, prefixlen,
490
0
             &prefixlen, copyfrom);
491
0
    if (!match) {
492
0
      const char *hint_path;
493
494
0
      if ((flags & PATHSPEC_NO_REPOSITORY) || !have_git_dir())
495
0
        die(_("'%s' is outside the directory tree"),
496
0
            copyfrom);
497
0
      hint_path = repo_get_work_tree(the_repository);
498
0
      if (!hint_path)
499
0
        hint_path = repo_get_git_dir(the_repository);
500
0
      die(_("%s: '%s' is outside repository at '%s'"), elt,
501
0
          copyfrom, absolute_path(hint_path));
502
0
    }
503
0
  }
504
505
0
  item->match = match;
506
0
  item->len = strlen(item->match);
507
0
  item->prefix = prefixlen;
508
509
  /*
510
   * Prefix the pathspec (keep all magic) and assign to
511
   * original. Useful for passing to another command.
512
   */
513
0
  if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
514
0
      !get_literal_global()) {
515
0
    struct strbuf sb = STRBUF_INIT;
516
517
    /* Preserve the actual prefix length of each pattern */
518
0
    prefix_magic(&sb, prefixlen, element_magic, elt);
519
520
0
    strbuf_addstr(&sb, match);
521
0
    item->original = strbuf_detach(&sb, NULL);
522
0
  } else {
523
0
    item->original = xstrdup(elt);
524
0
  }
525
526
0
  if (magic & PATHSPEC_LITERAL) {
527
0
    item->nowildcard_len = item->len;
528
0
  } else {
529
0
    item->nowildcard_len = simple_length(item->match);
530
0
    if (item->nowildcard_len < prefixlen)
531
0
      item->nowildcard_len = prefixlen;
532
0
  }
533
534
0
  item->flags = 0;
535
0
  if (magic & PATHSPEC_GLOB) {
536
    /*
537
     * FIXME: should we enable ONESTAR in _GLOB for
538
     * pattern "* * / * . c"?
539
     */
540
0
  } else {
541
0
    if (item->nowildcard_len < item->len &&
542
0
        item->match[item->nowildcard_len] == '*' &&
543
0
        no_wildcard(item->match + item->nowildcard_len + 1))
544
0
      item->flags |= PATHSPEC_ONESTAR;
545
0
  }
546
547
  /* sanity checks, pathspec matchers assume these are sane */
548
0
  if (item->nowildcard_len > item->len ||
549
0
      item->prefix         > item->len) {
550
0
    BUG("error initializing pathspec_item");
551
0
  }
552
0
}
553
554
static int pathspec_item_cmp(const void *a_, const void *b_)
555
0
{
556
0
  struct pathspec_item *a, *b;
557
558
0
  a = (struct pathspec_item *)a_;
559
0
  b = (struct pathspec_item *)b_;
560
0
  return strcmp(a->match, b->match);
561
0
}
562
563
void pathspec_magic_names(unsigned magic, struct strbuf *out)
564
0
{
565
0
  unsigned int i;
566
0
  for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
567
0
    const struct pathspec_magic *m = pathspec_magic + i;
568
0
    if (!(magic & m->bit))
569
0
      continue;
570
0
    if (out->len)
571
0
      strbuf_addstr(out, ", ");
572
573
0
    if (m->mnemonic)
574
0
      strbuf_addf(out, _("'%s' (mnemonic: '%c')"),
575
0
            m->name, m->mnemonic);
576
0
    else
577
0
      strbuf_addf(out, "'%s'", m->name);
578
0
  }
579
0
}
580
581
static void NORETURN unsupported_magic(const char *pattern,
582
               unsigned magic)
583
0
{
584
0
  struct strbuf sb = STRBUF_INIT;
585
0
  pathspec_magic_names(magic, &sb);
586
  /*
587
   * We may want to substitute "this command" with a command
588
   * name. E.g. when "git add -p" or "git add -i" dies when running
589
   * "checkout -p"
590
   */
591
0
  die(_("%s: pathspec magic not supported by this command: %s"),
592
0
      pattern, sb.buf);
593
0
}
594
595
void parse_pathspec(struct pathspec *pathspec,
596
        unsigned magic_mask, unsigned flags,
597
        const char *prefix, const char **argv)
598
0
{
599
0
  struct pathspec_item *item;
600
0
  const char *entry = argv ? *argv : NULL;
601
0
  int i, n, prefixlen, nr_exclude = 0;
602
603
0
  memset(pathspec, 0, sizeof(*pathspec));
604
605
0
  if (flags & PATHSPEC_MAXDEPTH_VALID)
606
0
    pathspec->magic |= PATHSPEC_MAXDEPTH;
607
608
  /* No arguments, no prefix -> no pathspec */
609
0
  if (!entry && !prefix)
610
0
    return;
611
612
0
  if ((flags & PATHSPEC_PREFER_CWD) &&
613
0
      (flags & PATHSPEC_PREFER_FULL))
614
0
    BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
615
616
0
  if ((flags & PATHSPEC_NO_REPOSITORY) &&
617
0
      (~magic_mask & (PATHSPEC_ATTR | PATHSPEC_FROMTOP)))
618
0
    BUG("PATHSPEC_NO_REPOSITORY is incompatible with PATHSPEC_ATTR and PATHSPEC_FROMTOP");
619
620
  /* No arguments with prefix -> prefix pathspec */
621
0
  if (!entry) {
622
0
    if (flags & PATHSPEC_PREFER_FULL)
623
0
      return;
624
625
0
    if (!(flags & PATHSPEC_PREFER_CWD))
626
0
      BUG("PATHSPEC_PREFER_CWD requires arguments");
627
628
0
    pathspec->items = CALLOC_ARRAY(item, 1);
629
0
    item->match = xstrdup(prefix);
630
0
    item->original = xstrdup(prefix);
631
0
    item->nowildcard_len = item->len = strlen(prefix);
632
0
    item->prefix = item->len;
633
0
    pathspec->nr = 1;
634
0
    return;
635
0
  }
636
637
0
  n = 0;
638
0
  while (argv[n]) {
639
0
    if (*argv[n] == '\0')
640
0
      die("empty string is not a valid pathspec. "
641
0
          "please use . instead if you meant to match all paths");
642
0
    n++;
643
0
  }
644
645
0
  pathspec->nr = n;
646
0
  ALLOC_ARRAY(pathspec->items, n + 1);
647
0
  item = pathspec->items;
648
0
  prefixlen = prefix ? strlen(prefix) : 0;
649
650
0
  for (i = 0; i < n; i++) {
651
0
    entry = argv[i];
652
653
0
    init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
654
655
0
    if (item[i].magic & PATHSPEC_EXCLUDE)
656
0
      nr_exclude++;
657
0
    if (item[i].magic & magic_mask)
658
0
      unsupported_magic(entry, item[i].magic & magic_mask);
659
660
0
    if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
661
0
        has_symlink_leading_path(item[i].match, item[i].len)) {
662
0
      die(_("pathspec '%s' is beyond a symbolic link"), entry);
663
0
    }
664
665
0
    if (item[i].nowildcard_len < item[i].len)
666
0
      pathspec->has_wildcard = 1;
667
0
    pathspec->magic |= item[i].magic;
668
0
  }
669
670
  /*
671
   * If everything is an exclude pattern, add one positive pattern
672
   * that matches everything. We allocated an extra one for this.
673
   */
674
0
  if (nr_exclude == n) {
675
0
    int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
676
0
    init_pathspec_item(item + n, 0, prefix, plen, ".");
677
0
    pathspec->nr++;
678
0
  }
679
680
0
  if (pathspec->magic & PATHSPEC_MAXDEPTH) {
681
0
    if (flags & PATHSPEC_KEEP_ORDER)
682
0
      BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
683
0
    QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
684
0
  }
685
0
}
686
687
void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
688
       unsigned flags, const char *prefix,
689
       const char *file, int nul_term_line)
690
0
{
691
0
  struct strvec parsed_file = STRVEC_INIT;
692
0
  strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :
693
0
                   strbuf_getline;
694
0
  struct strbuf buf = STRBUF_INIT;
695
0
  struct strbuf unquoted = STRBUF_INIT;
696
0
  FILE *in;
697
698
0
  if (!strcmp(file, "-"))
699
0
    in = stdin;
700
0
  else
701
0
    in = xfopen(file, "r");
702
703
0
  while (getline_fn(&buf, in) != EOF) {
704
0
    if (!nul_term_line && buf.buf[0] == '"') {
705
0
      strbuf_reset(&unquoted);
706
0
      if (unquote_c_style(&unquoted, buf.buf, NULL))
707
0
        die(_("line is badly quoted: %s"), buf.buf);
708
0
      strbuf_swap(&buf, &unquoted);
709
0
    }
710
0
    strvec_push(&parsed_file, buf.buf);
711
0
    strbuf_reset(&buf);
712
0
  }
713
714
0
  strbuf_release(&unquoted);
715
0
  strbuf_release(&buf);
716
0
  if (in != stdin)
717
0
    fclose(in);
718
719
0
  parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);
720
0
  strvec_clear(&parsed_file);
721
0
}
722
723
void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
724
0
{
725
0
  int i, j;
726
727
0
  *dst = *src;
728
0
  DUP_ARRAY(dst->items, src->items, dst->nr);
729
730
0
  for (i = 0; i < dst->nr; i++) {
731
0
    struct pathspec_item *d = &dst->items[i];
732
0
    struct pathspec_item *s = &src->items[i];
733
734
0
    d->match = xstrdup(s->match);
735
0
    d->original = xstrdup(s->original);
736
737
0
    DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
738
0
    for (j = 0; j < d->attr_match_nr; j++) {
739
0
      const char *value = s->attr_match[j].value;
740
0
      d->attr_match[j].value = xstrdup_or_null(value);
741
0
    }
742
743
0
    d->attr_check = attr_check_dup(s->attr_check);
744
0
  }
745
0
}
746
747
void clear_pathspec(struct pathspec *pathspec)
748
0
{
749
0
  int i, j;
750
751
0
  for (i = 0; i < pathspec->nr; i++) {
752
0
    free(pathspec->items[i].match);
753
0
    free(pathspec->items[i].original);
754
755
0
    for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
756
0
      free(pathspec->items[i].attr_match[j].value);
757
0
    free(pathspec->items[i].attr_match);
758
759
0
    if (pathspec->items[i].attr_check)
760
0
      attr_check_free(pathspec->items[i].attr_check);
761
0
  }
762
763
0
  FREE_AND_NULL(pathspec->items);
764
0
  pathspec->nr = 0;
765
0
}
766
767
int match_pathspec_attrs(struct index_state *istate,
768
       const char *name, int namelen,
769
       const struct pathspec_item *item)
770
0
{
771
0
  int i;
772
0
  char *to_free = NULL;
773
774
0
  if (name[namelen])
775
0
    name = to_free = xmemdupz(name, namelen);
776
777
0
  git_check_attr(istate, name, item->attr_check);
778
779
0
  free(to_free);
780
781
0
  for (i = 0; i < item->attr_match_nr; i++) {
782
0
    const char *value;
783
0
    int matched;
784
0
    enum attr_match_mode match_mode;
785
786
0
    value = item->attr_check->items[i].value;
787
0
    match_mode = item->attr_match[i].match_mode;
788
789
0
    if (ATTR_TRUE(value))
790
0
      matched = (match_mode == MATCH_SET);
791
0
    else if (ATTR_FALSE(value))
792
0
      matched = (match_mode == MATCH_UNSET);
793
0
    else if (ATTR_UNSET(value))
794
0
      matched = (match_mode == MATCH_UNSPECIFIED);
795
0
    else
796
0
      matched = (match_mode == MATCH_VALUE &&
797
0
           !strcmp(item->attr_match[i].value, value));
798
0
    if (!matched)
799
0
      return 0;
800
0
  }
801
802
0
  return 1;
803
0
}
804
805
int pathspec_needs_expanded_index(struct index_state *istate,
806
          const struct pathspec *pathspec)
807
0
{
808
0
  unsigned int pos;
809
0
  int i, res = 0;
810
0
  char *skip_worktree_seen = NULL;
811
812
  /*
813
   * If index is not sparse, no index expansion is needed.
814
   */
815
0
  if (!istate->sparse_index)
816
0
    return 0;
817
818
  /*
819
   * When using a magic pathspec, assume for the sake of simplicity that
820
   * the index needs to be expanded to match all matchable files.
821
   */
822
0
  if (pathspec->magic)
823
0
    return 1;
824
825
0
  for (i = 0; i < pathspec->nr; i++) {
826
0
    struct pathspec_item item = pathspec->items[i];
827
828
    /*
829
     * If the pathspec item has a wildcard, the index should be expanded
830
     * if the pathspec has the possibility of matching a subset of entries inside
831
     * of a sparse directory (but not the entire directory).
832
     *
833
     * If the pathspec item is a literal path, the index only needs to be expanded
834
     * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
835
     * expand for in-cone files) and b) it doesn't match any sparse directories
836
     * (since we can reset whole sparse directories without expanding them).
837
     */
838
0
    if (item.nowildcard_len < item.len) {
839
      /*
840
       * Special case: if the pattern is a path inside the cone
841
       * followed by only wildcards, the pattern cannot match
842
       * partial sparse directories, so we know we don't need to
843
       * expand the index.
844
       *
845
       * Examples:
846
       * - in-cone/foo***: doesn't need expanded index
847
       * - not-in-cone/bar*: may need expanded index
848
       * - **.c: may need expanded index
849
       */
850
0
      if (strspn(item.original + item.nowildcard_len, "*") ==
851
0
            (unsigned int)(item.len - item.nowildcard_len) &&
852
0
          path_in_cone_mode_sparse_checkout(item.original, istate))
853
0
        continue;
854
855
0
      for (pos = 0; pos < istate->cache_nr; pos++) {
856
0
        struct cache_entry *ce = istate->cache[pos];
857
858
0
        if (!S_ISSPARSEDIR(ce->ce_mode))
859
0
          continue;
860
861
        /*
862
         * If the pre-wildcard length is longer than the sparse
863
         * directory name and the sparse directory is the first
864
         * component of the pathspec, need to expand the index.
865
         */
866
0
        if ((unsigned int)item.nowildcard_len >
867
0
              ce_namelen(ce) &&
868
0
            !strncmp(item.original, ce->name,
869
0
               ce_namelen(ce))) {
870
0
          res = 1;
871
0
          break;
872
0
        }
873
874
        /*
875
         * If the pre-wildcard length is shorter than the sparse
876
         * directory and the pathspec does not match the whole
877
         * directory, need to expand the index.
878
         */
879
0
        if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
880
0
            wildmatch(item.original, ce->name, 0)) {
881
0
          res = 1;
882
0
          break;
883
0
        }
884
0
      }
885
0
    } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) &&
886
0
         !matches_skip_worktree(pathspec, i, &skip_worktree_seen))
887
0
      res = 1;
888
889
0
    if (res > 0)
890
0
      break;
891
0
  }
892
893
0
  free(skip_worktree_seen);
894
0
  return res;
895
0
}