12 * Finds which of the given pathspecs match items in the index.
14 * For each pathspec, sets the corresponding entry in the seen[] array
15 * (which should be specs items long, i.e. the same size as pathspec)
16 * to the nature of the "closest" (i.e. most specific) match found for
17 * that pathspec in the index, if it was a closer type of match than
18 * the existing entry. As an optimization, matching is skipped
19 * altogether if seen[] already only contains non-zero entries.
21 * If seen[] has not already been written to, it may make sense
22 * to use find_pathspecs_matching_against_index() instead.
24 void add_pathspec_matches_against_index(const struct pathspec
*pathspec
,
25 struct index_state
*istate
,
27 enum ps_skip_worktree_action sw_action
)
29 int num_unmatched
= 0, i
;
32 * Since we are walking the index as if we were walking the directory,
33 * we have to mark the matched pathspec as seen; otherwise we will
34 * mistakenly think that the user gave a pathspec that did not match
37 for (i
= 0; i
< pathspec
->nr
; i
++)
42 for (i
= 0; i
< istate
->cache_nr
; i
++) {
43 const struct cache_entry
*ce
= istate
->cache
[i
];
44 if (sw_action
== PS_IGNORE_SKIP_WORKTREE
&&
45 (ce_skip_worktree(ce
) || !path_in_sparse_checkout(ce
->name
, istate
)))
47 ce_path_match(istate
, ce
, pathspec
, seen
);
52 * Finds which of the given pathspecs match items in the index.
54 * This is a one-shot wrapper around add_pathspec_matches_against_index()
55 * which allocates, populates, and returns a seen[] array indicating the
56 * nature of the "closest" (i.e. most specific) matches which each of the
57 * given pathspecs achieves against all items in the index.
59 char *find_pathspecs_matching_against_index(const struct pathspec
*pathspec
,
60 struct index_state
*istate
,
61 enum ps_skip_worktree_action sw_action
)
63 char *seen
= xcalloc(pathspec
->nr
, 1);
64 add_pathspec_matches_against_index(pathspec
, istate
, seen
, sw_action
);
68 char *find_pathspecs_matching_skip_worktree(const struct pathspec
*pathspec
)
70 struct index_state
*istate
= the_repository
->index
;
71 char *seen
= xcalloc(pathspec
->nr
, 1);
74 for (i
= 0; i
< istate
->cache_nr
; i
++) {
75 struct cache_entry
*ce
= istate
->cache
[i
];
76 if (ce_skip_worktree(ce
) || !path_in_sparse_checkout(ce
->name
, istate
))
77 ce_path_match(istate
, ce
, pathspec
, seen
);
86 * Possible future magic semantics include stuff like:
88 * { PATHSPEC_RECURSIVE, '*', "recursive" },
89 * { PATHSPEC_REGEXP, '\0', "regexp" },
93 static struct pathspec_magic
{
95 char mnemonic
; /* this cannot be ':'! */
97 } pathspec_magic
[] = {
98 { PATHSPEC_FROMTOP
, '/', "top" },
99 { PATHSPEC_LITERAL
, '\0', "literal" },
100 { PATHSPEC_GLOB
, '\0', "glob" },
101 { PATHSPEC_ICASE
, '\0', "icase" },
102 { PATHSPEC_EXCLUDE
, '!', "exclude" },
103 { PATHSPEC_ATTR
, '\0', "attr" },
106 static void prefix_magic(struct strbuf
*sb
, int prefixlen
, unsigned magic
)
109 strbuf_addstr(sb
, ":(");
110 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
111 if (magic
& pathspec_magic
[i
].bit
) {
112 if (sb
->buf
[sb
->len
- 1] != '(')
113 strbuf_addch(sb
, ',');
114 strbuf_addstr(sb
, pathspec_magic
[i
].name
);
116 strbuf_addf(sb
, ",prefix:%d)", prefixlen
);
119 static size_t strcspn_escaped(const char *s
, const char *stop
)
123 for (i
= s
; *i
; i
++) {
124 /* skip the escaped character */
125 if (i
[0] == '\\' && i
[1]) {
130 if (strchr(stop
, *i
))
136 static inline int invalid_value_char(const char ch
)
138 if (isalnum(ch
) || strchr(",-_", ch
))
143 static char *attr_value_unescape(const char *value
)
148 ret
= xmallocz(strlen(value
));
149 for (src
= value
, dst
= ret
; *src
; src
++, dst
++) {
152 die(_("Escape character '\\' not allowed as "
153 "last character in attr value"));
156 if (invalid_value_char(*src
))
157 die("cannot use '%c' for value matching", *src
);
164 static void parse_pathspec_attr_match(struct pathspec_item
*item
, const char *value
)
166 struct string_list_item
*si
;
167 struct string_list list
= STRING_LIST_INIT_DUP
;
169 if (item
->attr_check
|| item
->attr_match
)
170 die(_("Only one 'attr:' specification is allowed."));
172 if (!value
|| !*value
)
173 die(_("attr spec must not be empty"));
175 string_list_split(&list
, value
, ' ', -1);
176 string_list_remove_empty_items(&list
, 0);
178 item
->attr_check
= attr_check_alloc();
179 CALLOC_ARRAY(item
->attr_match
, list
.nr
);
181 for_each_string_list_item(si
, &list
) {
184 const struct git_attr
*a
;
186 int j
= item
->attr_match_nr
++;
187 const char *attr
= si
->string
;
188 struct attr_match
*am
= &item
->attr_match
[j
];
192 am
->match_mode
= MATCH_UNSPECIFIED
;
194 attr_len
= strlen(attr
);
197 am
->match_mode
= MATCH_UNSET
;
199 attr_len
= strlen(attr
);
202 attr_len
= strcspn(attr
, "=");
203 if (attr
[attr_len
] != '=')
204 am
->match_mode
= MATCH_SET
;
206 const char *v
= &attr
[attr_len
+ 1];
207 am
->match_mode
= MATCH_VALUE
;
208 am
->value
= attr_value_unescape(v
);
213 attr_name
= xmemdupz(attr
, attr_len
);
214 a
= git_attr(attr_name
);
216 die(_("invalid attribute name %s"), attr_name
);
218 attr_check_append(item
->attr_check
, a
);
223 if (item
->attr_check
->nr
!= item
->attr_match_nr
)
224 BUG("should have same number of entries");
226 string_list_clear(&list
, 0);
229 static inline int get_literal_global(void)
231 static int literal
= -1;
234 literal
= git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, 0);
239 static inline int get_glob_global(void)
241 static int glob
= -1;
244 glob
= git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT
, 0);
249 static inline int get_noglob_global(void)
251 static int noglob
= -1;
254 noglob
= git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT
, 0);
259 static inline int get_icase_global(void)
261 static int icase
= -1;
264 icase
= git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT
, 0);
269 static int get_global_magic(int element_magic
)
271 int global_magic
= 0;
273 if (get_literal_global())
274 global_magic
|= PATHSPEC_LITERAL
;
276 /* --glob-pathspec is overridden by :(literal) */
277 if (get_glob_global() && !(element_magic
& PATHSPEC_LITERAL
))
278 global_magic
|= PATHSPEC_GLOB
;
280 if (get_glob_global() && get_noglob_global())
281 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
283 if (get_icase_global())
284 global_magic
|= PATHSPEC_ICASE
;
286 if ((global_magic
& PATHSPEC_LITERAL
) &&
287 (global_magic
& ~PATHSPEC_LITERAL
))
288 die(_("global 'literal' pathspec setting is incompatible "
289 "with all other global pathspec settings"));
291 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
292 if (get_noglob_global() && !(element_magic
& PATHSPEC_GLOB
))
293 global_magic
|= PATHSPEC_LITERAL
;
299 * Parse the pathspec element looking for long magic
301 * saves all magic in 'magic'
302 * if prefix magic is used, save the prefix length in 'prefix_len'
303 * returns the position in 'elem' after all magic has been parsed
305 static const char *parse_long_magic(unsigned *magic
, int *prefix_len
,
306 struct pathspec_item
*item
,
312 for (pos
= elem
+ 2; *pos
&& *pos
!= ')'; pos
= nextat
) {
313 size_t len
= strcspn_escaped(pos
, ",)");
317 nextat
= pos
+ len
+ 1; /* handle ',' */
319 nextat
= pos
+ len
; /* handle ')' and '\0' */
324 if (starts_with(pos
, "prefix:")) {
326 *prefix_len
= strtol(pos
+ 7, &endptr
, 10);
327 if (endptr
- pos
!= len
)
328 die(_("invalid parameter for pathspec magic 'prefix'"));
332 if (starts_with(pos
, "attr:")) {
333 char *attr_body
= xmemdupz(pos
+ 5, len
- 5);
334 parse_pathspec_attr_match(item
, attr_body
);
335 *magic
|= PATHSPEC_ATTR
;
340 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
341 if (strlen(pathspec_magic
[i
].name
) == len
&&
342 !strncmp(pathspec_magic
[i
].name
, pos
, len
)) {
343 *magic
|= pathspec_magic
[i
].bit
;
348 if (ARRAY_SIZE(pathspec_magic
) <= i
)
349 die(_("Invalid pathspec magic '%.*s' in '%s'"),
350 (int) len
, pos
, elem
);
354 die(_("Missing ')' at the end of pathspec magic in '%s'"),
362 * Parse the pathspec element looking for short magic
364 * saves all magic in 'magic'
365 * returns the position in 'elem' after all magic has been parsed
367 static const char *parse_short_magic(unsigned *magic
, const char *elem
)
371 for (pos
= elem
+ 1; *pos
&& *pos
!= ':'; pos
++) {
375 /* Special case alias for '!' */
377 *magic
|= PATHSPEC_EXCLUDE
;
381 if (!is_pathspec_magic(ch
))
384 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
385 if (pathspec_magic
[i
].mnemonic
== ch
) {
386 *magic
|= pathspec_magic
[i
].bit
;
391 if (ARRAY_SIZE(pathspec_magic
) <= i
)
392 die(_("Unimplemented pathspec magic '%c' in '%s'"),
402 static const char *parse_element_magic(unsigned *magic
, int *prefix_len
,
403 struct pathspec_item
*item
,
406 if (elem
[0] != ':' || get_literal_global())
407 return elem
; /* nothing to do */
408 else if (elem
[1] == '(')
410 return parse_long_magic(magic
, prefix_len
, item
, elem
);
413 return parse_short_magic(magic
, elem
);
417 * Perform the initialization of a pathspec_item based on a pathspec element.
419 static void init_pathspec_item(struct pathspec_item
*item
, unsigned flags
,
420 const char *prefix
, int prefixlen
,
423 unsigned magic
= 0, element_magic
= 0;
424 const char *copyfrom
= elt
;
426 int pathspec_prefix
= -1;
428 item
->attr_check
= NULL
;
429 item
->attr_match
= NULL
;
430 item
->attr_match_nr
= 0;
432 /* PATHSPEC_LITERAL_PATH ignores magic */
433 if (flags
& PATHSPEC_LITERAL_PATH
) {
434 magic
= PATHSPEC_LITERAL
;
436 copyfrom
= parse_element_magic(&element_magic
,
440 magic
|= element_magic
;
441 magic
|= get_global_magic(element_magic
);
446 if (pathspec_prefix
>= 0 &&
447 (prefixlen
|| (prefix
&& *prefix
)))
448 BUG("'prefix' magic is supposed to be used at worktree's root");
450 if ((magic
& PATHSPEC_LITERAL
) && (magic
& PATHSPEC_GLOB
))
451 die(_("%s: 'literal' and 'glob' are incompatible"), elt
);
453 /* Create match string which will be used for pathspec matching */
454 if (pathspec_prefix
>= 0) {
455 match
= xstrdup(copyfrom
);
456 prefixlen
= pathspec_prefix
;
457 } else if (magic
& PATHSPEC_FROMTOP
) {
458 match
= xstrdup(copyfrom
);
461 match
= prefix_path_gently(prefix
, prefixlen
,
462 &prefixlen
, copyfrom
);
464 const char *hint_path
= get_git_work_tree();
466 hint_path
= get_git_dir();
467 die(_("%s: '%s' is outside repository at '%s'"), elt
,
468 copyfrom
, absolute_path(hint_path
));
473 item
->len
= strlen(item
->match
);
474 item
->prefix
= prefixlen
;
477 * Prefix the pathspec (keep all magic) and assign to
478 * original. Useful for passing to another command.
480 if ((flags
& PATHSPEC_PREFIX_ORIGIN
) &&
481 !get_literal_global()) {
482 struct strbuf sb
= STRBUF_INIT
;
484 /* Preserve the actual prefix length of each pattern */
485 prefix_magic(&sb
, prefixlen
, element_magic
);
487 strbuf_addstr(&sb
, match
);
488 item
->original
= strbuf_detach(&sb
, NULL
);
490 item
->original
= xstrdup(elt
);
493 if (magic
& PATHSPEC_LITERAL
) {
494 item
->nowildcard_len
= item
->len
;
496 item
->nowildcard_len
= simple_length(item
->match
);
497 if (item
->nowildcard_len
< prefixlen
)
498 item
->nowildcard_len
= prefixlen
;
502 if (magic
& PATHSPEC_GLOB
) {
504 * FIXME: should we enable ONESTAR in _GLOB for
505 * pattern "* * / * . c"?
508 if (item
->nowildcard_len
< item
->len
&&
509 item
->match
[item
->nowildcard_len
] == '*' &&
510 no_wildcard(item
->match
+ item
->nowildcard_len
+ 1))
511 item
->flags
|= PATHSPEC_ONESTAR
;
514 /* sanity checks, pathspec matchers assume these are sane */
515 if (item
->nowildcard_len
> item
->len
||
516 item
->prefix
> item
->len
) {
517 BUG("error initializing pathspec_item");
521 static int pathspec_item_cmp(const void *a_
, const void *b_
)
523 struct pathspec_item
*a
, *b
;
525 a
= (struct pathspec_item
*)a_
;
526 b
= (struct pathspec_item
*)b_
;
527 return strcmp(a
->match
, b
->match
);
530 static void NORETURN
unsupported_magic(const char *pattern
,
533 struct strbuf sb
= STRBUF_INIT
;
535 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
536 const struct pathspec_magic
*m
= pathspec_magic
+ i
;
537 if (!(magic
& m
->bit
))
540 strbuf_addstr(&sb
, ", ");
543 strbuf_addf(&sb
, _("'%s' (mnemonic: '%c')"),
544 m
->name
, m
->mnemonic
);
546 strbuf_addf(&sb
, "'%s'", m
->name
);
549 * We may want to substitute "this command" with a command
550 * name. E.g. when "git add -p" or "git add -i" dies when running
553 die(_("%s: pathspec magic not supported by this command: %s"),
557 void parse_pathspec(struct pathspec
*pathspec
,
558 unsigned magic_mask
, unsigned flags
,
559 const char *prefix
, const char **argv
)
561 struct pathspec_item
*item
;
562 const char *entry
= argv
? *argv
: NULL
;
563 int i
, n
, prefixlen
, nr_exclude
= 0;
565 memset(pathspec
, 0, sizeof(*pathspec
));
567 if (flags
& PATHSPEC_MAXDEPTH_VALID
)
568 pathspec
->magic
|= PATHSPEC_MAXDEPTH
;
570 /* No arguments, no prefix -> no pathspec */
571 if (!entry
&& !prefix
)
574 if ((flags
& PATHSPEC_PREFER_CWD
) &&
575 (flags
& PATHSPEC_PREFER_FULL
))
576 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
578 /* No arguments with prefix -> prefix pathspec */
580 if (flags
& PATHSPEC_PREFER_FULL
)
583 if (!(flags
& PATHSPEC_PREFER_CWD
))
584 BUG("PATHSPEC_PREFER_CWD requires arguments");
586 pathspec
->items
= CALLOC_ARRAY(item
, 1);
587 item
->match
= xstrdup(prefix
);
588 item
->original
= xstrdup(prefix
);
589 item
->nowildcard_len
= item
->len
= strlen(prefix
);
590 item
->prefix
= item
->len
;
597 if (*argv
[n
] == '\0')
598 die("empty string is not a valid pathspec. "
599 "please use . instead if you meant to match all paths");
604 ALLOC_ARRAY(pathspec
->items
, n
+ 1);
605 item
= pathspec
->items
;
606 prefixlen
= prefix
? strlen(prefix
) : 0;
608 for (i
= 0; i
< n
; i
++) {
611 init_pathspec_item(item
+ i
, flags
, prefix
, prefixlen
, entry
);
613 if (item
[i
].magic
& PATHSPEC_EXCLUDE
)
615 if (item
[i
].magic
& magic_mask
)
616 unsupported_magic(entry
, item
[i
].magic
& magic_mask
);
618 if ((flags
& PATHSPEC_SYMLINK_LEADING_PATH
) &&
619 has_symlink_leading_path(item
[i
].match
, item
[i
].len
)) {
620 die(_("pathspec '%s' is beyond a symbolic link"), entry
);
623 if (item
[i
].nowildcard_len
< item
[i
].len
)
624 pathspec
->has_wildcard
= 1;
625 pathspec
->magic
|= item
[i
].magic
;
629 * If everything is an exclude pattern, add one positive pattern
630 * that matches everything. We allocated an extra one for this.
632 if (nr_exclude
== n
) {
633 int plen
= (!(flags
& PATHSPEC_PREFER_CWD
)) ? 0 : prefixlen
;
634 init_pathspec_item(item
+ n
, 0, prefix
, plen
, ".");
638 if (pathspec
->magic
& PATHSPEC_MAXDEPTH
) {
639 if (flags
& PATHSPEC_KEEP_ORDER
)
640 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
641 QSORT(pathspec
->items
, pathspec
->nr
, pathspec_item_cmp
);
645 void parse_pathspec_file(struct pathspec
*pathspec
, unsigned magic_mask
,
646 unsigned flags
, const char *prefix
,
647 const char *file
, int nul_term_line
)
649 struct strvec parsed_file
= STRVEC_INIT
;
650 strbuf_getline_fn getline_fn
= nul_term_line
? strbuf_getline_nul
:
652 struct strbuf buf
= STRBUF_INIT
;
653 struct strbuf unquoted
= STRBUF_INIT
;
656 if (!strcmp(file
, "-"))
659 in
= xfopen(file
, "r");
661 while (getline_fn(&buf
, in
) != EOF
) {
662 if (!nul_term_line
&& buf
.buf
[0] == '"') {
663 strbuf_reset(&unquoted
);
664 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
665 die(_("line is badly quoted: %s"), buf
.buf
);
666 strbuf_swap(&buf
, &unquoted
);
668 strvec_push(&parsed_file
, buf
.buf
);
672 strbuf_release(&unquoted
);
673 strbuf_release(&buf
);
677 parse_pathspec(pathspec
, magic_mask
, flags
, prefix
, parsed_file
.v
);
678 strvec_clear(&parsed_file
);
681 void copy_pathspec(struct pathspec
*dst
, const struct pathspec
*src
)
686 DUP_ARRAY(dst
->items
, src
->items
, dst
->nr
);
688 for (i
= 0; i
< dst
->nr
; i
++) {
689 struct pathspec_item
*d
= &dst
->items
[i
];
690 struct pathspec_item
*s
= &src
->items
[i
];
692 d
->match
= xstrdup(s
->match
);
693 d
->original
= xstrdup(s
->original
);
695 DUP_ARRAY(d
->attr_match
, s
->attr_match
, d
->attr_match_nr
);
696 for (j
= 0; j
< d
->attr_match_nr
; j
++) {
697 const char *value
= s
->attr_match
[j
].value
;
698 d
->attr_match
[j
].value
= xstrdup_or_null(value
);
701 d
->attr_check
= attr_check_dup(s
->attr_check
);
705 void clear_pathspec(struct pathspec
*pathspec
)
709 for (i
= 0; i
< pathspec
->nr
; i
++) {
710 free(pathspec
->items
[i
].match
);
711 free(pathspec
->items
[i
].original
);
713 for (j
= 0; j
< pathspec
->items
[i
].attr_match_nr
; j
++)
714 free(pathspec
->items
[i
].attr_match
[j
].value
);
715 free(pathspec
->items
[i
].attr_match
);
717 if (pathspec
->items
[i
].attr_check
)
718 attr_check_free(pathspec
->items
[i
].attr_check
);
721 FREE_AND_NULL(pathspec
->items
);
725 int match_pathspec_attrs(struct index_state
*istate
,
726 const char *name
, int namelen
,
727 const struct pathspec_item
*item
)
730 char *to_free
= NULL
;
733 name
= to_free
= xmemdupz(name
, namelen
);
735 git_check_attr(istate
, NULL
, name
, item
->attr_check
);
739 for (i
= 0; i
< item
->attr_match_nr
; i
++) {
742 enum attr_match_mode match_mode
;
744 value
= item
->attr_check
->items
[i
].value
;
745 match_mode
= item
->attr_match
[i
].match_mode
;
747 if (ATTR_TRUE(value
))
748 matched
= (match_mode
== MATCH_SET
);
749 else if (ATTR_FALSE(value
))
750 matched
= (match_mode
== MATCH_UNSET
);
751 else if (ATTR_UNSET(value
))
752 matched
= (match_mode
== MATCH_UNSPECIFIED
);
754 matched
= (match_mode
== MATCH_VALUE
&&
755 !strcmp(item
->attr_match
[i
].value
, value
));
763 int pathspec_needs_expanded_index(struct index_state
*istate
,
764 const struct pathspec
*pathspec
)
768 char *skip_worktree_seen
= NULL
;
771 * If index is not sparse, no index expansion is needed.
773 if (!istate
->sparse_index
)
777 * When using a magic pathspec, assume for the sake of simplicity that
778 * the index needs to be expanded to match all matchable files.
783 for (i
= 0; i
< pathspec
->nr
; i
++) {
784 struct pathspec_item item
= pathspec
->items
[i
];
787 * If the pathspec item has a wildcard, the index should be expanded
788 * if the pathspec has the possibility of matching a subset of entries inside
789 * of a sparse directory (but not the entire directory).
791 * If the pathspec item is a literal path, the index only needs to be expanded
792 * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
793 * expand for in-cone files) and b) it doesn't match any sparse directories
794 * (since we can reset whole sparse directories without expanding them).
796 if (item
.nowildcard_len
< item
.len
) {
798 * Special case: if the pattern is a path inside the cone
799 * followed by only wildcards, the pattern cannot match
800 * partial sparse directories, so we know we don't need to
804 * - in-cone/foo***: doesn't need expanded index
805 * - not-in-cone/bar*: may need expanded index
806 * - **.c: may need expanded index
808 if (strspn(item
.original
+ item
.nowildcard_len
, "*") == item
.len
- item
.nowildcard_len
&&
809 path_in_cone_mode_sparse_checkout(item
.original
, istate
))
812 for (pos
= 0; pos
< istate
->cache_nr
; pos
++) {
813 struct cache_entry
*ce
= istate
->cache
[pos
];
815 if (!S_ISSPARSEDIR(ce
->ce_mode
))
819 * If the pre-wildcard length is longer than the sparse
820 * directory name and the sparse directory is the first
821 * component of the pathspec, need to expand the index.
823 if (item
.nowildcard_len
> ce_namelen(ce
) &&
824 !strncmp(item
.original
, ce
->name
, ce_namelen(ce
))) {
830 * If the pre-wildcard length is shorter than the sparse
831 * directory and the pathspec does not match the whole
832 * directory, need to expand the index.
834 if (!strncmp(item
.original
, ce
->name
, item
.nowildcard_len
) &&
835 wildmatch(item
.original
, ce
->name
, 0)) {
840 } else if (!path_in_cone_mode_sparse_checkout(item
.original
, istate
) &&
841 !matches_skip_worktree(pathspec
, i
, &skip_worktree_seen
))
848 free(skip_worktree_seen
);