1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
9 * Finds which of the given pathspecs match items in the index.
11 * For each pathspec, sets the corresponding entry in the seen[] array
12 * (which should be specs items long, i.e. the same size as pathspec)
13 * to the nature of the "closest" (i.e. most specific) match found for
14 * that pathspec in the index, if it was a closer type of match than
15 * the existing entry. As an optimization, matching is skipped
16 * altogether if seen[] already only contains non-zero entries.
18 * If seen[] has not already been written to, it may make sense
19 * to use find_pathspecs_matching_against_index() instead.
21 void add_pathspec_matches_against_index(const struct pathspec
*pathspec
,
22 const struct index_state
*istate
,
25 int num_unmatched
= 0, i
;
28 * Since we are walking the index as if we were walking the directory,
29 * we have to mark the matched pathspec as seen; otherwise we will
30 * mistakenly think that the user gave a pathspec that did not match
33 for (i
= 0; i
< pathspec
->nr
; i
++)
38 for (i
= 0; i
< istate
->cache_nr
; i
++) {
39 const struct cache_entry
*ce
= istate
->cache
[i
];
40 ce_path_match(ce
, pathspec
, seen
);
45 * Finds which of the given pathspecs match items in the index.
47 * This is a one-shot wrapper around add_pathspec_matches_against_index()
48 * which allocates, populates, and returns a seen[] array indicating the
49 * nature of the "closest" (i.e. most specific) matches which each of the
50 * given pathspecs achieves against all items in the index.
52 char *find_pathspecs_matching_against_index(const struct pathspec
*pathspec
,
53 const struct index_state
*istate
)
55 char *seen
= xcalloc(pathspec
->nr
, 1);
56 add_pathspec_matches_against_index(pathspec
, istate
, seen
);
63 * Possible future magic semantics include stuff like:
65 * { PATHSPEC_RECURSIVE, '*', "recursive" },
66 * { PATHSPEC_REGEXP, '\0', "regexp" },
70 static struct pathspec_magic
{
72 char mnemonic
; /* this cannot be ':'! */
74 } pathspec_magic
[] = {
75 { PATHSPEC_FROMTOP
, '/', "top" },
76 { PATHSPEC_LITERAL
, '\0', "literal" },
77 { PATHSPEC_GLOB
, '\0', "glob" },
78 { PATHSPEC_ICASE
, '\0', "icase" },
79 { PATHSPEC_EXCLUDE
, '!', "exclude" },
80 { PATHSPEC_ATTR
, '\0', "attr" },
83 static void prefix_magic(struct strbuf
*sb
, int prefixlen
, unsigned magic
)
86 strbuf_addstr(sb
, ":(");
87 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
88 if (magic
& pathspec_magic
[i
].bit
) {
89 if (sb
->buf
[sb
->len
- 1] != '(')
90 strbuf_addch(sb
, ',');
91 strbuf_addstr(sb
, pathspec_magic
[i
].name
);
93 strbuf_addf(sb
, ",prefix:%d)", prefixlen
);
96 static size_t strcspn_escaped(const char *s
, const char *stop
)
100 for (i
= s
; *i
; i
++) {
101 /* skip the escaped character */
102 if (i
[0] == '\\' && i
[1]) {
107 if (strchr(stop
, *i
))
113 static inline int invalid_value_char(const char ch
)
115 if (isalnum(ch
) || strchr(",-_", ch
))
120 static char *attr_value_unescape(const char *value
)
125 ret
= xmallocz(strlen(value
));
126 for (src
= value
, dst
= ret
; *src
; src
++, dst
++) {
129 die(_("Escape character '\\' not allowed as "
130 "last character in attr value"));
133 if (invalid_value_char(*src
))
134 die("cannot use '%c' for value matching", *src
);
141 static void parse_pathspec_attr_match(struct pathspec_item
*item
, const char *value
)
143 struct string_list_item
*si
;
144 struct string_list list
= STRING_LIST_INIT_DUP
;
146 if (item
->attr_check
|| item
->attr_match
)
147 die(_("Only one 'attr:' specification is allowed."));
149 if (!value
|| !*value
)
150 die(_("attr spec must not be empty"));
152 string_list_split(&list
, value
, ' ', -1);
153 string_list_remove_empty_items(&list
, 0);
155 item
->attr_check
= attr_check_alloc();
156 item
->attr_match
= xcalloc(list
.nr
, sizeof(struct attr_match
));
158 for_each_string_list_item(si
, &list
) {
161 const struct git_attr
*a
;
163 int j
= item
->attr_match_nr
++;
164 const char *attr
= si
->string
;
165 struct attr_match
*am
= &item
->attr_match
[j
];
169 am
->match_mode
= MATCH_UNSPECIFIED
;
171 attr_len
= strlen(attr
);
174 am
->match_mode
= MATCH_UNSET
;
176 attr_len
= strlen(attr
);
179 attr_len
= strcspn(attr
, "=");
180 if (attr
[attr_len
] != '=')
181 am
->match_mode
= MATCH_SET
;
183 const char *v
= &attr
[attr_len
+ 1];
184 am
->match_mode
= MATCH_VALUE
;
185 am
->value
= attr_value_unescape(v
);
190 attr_name
= xmemdupz(attr
, attr_len
);
191 a
= git_attr(attr_name
);
193 die(_("invalid attribute name %s"), attr_name
);
195 attr_check_append(item
->attr_check
, a
);
200 if (item
->attr_check
->nr
!= item
->attr_match_nr
)
201 die("BUG: should have same number of entries");
203 string_list_clear(&list
, 0);
206 static inline int get_literal_global(void)
208 static int literal
= -1;
211 literal
= git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, 0);
216 static inline int get_glob_global(void)
218 static int glob
= -1;
221 glob
= git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT
, 0);
226 static inline int get_noglob_global(void)
228 static int noglob
= -1;
231 noglob
= git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT
, 0);
236 static inline int get_icase_global(void)
238 static int icase
= -1;
241 icase
= git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT
, 0);
246 static int get_global_magic(int element_magic
)
248 int global_magic
= 0;
250 if (get_literal_global())
251 global_magic
|= PATHSPEC_LITERAL
;
253 /* --glob-pathspec is overridden by :(literal) */
254 if (get_glob_global() && !(element_magic
& PATHSPEC_LITERAL
))
255 global_magic
|= PATHSPEC_GLOB
;
257 if (get_glob_global() && get_noglob_global())
258 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
260 if (get_icase_global())
261 global_magic
|= PATHSPEC_ICASE
;
263 if ((global_magic
& PATHSPEC_LITERAL
) &&
264 (global_magic
& ~PATHSPEC_LITERAL
))
265 die(_("global 'literal' pathspec setting is incompatible "
266 "with all other global pathspec settings"));
268 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
269 if (get_noglob_global() && !(element_magic
& PATHSPEC_GLOB
))
270 global_magic
|= PATHSPEC_LITERAL
;
276 * Parse the pathspec element looking for long magic
278 * saves all magic in 'magic'
279 * if prefix magic is used, save the prefix length in 'prefix_len'
280 * returns the position in 'elem' after all magic has been parsed
282 static const char *parse_long_magic(unsigned *magic
, int *prefix_len
,
283 struct pathspec_item
*item
,
289 for (pos
= elem
+ 2; *pos
&& *pos
!= ')'; pos
= nextat
) {
290 size_t len
= strcspn_escaped(pos
, ",)");
294 nextat
= pos
+ len
+ 1; /* handle ',' */
296 nextat
= pos
+ len
; /* handle ')' and '\0' */
301 if (starts_with(pos
, "prefix:")) {
303 *prefix_len
= strtol(pos
+ 7, &endptr
, 10);
304 if (endptr
- pos
!= len
)
305 die(_("invalid parameter for pathspec magic 'prefix'"));
309 if (starts_with(pos
, "attr:")) {
310 char *attr_body
= xmemdupz(pos
+ 5, len
- 5);
311 parse_pathspec_attr_match(item
, attr_body
);
312 *magic
|= PATHSPEC_ATTR
;
317 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
318 if (strlen(pathspec_magic
[i
].name
) == len
&&
319 !strncmp(pathspec_magic
[i
].name
, pos
, len
)) {
320 *magic
|= pathspec_magic
[i
].bit
;
325 if (ARRAY_SIZE(pathspec_magic
) <= i
)
326 die(_("Invalid pathspec magic '%.*s' in '%s'"),
327 (int) len
, pos
, elem
);
331 die(_("Missing ')' at the end of pathspec magic in '%s'"),
339 * Parse the pathspec element looking for short magic
341 * saves all magic in 'magic'
342 * returns the position in 'elem' after all magic has been parsed
344 static const char *parse_short_magic(unsigned *magic
, const char *elem
)
348 for (pos
= elem
+ 1; *pos
&& *pos
!= ':'; pos
++) {
352 /* Special case alias for '!' */
354 *magic
|= PATHSPEC_EXCLUDE
;
358 if (!is_pathspec_magic(ch
))
361 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
362 if (pathspec_magic
[i
].mnemonic
== ch
) {
363 *magic
|= pathspec_magic
[i
].bit
;
368 if (ARRAY_SIZE(pathspec_magic
) <= i
)
369 die(_("Unimplemented pathspec magic '%c' in '%s'"),
379 static const char *parse_element_magic(unsigned *magic
, int *prefix_len
,
380 struct pathspec_item
*item
,
383 if (elem
[0] != ':' || get_literal_global())
384 return elem
; /* nothing to do */
385 else if (elem
[1] == '(')
387 return parse_long_magic(magic
, prefix_len
, item
, elem
);
390 return parse_short_magic(magic
, elem
);
394 * Perform the initialization of a pathspec_item based on a pathspec element.
396 static void init_pathspec_item(struct pathspec_item
*item
, unsigned flags
,
397 const char *prefix
, int prefixlen
,
400 unsigned magic
= 0, element_magic
= 0;
401 const char *copyfrom
= elt
;
403 int pathspec_prefix
= -1;
405 item
->attr_check
= NULL
;
406 item
->attr_match
= NULL
;
407 item
->attr_match_nr
= 0;
409 /* PATHSPEC_LITERAL_PATH ignores magic */
410 if (flags
& PATHSPEC_LITERAL_PATH
) {
411 magic
= PATHSPEC_LITERAL
;
413 copyfrom
= parse_element_magic(&element_magic
,
417 magic
|= element_magic
;
418 magic
|= get_global_magic(element_magic
);
423 if (pathspec_prefix
>= 0 &&
424 (prefixlen
|| (prefix
&& *prefix
)))
425 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
427 if ((magic
& PATHSPEC_LITERAL
) && (magic
& PATHSPEC_GLOB
))
428 die(_("%s: 'literal' and 'glob' are incompatible"), elt
);
430 /* Create match string which will be used for pathspec matching */
431 if (pathspec_prefix
>= 0) {
432 match
= xstrdup(copyfrom
);
433 prefixlen
= pathspec_prefix
;
434 } else if (magic
& PATHSPEC_FROMTOP
) {
435 match
= xstrdup(copyfrom
);
438 match
= prefix_path_gently(prefix
, prefixlen
,
439 &prefixlen
, copyfrom
);
441 die(_("%s: '%s' is outside repository"), elt
, copyfrom
);
445 item
->len
= strlen(item
->match
);
446 item
->prefix
= prefixlen
;
449 * Prefix the pathspec (keep all magic) and assign to
450 * original. Useful for passing to another command.
452 if ((flags
& PATHSPEC_PREFIX_ORIGIN
) &&
453 !get_literal_global()) {
454 struct strbuf sb
= STRBUF_INIT
;
456 /* Preserve the actual prefix length of each pattern */
457 prefix_magic(&sb
, prefixlen
, element_magic
);
459 strbuf_addstr(&sb
, match
);
460 item
->original
= strbuf_detach(&sb
, NULL
);
462 item
->original
= xstrdup(elt
);
465 if (magic
& PATHSPEC_LITERAL
) {
466 item
->nowildcard_len
= item
->len
;
468 item
->nowildcard_len
= simple_length(item
->match
);
469 if (item
->nowildcard_len
< prefixlen
)
470 item
->nowildcard_len
= prefixlen
;
474 if (magic
& PATHSPEC_GLOB
) {
476 * FIXME: should we enable ONESTAR in _GLOB for
477 * pattern "* * / * . c"?
480 if (item
->nowildcard_len
< item
->len
&&
481 item
->match
[item
->nowildcard_len
] == '*' &&
482 no_wildcard(item
->match
+ item
->nowildcard_len
+ 1))
483 item
->flags
|= PATHSPEC_ONESTAR
;
486 /* sanity checks, pathspec matchers assume these are sane */
487 if (item
->nowildcard_len
> item
->len
||
488 item
->prefix
> item
->len
) {
489 die ("BUG: error initializing pathspec_item");
493 static int pathspec_item_cmp(const void *a_
, const void *b_
)
495 struct pathspec_item
*a
, *b
;
497 a
= (struct pathspec_item
*)a_
;
498 b
= (struct pathspec_item
*)b_
;
499 return strcmp(a
->match
, b
->match
);
502 static void NORETURN
unsupported_magic(const char *pattern
,
505 struct strbuf sb
= STRBUF_INIT
;
507 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
508 const struct pathspec_magic
*m
= pathspec_magic
+ i
;
509 if (!(magic
& m
->bit
))
512 strbuf_addstr(&sb
, ", ");
515 strbuf_addf(&sb
, _("'%s' (mnemonic: '%c')"),
516 m
->name
, m
->mnemonic
);
518 strbuf_addf(&sb
, "'%s'", m
->name
);
521 * We may want to substitute "this command" with a command
522 * name. E.g. when add--interactive dies when running
525 die(_("%s: pathspec magic not supported by this command: %s"),
529 void parse_pathspec(struct pathspec
*pathspec
,
530 unsigned magic_mask
, unsigned flags
,
531 const char *prefix
, const char **argv
)
533 struct pathspec_item
*item
;
534 const char *entry
= argv
? *argv
: NULL
;
535 int i
, n
, prefixlen
, warn_empty_string
, nr_exclude
= 0;
537 memset(pathspec
, 0, sizeof(*pathspec
));
539 if (flags
& PATHSPEC_MAXDEPTH_VALID
)
540 pathspec
->magic
|= PATHSPEC_MAXDEPTH
;
542 /* No arguments, no prefix -> no pathspec */
543 if (!entry
&& !prefix
)
546 if ((flags
& PATHSPEC_PREFER_CWD
) &&
547 (flags
& PATHSPEC_PREFER_FULL
))
548 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
550 /* No arguments with prefix -> prefix pathspec */
552 if (flags
& PATHSPEC_PREFER_FULL
)
555 if (!(flags
& PATHSPEC_PREFER_CWD
))
556 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
558 pathspec
->items
= item
= xcalloc(1, sizeof(*item
));
559 item
->match
= xstrdup(prefix
);
560 item
->original
= xstrdup(prefix
);
561 item
->nowildcard_len
= item
->len
= strlen(prefix
);
562 item
->prefix
= item
->len
;
568 warn_empty_string
= 1;
570 if (*argv
[n
] == '\0' && warn_empty_string
) {
571 warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
572 "please use . instead if you meant to match all paths"));
573 warn_empty_string
= 0;
579 ALLOC_ARRAY(pathspec
->items
, n
+ 1);
580 item
= pathspec
->items
;
581 prefixlen
= prefix
? strlen(prefix
) : 0;
583 for (i
= 0; i
< n
; i
++) {
586 init_pathspec_item(item
+ i
, flags
, prefix
, prefixlen
, entry
);
588 if (item
[i
].magic
& PATHSPEC_EXCLUDE
)
590 if (item
[i
].magic
& magic_mask
)
591 unsupported_magic(entry
, item
[i
].magic
& magic_mask
);
593 if ((flags
& PATHSPEC_SYMLINK_LEADING_PATH
) &&
594 has_symlink_leading_path(item
[i
].match
, item
[i
].len
)) {
595 die(_("pathspec '%s' is beyond a symbolic link"), entry
);
598 if (item
[i
].nowildcard_len
< item
[i
].len
)
599 pathspec
->has_wildcard
= 1;
600 pathspec
->magic
|= item
[i
].magic
;
604 * If everything is an exclude pattern, add one positive pattern
605 * that matches everything. We allocated an extra one for this.
607 if (nr_exclude
== n
) {
608 int plen
= (!(flags
& PATHSPEC_PREFER_CWD
)) ? 0 : prefixlen
;
609 init_pathspec_item(item
+ n
, 0, prefix
, plen
, "");
613 if (pathspec
->magic
& PATHSPEC_MAXDEPTH
) {
614 if (flags
& PATHSPEC_KEEP_ORDER
)
615 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
616 QSORT(pathspec
->items
, pathspec
->nr
, pathspec_item_cmp
);
620 void copy_pathspec(struct pathspec
*dst
, const struct pathspec
*src
)
625 ALLOC_ARRAY(dst
->items
, dst
->nr
);
626 COPY_ARRAY(dst
->items
, src
->items
, dst
->nr
);
628 for (i
= 0; i
< dst
->nr
; i
++) {
629 struct pathspec_item
*d
= &dst
->items
[i
];
630 struct pathspec_item
*s
= &src
->items
[i
];
632 d
->match
= xstrdup(s
->match
);
633 d
->original
= xstrdup(s
->original
);
635 ALLOC_ARRAY(d
->attr_match
, d
->attr_match_nr
);
636 COPY_ARRAY(d
->attr_match
, s
->attr_match
, d
->attr_match_nr
);
637 for (j
= 0; j
< d
->attr_match_nr
; j
++) {
638 const char *value
= s
->attr_match
[j
].value
;
639 d
->attr_match
[j
].value
= xstrdup_or_null(value
);
642 d
->attr_check
= attr_check_dup(s
->attr_check
);
646 void clear_pathspec(struct pathspec
*pathspec
)
650 for (i
= 0; i
< pathspec
->nr
; i
++) {
651 free(pathspec
->items
[i
].match
);
652 free(pathspec
->items
[i
].original
);
654 for (j
= 0; j
< pathspec
->items
[i
].attr_match_nr
; j
++)
655 free(pathspec
->items
[i
].attr_match
[j
].value
);
656 free(pathspec
->items
[i
].attr_match
);
658 if (pathspec
->items
[i
].attr_check
)
659 attr_check_free(pathspec
->items
[i
].attr_check
);
662 FREE_AND_NULL(pathspec
->items
);