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_short_magic(struct strbuf
*sb
, int prefixlen
,
81 strbuf_addstr(sb
, ":(");
82 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
83 if (short_magic
& pathspec_magic
[i
].bit
) {
84 if (sb
->buf
[sb
->len
- 1] != '(')
85 strbuf_addch(sb
, ',');
86 strbuf_addstr(sb
, pathspec_magic
[i
].name
);
88 strbuf_addf(sb
, ",prefix:%d)", prefixlen
);
92 * Take an element of a pathspec and check for magic signatures.
93 * Append the result to the prefix. Return the magic bitmap.
95 * For now, we only parse the syntax and throw out anything other than
98 * NEEDSWORK: This needs to be rewritten when we start migrating
99 * get_pathspec() users to use the "struct pathspec" interface. For
100 * example, a pathspec element may be marked as case-insensitive, but
101 * the prefix part must always match literally, and a single stupid
102 * string cannot express such a case.
104 static unsigned prefix_pathspec(struct pathspec_item
*item
, unsigned flags
,
105 const char *prefix
, int prefixlen
,
108 static int literal_global
= -1;
109 static int glob_global
= -1;
110 static int noglob_global
= -1;
111 static int icase_global
= -1;
112 unsigned magic
= 0, short_magic
= 0, global_magic
= 0;
113 const char *copyfrom
= elt
, *long_magic_end
= NULL
;
115 int i
, pathspec_prefix
= -1;
117 if (literal_global
< 0)
118 literal_global
= git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, 0);
120 global_magic
|= PATHSPEC_LITERAL
;
123 glob_global
= git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT
, 0);
125 global_magic
|= PATHSPEC_GLOB
;
127 if (noglob_global
< 0)
128 noglob_global
= git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT
, 0);
130 if (glob_global
&& noglob_global
)
131 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
134 if (icase_global
< 0)
135 icase_global
= git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT
, 0);
137 global_magic
|= PATHSPEC_ICASE
;
139 if ((global_magic
& PATHSPEC_LITERAL
) &&
140 (global_magic
& ~PATHSPEC_LITERAL
))
141 die(_("global 'literal' pathspec setting is incompatible "
142 "with all other global pathspec settings"));
144 if (flags
& PATHSPEC_LITERAL_PATH
)
147 if (elt
[0] != ':' || literal_global
||
148 (flags
& PATHSPEC_LITERAL_PATH
)) {
149 ; /* nothing to do */
150 } else if (elt
[1] == '(') {
153 for (copyfrom
= elt
+ 2;
154 *copyfrom
&& *copyfrom
!= ')';
156 size_t len
= strcspn(copyfrom
, ",)");
157 if (copyfrom
[len
] == ',')
158 nextat
= copyfrom
+ len
+ 1;
160 /* handle ')' and '\0' */
161 nextat
= copyfrom
+ len
;
164 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
165 if (strlen(pathspec_magic
[i
].name
) == len
&&
166 !strncmp(pathspec_magic
[i
].name
, copyfrom
, len
)) {
167 magic
|= pathspec_magic
[i
].bit
;
170 if (starts_with(copyfrom
, "prefix:")) {
172 pathspec_prefix
= strtol(copyfrom
+ 7,
174 if (endptr
- copyfrom
!= len
)
175 die(_("invalid parameter for pathspec magic 'prefix'"));
176 /* "i" would be wrong, but it does not matter */
180 if (ARRAY_SIZE(pathspec_magic
) <= i
)
181 die(_("Invalid pathspec magic '%.*s' in '%s'"),
182 (int) len
, copyfrom
, elt
);
184 if (*copyfrom
!= ')')
185 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt
);
186 long_magic_end
= copyfrom
;
190 for (copyfrom
= elt
+ 1;
191 *copyfrom
&& *copyfrom
!= ':';
195 if (!is_pathspec_magic(ch
))
197 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
198 if (pathspec_magic
[i
].mnemonic
== ch
) {
199 short_magic
|= pathspec_magic
[i
].bit
;
202 if (ARRAY_SIZE(pathspec_magic
) <= i
)
203 die(_("Unimplemented pathspec magic '%c' in '%s'"),
206 if (*copyfrom
== ':')
210 magic
|= short_magic
;
212 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
213 if (noglob_global
&& !(magic
& PATHSPEC_GLOB
))
214 global_magic
|= PATHSPEC_LITERAL
;
216 /* --glob-pathspec is overridden by :(literal) */
217 if ((global_magic
& PATHSPEC_GLOB
) && (magic
& PATHSPEC_LITERAL
))
218 global_magic
&= ~PATHSPEC_GLOB
;
220 magic
|= global_magic
;
222 if (pathspec_prefix
>= 0 &&
223 (prefixlen
|| (prefix
&& *prefix
)))
224 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
226 if ((magic
& PATHSPEC_LITERAL
) && (magic
& PATHSPEC_GLOB
))
227 die(_("%s: 'literal' and 'glob' are incompatible"), elt
);
229 if (pathspec_prefix
>= 0) {
230 match
= xstrdup(copyfrom
);
231 prefixlen
= pathspec_prefix
;
232 } else if (magic
& PATHSPEC_FROMTOP
) {
233 match
= xstrdup(copyfrom
);
236 match
= prefix_path_gently(prefix
, prefixlen
, &prefixlen
, copyfrom
);
238 die(_("%s: '%s' is outside repository"), elt
, copyfrom
);
242 * Prefix the pathspec (keep all magic) and assign to
243 * original. Useful for passing to another command.
245 if (flags
& PATHSPEC_PREFIX_ORIGIN
) {
246 struct strbuf sb
= STRBUF_INIT
;
247 if (prefixlen
&& !literal_global
) {
248 /* Preserve the actual prefix length of each pattern */
250 prefix_short_magic(&sb
, prefixlen
, short_magic
);
251 else if (long_magic_end
) {
252 strbuf_add(&sb
, elt
, long_magic_end
- elt
);
253 strbuf_addf(&sb
, ",prefix:%d)", prefixlen
);
255 strbuf_addf(&sb
, ":(prefix:%d)", prefixlen
);
257 strbuf_addstr(&sb
, match
);
258 item
->original
= strbuf_detach(&sb
, NULL
);
260 item
->original
= xstrdup(elt
);
262 item
->len
= strlen(item
->match
);
263 item
->prefix
= prefixlen
;
265 if ((flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
) &&
266 (item
->len
>= 1 && item
->match
[item
->len
- 1] == '/') &&
267 (i
= cache_name_pos(item
->match
, item
->len
- 1)) >= 0 &&
268 S_ISGITLINK(active_cache
[i
]->ce_mode
)) {
270 match
[item
->len
] = '\0';
273 if (flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE
)
274 for (i
= 0; i
< active_nr
; i
++) {
275 struct cache_entry
*ce
= active_cache
[i
];
276 int ce_len
= ce_namelen(ce
);
278 if (!S_ISGITLINK(ce
->ce_mode
))
281 if (item
->len
<= ce_len
|| match
[ce_len
] != '/' ||
282 memcmp(ce
->name
, match
, ce_len
))
284 if (item
->len
== ce_len
+ 1) {
285 /* strip trailing slash */
287 match
[item
->len
] = '\0';
289 die (_("Pathspec '%s' is in submodule '%.*s'"),
290 elt
, ce_len
, ce
->name
);
293 if (magic
& PATHSPEC_LITERAL
)
294 item
->nowildcard_len
= item
->len
;
296 item
->nowildcard_len
= simple_length(item
->match
);
297 if (item
->nowildcard_len
< prefixlen
)
298 item
->nowildcard_len
= prefixlen
;
301 if (magic
& PATHSPEC_GLOB
) {
303 * FIXME: should we enable ONESTAR in _GLOB for
304 * pattern "* * / * . c"?
307 if (item
->nowildcard_len
< item
->len
&&
308 item
->match
[item
->nowildcard_len
] == '*' &&
309 no_wildcard(item
->match
+ item
->nowildcard_len
+ 1))
310 item
->flags
|= PATHSPEC_ONESTAR
;
313 /* sanity checks, pathspec matchers assume these are sane */
314 assert(item
->nowildcard_len
<= item
->len
&&
315 item
->prefix
<= item
->len
);
319 static int pathspec_item_cmp(const void *a_
, const void *b_
)
321 struct pathspec_item
*a
, *b
;
323 a
= (struct pathspec_item
*)a_
;
324 b
= (struct pathspec_item
*)b_
;
325 return strcmp(a
->match
, b
->match
);
328 static void NORETURN
unsupported_magic(const char *pattern
,
331 struct strbuf sb
= STRBUF_INIT
;
333 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
334 const struct pathspec_magic
*m
= pathspec_magic
+ i
;
335 if (!(magic
& m
->bit
))
338 strbuf_addstr(&sb
, ", ");
341 strbuf_addf(&sb
, _("'%s' (mnemonic: '%c')"),
342 m
->name
, m
->mnemonic
);
344 strbuf_addf(&sb
, "'%s'", m
->name
);
347 * We may want to substitute "this command" with a command
348 * name. E.g. when add--interactive dies when running
351 die(_("%s: pathspec magic not supported by this command: %s"),
356 * Given command line arguments and a prefix, convert the input to
357 * pathspec. die() if any magic in magic_mask is used.
359 void parse_pathspec(struct pathspec
*pathspec
,
360 unsigned magic_mask
, unsigned flags
,
361 const char *prefix
, const char **argv
)
363 struct pathspec_item
*item
;
364 const char *entry
= argv
? *argv
: NULL
;
365 int i
, n
, prefixlen
, warn_empty_string
, nr_exclude
= 0;
367 memset(pathspec
, 0, sizeof(*pathspec
));
369 if (flags
& PATHSPEC_MAXDEPTH_VALID
)
370 pathspec
->magic
|= PATHSPEC_MAXDEPTH
;
372 /* No arguments, no prefix -> no pathspec */
373 if (!entry
&& !prefix
)
376 if ((flags
& PATHSPEC_PREFER_CWD
) &&
377 (flags
& PATHSPEC_PREFER_FULL
))
378 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
380 /* No arguments with prefix -> prefix pathspec */
382 if (flags
& PATHSPEC_PREFER_FULL
)
385 if (!(flags
& PATHSPEC_PREFER_CWD
))
386 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
388 pathspec
->items
= item
= xcalloc(1, sizeof(*item
));
389 item
->match
= xstrdup(prefix
);
390 item
->original
= xstrdup(prefix
);
391 item
->nowildcard_len
= item
->len
= strlen(prefix
);
392 item
->prefix
= item
->len
;
398 warn_empty_string
= 1;
400 if (*argv
[n
] == '\0' && warn_empty_string
) {
401 warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
402 "please use . instead if you meant to match all paths"));
403 warn_empty_string
= 0;
409 ALLOC_ARRAY(pathspec
->items
, n
);
410 item
= pathspec
->items
;
411 prefixlen
= prefix
? strlen(prefix
) : 0;
413 for (i
= 0; i
< n
; i
++) {
416 item
[i
].magic
= prefix_pathspec(item
+ i
, flags
,
417 prefix
, prefixlen
, entry
);
418 if ((flags
& PATHSPEC_LITERAL_PATH
) &&
419 !(magic_mask
& PATHSPEC_LITERAL
))
420 item
[i
].magic
|= PATHSPEC_LITERAL
;
421 if (item
[i
].magic
& PATHSPEC_EXCLUDE
)
423 if (item
[i
].magic
& magic_mask
)
424 unsupported_magic(entry
, item
[i
].magic
& magic_mask
);
426 if ((flags
& PATHSPEC_SYMLINK_LEADING_PATH
) &&
427 has_symlink_leading_path(item
[i
].match
, item
[i
].len
)) {
428 die(_("pathspec '%s' is beyond a symbolic link"), entry
);
431 if (item
[i
].nowildcard_len
< item
[i
].len
)
432 pathspec
->has_wildcard
= 1;
433 pathspec
->magic
|= item
[i
].magic
;
437 die(_("There is nothing to exclude from by :(exclude) patterns.\n"
438 "Perhaps you forgot to add either ':/' or '.' ?"));
441 if (pathspec
->magic
& PATHSPEC_MAXDEPTH
) {
442 if (flags
& PATHSPEC_KEEP_ORDER
)
443 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
444 QSORT(pathspec
->items
, pathspec
->nr
, pathspec_item_cmp
);
448 void copy_pathspec(struct pathspec
*dst
, const struct pathspec
*src
)
453 ALLOC_ARRAY(dst
->items
, dst
->nr
);
454 COPY_ARRAY(dst
->items
, src
->items
, dst
->nr
);
456 for (i
= 0; i
< dst
->nr
; i
++) {
457 dst
->items
[i
].match
= xstrdup(src
->items
[i
].match
);
458 dst
->items
[i
].original
= xstrdup(src
->items
[i
].original
);
462 void clear_pathspec(struct pathspec
*pathspec
)
466 for (i
= 0; i
< pathspec
->nr
; i
++) {
467 free(pathspec
->items
[i
].match
);
468 free(pathspec
->items
[i
].original
);
470 free(pathspec
->items
);
471 pathspec
->items
= NULL
;