10 * Finds which of the given pathspecs match items in the index.
12 * For each pathspec, sets the corresponding entry in the seen[] array
13 * (which should be specs items long, i.e. the same size as pathspec)
14 * to the nature of the "closest" (i.e. most specific) match found for
15 * that pathspec in the index, if it was a closer type of match than
16 * the existing entry. As an optimization, matching is skipped
17 * altogether if seen[] already only contains non-zero entries.
19 * If seen[] has not already been written to, it may make sense
20 * to use find_pathspecs_matching_against_index() instead.
22 void add_pathspec_matches_against_index(const struct pathspec
*pathspec
,
23 struct index_state
*istate
,
26 int num_unmatched
= 0, i
;
29 * Since we are walking the index as if we were walking the directory,
30 * we have to mark the matched pathspec as seen; otherwise we will
31 * mistakenly think that the user gave a pathspec that did not match
34 for (i
= 0; i
< pathspec
->nr
; i
++)
39 /* TODO: audit for interaction with sparse-index. */
40 ensure_full_index(istate
);
41 for (i
= 0; i
< istate
->cache_nr
; i
++) {
42 const struct cache_entry
*ce
= istate
->cache
[i
];
43 ce_path_match(istate
, ce
, pathspec
, seen
);
48 * Finds which of the given pathspecs match items in the index.
50 * This is a one-shot wrapper around add_pathspec_matches_against_index()
51 * which allocates, populates, and returns a seen[] array indicating the
52 * nature of the "closest" (i.e. most specific) matches which each of the
53 * given pathspecs achieves against all items in the index.
55 char *find_pathspecs_matching_against_index(const struct pathspec
*pathspec
,
56 struct index_state
*istate
)
58 char *seen
= xcalloc(pathspec
->nr
, 1);
59 add_pathspec_matches_against_index(pathspec
, istate
, seen
);
66 * Possible future magic semantics include stuff like:
68 * { PATHSPEC_RECURSIVE, '*', "recursive" },
69 * { PATHSPEC_REGEXP, '\0', "regexp" },
73 static struct pathspec_magic
{
75 char mnemonic
; /* this cannot be ':'! */
77 } pathspec_magic
[] = {
78 { PATHSPEC_FROMTOP
, '/', "top" },
79 { PATHSPEC_LITERAL
, '\0', "literal" },
80 { PATHSPEC_GLOB
, '\0', "glob" },
81 { PATHSPEC_ICASE
, '\0', "icase" },
82 { PATHSPEC_EXCLUDE
, '!', "exclude" },
83 { PATHSPEC_ATTR
, '\0', "attr" },
86 static void prefix_magic(struct strbuf
*sb
, int prefixlen
, unsigned magic
)
89 strbuf_addstr(sb
, ":(");
90 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
91 if (magic
& pathspec_magic
[i
].bit
) {
92 if (sb
->buf
[sb
->len
- 1] != '(')
93 strbuf_addch(sb
, ',');
94 strbuf_addstr(sb
, pathspec_magic
[i
].name
);
96 strbuf_addf(sb
, ",prefix:%d)", prefixlen
);
99 static size_t strcspn_escaped(const char *s
, const char *stop
)
103 for (i
= s
; *i
; i
++) {
104 /* skip the escaped character */
105 if (i
[0] == '\\' && i
[1]) {
110 if (strchr(stop
, *i
))
116 static inline int invalid_value_char(const char ch
)
118 if (isalnum(ch
) || strchr(",-_", ch
))
123 static char *attr_value_unescape(const char *value
)
128 ret
= xmallocz(strlen(value
));
129 for (src
= value
, dst
= ret
; *src
; src
++, dst
++) {
132 die(_("Escape character '\\' not allowed as "
133 "last character in attr value"));
136 if (invalid_value_char(*src
))
137 die("cannot use '%c' for value matching", *src
);
144 static void parse_pathspec_attr_match(struct pathspec_item
*item
, const char *value
)
146 struct string_list_item
*si
;
147 struct string_list list
= STRING_LIST_INIT_DUP
;
149 if (item
->attr_check
|| item
->attr_match
)
150 die(_("Only one 'attr:' specification is allowed."));
152 if (!value
|| !*value
)
153 die(_("attr spec must not be empty"));
155 string_list_split(&list
, value
, ' ', -1);
156 string_list_remove_empty_items(&list
, 0);
158 item
->attr_check
= attr_check_alloc();
159 CALLOC_ARRAY(item
->attr_match
, list
.nr
);
161 for_each_string_list_item(si
, &list
) {
164 const struct git_attr
*a
;
166 int j
= item
->attr_match_nr
++;
167 const char *attr
= si
->string
;
168 struct attr_match
*am
= &item
->attr_match
[j
];
172 am
->match_mode
= MATCH_UNSPECIFIED
;
174 attr_len
= strlen(attr
);
177 am
->match_mode
= MATCH_UNSET
;
179 attr_len
= strlen(attr
);
182 attr_len
= strcspn(attr
, "=");
183 if (attr
[attr_len
] != '=')
184 am
->match_mode
= MATCH_SET
;
186 const char *v
= &attr
[attr_len
+ 1];
187 am
->match_mode
= MATCH_VALUE
;
188 am
->value
= attr_value_unescape(v
);
193 attr_name
= xmemdupz(attr
, attr_len
);
194 a
= git_attr(attr_name
);
196 die(_("invalid attribute name %s"), attr_name
);
198 attr_check_append(item
->attr_check
, a
);
203 if (item
->attr_check
->nr
!= item
->attr_match_nr
)
204 BUG("should have same number of entries");
206 string_list_clear(&list
, 0);
209 static inline int get_literal_global(void)
211 static int literal
= -1;
214 literal
= git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, 0);
219 static inline int get_glob_global(void)
221 static int glob
= -1;
224 glob
= git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT
, 0);
229 static inline int get_noglob_global(void)
231 static int noglob
= -1;
234 noglob
= git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT
, 0);
239 static inline int get_icase_global(void)
241 static int icase
= -1;
244 icase
= git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT
, 0);
249 static int get_global_magic(int element_magic
)
251 int global_magic
= 0;
253 if (get_literal_global())
254 global_magic
|= PATHSPEC_LITERAL
;
256 /* --glob-pathspec is overridden by :(literal) */
257 if (get_glob_global() && !(element_magic
& PATHSPEC_LITERAL
))
258 global_magic
|= PATHSPEC_GLOB
;
260 if (get_glob_global() && get_noglob_global())
261 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
263 if (get_icase_global())
264 global_magic
|= PATHSPEC_ICASE
;
266 if ((global_magic
& PATHSPEC_LITERAL
) &&
267 (global_magic
& ~PATHSPEC_LITERAL
))
268 die(_("global 'literal' pathspec setting is incompatible "
269 "with all other global pathspec settings"));
271 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
272 if (get_noglob_global() && !(element_magic
& PATHSPEC_GLOB
))
273 global_magic
|= PATHSPEC_LITERAL
;
279 * Parse the pathspec element looking for long magic
281 * saves all magic in 'magic'
282 * if prefix magic is used, save the prefix length in 'prefix_len'
283 * returns the position in 'elem' after all magic has been parsed
285 static const char *parse_long_magic(unsigned *magic
, int *prefix_len
,
286 struct pathspec_item
*item
,
292 for (pos
= elem
+ 2; *pos
&& *pos
!= ')'; pos
= nextat
) {
293 size_t len
= strcspn_escaped(pos
, ",)");
297 nextat
= pos
+ len
+ 1; /* handle ',' */
299 nextat
= pos
+ len
; /* handle ')' and '\0' */
304 if (starts_with(pos
, "prefix:")) {
306 *prefix_len
= strtol(pos
+ 7, &endptr
, 10);
307 if (endptr
- pos
!= len
)
308 die(_("invalid parameter for pathspec magic 'prefix'"));
312 if (starts_with(pos
, "attr:")) {
313 char *attr_body
= xmemdupz(pos
+ 5, len
- 5);
314 parse_pathspec_attr_match(item
, attr_body
);
315 *magic
|= PATHSPEC_ATTR
;
320 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
321 if (strlen(pathspec_magic
[i
].name
) == len
&&
322 !strncmp(pathspec_magic
[i
].name
, pos
, len
)) {
323 *magic
|= pathspec_magic
[i
].bit
;
328 if (ARRAY_SIZE(pathspec_magic
) <= i
)
329 die(_("Invalid pathspec magic '%.*s' in '%s'"),
330 (int) len
, pos
, elem
);
334 die(_("Missing ')' at the end of pathspec magic in '%s'"),
342 * Parse the pathspec element looking for short magic
344 * saves all magic in 'magic'
345 * returns the position in 'elem' after all magic has been parsed
347 static const char *parse_short_magic(unsigned *magic
, const char *elem
)
351 for (pos
= elem
+ 1; *pos
&& *pos
!= ':'; pos
++) {
355 /* Special case alias for '!' */
357 *magic
|= PATHSPEC_EXCLUDE
;
361 if (!is_pathspec_magic(ch
))
364 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
365 if (pathspec_magic
[i
].mnemonic
== ch
) {
366 *magic
|= pathspec_magic
[i
].bit
;
371 if (ARRAY_SIZE(pathspec_magic
) <= i
)
372 die(_("Unimplemented pathspec magic '%c' in '%s'"),
382 static const char *parse_element_magic(unsigned *magic
, int *prefix_len
,
383 struct pathspec_item
*item
,
386 if (elem
[0] != ':' || get_literal_global())
387 return elem
; /* nothing to do */
388 else if (elem
[1] == '(')
390 return parse_long_magic(magic
, prefix_len
, item
, elem
);
393 return parse_short_magic(magic
, elem
);
397 * Perform the initialization of a pathspec_item based on a pathspec element.
399 static void init_pathspec_item(struct pathspec_item
*item
, unsigned flags
,
400 const char *prefix
, int prefixlen
,
403 unsigned magic
= 0, element_magic
= 0;
404 const char *copyfrom
= elt
;
406 int pathspec_prefix
= -1;
408 item
->attr_check
= NULL
;
409 item
->attr_match
= NULL
;
410 item
->attr_match_nr
= 0;
412 /* PATHSPEC_LITERAL_PATH ignores magic */
413 if (flags
& PATHSPEC_LITERAL_PATH
) {
414 magic
= PATHSPEC_LITERAL
;
416 copyfrom
= parse_element_magic(&element_magic
,
420 magic
|= element_magic
;
421 magic
|= get_global_magic(element_magic
);
426 if (pathspec_prefix
>= 0 &&
427 (prefixlen
|| (prefix
&& *prefix
)))
428 BUG("'prefix' magic is supposed to be used at worktree's root");
430 if ((magic
& PATHSPEC_LITERAL
) && (magic
& PATHSPEC_GLOB
))
431 die(_("%s: 'literal' and 'glob' are incompatible"), elt
);
433 /* Create match string which will be used for pathspec matching */
434 if (pathspec_prefix
>= 0) {
435 match
= xstrdup(copyfrom
);
436 prefixlen
= pathspec_prefix
;
437 } else if (magic
& PATHSPEC_FROMTOP
) {
438 match
= xstrdup(copyfrom
);
441 match
= prefix_path_gently(prefix
, prefixlen
,
442 &prefixlen
, copyfrom
);
444 const char *hint_path
= get_git_work_tree();
446 hint_path
= get_git_dir();
447 die(_("%s: '%s' is outside repository at '%s'"), elt
,
448 copyfrom
, absolute_path(hint_path
));
453 item
->len
= strlen(item
->match
);
454 item
->prefix
= prefixlen
;
457 * Prefix the pathspec (keep all magic) and assign to
458 * original. Useful for passing to another command.
460 if ((flags
& PATHSPEC_PREFIX_ORIGIN
) &&
461 !get_literal_global()) {
462 struct strbuf sb
= STRBUF_INIT
;
464 /* Preserve the actual prefix length of each pattern */
465 prefix_magic(&sb
, prefixlen
, element_magic
);
467 strbuf_addstr(&sb
, match
);
468 item
->original
= strbuf_detach(&sb
, NULL
);
470 item
->original
= xstrdup(elt
);
473 if (magic
& PATHSPEC_LITERAL
) {
474 item
->nowildcard_len
= item
->len
;
476 item
->nowildcard_len
= simple_length(item
->match
);
477 if (item
->nowildcard_len
< prefixlen
)
478 item
->nowildcard_len
= prefixlen
;
482 if (magic
& PATHSPEC_GLOB
) {
484 * FIXME: should we enable ONESTAR in _GLOB for
485 * pattern "* * / * . c"?
488 if (item
->nowildcard_len
< item
->len
&&
489 item
->match
[item
->nowildcard_len
] == '*' &&
490 no_wildcard(item
->match
+ item
->nowildcard_len
+ 1))
491 item
->flags
|= PATHSPEC_ONESTAR
;
494 /* sanity checks, pathspec matchers assume these are sane */
495 if (item
->nowildcard_len
> item
->len
||
496 item
->prefix
> item
->len
) {
497 BUG("error initializing pathspec_item");
501 static int pathspec_item_cmp(const void *a_
, const void *b_
)
503 struct pathspec_item
*a
, *b
;
505 a
= (struct pathspec_item
*)a_
;
506 b
= (struct pathspec_item
*)b_
;
507 return strcmp(a
->match
, b
->match
);
510 static void NORETURN
unsupported_magic(const char *pattern
,
513 struct strbuf sb
= STRBUF_INIT
;
515 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
516 const struct pathspec_magic
*m
= pathspec_magic
+ i
;
517 if (!(magic
& m
->bit
))
520 strbuf_addstr(&sb
, ", ");
523 strbuf_addf(&sb
, _("'%s' (mnemonic: '%c')"),
524 m
->name
, m
->mnemonic
);
526 strbuf_addf(&sb
, "'%s'", m
->name
);
529 * We may want to substitute "this command" with a command
530 * name. E.g. when add--interactive dies when running
533 die(_("%s: pathspec magic not supported by this command: %s"),
537 void parse_pathspec(struct pathspec
*pathspec
,
538 unsigned magic_mask
, unsigned flags
,
539 const char *prefix
, const char **argv
)
541 struct pathspec_item
*item
;
542 const char *entry
= argv
? *argv
: NULL
;
543 int i
, n
, prefixlen
, nr_exclude
= 0;
545 memset(pathspec
, 0, sizeof(*pathspec
));
547 if (flags
& PATHSPEC_MAXDEPTH_VALID
)
548 pathspec
->magic
|= PATHSPEC_MAXDEPTH
;
550 /* No arguments, no prefix -> no pathspec */
551 if (!entry
&& !prefix
)
554 if ((flags
& PATHSPEC_PREFER_CWD
) &&
555 (flags
& PATHSPEC_PREFER_FULL
))
556 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
558 /* No arguments with prefix -> prefix pathspec */
560 if (flags
& PATHSPEC_PREFER_FULL
)
563 if (!(flags
& PATHSPEC_PREFER_CWD
))
564 BUG("PATHSPEC_PREFER_CWD requires arguments");
566 pathspec
->items
= CALLOC_ARRAY(item
, 1);
567 item
->match
= xstrdup(prefix
);
568 item
->original
= xstrdup(prefix
);
569 item
->nowildcard_len
= item
->len
= strlen(prefix
);
570 item
->prefix
= item
->len
;
577 if (*argv
[n
] == '\0')
578 die("empty string is not a valid pathspec. "
579 "please use . instead if you meant to match all paths");
584 ALLOC_ARRAY(pathspec
->items
, n
+ 1);
585 item
= pathspec
->items
;
586 prefixlen
= prefix
? strlen(prefix
) : 0;
588 for (i
= 0; i
< n
; i
++) {
591 init_pathspec_item(item
+ i
, flags
, prefix
, prefixlen
, entry
);
593 if (item
[i
].magic
& PATHSPEC_EXCLUDE
)
595 if (item
[i
].magic
& magic_mask
)
596 unsupported_magic(entry
, item
[i
].magic
& magic_mask
);
598 if ((flags
& PATHSPEC_SYMLINK_LEADING_PATH
) &&
599 has_symlink_leading_path(item
[i
].match
, item
[i
].len
)) {
600 die(_("pathspec '%s' is beyond a symbolic link"), entry
);
603 if (item
[i
].nowildcard_len
< item
[i
].len
)
604 pathspec
->has_wildcard
= 1;
605 pathspec
->magic
|= item
[i
].magic
;
609 * If everything is an exclude pattern, add one positive pattern
610 * that matches everything. We allocated an extra one for this.
612 if (nr_exclude
== n
) {
613 int plen
= (!(flags
& PATHSPEC_PREFER_CWD
)) ? 0 : prefixlen
;
614 init_pathspec_item(item
+ n
, 0, prefix
, plen
, "");
618 if (pathspec
->magic
& PATHSPEC_MAXDEPTH
) {
619 if (flags
& PATHSPEC_KEEP_ORDER
)
620 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
621 QSORT(pathspec
->items
, pathspec
->nr
, pathspec_item_cmp
);
625 void parse_pathspec_file(struct pathspec
*pathspec
, unsigned magic_mask
,
626 unsigned flags
, const char *prefix
,
627 const char *file
, int nul_term_line
)
629 struct strvec parsed_file
= STRVEC_INIT
;
630 strbuf_getline_fn getline_fn
= nul_term_line
? strbuf_getline_nul
:
632 struct strbuf buf
= STRBUF_INIT
;
633 struct strbuf unquoted
= STRBUF_INIT
;
636 if (!strcmp(file
, "-"))
639 in
= xfopen(file
, "r");
641 while (getline_fn(&buf
, in
) != EOF
) {
642 if (!nul_term_line
&& buf
.buf
[0] == '"') {
643 strbuf_reset(&unquoted
);
644 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
645 die(_("line is badly quoted: %s"), buf
.buf
);
646 strbuf_swap(&buf
, &unquoted
);
648 strvec_push(&parsed_file
, buf
.buf
);
652 strbuf_release(&unquoted
);
653 strbuf_release(&buf
);
657 parse_pathspec(pathspec
, magic_mask
, flags
, prefix
, parsed_file
.v
);
658 strvec_clear(&parsed_file
);
661 void copy_pathspec(struct pathspec
*dst
, const struct pathspec
*src
)
666 ALLOC_ARRAY(dst
->items
, dst
->nr
);
667 COPY_ARRAY(dst
->items
, src
->items
, dst
->nr
);
669 for (i
= 0; i
< dst
->nr
; i
++) {
670 struct pathspec_item
*d
= &dst
->items
[i
];
671 struct pathspec_item
*s
= &src
->items
[i
];
673 d
->match
= xstrdup(s
->match
);
674 d
->original
= xstrdup(s
->original
);
676 ALLOC_ARRAY(d
->attr_match
, d
->attr_match_nr
);
677 COPY_ARRAY(d
->attr_match
, s
->attr_match
, d
->attr_match_nr
);
678 for (j
= 0; j
< d
->attr_match_nr
; j
++) {
679 const char *value
= s
->attr_match
[j
].value
;
680 d
->attr_match
[j
].value
= xstrdup_or_null(value
);
683 d
->attr_check
= attr_check_dup(s
->attr_check
);
687 void clear_pathspec(struct pathspec
*pathspec
)
691 for (i
= 0; i
< pathspec
->nr
; i
++) {
692 free(pathspec
->items
[i
].match
);
693 free(pathspec
->items
[i
].original
);
695 for (j
= 0; j
< pathspec
->items
[i
].attr_match_nr
; j
++)
696 free(pathspec
->items
[i
].attr_match
[j
].value
);
697 free(pathspec
->items
[i
].attr_match
);
699 if (pathspec
->items
[i
].attr_check
)
700 attr_check_free(pathspec
->items
[i
].attr_check
);
703 FREE_AND_NULL(pathspec
->items
);
707 int match_pathspec_attrs(struct index_state
*istate
,
708 const char *name
, int namelen
,
709 const struct pathspec_item
*item
)
712 char *to_free
= NULL
;
715 name
= to_free
= xmemdupz(name
, namelen
);
717 git_check_attr(istate
, name
, item
->attr_check
);
721 for (i
= 0; i
< item
->attr_match_nr
; i
++) {
724 enum attr_match_mode match_mode
;
726 value
= item
->attr_check
->items
[i
].value
;
727 match_mode
= item
->attr_match
[i
].match_mode
;
729 if (ATTR_TRUE(value
))
730 matched
= (match_mode
== MATCH_SET
);
731 else if (ATTR_FALSE(value
))
732 matched
= (match_mode
== MATCH_UNSET
);
733 else if (ATTR_UNSET(value
))
734 matched
= (match_mode
== MATCH_UNSPECIFIED
);
736 matched
= (match_mode
== MATCH_VALUE
&&
737 !strcmp(item
->attr_match
[i
].value
, value
));