6 * Finds which of the given pathspecs match items in the index.
8 * For each pathspec, sets the corresponding entry in the seen[] array
9 * (which should be specs items long, i.e. the same size as pathspec)
10 * to the nature of the "closest" (i.e. most specific) match found for
11 * that pathspec in the index, if it was a closer type of match than
12 * the existing entry. As an optimization, matching is skipped
13 * altogether if seen[] already only contains non-zero entries.
15 * If seen[] has not already been written to, it may make sense
16 * to use find_pathspecs_matching_against_index() instead.
18 void add_pathspec_matches_against_index(const struct pathspec
*pathspec
,
21 int num_unmatched
= 0, i
;
24 * Since we are walking the index as if we were walking the directory,
25 * we have to mark the matched pathspec as seen; otherwise we will
26 * mistakenly think that the user gave a pathspec that did not match
29 for (i
= 0; i
< pathspec
->nr
; i
++)
34 for (i
= 0; i
< active_nr
; i
++) {
35 const struct cache_entry
*ce
= active_cache
[i
];
36 ce_path_match(ce
, pathspec
, seen
);
41 * Finds which of the given pathspecs match items in the index.
43 * This is a one-shot wrapper around add_pathspec_matches_against_index()
44 * which allocates, populates, and returns a seen[] array indicating the
45 * nature of the "closest" (i.e. most specific) matches which each of the
46 * given pathspecs achieves against all items in the index.
48 char *find_pathspecs_matching_against_index(const struct pathspec
*pathspec
)
50 char *seen
= xcalloc(pathspec
->nr
, 1);
51 add_pathspec_matches_against_index(pathspec
, seen
);
58 * Possible future magic semantics include stuff like:
60 * { PATHSPEC_RECURSIVE, '*', "recursive" },
61 * { PATHSPEC_REGEXP, '\0', "regexp" },
65 static struct pathspec_magic
{
67 char mnemonic
; /* this cannot be ':'! */
69 } pathspec_magic
[] = {
70 { PATHSPEC_FROMTOP
, '/', "top" },
71 { PATHSPEC_LITERAL
, '\0', "literal" },
72 { PATHSPEC_GLOB
, '\0', "glob" },
73 { PATHSPEC_ICASE
, '\0', "icase" },
74 { PATHSPEC_EXCLUDE
, '!', "exclude" },
77 static void prefix_magic(struct strbuf
*sb
, int prefixlen
, unsigned magic
)
80 strbuf_addstr(sb
, ":(");
81 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
82 if (magic
& pathspec_magic
[i
].bit
) {
83 if (sb
->buf
[sb
->len
- 1] != '(')
84 strbuf_addch(sb
, ',');
85 strbuf_addstr(sb
, pathspec_magic
[i
].name
);
87 strbuf_addf(sb
, ",prefix:%d)", prefixlen
);
90 static inline int get_literal_global(void)
92 static int literal
= -1;
95 literal
= git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, 0);
100 static inline int get_glob_global(void)
102 static int glob
= -1;
105 glob
= git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT
, 0);
110 static inline int get_noglob_global(void)
112 static int noglob
= -1;
115 noglob
= git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT
, 0);
120 static inline int get_icase_global(void)
122 static int icase
= -1;
125 icase
= git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT
, 0);
130 static int get_global_magic(int element_magic
)
132 int global_magic
= 0;
134 if (get_literal_global())
135 global_magic
|= PATHSPEC_LITERAL
;
137 /* --glob-pathspec is overridden by :(literal) */
138 if (get_glob_global() && !(element_magic
& PATHSPEC_LITERAL
))
139 global_magic
|= PATHSPEC_GLOB
;
141 if (get_glob_global() && get_noglob_global())
142 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
144 if (get_icase_global())
145 global_magic
|= PATHSPEC_ICASE
;
147 if ((global_magic
& PATHSPEC_LITERAL
) &&
148 (global_magic
& ~PATHSPEC_LITERAL
))
149 die(_("global 'literal' pathspec setting is incompatible "
150 "with all other global pathspec settings"));
152 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
153 if (get_noglob_global() && !(element_magic
& PATHSPEC_GLOB
))
154 global_magic
|= PATHSPEC_LITERAL
;
160 * Parse the pathspec element looking for long magic
162 * saves all magic in 'magic'
163 * if prefix magic is used, save the prefix length in 'prefix_len'
164 * returns the position in 'elem' after all magic has been parsed
166 static const char *parse_long_magic(unsigned *magic
, int *prefix_len
,
172 for (pos
= elem
+ 2; *pos
&& *pos
!= ')'; pos
= nextat
) {
173 size_t len
= strcspn(pos
, ",)");
177 nextat
= pos
+ len
+ 1; /* handle ',' */
179 nextat
= pos
+ len
; /* handle ')' and '\0' */
184 if (starts_with(pos
, "prefix:")) {
186 *prefix_len
= strtol(pos
+ 7, &endptr
, 10);
187 if (endptr
- pos
!= len
)
188 die(_("invalid parameter for pathspec magic 'prefix'"));
192 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
193 if (strlen(pathspec_magic
[i
].name
) == len
&&
194 !strncmp(pathspec_magic
[i
].name
, pos
, len
)) {
195 *magic
|= pathspec_magic
[i
].bit
;
200 if (ARRAY_SIZE(pathspec_magic
) <= i
)
201 die(_("Invalid pathspec magic '%.*s' in '%s'"),
202 (int) len
, pos
, elem
);
206 die(_("Missing ')' at the end of pathspec magic in '%s'"),
214 * Parse the pathspec element looking for short magic
216 * saves all magic in 'magic'
217 * returns the position in 'elem' after all magic has been parsed
219 static const char *parse_short_magic(unsigned *magic
, const char *elem
)
223 for (pos
= elem
+ 1; *pos
&& *pos
!= ':'; pos
++) {
227 if (!is_pathspec_magic(ch
))
230 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
231 if (pathspec_magic
[i
].mnemonic
== ch
) {
232 *magic
|= pathspec_magic
[i
].bit
;
237 if (ARRAY_SIZE(pathspec_magic
) <= i
)
238 die(_("Unimplemented pathspec magic '%c' in '%s'"),
248 static const char *parse_element_magic(unsigned *magic
, int *prefix_len
,
251 if (elem
[0] != ':' || get_literal_global())
252 return elem
; /* nothing to do */
253 else if (elem
[1] == '(')
255 return parse_long_magic(magic
, prefix_len
, elem
);
258 return parse_short_magic(magic
, elem
);
261 static void strip_submodule_slash_cheap(struct pathspec_item
*item
)
263 if (item
->len
>= 1 && item
->match
[item
->len
- 1] == '/') {
264 int i
= cache_name_pos(item
->match
, item
->len
- 1);
266 if (i
>= 0 && S_ISGITLINK(active_cache
[i
]->ce_mode
)) {
268 item
->match
[item
->len
] = '\0';
273 static void strip_submodule_slash_expensive(struct pathspec_item
*item
)
277 for (i
= 0; i
< active_nr
; i
++) {
278 struct cache_entry
*ce
= active_cache
[i
];
279 int ce_len
= ce_namelen(ce
);
281 if (!S_ISGITLINK(ce
->ce_mode
))
284 if (item
->len
<= ce_len
|| item
->match
[ce_len
] != '/' ||
285 memcmp(ce
->name
, item
->match
, ce_len
))
288 if (item
->len
== ce_len
+ 1) {
289 /* strip trailing slash */
291 item
->match
[item
->len
] = '\0';
293 die(_("Pathspec '%s' is in submodule '%.*s'"),
294 item
->original
, ce_len
, ce
->name
);
300 * Perform the initialization of a pathspec_item based on a pathspec element.
302 static void init_pathspec_item(struct pathspec_item
*item
, unsigned flags
,
303 const char *prefix
, int prefixlen
,
306 unsigned magic
= 0, element_magic
= 0;
307 const char *copyfrom
= elt
;
309 int pathspec_prefix
= -1;
311 /* PATHSPEC_LITERAL_PATH ignores magic */
312 if (flags
& PATHSPEC_LITERAL_PATH
) {
313 magic
= PATHSPEC_LITERAL
;
315 copyfrom
= parse_element_magic(&element_magic
,
318 magic
|= element_magic
;
319 magic
|= get_global_magic(element_magic
);
324 if (pathspec_prefix
>= 0 &&
325 (prefixlen
|| (prefix
&& *prefix
)))
326 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
328 if ((magic
& PATHSPEC_LITERAL
) && (magic
& PATHSPEC_GLOB
))
329 die(_("%s: 'literal' and 'glob' are incompatible"), elt
);
331 /* Create match string which will be used for pathspec matching */
332 if (pathspec_prefix
>= 0) {
333 match
= xstrdup(copyfrom
);
334 prefixlen
= pathspec_prefix
;
335 } else if (magic
& PATHSPEC_FROMTOP
) {
336 match
= xstrdup(copyfrom
);
339 match
= prefix_path_gently(prefix
, prefixlen
,
340 &prefixlen
, copyfrom
);
342 die(_("%s: '%s' is outside repository"), elt
, copyfrom
);
346 item
->len
= strlen(item
->match
);
347 item
->prefix
= prefixlen
;
350 * Prefix the pathspec (keep all magic) and assign to
351 * original. Useful for passing to another command.
353 if ((flags
& PATHSPEC_PREFIX_ORIGIN
) &&
354 prefixlen
&& !get_literal_global()) {
355 struct strbuf sb
= STRBUF_INIT
;
357 /* Preserve the actual prefix length of each pattern */
358 prefix_magic(&sb
, prefixlen
, element_magic
);
360 strbuf_addstr(&sb
, match
);
361 item
->original
= strbuf_detach(&sb
, NULL
);
363 item
->original
= xstrdup(elt
);
366 if (flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
)
367 strip_submodule_slash_cheap(item
);
369 if (flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE
)
370 strip_submodule_slash_expensive(item
);
372 if (magic
& PATHSPEC_LITERAL
) {
373 item
->nowildcard_len
= item
->len
;
375 item
->nowildcard_len
= simple_length(item
->match
);
376 if (item
->nowildcard_len
< prefixlen
)
377 item
->nowildcard_len
= prefixlen
;
381 if (magic
& PATHSPEC_GLOB
) {
383 * FIXME: should we enable ONESTAR in _GLOB for
384 * pattern "* * / * . c"?
387 if (item
->nowildcard_len
< item
->len
&&
388 item
->match
[item
->nowildcard_len
] == '*' &&
389 no_wildcard(item
->match
+ item
->nowildcard_len
+ 1))
390 item
->flags
|= PATHSPEC_ONESTAR
;
393 /* sanity checks, pathspec matchers assume these are sane */
394 assert(item
->nowildcard_len
<= item
->len
&&
395 item
->prefix
<= item
->len
);
398 static int pathspec_item_cmp(const void *a_
, const void *b_
)
400 struct pathspec_item
*a
, *b
;
402 a
= (struct pathspec_item
*)a_
;
403 b
= (struct pathspec_item
*)b_
;
404 return strcmp(a
->match
, b
->match
);
407 static void NORETURN
unsupported_magic(const char *pattern
,
410 struct strbuf sb
= STRBUF_INIT
;
412 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
413 const struct pathspec_magic
*m
= pathspec_magic
+ i
;
414 if (!(magic
& m
->bit
))
417 strbuf_addstr(&sb
, ", ");
420 strbuf_addf(&sb
, _("'%s' (mnemonic: '%c')"),
421 m
->name
, m
->mnemonic
);
423 strbuf_addf(&sb
, "'%s'", m
->name
);
426 * We may want to substitute "this command" with a command
427 * name. E.g. when add--interactive dies when running
430 die(_("%s: pathspec magic not supported by this command: %s"),
435 * Given command line arguments and a prefix, convert the input to
436 * pathspec. die() if any magic in magic_mask is used.
438 void parse_pathspec(struct pathspec
*pathspec
,
439 unsigned magic_mask
, unsigned flags
,
440 const char *prefix
, const char **argv
)
442 struct pathspec_item
*item
;
443 const char *entry
= argv
? *argv
: NULL
;
444 int i
, n
, prefixlen
, warn_empty_string
, nr_exclude
= 0;
446 memset(pathspec
, 0, sizeof(*pathspec
));
448 if (flags
& PATHSPEC_MAXDEPTH_VALID
)
449 pathspec
->magic
|= PATHSPEC_MAXDEPTH
;
451 /* No arguments, no prefix -> no pathspec */
452 if (!entry
&& !prefix
)
455 if ((flags
& PATHSPEC_PREFER_CWD
) &&
456 (flags
& PATHSPEC_PREFER_FULL
))
457 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
459 /* No arguments with prefix -> prefix pathspec */
461 if (flags
& PATHSPEC_PREFER_FULL
)
464 if (!(flags
& PATHSPEC_PREFER_CWD
))
465 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
467 pathspec
->items
= item
= xcalloc(1, sizeof(*item
));
468 item
->match
= xstrdup(prefix
);
469 item
->original
= xstrdup(prefix
);
470 item
->nowildcard_len
= item
->len
= strlen(prefix
);
471 item
->prefix
= item
->len
;
477 warn_empty_string
= 1;
479 if (*argv
[n
] == '\0' && warn_empty_string
) {
480 warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
481 "please use . instead if you meant to match all paths"));
482 warn_empty_string
= 0;
488 ALLOC_ARRAY(pathspec
->items
, n
);
489 item
= pathspec
->items
;
490 prefixlen
= prefix
? strlen(prefix
) : 0;
492 for (i
= 0; i
< n
; i
++) {
495 init_pathspec_item(item
+ i
, flags
, prefix
, prefixlen
, entry
);
497 if (item
[i
].magic
& PATHSPEC_EXCLUDE
)
499 if (item
[i
].magic
& magic_mask
)
500 unsupported_magic(entry
, item
[i
].magic
& magic_mask
);
502 if ((flags
& PATHSPEC_SYMLINK_LEADING_PATH
) &&
503 has_symlink_leading_path(item
[i
].match
, item
[i
].len
)) {
504 die(_("pathspec '%s' is beyond a symbolic link"), entry
);
507 if (item
[i
].nowildcard_len
< item
[i
].len
)
508 pathspec
->has_wildcard
= 1;
509 pathspec
->magic
|= item
[i
].magic
;
513 die(_("There is nothing to exclude from by :(exclude) patterns.\n"
514 "Perhaps you forgot to add either ':/' or '.' ?"));
517 if (pathspec
->magic
& PATHSPEC_MAXDEPTH
) {
518 if (flags
& PATHSPEC_KEEP_ORDER
)
519 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
520 QSORT(pathspec
->items
, pathspec
->nr
, pathspec_item_cmp
);
524 void copy_pathspec(struct pathspec
*dst
, const struct pathspec
*src
)
529 ALLOC_ARRAY(dst
->items
, dst
->nr
);
530 COPY_ARRAY(dst
->items
, src
->items
, dst
->nr
);
532 for (i
= 0; i
< dst
->nr
; i
++) {
533 dst
->items
[i
].match
= xstrdup(src
->items
[i
].match
);
534 dst
->items
[i
].original
= xstrdup(src
->items
[i
].original
);
538 void clear_pathspec(struct pathspec
*pathspec
)
542 for (i
= 0; i
< pathspec
->nr
; i
++) {
543 free(pathspec
->items
[i
].match
);
544 free(pathspec
->items
[i
].original
);
546 free(pathspec
->items
);
547 pathspec
->items
= NULL
;