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 match_pathspec_depth(pathspec
, ce
->name
, ce_namelen(ce
), 0, 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" },
77 * Take an element of a pathspec and check for magic signatures.
78 * Append the result to the prefix. Return the magic bitmap.
80 * For now, we only parse the syntax and throw out anything other than
83 * NEEDSWORK: This needs to be rewritten when we start migrating
84 * get_pathspec() users to use the "struct pathspec" interface. For
85 * example, a pathspec element may be marked as case-insensitive, but
86 * the prefix part must always match literally, and a single stupid
87 * string cannot express such a case.
89 static unsigned prefix_pathspec(struct pathspec_item
*item
,
90 unsigned *p_short_magic
,
91 const char **raw
, unsigned flags
,
92 const char *prefix
, int prefixlen
,
95 static int literal_global
= -1;
96 static int glob_global
= -1;
97 static int noglob_global
= -1;
98 static int icase_global
= -1;
99 unsigned magic
= 0, short_magic
= 0, global_magic
= 0;
100 const char *copyfrom
= elt
, *long_magic_end
= NULL
;
102 int i
, pathspec_prefix
= -1;
104 if (literal_global
< 0)
105 literal_global
= git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, 0);
107 global_magic
|= PATHSPEC_LITERAL
;
110 glob_global
= git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT
, 0);
112 global_magic
|= PATHSPEC_GLOB
;
114 if (noglob_global
< 0)
115 noglob_global
= git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT
, 0);
117 if (glob_global
&& noglob_global
)
118 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
121 if (icase_global
< 0)
122 icase_global
= git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT
, 0);
124 global_magic
|= PATHSPEC_ICASE
;
126 if ((global_magic
& PATHSPEC_LITERAL
) &&
127 (global_magic
& ~PATHSPEC_LITERAL
))
128 die(_("global 'literal' pathspec setting is incompatible "
129 "with all other global pathspec settings"));
131 if (flags
& PATHSPEC_LITERAL_PATH
)
134 if (elt
[0] != ':' || literal_global
||
135 (flags
& PATHSPEC_LITERAL_PATH
)) {
136 ; /* nothing to do */
137 } else if (elt
[1] == '(') {
140 for (copyfrom
= elt
+ 2;
141 *copyfrom
&& *copyfrom
!= ')';
143 size_t len
= strcspn(copyfrom
, ",)");
144 if (copyfrom
[len
] == ',')
145 nextat
= copyfrom
+ len
+ 1;
147 /* handle ')' and '\0' */
148 nextat
= copyfrom
+ len
;
151 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
152 if (strlen(pathspec_magic
[i
].name
) == len
&&
153 !strncmp(pathspec_magic
[i
].name
, copyfrom
, len
)) {
154 magic
|= pathspec_magic
[i
].bit
;
157 if (starts_with(copyfrom
, "prefix:")) {
159 pathspec_prefix
= strtol(copyfrom
+ 7,
161 if (endptr
- copyfrom
!= len
)
162 die(_("invalid parameter for pathspec magic 'prefix'"));
163 /* "i" would be wrong, but it does not matter */
167 if (ARRAY_SIZE(pathspec_magic
) <= i
)
168 die(_("Invalid pathspec magic '%.*s' in '%s'"),
169 (int) len
, copyfrom
, elt
);
171 if (*copyfrom
!= ')')
172 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt
);
173 long_magic_end
= copyfrom
;
177 for (copyfrom
= elt
+ 1;
178 *copyfrom
&& *copyfrom
!= ':';
182 if (!is_pathspec_magic(ch
))
184 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
185 if (pathspec_magic
[i
].mnemonic
== ch
) {
186 short_magic
|= pathspec_magic
[i
].bit
;
189 if (ARRAY_SIZE(pathspec_magic
) <= i
)
190 die(_("Unimplemented pathspec magic '%c' in '%s'"),
193 if (*copyfrom
== ':')
197 magic
|= short_magic
;
198 *p_short_magic
= short_magic
;
200 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
201 if (noglob_global
&& !(magic
& PATHSPEC_GLOB
))
202 global_magic
|= PATHSPEC_LITERAL
;
204 /* --glob-pathspec is overridden by :(literal) */
205 if ((global_magic
& PATHSPEC_GLOB
) && (magic
& PATHSPEC_LITERAL
))
206 global_magic
&= ~PATHSPEC_GLOB
;
208 magic
|= global_magic
;
210 if (pathspec_prefix
>= 0 &&
211 (prefixlen
|| (prefix
&& *prefix
)))
212 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
214 if ((magic
& PATHSPEC_LITERAL
) && (magic
& PATHSPEC_GLOB
))
215 die(_("%s: 'literal' and 'glob' are incompatible"), elt
);
217 if (pathspec_prefix
>= 0) {
218 match
= xstrdup(copyfrom
);
219 prefixlen
= pathspec_prefix
;
220 } else if (magic
& PATHSPEC_FROMTOP
) {
221 match
= xstrdup(copyfrom
);
224 match
= prefix_path_gently(prefix
, prefixlen
, &prefixlen
, copyfrom
);
226 die(_("%s: '%s' is outside repository"), elt
, copyfrom
);
228 *raw
= item
->match
= match
;
230 * Prefix the pathspec (keep all magic) and assign to
231 * original. Useful for passing to another command.
233 if (flags
& PATHSPEC_PREFIX_ORIGIN
) {
234 struct strbuf sb
= STRBUF_INIT
;
235 const char *start
= elt
;
236 if (prefixlen
&& !literal_global
) {
237 /* Preserve the actual prefix length of each pattern */
239 die("BUG: prefixing on short magic is not supported");
240 else if (long_magic_end
) {
241 strbuf_add(&sb
, start
, long_magic_end
- start
);
242 strbuf_addf(&sb
, ",prefix:%d", prefixlen
);
243 start
= long_magic_end
;
247 strbuf_addf(&sb
, ":(prefix:%d)", prefixlen
);
250 strbuf_add(&sb
, start
, copyfrom
- start
);
251 strbuf_addstr(&sb
, match
);
252 item
->original
= strbuf_detach(&sb
, NULL
);
254 item
->original
= elt
;
255 item
->len
= strlen(item
->match
);
256 item
->prefix
= prefixlen
;
258 if ((flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
) &&
259 (item
->len
>= 1 && item
->match
[item
->len
- 1] == '/') &&
260 (i
= cache_name_pos(item
->match
, item
->len
- 1)) >= 0 &&
261 S_ISGITLINK(active_cache
[i
]->ce_mode
)) {
263 match
[item
->len
] = '\0';
266 if (flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE
)
267 for (i
= 0; i
< active_nr
; i
++) {
268 struct cache_entry
*ce
= active_cache
[i
];
269 int ce_len
= ce_namelen(ce
);
271 if (!S_ISGITLINK(ce
->ce_mode
))
274 if (item
->len
<= ce_len
|| match
[ce_len
] != '/' ||
275 memcmp(ce
->name
, match
, ce_len
))
277 if (item
->len
== ce_len
+ 1) {
278 /* strip trailing slash */
280 match
[item
->len
] = '\0';
282 die (_("Pathspec '%s' is in submodule '%.*s'"),
283 elt
, ce_len
, ce
->name
);
286 if (magic
& PATHSPEC_LITERAL
)
287 item
->nowildcard_len
= item
->len
;
289 item
->nowildcard_len
= simple_length(item
->match
);
290 if (item
->nowildcard_len
< prefixlen
)
291 item
->nowildcard_len
= prefixlen
;
294 if (magic
& PATHSPEC_GLOB
) {
296 * FIXME: should we enable ONESTAR in _GLOB for
297 * pattern "* * / * . c"?
300 if (item
->nowildcard_len
< item
->len
&&
301 item
->match
[item
->nowildcard_len
] == '*' &&
302 no_wildcard(item
->match
+ item
->nowildcard_len
+ 1))
303 item
->flags
|= PATHSPEC_ONESTAR
;
306 /* sanity checks, pathspec matchers assume these are sane */
307 assert(item
->nowildcard_len
<= item
->len
&&
308 item
->prefix
<= item
->len
);
312 static int pathspec_item_cmp(const void *a_
, const void *b_
)
314 struct pathspec_item
*a
, *b
;
316 a
= (struct pathspec_item
*)a_
;
317 b
= (struct pathspec_item
*)b_
;
318 return strcmp(a
->match
, b
->match
);
321 static void NORETURN
unsupported_magic(const char *pattern
,
323 unsigned short_magic
)
325 struct strbuf sb
= STRBUF_INIT
;
327 for (n
= i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
328 const struct pathspec_magic
*m
= pathspec_magic
+ i
;
329 if (!(magic
& m
->bit
))
332 strbuf_addstr(&sb
, " ");
333 if (short_magic
& m
->bit
)
334 strbuf_addf(&sb
, "'%c'", m
->mnemonic
);
336 strbuf_addf(&sb
, "'%s'", m
->name
);
340 * We may want to substitute "this command" with a command
341 * name. E.g. when add--interactive dies when running
344 die(_("%s: pathspec magic not supported by this command: %s"),
349 * Given command line arguments and a prefix, convert the input to
350 * pathspec. die() if any magic in magic_mask is used.
352 void parse_pathspec(struct pathspec
*pathspec
,
353 unsigned magic_mask
, unsigned flags
,
354 const char *prefix
, const char **argv
)
356 struct pathspec_item
*item
;
357 const char *entry
= argv
? *argv
: NULL
;
360 memset(pathspec
, 0, sizeof(*pathspec
));
362 if (flags
& PATHSPEC_MAXDEPTH_VALID
)
363 pathspec
->magic
|= PATHSPEC_MAXDEPTH
;
365 /* No arguments, no prefix -> no pathspec */
366 if (!entry
&& !prefix
)
369 if ((flags
& PATHSPEC_PREFER_CWD
) &&
370 (flags
& PATHSPEC_PREFER_FULL
))
371 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
373 /* No arguments with prefix -> prefix pathspec */
375 static const char *raw
[2];
377 if (flags
& PATHSPEC_PREFER_FULL
)
380 if (!(flags
& PATHSPEC_PREFER_CWD
))
381 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
383 pathspec
->items
= item
= xmalloc(sizeof(*item
));
384 memset(item
, 0, sizeof(*item
));
385 item
->match
= prefix
;
386 item
->original
= prefix
;
387 item
->nowildcard_len
= item
->len
= strlen(prefix
);
388 item
->prefix
= item
->len
;
392 pathspec
->_raw
= raw
;
401 pathspec
->items
= item
= xmalloc(sizeof(*item
) * n
);
402 pathspec
->_raw
= argv
;
403 prefixlen
= prefix
? strlen(prefix
) : 0;
405 for (i
= 0; i
< n
; i
++) {
406 unsigned short_magic
;
409 item
[i
].magic
= prefix_pathspec(item
+ i
, &short_magic
,
411 prefix
, prefixlen
, entry
);
412 if ((flags
& PATHSPEC_LITERAL_PATH
) &&
413 !(magic_mask
& PATHSPEC_LITERAL
))
414 item
[i
].magic
|= PATHSPEC_LITERAL
;
415 if (item
[i
].magic
& magic_mask
)
416 unsupported_magic(entry
,
417 item
[i
].magic
& magic_mask
,
420 if ((flags
& PATHSPEC_SYMLINK_LEADING_PATH
) &&
421 has_symlink_leading_path(item
[i
].match
, item
[i
].len
)) {
422 die(_("pathspec '%s' is beyond a symbolic link"), entry
);
425 if (item
[i
].nowildcard_len
< item
[i
].len
)
426 pathspec
->has_wildcard
= 1;
427 pathspec
->magic
|= item
[i
].magic
;
431 if (pathspec
->magic
& PATHSPEC_MAXDEPTH
) {
432 if (flags
& PATHSPEC_KEEP_ORDER
)
433 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
434 qsort(pathspec
->items
, pathspec
->nr
,
435 sizeof(struct pathspec_item
), pathspec_item_cmp
);
440 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
441 * based interface - see pathspec.c:parse_pathspec().
444 * - prefix - a path relative to the root of the working tree
445 * - pathspec - a list of paths underneath the prefix path
447 * Iterates over pathspec, prepending each path with prefix,
448 * and return the resulting list.
450 * If pathspec is empty, return a singleton list containing prefix.
452 * If pathspec and prefix are both empty, return an empty list.
454 * This is typically used by built-in commands such as add.c, in order
455 * to normalize argv arguments provided to the built-in into a list of
456 * paths to process, all relative to the root of the working tree.
458 const char **get_pathspec(const char *prefix
, const char **pathspec
)
463 ~(PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
),
469 void copy_pathspec(struct pathspec
*dst
, const struct pathspec
*src
)
472 dst
->items
= xmalloc(sizeof(struct pathspec_item
) * dst
->nr
);
473 memcpy(dst
->items
, src
->items
,
474 sizeof(struct pathspec_item
) * dst
->nr
);
477 void free_pathspec(struct pathspec
*pathspec
)
479 free(pathspec
->items
);
480 pathspec
->items
= NULL
;