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
);
61 * Check the index to see whether path refers to a submodule, or
62 * something inside a submodule. If the former, returns the path with
63 * any trailing slash stripped. If the latter, dies with an error
66 const char *check_path_for_gitlink(const char *path
)
68 int i
, path_len
= strlen(path
);
69 for (i
= 0; i
< active_nr
; i
++) {
70 struct cache_entry
*ce
= active_cache
[i
];
71 if (S_ISGITLINK(ce
->ce_mode
)) {
72 int ce_len
= ce_namelen(ce
);
73 if (path_len
<= ce_len
|| path
[ce_len
] != '/' ||
74 memcmp(ce
->name
, path
, ce_len
))
75 /* path does not refer to this
76 * submodule or anything inside it */
78 if (path_len
== ce_len
+ 1) {
79 /* path refers to submodule;
80 * strip trailing slash */
81 return xstrndup(ce
->name
, ce_len
);
83 die (_("Path '%s' is in submodule '%.*s'"),
84 path
, ce_len
, ce
->name
);
92 * Dies if the given path refers to a file inside a symlinked
93 * directory in the index.
95 void die_if_path_beyond_symlink(const char *path
, const char *prefix
)
97 if (has_symlink_leading_path(path
, strlen(path
))) {
98 int len
= prefix
? strlen(prefix
) : 0;
99 die(_("'%s' is beyond a symbolic link"), path
+ len
);
106 * Possible future magic semantics include stuff like:
108 * { PATHSPEC_NOGLOB, '!', "noglob" },
109 * { PATHSPEC_ICASE, '\0', "icase" },
110 * { PATHSPEC_RECURSIVE, '*', "recursive" },
111 * { PATHSPEC_REGEXP, '\0', "regexp" },
115 static struct pathspec_magic
{
117 char mnemonic
; /* this cannot be ':'! */
119 } pathspec_magic
[] = {
120 { PATHSPEC_FROMTOP
, '/', "top" },
124 * Take an element of a pathspec and check for magic signatures.
125 * Append the result to the prefix. Return the magic bitmap.
127 * For now, we only parse the syntax and throw out anything other than
130 * NEEDSWORK: This needs to be rewritten when we start migrating
131 * get_pathspec() users to use the "struct pathspec" interface. For
132 * example, a pathspec element may be marked as case-insensitive, but
133 * the prefix part must always match literally, and a single stupid
134 * string cannot express such a case.
136 static unsigned prefix_pathspec(struct pathspec_item
*item
,
137 unsigned *p_short_magic
,
138 const char **raw
, unsigned flags
,
139 const char *prefix
, int prefixlen
,
142 unsigned magic
= 0, short_magic
= 0;
143 const char *copyfrom
= elt
;
148 ; /* nothing to do */
149 } else if (elt
[1] == '(') {
152 for (copyfrom
= elt
+ 2;
153 *copyfrom
&& *copyfrom
!= ')';
155 size_t len
= strcspn(copyfrom
, ",)");
156 if (copyfrom
[len
] == ',')
157 nextat
= copyfrom
+ len
+ 1;
159 /* handle ')' and '\0' */
160 nextat
= copyfrom
+ len
;
163 for (i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++)
164 if (strlen(pathspec_magic
[i
].name
) == len
&&
165 !strncmp(pathspec_magic
[i
].name
, copyfrom
, len
)) {
166 magic
|= pathspec_magic
[i
].bit
;
169 if (ARRAY_SIZE(pathspec_magic
) <= i
)
170 die(_("Invalid pathspec magic '%.*s' in '%s'"),
171 (int) len
, copyfrom
, elt
);
173 if (*copyfrom
!= ')')
174 die(_("Missing ')' at the end of pathspec magic in '%s'"), elt
);
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 if (magic
& PATHSPEC_FROMTOP
)
202 match
= xstrdup(copyfrom
);
204 match
= prefix_path(prefix
, prefixlen
, copyfrom
);
205 *raw
= item
->match
= match
;
207 * Prefix the pathspec (keep all magic) and assign to
208 * original. Useful for passing to another command.
210 if (flags
& PATHSPEC_PREFIX_ORIGIN
) {
211 struct strbuf sb
= STRBUF_INIT
;
212 strbuf_add(&sb
, elt
, copyfrom
- elt
);
213 strbuf_addstr(&sb
, match
);
214 item
->original
= strbuf_detach(&sb
, NULL
);
216 item
->original
= elt
;
217 item
->len
= strlen(item
->match
);
219 if ((flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
) &&
220 (item
->len
>= 1 && item
->match
[item
->len
- 1] == '/') &&
221 (i
= cache_name_pos(item
->match
, item
->len
- 1)) >= 0 &&
222 S_ISGITLINK(active_cache
[i
]->ce_mode
)) {
224 match
[item
->len
] = '\0';
227 if (flags
& PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE
)
228 for (i
= 0; i
< active_nr
; i
++) {
229 struct cache_entry
*ce
= active_cache
[i
];
230 int ce_len
= ce_namelen(ce
);
232 if (!S_ISGITLINK(ce
->ce_mode
))
235 if (item
->len
<= ce_len
|| match
[ce_len
] != '/' ||
236 memcmp(ce
->name
, match
, ce_len
))
238 if (item
->len
== ce_len
+ 1) {
239 /* strip trailing slash */
241 match
[item
->len
] = '\0';
243 die (_("Pathspec '%s' is in submodule '%.*s'"),
244 elt
, ce_len
, ce
->name
);
247 if (limit_pathspec_to_literal())
248 item
->nowildcard_len
= item
->len
;
250 item
->nowildcard_len
= simple_length(item
->match
);
252 if (item
->nowildcard_len
< item
->len
&&
253 item
->match
[item
->nowildcard_len
] == '*' &&
254 no_wildcard(item
->match
+ item
->nowildcard_len
+ 1))
255 item
->flags
|= PATHSPEC_ONESTAR
;
259 static int pathspec_item_cmp(const void *a_
, const void *b_
)
261 struct pathspec_item
*a
, *b
;
263 a
= (struct pathspec_item
*)a_
;
264 b
= (struct pathspec_item
*)b_
;
265 return strcmp(a
->match
, b
->match
);
268 static void NORETURN
unsupported_magic(const char *pattern
,
270 unsigned short_magic
)
272 struct strbuf sb
= STRBUF_INIT
;
274 for (n
= i
= 0; i
< ARRAY_SIZE(pathspec_magic
); i
++) {
275 const struct pathspec_magic
*m
= pathspec_magic
+ i
;
276 if (!(magic
& m
->bit
))
279 strbuf_addstr(&sb
, " ");
280 if (short_magic
& m
->bit
)
281 strbuf_addf(&sb
, "'%c'", m
->mnemonic
);
283 strbuf_addf(&sb
, "'%s'", m
->name
);
287 * We may want to substitute "this command" with a command
288 * name. E.g. when add--interactive dies when running
291 die(_("%s: pathspec magic not supported by this command: %s"),
296 * Given command line arguments and a prefix, convert the input to
297 * pathspec. die() if any magic in magic_mask is used.
299 void parse_pathspec(struct pathspec
*pathspec
,
300 unsigned magic_mask
, unsigned flags
,
301 const char *prefix
, const char **argv
)
303 struct pathspec_item
*item
;
304 const char *entry
= argv
? *argv
: NULL
;
307 memset(pathspec
, 0, sizeof(*pathspec
));
309 if (flags
& PATHSPEC_MAXDEPTH_VALID
)
310 pathspec
->magic
|= PATHSPEC_MAXDEPTH
;
312 /* No arguments, no prefix -> no pathspec */
313 if (!entry
&& !prefix
)
316 if ((flags
& PATHSPEC_PREFER_CWD
) &&
317 (flags
& PATHSPEC_PREFER_FULL
))
318 die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
320 /* No arguments with prefix -> prefix pathspec */
322 static const char *raw
[2];
324 if (flags
& PATHSPEC_PREFER_FULL
)
327 if (!(flags
& PATHSPEC_PREFER_CWD
))
328 die("BUG: PATHSPEC_PREFER_CWD requires arguments");
330 pathspec
->items
= item
= xmalloc(sizeof(*item
));
331 memset(item
, 0, sizeof(*item
));
332 item
->match
= prefix
;
333 item
->original
= prefix
;
334 item
->nowildcard_len
= item
->len
= strlen(prefix
);
347 pathspec
->items
= item
= xmalloc(sizeof(*item
) * n
);
348 pathspec
->raw
= argv
;
349 prefixlen
= prefix
? strlen(prefix
) : 0;
351 for (i
= 0; i
< n
; i
++) {
352 unsigned short_magic
;
355 item
[i
].magic
= prefix_pathspec(item
+ i
, &short_magic
,
357 prefix
, prefixlen
, entry
);
358 if (item
[i
].magic
& magic_mask
)
359 unsupported_magic(entry
,
360 item
[i
].magic
& magic_mask
,
363 if ((flags
& PATHSPEC_SYMLINK_LEADING_PATH
) &&
364 has_symlink_leading_path(item
[i
].match
, item
[i
].len
)) {
365 die(_("pathspec '%s' is beyond a symbolic link"), entry
);
368 if (item
[i
].nowildcard_len
< item
[i
].len
)
369 pathspec
->has_wildcard
= 1;
370 pathspec
->magic
|= item
[i
].magic
;
373 if (pathspec
->magic
& PATHSPEC_MAXDEPTH
)
374 qsort(pathspec
->items
, pathspec
->nr
,
375 sizeof(struct pathspec_item
), pathspec_item_cmp
);
379 * N.B. get_pathspec() is deprecated in favor of the "struct pathspec"
380 * based interface - see pathspec.c:parse_pathspec().
383 * - prefix - a path relative to the root of the working tree
384 * - pathspec - a list of paths underneath the prefix path
386 * Iterates over pathspec, prepending each path with prefix,
387 * and return the resulting list.
389 * If pathspec is empty, return a singleton list containing prefix.
391 * If pathspec and prefix are both empty, return an empty list.
393 * This is typically used by built-in commands such as add.c, in order
394 * to normalize argv arguments provided to the built-in into a list of
395 * paths to process, all relative to the root of the working tree.
397 const char **get_pathspec(const char *prefix
, const char **pathspec
)
401 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_FROMTOP
,
407 void copy_pathspec(struct pathspec
*dst
, const struct pathspec
*src
)
410 dst
->items
= xmalloc(sizeof(struct pathspec_item
) * dst
->nr
);
411 memcpy(dst
->items
, src
->items
,
412 sizeof(struct pathspec_item
) * dst
->nr
);