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" },
74 { PATHSPEC_EXCLUDE
, '!', "exclude" },
78 * Take an element of a pathspec and check for magic signatures.
79 * Append the result to the prefix. Return the magic bitmap.
81 * For now, we only parse the syntax and throw out anything other than
84 * NEEDSWORK: This needs to be rewritten when we start migrating
85 * get_pathspec() users to use the "struct pathspec" interface. For
86 * example, a pathspec element may be marked as case-insensitive, but
87 * the prefix part must always match literally, and a single stupid
88 * string cannot express such a case.
90 static unsigned prefix_pathspec(struct pathspec_item
*item
,
91 unsigned *p_short_magic
,
92 const char **raw
, unsigned flags
,
93 const char *prefix
, int prefixlen
,
96 static int literal_global
= -1;
97 static int glob_global
= -1;
98 static int noglob_global
= -1;
99 static int icase_global
= -1;
100 unsigned magic
= 0, short_magic
= 0, global_magic
= 0;
101 const char *copyfrom
= elt
, *long_magic_end
= NULL
;
103 int i
, pathspec_prefix
= -1;
105 if (literal_global
< 0)
106 literal_global
= git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, 0);
108 global_magic
|= PATHSPEC_LITERAL
;
111 glob_global
= git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT
, 0);
113 global_magic
|= PATHSPEC_GLOB
;
115 if (noglob_global
< 0)
116 noglob_global
= git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT
, 0);
118 if (glob_global
&& noglob_global
)
119 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
122 if (icase_global
< 0)
123 icase_global
= git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT
, 0);
125 global_magic
|= PATHSPEC_ICASE
;
127 if ((global_magic
& PATHSPEC_LITERAL
) &&
128 (global_magic
& ~PATHSPEC_LITERAL
))
129 die(_("global 'literal' pathspec setting is incompatible "
130 "with all other global pathspec settings"));
132 if (flags
& PATHSPEC_LITERAL_PATH
)
135 if (elt
[0] != ':' || literal_global
||
136 (flags
& PATHSPEC_LITERAL_PATH
)) {
137 ; /* nothing to do */
138 } else if (elt
[1] == '(') {
141 for (copyfrom
= elt
+ 2;
142 *copyfrom
&& *copyfrom
!= ')';
144 size_t len
= strcspn(copyfrom
, ",)");
145 if (copyfrom
[len
] == ',')
146 nextat
= copyfrom
+ len
+ 1;
148 /* handle ')' and '\0' */
149 nextat
= copyfrom
+ len
;
152 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
153 if (strlen(pathspec_magic
[i
].name
) == len
&&
154 !strncmp(pathspec_magic
[i
].name
, copyfrom
, len
)) {
155 magic
|= pathspec_magic
[i
].bit
;
158 if (!prefixcmp(copyfrom
, "prefix:")) {
160 pathspec_prefix
= strtol(copyfrom
+ 7,
162 if (endptr
- copyfrom
!= len
)
163 die(_("invalid parameter for pathspec magic 'prefix'"));
164 /* "i" would be wrong, but it does not matter */
168 if (ARRAY_SIZE(pathspec_magic
) <= i
)
169 die(_("Invalid pathspec magic '%.*s' in '%s'"),
170 (int) len
, copyfrom
, elt
);
172 if (*copyfrom
!= ')')
173 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt
);
174 long_magic_end
= copyfrom
;
178 for (copyfrom
= elt
+ 1;
179 *copyfrom
&& *copyfrom
!= ':';
183 if (!is_pathspec_magic(ch
))
185 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
186 if (pathspec_magic
[i
].mnemonic
== ch
) {
187 short_magic
|= pathspec_magic
[i
].bit
;
190 if (ARRAY_SIZE(pathspec_magic
) <= i
)
191 die(_("Unimplemented pathspec magic '%c' in '%s'"),
194 if (*copyfrom
== ':')
198 magic
|= short_magic
;
199 *p_short_magic
= short_magic
;
201 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
202 if (noglob_global
&& !(magic
& PATHSPEC_GLOB
))
203 global_magic
|= PATHSPEC_LITERAL
;
205 /* --glob-pathspec is overridden by :(literal) */
206 if ((global_magic
& PATHSPEC_GLOB
) && (magic
& PATHSPEC_LITERAL
))
207 global_magic
&= ~PATHSPEC_GLOB
;
209 magic
|= global_magic
;
211 if (pathspec_prefix
>= 0 &&
212 (prefixlen
|| (prefix
&& *prefix
)))
213 die("BUG: 'prefix' magic is supposed to be used at worktree's root");
215 if ((magic
& PATHSPEC_LITERAL
) && (magic
& PATHSPEC_GLOB
))
216 die(_("%s: 'literal' and 'glob' are incompatible"), elt
);
218 if (pathspec_prefix
>= 0) {
219 match
= xstrdup(copyfrom
);
220 prefixlen
= pathspec_prefix
;
221 } else if (magic
& PATHSPEC_FROMTOP
) {
222 match
= xstrdup(copyfrom
);
225 match
= prefix_path_gently(prefix
, prefixlen
, &prefixlen
, copyfrom
);
227 die(_("%s: '%s' is outside repository"), elt
, copyfrom
);
229 *raw
= item
->match
= match
;
231 * Prefix the pathspec (keep all magic) and assign to
232 * original. Useful for passing to another command.
234 if (flags
& PATHSPEC_PREFIX_ORIGIN
) {
235 struct strbuf sb
= STRBUF_INIT
;
236 const char *start
= elt
;
237 if (prefixlen
&& !literal_global
) {
238 /* Preserve the actual prefix length of each pattern */
240 die("BUG: prefixing on short magic is not supported");
241 else if (long_magic_end
) {
242 strbuf_add(&sb
, start
, long_magic_end
- start
);
243 strbuf_addf(&sb
, ",prefix:%d", prefixlen
);
244 start
= long_magic_end
;
248 strbuf_addf(&sb
, ":(prefix:%d)", prefixlen
);
251 strbuf_add(&sb
, start
, copyfrom
- start
);
252 strbuf_addstr(&sb
, match
);
253 item
->original
= strbuf_detach(&sb
, NULL
);
255 item
->original
= elt
;
256 item
->len
= strlen(item
->match
);
257 item
->prefix
= prefixlen
;
259 if ((flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
) &&
260 (item
->len
>= 1 && item
->match
[item
->len
- 1] == '/') &&
261 (i
= cache_name_pos(item
->match
, item
->len
- 1)) >= 0 &&
262 S_ISGITLINK(active_cache
[i
]->ce_mode
)) {
264 match
[item
->len
] = '\0';
267 if (flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE
)
268 for (i
= 0; i
< active_nr
; i
++) {
269 struct cache_entry
*ce
= active_cache
[i
];
270 int ce_len
= ce_namelen(ce
);
272 if (!S_ISGITLINK(ce
->ce_mode
))
275 if (item
->len
<= ce_len
|| match
[ce_len
] != '/' ||
276 memcmp(ce
->name
, match
, ce_len
))
278 if (item
->len
== ce_len
+ 1) {
279 /* strip trailing slash */
281 match
[item
->len
] = '\0';
283 die (_("Pathspec '%s' is in submodule '%.*s'"),
284 elt
, ce_len
, ce
->name
);
287 if (magic
& PATHSPEC_LITERAL
)
288 item
->nowildcard_len
= item
->len
;
290 item
->nowildcard_len
= simple_length(item
->match
);
291 if (item
->nowildcard_len
< prefixlen
)
292 item
->nowildcard_len
= prefixlen
;
295 if (magic
& PATHSPEC_GLOB
) {
297 * FIXME: should we enable ONESTAR in _GLOB for
298 * pattern "* * / * . c"?
301 if (item
->nowildcard_len
< item
->len
&&
302 item
->match
[item
->nowildcard_len
] == '*' &&
303 no_wildcard(item
->match
+ item
->nowildcard_len
+ 1))
304 item
->flags
|= PATHSPEC_ONESTAR
;
307 /* sanity checks, pathspec matchers assume these are sane */
308 assert(item
->nowildcard_len
<= item
->len
&&
309 item
->prefix
<= item
->len
);
313 static int pathspec_item_cmp(const void *a_
, const void *b_
)
315 struct pathspec_item
*a
, *b
;
317 a
= (struct pathspec_item
*)a_
;
318 b
= (struct pathspec_item
*)b_
;
319 return strcmp(a
->match
, b
->match
);
322 static void NORETURN
unsupported_magic(const char *pattern
,
324 unsigned short_magic
)
326 struct strbuf sb
= STRBUF_INIT
;
328 for (n
= i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
329 const struct pathspec_magic
*m
= pathspec_magic
+ i
;
330 if (!(magic
& m
->bit
))
333 strbuf_addstr(&sb
, " ");
334 if (short_magic
& m
->bit
)
335 strbuf_addf(&sb
, "'%c'", m
->mnemonic
);
337 strbuf_addf(&sb
, "'%s'", m
->name
);
341 * We may want to substitute "this command" with a command
342 * name. E.g. when add--interactive dies when running
345 die(_("%s: pathspec magic not supported by this command: %s"),
350 * Given command line arguments and a prefix, convert the input to
351 * pathspec. die() if any magic in magic_mask is used.
353 void parse_pathspec(struct pathspec
*pathspec
,
354 unsigned magic_mask
, unsigned flags
,
355 const char *prefix
, const char **argv
)
357 struct pathspec_item
*item
;
358 const char *entry
= argv
? *argv
: NULL
;
359 int i
, n
, prefixlen
, nr_exclude
= 0;
361 memset(pathspec
, 0, sizeof(*pathspec
));
363 if (flags
& PATHSPEC_MAXDEPTH_VALID
)
364 pathspec
->magic
|= PATHSPEC_MAXDEPTH
;
366 /* No arguments, no prefix -> no pathspec */
367 if (!entry
&& !prefix
)
370 if ((flags
& PATHSPEC_PREFER_CWD
) &&
371 (flags
& PATHSPEC_PREFER_FULL
))
372 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
374 /* No arguments with prefix -> prefix pathspec */
376 static const char *raw
[2];
378 if (flags
& PATHSPEC_PREFER_FULL
)
381 if (!(flags
& PATHSPEC_PREFER_CWD
))
382 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
384 pathspec
->items
= item
= xmalloc(sizeof(*item
));
385 memset(item
, 0, sizeof(*item
));
386 item
->match
= prefix
;
387 item
->original
= prefix
;
388 item
->nowildcard_len
= item
->len
= strlen(prefix
);
389 item
->prefix
= item
->len
;
393 pathspec
->_raw
= raw
;
402 pathspec
->items
= item
= xmalloc(sizeof(*item
) * n
);
403 pathspec
->_raw
= argv
;
404 prefixlen
= prefix
? strlen(prefix
) : 0;
406 for (i
= 0; i
< n
; i
++) {
407 unsigned short_magic
;
410 item
[i
].magic
= prefix_pathspec(item
+ i
, &short_magic
,
412 prefix
, prefixlen
, entry
);
413 if ((flags
& PATHSPEC_LITERAL_PATH
) &&
414 !(magic_mask
& PATHSPEC_LITERAL
))
415 item
[i
].magic
|= PATHSPEC_LITERAL
;
416 if (item
[i
].magic
& PATHSPEC_EXCLUDE
)
418 if (item
[i
].magic
& magic_mask
)
419 unsupported_magic(entry
,
420 item
[i
].magic
& magic_mask
,
423 if ((flags
& PATHSPEC_SYMLINK_LEADING_PATH
) &&
424 has_symlink_leading_path(item
[i
].match
, item
[i
].len
)) {
425 die(_("pathspec '%s' is beyond a symbolic link"), entry
);
428 if (item
[i
].nowildcard_len
< item
[i
].len
)
429 pathspec
->has_wildcard
= 1;
430 pathspec
->magic
|= item
[i
].magic
;
434 die(_("There is nothing to exclude from by :(exclude) patterns.\n"
435 "Perhaps you forgot to add either ':/' or '.' ?"));
438 if (pathspec
->magic
& PATHSPEC_MAXDEPTH
) {
439 if (flags
& PATHSPEC_KEEP_ORDER
)
440 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
441 qsort(pathspec
->items
, pathspec
->nr
,
442 sizeof(struct pathspec_item
), pathspec_item_cmp
);
447 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
448 * based interface - see pathspec.c:parse_pathspec().
451 * - prefix - a path relative to the root of the working tree
452 * - pathspec - a list of paths underneath the prefix path
454 * Iterates over pathspec, prepending each path with prefix,
455 * and return the resulting list.
457 * If pathspec is empty, return a singleton list containing prefix.
459 * If pathspec and prefix are both empty, return an empty list.
461 * This is typically used by built-in commands such as add.c, in order
462 * to normalize argv arguments provided to the built-in into a list of
463 * paths to process, all relative to the root of the working tree.
465 const char **get_pathspec(const char *prefix
, const char **pathspec
)
470 ~(PATHSPEC_FROMTOP
| PATHSPEC_LITERAL
),
476 void copy_pathspec(struct pathspec
*dst
, const struct pathspec
*src
)
479 dst
->items
= xmalloc(sizeof(struct pathspec_item
) * dst
->nr
);
480 memcpy(dst
->items
, src
->items
,
481 sizeof(struct pathspec_item
) * dst
->nr
);
484 void free_pathspec(struct pathspec
*pathspec
)
486 free(pathspec
->items
);
487 pathspec
->items
= NULL
;