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 * Take an element of a pathspec and check for magic signatures.
161 * Append the result to the prefix. Return the magic bitmap.
163 * For now, we only parse the syntax and throw out anything other than
166 * NEEDSWORK: This needs to be rewritten when we start migrating
167 * get_pathspec() users to use the "struct pathspec" interface. For
168 * example, a pathspec element may be marked as case-insensitive, but
169 * the prefix part must always match literally, and a single stupid
170 * string cannot express such a case.
172 static unsigned prefix_pathspec(struct pathspec_item
*item
, unsigned flags
,
173 const char *prefix
, int prefixlen
,
176 unsigned magic
= 0, element_magic
= 0;
177 const char *copyfrom
= elt
;
179 int i
, pathspec_prefix
= -1;
181 if (elt
[0] != ':' || get_literal_global() ||
182 (flags
& PATHSPEC_LITERAL_PATH
)) {
183 ; /* nothing to do */
184 } else if (elt
[1] == '(') {
187 for (copyfrom
= elt
+ 2;
188 *copyfrom
&& *copyfrom
!= ')';
190 size_t len
= strcspn(copyfrom
, ",)");
191 if (copyfrom
[len
] == ',')
192 nextat
= copyfrom
+ len
+ 1;
194 /* handle ')' and '\0' */
195 nextat
= copyfrom
+ len
;
198 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
199 if (strlen(pathspec_magic
[i
].name
) == len
&&
200 !strncmp(pathspec_magic
[i
].name
, copyfrom
, len
)) {
201 element_magic
|= pathspec_magic
[i
].bit
;
204 if (starts_with(copyfrom
, "prefix:")) {
206 pathspec_prefix
= strtol(copyfrom
+ 7,
208 if (endptr
- copyfrom
!= len
)
209 die(_("invalid parameter for pathspec magic 'prefix'"));
210 /* "i" would be wrong, but it does not matter */
214 if (ARRAY_SIZE(pathspec_magic
) <= i
)
215 die(_("Invalid pathspec magic '%.*s' in '%s'"),
216 (int) len
, copyfrom
, elt
);
218 if (*copyfrom
!= ')')
219 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt
);
223 for (copyfrom
= elt
+ 1;
224 *copyfrom
&& *copyfrom
!= ':';
228 if (!is_pathspec_magic(ch
))
230 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
231 if (pathspec_magic
[i
].mnemonic
== ch
) {
232 element_magic
|= pathspec_magic
[i
].bit
;
235 if (ARRAY_SIZE(pathspec_magic
) <= i
)
236 die(_("Unimplemented pathspec magic '%c' in '%s'"),
239 if (*copyfrom
== ':')
243 magic
|= element_magic
;
245 /* PATHSPEC_LITERAL_PATH ignores magic */
246 if (flags
& PATHSPEC_LITERAL_PATH
)
247 magic
= PATHSPEC_LITERAL
;
249 magic
|= get_global_magic(element_magic
);
251 if (pathspec_prefix
>= 0 &&
252 (prefixlen
|| (prefix
&& *prefix
)))
253 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
255 if ((magic
& PATHSPEC_LITERAL
) && (magic
& PATHSPEC_GLOB
))
256 die(_("%s: 'literal' and 'glob' are incompatible"), elt
);
258 if (pathspec_prefix
>= 0) {
259 match
= xstrdup(copyfrom
);
260 prefixlen
= pathspec_prefix
;
261 } else if (magic
& PATHSPEC_FROMTOP
) {
262 match
= xstrdup(copyfrom
);
265 match
= prefix_path_gently(prefix
, prefixlen
, &prefixlen
, copyfrom
);
267 die(_("%s: '%s' is outside repository"), elt
, copyfrom
);
271 * Prefix the pathspec (keep all magic) and assign to
272 * original. Useful for passing to another command.
274 if ((flags
& PATHSPEC_PREFIX_ORIGIN
) &&
275 prefixlen
&& !get_literal_global()) {
276 struct strbuf sb
= STRBUF_INIT
;
278 /* Preserve the actual prefix length of each pattern */
279 prefix_magic(&sb
, prefixlen
, element_magic
);
281 strbuf_addstr(&sb
, match
);
282 item
->original
= strbuf_detach(&sb
, NULL
);
284 item
->original
= xstrdup(elt
);
286 item
->len
= strlen(item
->match
);
287 item
->prefix
= prefixlen
;
289 if ((flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
) &&
290 (item
->len
>= 1 && item
->match
[item
->len
- 1] == '/') &&
291 (i
= cache_name_pos(item
->match
, item
->len
- 1)) >= 0 &&
292 S_ISGITLINK(active_cache
[i
]->ce_mode
)) {
294 match
[item
->len
] = '\0';
297 if (flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE
)
298 for (i
= 0; i
< active_nr
; i
++) {
299 struct cache_entry
*ce
= active_cache
[i
];
300 int ce_len
= ce_namelen(ce
);
302 if (!S_ISGITLINK(ce
->ce_mode
))
305 if (item
->len
<= ce_len
|| match
[ce_len
] != '/' ||
306 memcmp(ce
->name
, match
, ce_len
))
308 if (item
->len
== ce_len
+ 1) {
309 /* strip trailing slash */
311 match
[item
->len
] = '\0';
313 die (_("Pathspec '%s' is in submodule '%.*s'"),
314 elt
, ce_len
, ce
->name
);
317 if (magic
& PATHSPEC_LITERAL
)
318 item
->nowildcard_len
= item
->len
;
320 item
->nowildcard_len
= simple_length(item
->match
);
321 if (item
->nowildcard_len
< prefixlen
)
322 item
->nowildcard_len
= prefixlen
;
325 if (magic
& PATHSPEC_GLOB
) {
327 * FIXME: should we enable ONESTAR in _GLOB for
328 * pattern "* * / * . c"?
331 if (item
->nowildcard_len
< item
->len
&&
332 item
->match
[item
->nowildcard_len
] == '*' &&
333 no_wildcard(item
->match
+ item
->nowildcard_len
+ 1))
334 item
->flags
|= PATHSPEC_ONESTAR
;
337 /* sanity checks, pathspec matchers assume these are sane */
338 assert(item
->nowildcard_len
<= item
->len
&&
339 item
->prefix
<= item
->len
);
343 static int pathspec_item_cmp(const void *a_
, const void *b_
)
345 struct pathspec_item
*a
, *b
;
347 a
= (struct pathspec_item
*)a_
;
348 b
= (struct pathspec_item
*)b_
;
349 return strcmp(a
->match
, b
->match
);
352 static void NORETURN
unsupported_magic(const char *pattern
,
355 struct strbuf sb
= STRBUF_INIT
;
357 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
358 const struct pathspec_magic
*m
= pathspec_magic
+ i
;
359 if (!(magic
& m
->bit
))
362 strbuf_addstr(&sb
, ", ");
365 strbuf_addf(&sb
, _("'%s' (mnemonic: '%c')"),
366 m
->name
, m
->mnemonic
);
368 strbuf_addf(&sb
, "'%s'", m
->name
);
371 * We may want to substitute "this command" with a command
372 * name. E.g. when add--interactive dies when running
375 die(_("%s: pathspec magic not supported by this command: %s"),
380 * Given command line arguments and a prefix, convert the input to
381 * pathspec. die() if any magic in magic_mask is used.
383 void parse_pathspec(struct pathspec
*pathspec
,
384 unsigned magic_mask
, unsigned flags
,
385 const char *prefix
, const char **argv
)
387 struct pathspec_item
*item
;
388 const char *entry
= argv
? *argv
: NULL
;
389 int i
, n
, prefixlen
, warn_empty_string
, nr_exclude
= 0;
391 memset(pathspec
, 0, sizeof(*pathspec
));
393 if (flags
& PATHSPEC_MAXDEPTH_VALID
)
394 pathspec
->magic
|= PATHSPEC_MAXDEPTH
;
396 /* No arguments, no prefix -> no pathspec */
397 if (!entry
&& !prefix
)
400 if ((flags
& PATHSPEC_PREFER_CWD
) &&
401 (flags
& PATHSPEC_PREFER_FULL
))
402 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
404 /* No arguments with prefix -> prefix pathspec */
406 if (flags
& PATHSPEC_PREFER_FULL
)
409 if (!(flags
& PATHSPEC_PREFER_CWD
))
410 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
412 pathspec
->items
= item
= xcalloc(1, sizeof(*item
));
413 item
->match
= xstrdup(prefix
);
414 item
->original
= xstrdup(prefix
);
415 item
->nowildcard_len
= item
->len
= strlen(prefix
);
416 item
->prefix
= item
->len
;
422 warn_empty_string
= 1;
424 if (*argv
[n
] == '\0' && warn_empty_string
) {
425 warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
426 "please use . instead if you meant to match all paths"));
427 warn_empty_string
= 0;
433 ALLOC_ARRAY(pathspec
->items
, n
);
434 item
= pathspec
->items
;
435 prefixlen
= prefix
? strlen(prefix
) : 0;
437 for (i
= 0; i
< n
; i
++) {
440 item
[i
].magic
= prefix_pathspec(item
+ i
, flags
,
441 prefix
, prefixlen
, entry
);
443 if (item
[i
].magic
& PATHSPEC_EXCLUDE
)
445 if (item
[i
].magic
& magic_mask
)
446 unsupported_magic(entry
, item
[i
].magic
& magic_mask
);
448 if ((flags
& PATHSPEC_SYMLINK_LEADING_PATH
) &&
449 has_symlink_leading_path(item
[i
].match
, item
[i
].len
)) {
450 die(_("pathspec '%s' is beyond a symbolic link"), entry
);
453 if (item
[i
].nowildcard_len
< item
[i
].len
)
454 pathspec
->has_wildcard
= 1;
455 pathspec
->magic
|= item
[i
].magic
;
459 die(_("There is nothing to exclude from by :(exclude) patterns.\n"
460 "Perhaps you forgot to add either ':/' or '.' ?"));
463 if (pathspec
->magic
& PATHSPEC_MAXDEPTH
) {
464 if (flags
& PATHSPEC_KEEP_ORDER
)
465 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
466 QSORT(pathspec
->items
, pathspec
->nr
, pathspec_item_cmp
);
470 void copy_pathspec(struct pathspec
*dst
, const struct pathspec
*src
)
475 ALLOC_ARRAY(dst
->items
, dst
->nr
);
476 COPY_ARRAY(dst
->items
, src
->items
, dst
->nr
);
478 for (i
= 0; i
< dst
->nr
; i
++) {
479 dst
->items
[i
].match
= xstrdup(src
->items
[i
].match
);
480 dst
->items
[i
].original
= xstrdup(src
->items
[i
].original
);
484 void clear_pathspec(struct pathspec
*pathspec
)
488 for (i
= 0; i
< pathspec
->nr
; i
++) {
489 free(pathspec
->items
[i
].match
);
490 free(pathspec
->items
[i
].original
);
492 free(pathspec
->items
);
493 pathspec
->items
= NULL
;