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 char **pathspec
,
19 char *seen
, int specs
)
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
< specs
; i
++)
34 for (i
= 0; i
< active_nr
; i
++) {
35 struct cache_entry
*ce
= active_cache
[i
];
36 match_pathspec(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 char **pathspec
)
53 for (i
= 0; pathspec
[i
]; i
++)
56 add_pathspec_matches_against_index(pathspec
, seen
, i
);
63 * Possible future magic semantics include stuff like:
65 * { PATHSPEC_NOGLOB, '!', "noglob" },
66 * { PATHSPEC_ICASE, '\0', "icase" },
67 * { PATHSPEC_RECURSIVE, '*', "recursive" },
68 * { PATHSPEC_REGEXP, '\0', "regexp" },
72 static struct pathspec_magic
{
74 char mnemonic
; /* this cannot be ':'! */
76 } pathspec_magic
[] = {
77 { PATHSPEC_FROMTOP
, '/', "top" },
81 * Take an element of a pathspec and check for magic signatures.
82 * Append the result to the prefix. Return the magic bitmap.
84 * For now, we only parse the syntax and throw out anything other than
87 * NEEDSWORK: This needs to be rewritten when we start migrating
88 * get_pathspec() users to use the "struct pathspec" interface. For
89 * example, a pathspec element may be marked as case-insensitive, but
90 * the prefix part must always match literally, and a single stupid
91 * string cannot express such a case.
93 static unsigned prefix_pathspec(struct pathspec_item
*item
,
94 unsigned *p_short_magic
,
95 const char **raw
, unsigned flags
,
96 const char *prefix
, int prefixlen
,
99 unsigned magic
= 0, short_magic
= 0;
100 const char *copyfrom
= elt
;
105 ; /* nothing to do */
106 } else if (elt
[1] == '(') {
109 for (copyfrom
= elt
+ 2;
110 *copyfrom
&& *copyfrom
!= ')';
112 size_t len
= strcspn(copyfrom
, ",)");
113 if (copyfrom
[len
] == ',')
114 nextat
= copyfrom
+ len
+ 1;
116 /* handle ')' and '\0' */
117 nextat
= copyfrom
+ len
;
120 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
121 if (strlen(pathspec_magic
[i
].name
) == len
&&
122 !strncmp(pathspec_magic
[i
].name
, copyfrom
, len
)) {
123 magic
|= pathspec_magic
[i
].bit
;
126 if (ARRAY_SIZE(pathspec_magic
) <= i
)
127 die(_("Invalid pathspec magic '%.*s' in '%s'"),
128 (int) len
, copyfrom
, elt
);
130 if (*copyfrom
!= ')')
131 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt
);
135 for (copyfrom
= elt
+ 1;
136 *copyfrom
&& *copyfrom
!= ':';
140 if (!is_pathspec_magic(ch
))
142 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
143 if (pathspec_magic
[i
].mnemonic
== ch
) {
144 short_magic
|= pathspec_magic
[i
].bit
;
147 if (ARRAY_SIZE(pathspec_magic
) <= i
)
148 die(_("Unimplemented pathspec magic '%c' in '%s'"),
151 if (*copyfrom
== ':')
155 magic
|= short_magic
;
156 *p_short_magic
= short_magic
;
158 if (magic
& PATHSPEC_FROMTOP
)
159 match
= xstrdup(copyfrom
);
161 match
= prefix_path(prefix
, prefixlen
, copyfrom
);
162 *raw
= item
->match
= match
;
164 * Prefix the pathspec (keep all magic) and assign to
165 * original. Useful for passing to another command.
167 if (flags
& PATHSPEC_PREFIX_ORIGIN
) {
168 struct strbuf sb
= STRBUF_INIT
;
169 strbuf_add(&sb
, elt
, copyfrom
- elt
);
170 strbuf_addstr(&sb
, match
);
171 item
->original
= strbuf_detach(&sb
, NULL
);
173 item
->original
= elt
;
174 item
->len
= strlen(item
->match
);
176 if ((flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
) &&
177 (item
->len
>= 1 && item
->match
[item
->len
- 1] == '/') &&
178 (i
= cache_name_pos(item
->match
, item
->len
- 1)) >= 0 &&
179 S_ISGITLINK(active_cache
[i
]->ce_mode
)) {
181 match
[item
->len
] = '\0';
184 if (flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE
)
185 for (i
= 0; i
< active_nr
; i
++) {
186 struct cache_entry
*ce
= active_cache
[i
];
187 int ce_len
= ce_namelen(ce
);
189 if (!S_ISGITLINK(ce
->ce_mode
))
192 if (item
->len
<= ce_len
|| match
[ce_len
] != '/' ||
193 memcmp(ce
->name
, match
, ce_len
))
195 if (item
->len
== ce_len
+ 1) {
196 /* strip trailing slash */
198 match
[item
->len
] = '\0';
200 die (_("Pathspec '%s' is in submodule '%.*s'"),
201 elt
, ce_len
, ce
->name
);
204 if (limit_pathspec_to_literal())
205 item
->nowildcard_len
= item
->len
;
207 item
->nowildcard_len
= simple_length(item
->match
);
209 if (item
->nowildcard_len
< item
->len
&&
210 item
->match
[item
->nowildcard_len
] == '*' &&
211 no_wildcard(item
->match
+ item
->nowildcard_len
+ 1))
212 item
->flags
|= PATHSPEC_ONESTAR
;
216 static int pathspec_item_cmp(const void *a_
, const void *b_
)
218 struct pathspec_item
*a
, *b
;
220 a
= (struct pathspec_item
*)a_
;
221 b
= (struct pathspec_item
*)b_
;
222 return strcmp(a
->match
, b
->match
);
225 static void NORETURN
unsupported_magic(const char *pattern
,
227 unsigned short_magic
)
229 struct strbuf sb
= STRBUF_INIT
;
231 for (n
= i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
232 const struct pathspec_magic
*m
= pathspec_magic
+ i
;
233 if (!(magic
& m
->bit
))
236 strbuf_addstr(&sb
, " ");
237 if (short_magic
& m
->bit
)
238 strbuf_addf(&sb
, "'%c'", m
->mnemonic
);
240 strbuf_addf(&sb
, "'%s'", m
->name
);
244 * We may want to substitute "this command" with a command
245 * name. E.g. when add--interactive dies when running
248 die(_("%s: pathspec magic not supported by this command: %s"),
253 * Given command line arguments and a prefix, convert the input to
254 * pathspec. die() if any magic in magic_mask is used.
256 void parse_pathspec(struct pathspec
*pathspec
,
257 unsigned magic_mask
, unsigned flags
,
258 const char *prefix
, const char **argv
)
260 struct pathspec_item
*item
;
261 const char *entry
= argv
? *argv
: NULL
;
264 memset(pathspec
, 0, sizeof(*pathspec
));
266 if (flags
& PATHSPEC_MAXDEPTH_VALID
)
267 pathspec
->magic
|= PATHSPEC_MAXDEPTH
;
269 /* No arguments, no prefix -> no pathspec */
270 if (!entry
&& !prefix
)
273 if ((flags
& PATHSPEC_PREFER_CWD
) &&
274 (flags
& PATHSPEC_PREFER_FULL
))
275 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
277 /* No arguments with prefix -> prefix pathspec */
279 static const char *raw
[2];
281 if (flags
& PATHSPEC_PREFER_FULL
)
284 if (!(flags
& PATHSPEC_PREFER_CWD
))
285 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
287 pathspec
->items
= item
= xmalloc(sizeof(*item
));
288 memset(item
, 0, sizeof(*item
));
289 item
->match
= prefix
;
290 item
->original
= prefix
;
291 item
->nowildcard_len
= item
->len
= strlen(prefix
);
304 pathspec
->items
= item
= xmalloc(sizeof(*item
) * n
);
305 pathspec
->raw
= argv
;
306 prefixlen
= prefix
? strlen(prefix
) : 0;
308 for (i
= 0; i
< n
; i
++) {
309 unsigned short_magic
;
312 item
[i
].magic
= prefix_pathspec(item
+ i
, &short_magic
,
314 prefix
, prefixlen
, entry
);
315 if (item
[i
].magic
& magic_mask
)
316 unsupported_magic(entry
,
317 item
[i
].magic
& magic_mask
,
320 if ((flags
& PATHSPEC_SYMLINK_LEADING_PATH
) &&
321 has_symlink_leading_path(item
[i
].match
, item
[i
].len
)) {
322 die(_("pathspec '%s' is beyond a symbolic link"), entry
);
325 if (item
[i
].nowildcard_len
< item
[i
].len
)
326 pathspec
->has_wildcard
= 1;
327 pathspec
->magic
|= item
[i
].magic
;
331 if (pathspec
->magic
& PATHSPEC_MAXDEPTH
) {
332 if (flags
& PATHSPEC_KEEP_ORDER
)
333 die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
334 qsort(pathspec
->items
, pathspec
->nr
,
335 sizeof(struct pathspec_item
), pathspec_item_cmp
);
340 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
341 * based interface - see pathspec.c:parse_pathspec().
344 * - prefix - a path relative to the root of the working tree
345 * - pathspec - a list of paths underneath the prefix path
347 * Iterates over pathspec, prepending each path with prefix,
348 * and return the resulting list.
350 * If pathspec is empty, return a singleton list containing prefix.
352 * If pathspec and prefix are both empty, return an empty list.
354 * This is typically used by built-in commands such as add.c, in order
355 * to normalize argv arguments provided to the built-in into a list of
356 * paths to process, all relative to the root of the working tree.
358 const char **get_pathspec(const char *prefix
, const char **pathspec
)
362 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_FROMTOP
,
368 void copy_pathspec(struct pathspec
*dst
, const struct pathspec
*src
)
371 dst
->items
= xmalloc(sizeof(struct pathspec_item
) * dst
->nr
);
372 memcpy(dst
->items
, src
->items
,
373 sizeof(struct pathspec_item
) * dst
->nr
);