Coverage Report

Created: 2023-11-19 07:08

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