🌐 AI搜索 & 代理 主页
blob: bb1efe1f3929f116a216de5cae6fa6b83227569e [file] [log] [blame]
Elijah Newrenbc5c5ec2023-05-16 06:33:571#include "git-compat-util.h"
Elijah Newren0b027f62023-03-21 06:25:582#include "abspath.h"
Calvin Wanb1bda752023-09-29 21:20:513#include "parse.h"
Adam Spiers6f525e72013-01-06 16:58:084#include "dir.h"
Elijah Newren32a8f512023-03-21 06:26:035#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:546#include "gettext.h"
Adam Spiers6f525e72013-01-06 16:58:087#include "pathspec.h"
Brandon Williamsb0db7042017-03-13 18:23:218#include "attr.h"
Elijah Newren08c46a42023-05-16 06:33:569#include "read-cache.h"
Elijah Newrend1cbe1e2023-04-22 20:17:2010#include "repository.h"
Elijah Newrene38da482023-03-21 06:26:0511#include "setup.h"
Jeff Kingdbbcd442020-07-28 20:23:3912#include "strvec.h"
Elijah Newrencb2a5132023-04-22 20:17:0913#include "symlinks.h"
Alexandr Miloslavskiy24e47502019-11-19 16:48:5114#include "quote.h"
Elijah Newrendd77d582023-05-16 06:34:0315#include "wildmatch.h"
Adam Spiers6f525e72013-01-06 16:58:0816
17/*
18 * Finds which of the given pathspecs match items in the index.
19 *
20 * For each pathspec, sets the corresponding entry in the seen[] array
21 * (which should be specs items long, i.e. the same size as pathspec)
22 * to the nature of the "closest" (i.e. most specific) match found for
23 * that pathspec in the index, if it was a closer type of match than
24 * the existing entry. As an optimization, matching is skipped
25 * altogether if seen[] already only contains non-zero entries.
26 *
27 * If seen[] has not already been written to, it may make sense
Adam Spiers4b78d7b2013-01-06 16:58:0928 * to use find_pathspecs_matching_against_index() instead.
Adam Spiers6f525e72013-01-06 16:58:0829 */
Nguyễn Thái Ngọc Duy84b8b5d2013-07-14 08:36:0030void add_pathspec_matches_against_index(const struct pathspec *pathspec,
Derrick Stolee847a9e52021-04-01 01:49:3931 struct index_state *istate,
Matheus Tavares719630e2021-04-08 20:41:2532 char *seen,
33 enum ps_skip_worktree_action sw_action)
Adam Spiers6f525e72013-01-06 16:58:0834{
35 int num_unmatched = 0, i;
36
37 /*
38 * Since we are walking the index as if we were walking the directory,
39 * we have to mark the matched pathspec as seen; otherwise we will
40 * mistakenly think that the user gave a pathspec that did not match
41 * anything.
42 */
Nguyễn Thái Ngọc Duy84b8b5d2013-07-14 08:36:0043 for (i = 0; i < pathspec->nr; i++)
Adam Spiers6f525e72013-01-06 16:58:0844 if (!seen[i])
45 num_unmatched++;
46 if (!num_unmatched)
47 return;
Brandon Williams08de9152017-05-11 22:04:2748 for (i = 0; i < istate->cache_nr; i++) {
49 const struct cache_entry *ce = istate->cache[i];
Derrick Stolee49fdd512021-09-24 15:39:0750 if (sw_action == PS_IGNORE_SKIP_WORKTREE &&
51 (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)))
Matheus Tavares719630e2021-04-08 20:41:2552 continue;
Nguyễn Thái Ngọc Duyd17ef3a2018-08-13 16:14:3053 ce_path_match(istate, ce, pathspec, seen);
Adam Spiers6f525e72013-01-06 16:58:0854 }
55}
56
57/*
58 * Finds which of the given pathspecs match items in the index.
59 *
Adam Spiers4b78d7b2013-01-06 16:58:0960 * This is a one-shot wrapper around add_pathspec_matches_against_index()
61 * which allocates, populates, and returns a seen[] array indicating the
62 * nature of the "closest" (i.e. most specific) matches which each of the
63 * given pathspecs achieves against all items in the index.
Adam Spiers6f525e72013-01-06 16:58:0864 */
Brandon Williams08de9152017-05-11 22:04:2765char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
Junio C Hamanofe069dc2021-05-07 03:47:3966 struct index_state *istate,
Matheus Tavares719630e2021-04-08 20:41:2567 enum ps_skip_worktree_action sw_action)
Adam Spiers6f525e72013-01-06 16:58:0868{
Nguyễn Thái Ngọc Duy84b8b5d2013-07-14 08:36:0069 char *seen = xcalloc(pathspec->nr, 1);
Matheus Tavares719630e2021-04-08 20:41:2570 add_pathspec_matches_against_index(pathspec, istate, seen, sw_action);
Adam Spiers6f525e72013-01-06 16:58:0871 return seen;
72}
Adam Spiers9d67b612013-01-06 16:58:1073
Matheus Tavaresa20f7042021-04-08 20:41:2774char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec)
75{
76 struct index_state *istate = the_repository->index;
77 char *seen = xcalloc(pathspec->nr, 1);
78 int i;
79
80 for (i = 0; i < istate->cache_nr; i++) {
81 struct cache_entry *ce = istate->cache[i];
Derrick Stolee49fdd512021-09-24 15:39:0782 if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))
Matheus Tavaresa20f7042021-04-08 20:41:2783 ce_path_match(istate, ce, pathspec, seen);
84 }
85
Adam Spiers6f525e72013-01-06 16:58:0886 return seen;
87}
Adam Spiers9d67b612013-01-06 16:58:1088
89/*
Nguyễn Thái Ngọc Duy64acde92013-07-14 08:35:2590 * Magic pathspec
91 *
Nguyễn Thái Ngọc Duy64acde92013-07-14 08:35:2592 * Possible future magic semantics include stuff like:
93 *
Nguyễn Thái Ngọc Duy64acde92013-07-14 08:35:2594 * { PATHSPEC_RECURSIVE, '*', "recursive" },
95 * { PATHSPEC_REGEXP, '\0', "regexp" },
96 *
Adam Spiers9d67b612013-01-06 16:58:1097 */
Nguyễn Thái Ngọc Duy64acde92013-07-14 08:35:2598
99static struct pathspec_magic {
100 unsigned bit;
101 char mnemonic; /* this cannot be ':'! */
102 const char *name;
103} pathspec_magic[] = {
Brandon Williams4f1bf4d2017-01-04 18:04:10104 { PATHSPEC_FROMTOP, '/', "top" },
105 { PATHSPEC_LITERAL, '\0', "literal" },
106 { PATHSPEC_GLOB, '\0', "glob" },
107 { PATHSPEC_ICASE, '\0', "icase" },
108 { PATHSPEC_EXCLUDE, '!', "exclude" },
Brandon Williamsb0db7042017-03-13 18:23:21109 { PATHSPEC_ATTR, '\0', "attr" },
Nguyễn Thái Ngọc Duy64acde92013-07-14 08:35:25110};
111
Brandon Williams5d8f0842017-01-04 18:04:04112static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
Nguyễn Thái Ngọc Duy16496122013-12-06 07:30:49113{
114 int i;
115 strbuf_addstr(sb, ":(");
116 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
Brandon Williams5d8f0842017-01-04 18:04:04117 if (magic & pathspec_magic[i].bit) {
Nguyễn Thái Ngọc Duy16496122013-12-06 07:30:49118 if (sb->buf[sb->len - 1] != '(')
119 strbuf_addch(sb, ',');
120 strbuf_addstr(sb, pathspec_magic[i].name);
121 }
122 strbuf_addf(sb, ",prefix:%d)", prefixlen);
123}
124
Brandon Williamsc5af19f2017-03-13 18:23:22125static size_t strcspn_escaped(const char *s, const char *stop)
126{
127 const char *i;
128
129 for (i = s; *i; i++) {
130 /* skip the escaped character */
131 if (i[0] == '\\' && i[1]) {
132 i++;
133 continue;
134 }
135
136 if (strchr(stop, *i))
137 break;
138 }
139 return i - s;
140}
141
142static inline int invalid_value_char(const char ch)
143{
144 if (isalnum(ch) || strchr(",-_", ch))
145 return 0;
146 return -1;
147}
148
149static char *attr_value_unescape(const char *value)
150{
151 const char *src;
152 char *dst, *ret;
153
154 ret = xmallocz(strlen(value));
155 for (src = value, dst = ret; *src; src++, dst++) {
156 if (*src == '\\') {
157 if (!src[1])
158 die(_("Escape character '\\' not allowed as "
159 "last character in attr value"));
160 src++;
161 }
162 if (invalid_value_char(*src))
163 die("cannot use '%c' for value matching", *src);
164 *dst = *src;
165 }
166 *dst = '\0';
167 return ret;
168}
169
Brandon Williamsb0db7042017-03-13 18:23:21170static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
171{
172 struct string_list_item *si;
173 struct string_list list = STRING_LIST_INIT_DUP;
174
175 if (item->attr_check || item->attr_match)
176 die(_("Only one 'attr:' specification is allowed."));
177
178 if (!value || !*value)
179 die(_("attr spec must not be empty"));
180
181 string_list_split(&list, value, ' ', -1);
182 string_list_remove_empty_items(&list, 0);
183
184 item->attr_check = attr_check_alloc();
René Scharfeca56dad2021-03-13 16:17:22185 CALLOC_ARRAY(item->attr_match, list.nr);
Brandon Williamsb0db7042017-03-13 18:23:21186
187 for_each_string_list_item(si, &list) {
188 size_t attr_len;
189 char *attr_name;
190 const struct git_attr *a;
191
192 int j = item->attr_match_nr++;
193 const char *attr = si->string;
194 struct attr_match *am = &item->attr_match[j];
195
196 switch (*attr) {
197 case '!':
198 am->match_mode = MATCH_UNSPECIFIED;
199 attr++;
200 attr_len = strlen(attr);
201 break;
202 case '-':
203 am->match_mode = MATCH_UNSET;
204 attr++;
205 attr_len = strlen(attr);
206 break;
207 default:
208 attr_len = strcspn(attr, "=");
209 if (attr[attr_len] != '=')
210 am->match_mode = MATCH_SET;
211 else {
Brandon Williamsc5af19f2017-03-13 18:23:22212 const char *v = &attr[attr_len + 1];
Brandon Williamsb0db7042017-03-13 18:23:21213 am->match_mode = MATCH_VALUE;
Brandon Williamsc5af19f2017-03-13 18:23:22214 am->value = attr_value_unescape(v);
Brandon Williamsb0db7042017-03-13 18:23:21215 }
216 break;
217 }
218
219 attr_name = xmemdupz(attr, attr_len);
220 a = git_attr(attr_name);
221 if (!a)
222 die(_("invalid attribute name %s"), attr_name);
223
224 attr_check_append(item->attr_check, a);
225
226 free(attr_name);
227 }
228
229 if (item->attr_check->nr != item->attr_match_nr)
Johannes Schindelin033abf92018-05-02 09:38:39230 BUG("should have same number of entries");
Brandon Williamsb0db7042017-03-13 18:23:21231
232 string_list_clear(&list, 0);
233}
234
Brandon Williamsdb7e8592017-01-04 18:04:05235static inline int get_literal_global(void)
236{
237 static int literal = -1;
238
239 if (literal < 0)
240 literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
241
242 return literal;
243}
244
245static inline int get_glob_global(void)
246{
247 static int glob = -1;
248
249 if (glob < 0)
250 glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
251
252 return glob;
253}
254
255static inline int get_noglob_global(void)
256{
257 static int noglob = -1;
258
259 if (noglob < 0)
260 noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
261
262 return noglob;
263}
264
265static inline int get_icase_global(void)
266{
267 static int icase = -1;
268
269 if (icase < 0)
270 icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
271
272 return icase;
273}
274
275static int get_global_magic(int element_magic)
276{
277 int global_magic = 0;
278
279 if (get_literal_global())
280 global_magic |= PATHSPEC_LITERAL;
281
282 /* --glob-pathspec is overridden by :(literal) */
283 if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
284 global_magic |= PATHSPEC_GLOB;
285
286 if (get_glob_global() && get_noglob_global())
287 die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
288
289 if (get_icase_global())
290 global_magic |= PATHSPEC_ICASE;
291
292 if ((global_magic & PATHSPEC_LITERAL) &&
293 (global_magic & ~PATHSPEC_LITERAL))
294 die(_("global 'literal' pathspec setting is incompatible "
295 "with all other global pathspec settings"));
296
297 /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
298 if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
299 global_magic |= PATHSPEC_LITERAL;
300
301 return global_magic;
302}
303
Nguyễn Thái Ngọc Duy64acde92013-07-14 08:35:25304/*
Brandon Williams8881fde2017-01-04 18:04:07305 * Parse the pathspec element looking for long magic
306 *
307 * saves all magic in 'magic'
308 * if prefix magic is used, save the prefix length in 'prefix_len'
309 * returns the position in 'elem' after all magic has been parsed
310 */
311static const char *parse_long_magic(unsigned *magic, int *prefix_len,
Brandon Williamsb0db7042017-03-13 18:23:21312 struct pathspec_item *item,
Brandon Williams8881fde2017-01-04 18:04:07313 const char *elem)
314{
315 const char *pos;
316 const char *nextat;
317
318 for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
Brandon Williamsc5af19f2017-03-13 18:23:22319 size_t len = strcspn_escaped(pos, ",)");
Brandon Williams8881fde2017-01-04 18:04:07320 int i;
321
322 if (pos[len] == ',')
323 nextat = pos + len + 1; /* handle ',' */
324 else
325 nextat = pos + len; /* handle ')' and '\0' */
326
327 if (!len)
328 continue;
329
330 if (starts_with(pos, "prefix:")) {
331 char *endptr;
332 *prefix_len = strtol(pos + 7, &endptr, 10);
333 if (endptr - pos != len)
334 die(_("invalid parameter for pathspec magic 'prefix'"));
335 continue;
336 }
337
Brandon Williamsb0db7042017-03-13 18:23:21338 if (starts_with(pos, "attr:")) {
339 char *attr_body = xmemdupz(pos + 5, len - 5);
340 parse_pathspec_attr_match(item, attr_body);
341 *magic |= PATHSPEC_ATTR;
342 free(attr_body);
343 continue;
344 }
345
Brandon Williams8881fde2017-01-04 18:04:07346 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
347 if (strlen(pathspec_magic[i].name) == len &&
348 !strncmp(pathspec_magic[i].name, pos, len)) {
349 *magic |= pathspec_magic[i].bit;
350 break;
351 }
352 }
353
354 if (ARRAY_SIZE(pathspec_magic) <= i)
355 die(_("Invalid pathspec magic '%.*s' in '%s'"),
356 (int) len, pos, elem);
357 }
358
359 if (*pos != ')')
360 die(_("Missing ')' at the end of pathspec magic in '%s'"),
361 elem);
362 pos++;
363
364 return pos;
365}
366
367/*
Brandon Williamsb4bebdc2017-01-04 18:04:06368 * Parse the pathspec element looking for short magic
369 *
370 * saves all magic in 'magic'
371 * returns the position in 'elem' after all magic has been parsed
372 */
373static const char *parse_short_magic(unsigned *magic, const char *elem)
374{
375 const char *pos;
376
377 for (pos = elem + 1; *pos && *pos != ':'; pos++) {
378 char ch = *pos;
379 int i;
380
Linus Torvalds42ebeb92017-02-08 05:05:28381 /* Special case alias for '!' */
382 if (ch == '^') {
383 *magic |= PATHSPEC_EXCLUDE;
384 continue;
385 }
386
Brandon Williamsb4bebdc2017-01-04 18:04:06387 if (!is_pathspec_magic(ch))
388 break;
389
390 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
391 if (pathspec_magic[i].mnemonic == ch) {
392 *magic |= pathspec_magic[i].bit;
393 break;
394 }
395 }
396
397 if (ARRAY_SIZE(pathspec_magic) <= i)
398 die(_("Unimplemented pathspec magic '%c' in '%s'"),
399 ch, elem);
400 }
401
402 if (*pos == ':')
403 pos++;
404
405 return pos;
406}
407
Brandon Williams1b6112c2017-01-04 18:04:08408static const char *parse_element_magic(unsigned *magic, int *prefix_len,
Brandon Williamsb0db7042017-03-13 18:23:21409 struct pathspec_item *item,
Brandon Williams1b6112c2017-01-04 18:04:08410 const char *elem)
411{
412 if (elem[0] != ':' || get_literal_global())
413 return elem; /* nothing to do */
414 else if (elem[1] == '(')
415 /* longhand */
Brandon Williamsb0db7042017-03-13 18:23:21416 return parse_long_magic(magic, prefix_len, item, elem);
Brandon Williams1b6112c2017-01-04 18:04:08417 else
418 /* shorthand */
419 return parse_short_magic(magic, elem);
420}
421
Brandon Williamsb4bebdc2017-01-04 18:04:06422/*
Brandon Williams27ec4282017-01-04 18:04:11423 * Perform the initialization of a pathspec_item based on a pathspec element.
Nguyễn Thái Ngọc Duy64acde92013-07-14 08:35:25424 */
Brandon Williams27ec4282017-01-04 18:04:11425static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
426 const char *prefix, int prefixlen,
427 const char *elt)
Adam Spiers9d67b612013-01-06 16:58:10428{
Brandon Williamsdb7e8592017-01-04 18:04:05429 unsigned magic = 0, element_magic = 0;
Brandon Williams5d8f0842017-01-04 18:04:04430 const char *copyfrom = elt;
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28431 char *match;
Brandon Williams55902152017-01-04 18:04:09432 int pathspec_prefix = -1;
Nguyễn Thái Ngọc Duy64acde92013-07-14 08:35:25433
Brandon Williamsb0db7042017-03-13 18:23:21434 item->attr_check = NULL;
435 item->attr_match = NULL;
436 item->attr_match_nr = 0;
437
Brandon Williamsdb7e8592017-01-04 18:04:05438 /* PATHSPEC_LITERAL_PATH ignores magic */
Brandon Williams1b6112c2017-01-04 18:04:08439 if (flags & PATHSPEC_LITERAL_PATH) {
Brandon Williamsdb7e8592017-01-04 18:04:05440 magic = PATHSPEC_LITERAL;
Brandon Williams1b6112c2017-01-04 18:04:08441 } else {
442 copyfrom = parse_element_magic(&element_magic,
443 &pathspec_prefix,
Brandon Williamsb0db7042017-03-13 18:23:21444 item,
Brandon Williams1b6112c2017-01-04 18:04:08445 elt);
446 magic |= element_magic;
Brandon Williamsdb7e8592017-01-04 18:04:05447 magic |= get_global_magic(element_magic);
Brandon Williams1b6112c2017-01-04 18:04:08448 }
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28449
Brandon Williams27ec4282017-01-04 18:04:11450 item->magic = magic;
451
Nguyễn Thái Ngọc Duy233c3e62013-07-14 08:36:04452 if (pathspec_prefix >= 0 &&
453 (prefixlen || (prefix && *prefix)))
Johannes Schindelin033abf92018-05-02 09:38:39454 BUG("'prefix' magic is supposed to be used at worktree's root");
Nguyễn Thái Ngọc Duy233c3e62013-07-14 08:36:04455
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 08:36:08456 if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
457 die(_("%s: 'literal' and 'glob' are incompatible"), elt);
458
Brandon Williams4f1bf4d2017-01-04 18:04:10459 /* Create match string which will be used for pathspec matching */
Nguyễn Thái Ngọc Duy233c3e62013-07-14 08:36:04460 if (pathspec_prefix >= 0) {
461 match = xstrdup(copyfrom);
462 prefixlen = pathspec_prefix;
463 } else if (magic & PATHSPEC_FROMTOP) {
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28464 match = xstrdup(copyfrom);
Nguyễn Thái Ngọc Duy645a29c2013-07-14 08:36:03465 prefixlen = 0;
466 } else {
Brandon Williams4f1bf4d2017-01-04 18:04:10467 match = prefix_path_gently(prefix, prefixlen,
468 &prefixlen, copyfrom);
Emily Shaffer5c203982020-03-03 04:05:06469 if (!match) {
Kristoffer Haugsbakkb1688ea2023-10-20 16:40:07470 const char *hint_path;
471
472 if (!have_git_dir())
473 die(_("'%s' is outside the directory tree"),
474 copyfrom);
475 hint_path = get_git_work_tree();
Emily Shaffer5c203982020-03-03 04:05:06476 if (!hint_path)
477 hint_path = get_git_dir();
Emily Shaffere0020b22020-02-15 01:00:13478 die(_("%s: '%s' is outside repository at '%s'"), elt,
Emily Shaffer5c203982020-03-03 04:05:06479 copyfrom, absolute_path(hint_path));
480 }
Nguyễn Thái Ngọc Duy645a29c2013-07-14 08:36:03481 }
Brandon Williams4f1bf4d2017-01-04 18:04:10482
Brandon Williams34305f72017-01-04 18:04:00483 item->match = match;
Brandon Williams4f1bf4d2017-01-04 18:04:10484 item->len = strlen(item->match);
485 item->prefix = prefixlen;
486
Nguyễn Thái Ngọc Duydad25862013-07-14 08:35:35487 /*
488 * Prefix the pathspec (keep all magic) and assign to
489 * original. Useful for passing to another command.
490 */
Brandon Williams5d8f0842017-01-04 18:04:04491 if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
Patrick Steinhardtbe4dbbb2017-04-04 09:16:56492 !get_literal_global()) {
Nguyễn Thái Ngọc Duydad25862013-07-14 08:35:35493 struct strbuf sb = STRBUF_INIT;
Brandon Williams5d8f0842017-01-04 18:04:04494
495 /* Preserve the actual prefix length of each pattern */
496 prefix_magic(&sb, prefixlen, element_magic);
497
Nguyễn Thái Ngọc Duydad25862013-07-14 08:35:35498 strbuf_addstr(&sb, match);
499 item->original = strbuf_detach(&sb, NULL);
Brandon Williams8aee7692017-01-04 18:04:01500 } else {
501 item->original = xstrdup(elt);
502 }
Nguyễn Thái Ngọc Duyb69bb3f2013-07-14 08:35:33503
Brandon Williams4f1bf4d2017-01-04 18:04:10504 if (magic & PATHSPEC_LITERAL) {
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28505 item->nowildcard_len = item->len;
Brandon Williams4f1bf4d2017-01-04 18:04:10506 } else {
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28507 item->nowildcard_len = simple_length(item->match);
Nguyễn Thái Ngọc Duy645a29c2013-07-14 08:36:03508 if (item->nowildcard_len < prefixlen)
509 item->nowildcard_len = prefixlen;
510 }
Brandon Williams4f1bf4d2017-01-04 18:04:10511
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28512 item->flags = 0;
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 08:36:08513 if (magic & PATHSPEC_GLOB) {
514 /*
515 * FIXME: should we enable ONESTAR in _GLOB for
516 * pattern "* * / * . c"?
517 */
518 } else {
519 if (item->nowildcard_len < item->len &&
520 item->match[item->nowildcard_len] == '*' &&
521 no_wildcard(item->match + item->nowildcard_len + 1))
522 item->flags |= PATHSPEC_ONESTAR;
523 }
Nguyễn Thái Ngọc Duy645a29c2013-07-14 08:36:03524
525 /* sanity checks, pathspec matchers assume these are sane */
Stefan Beller2d81c482017-01-09 23:16:50526 if (item->nowildcard_len > item->len ||
527 item->prefix > item->len) {
Johannes Schindelinc3c34862018-05-02 09:38:41528 BUG("error initializing pathspec_item");
Stefan Beller2d81c482017-01-09 23:16:50529 }
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28530}
531
532static int pathspec_item_cmp(const void *a_, const void *b_)
533{
534 struct pathspec_item *a, *b;
535
536 a = (struct pathspec_item *)a_;
537 b = (struct pathspec_item *)b_;
538 return strcmp(a->match, b->match);
539}
540
Jeff King8e32caa2023-06-01 17:38:14541void pathspec_magic_names(unsigned magic, struct strbuf *out)
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28542{
Brandon Williams93f3ddb2017-01-04 18:04:02543 int i;
544 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28545 const struct pathspec_magic *m = pathspec_magic + i;
546 if (!(magic & m->bit))
547 continue;
Jeff King8e32caa2023-06-01 17:38:14548 if (out->len)
549 strbuf_addstr(out, ", ");
Brandon Williams2aee5842017-01-04 18:04:03550
551 if (m->mnemonic)
Jeff King8e32caa2023-06-01 17:38:14552 strbuf_addf(out, _("'%s' (mnemonic: '%c')"),
Brandon Williams2aee5842017-01-04 18:04:03553 m->name, m->mnemonic);
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28554 else
Jeff King8e32caa2023-06-01 17:38:14555 strbuf_addf(out, "'%s'", m->name);
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28556 }
Jeff King8e32caa2023-06-01 17:38:14557}
558
559static void NORETURN unsupported_magic(const char *pattern,
560 unsigned magic)
561{
562 struct strbuf sb = STRBUF_INIT;
563 pathspec_magic_names(magic, &sb);
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28564 /*
565 * We may want to substitute "this command" with a command
Ævar Arnfjörð Bjarmason5a7d41d2023-02-06 22:58:58566 * name. E.g. when "git add -p" or "git add -i" dies when running
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28567 * "checkout -p"
568 */
569 die(_("%s: pathspec magic not supported by this command: %s"),
570 pattern, sb.buf);
Adam Spiers9d67b612013-01-06 16:58:10571}
Adam Spiers512aaf92013-01-06 16:58:11572
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28573void parse_pathspec(struct pathspec *pathspec,
574 unsigned magic_mask, unsigned flags,
575 const char *prefix, const char **argv)
Adam Spiers512aaf92013-01-06 16:58:11576{
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28577 struct pathspec_item *item;
578 const char *entry = argv ? *argv : NULL;
Emily Xie9e4e8a62017-06-07 03:33:08579 int i, n, prefixlen, nr_exclude = 0;
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28580
581 memset(pathspec, 0, sizeof(*pathspec));
582
Nguyễn Thái Ngọc Duy6330a172013-07-14 08:35:32583 if (flags & PATHSPEC_MAXDEPTH_VALID)
584 pathspec->magic |= PATHSPEC_MAXDEPTH;
585
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28586 /* No arguments, no prefix -> no pathspec */
587 if (!entry && !prefix)
588 return;
589
Nguyễn Thái Ngọc Duyfc122612013-07-14 08:35:30590 if ((flags & PATHSPEC_PREFER_CWD) &&
591 (flags & PATHSPEC_PREFER_FULL))
Johannes Schindelin033abf92018-05-02 09:38:39592 BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
Nguyễn Thái Ngọc Duyfc122612013-07-14 08:35:30593
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28594 /* No arguments with prefix -> prefix pathspec */
595 if (!entry) {
Nguyễn Thái Ngọc Duyfc122612013-07-14 08:35:30596 if (flags & PATHSPEC_PREFER_FULL)
597 return;
598
599 if (!(flags & PATHSPEC_PREFER_CWD))
Johannes Schindelin033abf92018-05-02 09:38:39600 BUG("PATHSPEC_PREFER_CWD requires arguments");
Nguyễn Thái Ngọc Duyfc122612013-07-14 08:35:30601
René Scharfeca56dad2021-03-13 16:17:22602 pathspec->items = CALLOC_ARRAY(item, 1);
Brandon Williams8aee7692017-01-04 18:04:01603 item->match = xstrdup(prefix);
604 item->original = xstrdup(prefix);
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28605 item->nowildcard_len = item->len = strlen(prefix);
Nguyễn Thái Ngọc Duy645a29c2013-07-14 08:36:03606 item->prefix = item->len;
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28607 pathspec->nr = 1;
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28608 return;
Adam Spiers512aaf92013-01-06 16:58:11609 }
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28610
611 n = 0;
Emily Xied4264302016-06-22 23:00:24612 while (argv[n]) {
Emily Xie9e4e8a62017-06-07 03:33:08613 if (*argv[n] == '\0')
614 die("empty string is not a valid pathspec. "
615 "please use . instead if you meant to match all paths");
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28616 n++;
Emily Xied4264302016-06-22 23:00:24617 }
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28618
619 pathspec->nr = n;
Linus Torvalds859b7f12017-02-08 05:08:15620 ALLOC_ARRAY(pathspec->items, n + 1);
Jeff Kingb32fa952016-02-22 22:44:25621 item = pathspec->items;
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28622 prefixlen = prefix ? strlen(prefix) : 0;
623
624 for (i = 0; i < n; i++) {
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28625 entry = argv[i];
626
Brandon Williams27ec4282017-01-04 18:04:11627 init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
Brandon Williamsdb7e8592017-01-04 18:04:05628
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 07:30:48629 if (item[i].magic & PATHSPEC_EXCLUDE)
630 nr_exclude++;
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28631 if (item[i].magic & magic_mask)
Brandon Williams2aee5842017-01-04 18:04:03632 unsupported_magic(entry, item[i].magic & magic_mask);
Nguyễn Thái Ngọc Duy87450242013-07-14 08:35:34633
634 if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
635 has_symlink_leading_path(item[i].match, item[i].len)) {
636 die(_("pathspec '%s' is beyond a symbolic link"), entry);
637 }
638
Nguyễn Thái Ngọc Duy87323bd2013-07-14 08:35:28639 if (item[i].nowildcard_len < item[i].len)
640 pathspec->has_wildcard = 1;
641 pathspec->magic |= item[i].magic;
642 }
643
Linus Torvalds859b7f12017-02-08 05:08:15644 /*
645 * If everything is an exclude pattern, add one positive pattern
Ville Skyttä64127572017-06-25 10:20:41646 * that matches everything. We allocated an extra one for this.
Linus Torvalds859b7f12017-02-08 05:08:15647 */
648 if (nr_exclude == n) {
649 int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
Junio C Hamanob02fdbc2022-05-29 22:39:51650 init_pathspec_item(item + n, 0, prefix, plen, ".");
Linus Torvalds859b7f12017-02-08 05:08:15651 pathspec->nr++;
652 }
Nguyễn Thái Ngọc Duy931eab62013-07-14 08:35:45653
654 if (pathspec->magic & PATHSPEC_MAXDEPTH) {
655 if (flags & PATHSPEC_KEEP_ORDER)
Johannes Schindelin033abf92018-05-02 09:38:39656 BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
René Scharfe9ed0d8d2016-09-29 15:27:31657 QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
Nguyễn Thái Ngọc Duy931eab62013-07-14 08:35:45658 }
Nguyễn Thái Ngọc Duy64acde92013-07-14 08:35:25659}
660
Alexandr Miloslavskiy24e47502019-11-19 16:48:51661void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
662 unsigned flags, const char *prefix,
663 const char *file, int nul_term_line)
664{
Jeff Kingc972bf42020-07-28 20:25:12665 struct strvec parsed_file = STRVEC_INIT;
Alexandr Miloslavskiy24e47502019-11-19 16:48:51666 strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :
667 strbuf_getline;
668 struct strbuf buf = STRBUF_INIT;
669 struct strbuf unquoted = STRBUF_INIT;
670 FILE *in;
671
672 if (!strcmp(file, "-"))
673 in = stdin;
674 else
675 in = xfopen(file, "r");
676
677 while (getline_fn(&buf, in) != EOF) {
678 if (!nul_term_line && buf.buf[0] == '"') {
679 strbuf_reset(&unquoted);
680 if (unquote_c_style(&unquoted, buf.buf, NULL))
681 die(_("line is badly quoted: %s"), buf.buf);
682 strbuf_swap(&buf, &unquoted);
683 }
Jeff Kingc972bf42020-07-28 20:25:12684 strvec_push(&parsed_file, buf.buf);
Alexandr Miloslavskiy24e47502019-11-19 16:48:51685 strbuf_reset(&buf);
686 }
687
688 strbuf_release(&unquoted);
689 strbuf_release(&buf);
690 if (in != stdin)
691 fclose(in);
692
Jeff Kingd70a9eb2020-07-29 00:37:20693 parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v);
Jeff Kingc972bf42020-07-28 20:25:12694 strvec_clear(&parsed_file);
Alexandr Miloslavskiy24e47502019-11-19 16:48:51695}
696
Nguyễn Thái Ngọc Duye4d92cd2013-07-14 08:35:27697void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
698{
Brandon Williamsb0db7042017-03-13 18:23:21699 int i, j;
Brandon Williams8aee7692017-01-04 18:04:01700
Nguyễn Thái Ngọc Duye4d92cd2013-07-14 08:35:27701 *dst = *src;
René Scharfe6e578412023-01-01 21:16:48702 DUP_ARRAY(dst->items, src->items, dst->nr);
Brandon Williams8aee7692017-01-04 18:04:01703
704 for (i = 0; i < dst->nr; i++) {
Brandon Williamsb0db7042017-03-13 18:23:21705 struct pathspec_item *d = &dst->items[i];
706 struct pathspec_item *s = &src->items[i];
707
708 d->match = xstrdup(s->match);
709 d->original = xstrdup(s->original);
710
René Scharfe6e578412023-01-01 21:16:48711 DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
Brandon Williamsb0db7042017-03-13 18:23:21712 for (j = 0; j < d->attr_match_nr; j++) {
713 const char *value = s->attr_match[j].value;
714 d->attr_match[j].value = xstrdup_or_null(value);
715 }
716
717 d->attr_check = attr_check_dup(s->attr_check);
Brandon Williams8aee7692017-01-04 18:04:01718 }
Nguyễn Thái Ngọc Duye4d92cd2013-07-14 08:35:27719}
Nguyễn Thái Ngọc Duy9a087272013-07-14 08:35:59720
Junio C Hamanoed6e8032016-06-02 21:09:22721void clear_pathspec(struct pathspec *pathspec)
Nguyễn Thái Ngọc Duy9a087272013-07-14 08:35:59722{
Brandon Williamsb0db7042017-03-13 18:23:21723 int i, j;
Brandon Williams8aee7692017-01-04 18:04:01724
725 for (i = 0; i < pathspec->nr; i++) {
726 free(pathspec->items[i].match);
727 free(pathspec->items[i].original);
Brandon Williamsb0db7042017-03-13 18:23:21728
Brandon Williams5ce10c02017-04-07 19:29:19729 for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
Brandon Williamsb0db7042017-03-13 18:23:21730 free(pathspec->items[i].attr_match[j].value);
731 free(pathspec->items[i].attr_match);
732
733 if (pathspec->items[i].attr_check)
734 attr_check_free(pathspec->items[i].attr_check);
Brandon Williams8aee7692017-01-04 18:04:01735 }
Brandon Williamsb0db7042017-03-13 18:23:21736
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46737 FREE_AND_NULL(pathspec->items);
Brandon Williams8aee7692017-01-04 18:04:01738 pathspec->nr = 0;
Adam Spiers512aaf92013-01-06 16:58:11739}
Nguyễn Thái Ngọc Duy22af33b2018-11-18 16:47:59740
Derrick Stolee847a9e52021-04-01 01:49:39741int match_pathspec_attrs(struct index_state *istate,
Nguyễn Thái Ngọc Duy22af33b2018-11-18 16:47:59742 const char *name, int namelen,
743 const struct pathspec_item *item)
744{
745 int i;
746 char *to_free = NULL;
747
748 if (name[namelen])
749 name = to_free = xmemdupz(name, namelen);
750
John Cai44451a22023-05-06 04:15:29751 git_check_attr(istate, name, item->attr_check);
Nguyễn Thái Ngọc Duy22af33b2018-11-18 16:47:59752
753 free(to_free);
754
755 for (i = 0; i < item->attr_match_nr; i++) {
756 const char *value;
757 int matched;
758 enum attr_match_mode match_mode;
759
760 value = item->attr_check->items[i].value;
761 match_mode = item->attr_match[i].match_mode;
762
763 if (ATTR_TRUE(value))
764 matched = (match_mode == MATCH_SET);
765 else if (ATTR_FALSE(value))
766 matched = (match_mode == MATCH_UNSET);
767 else if (ATTR_UNSET(value))
768 matched = (match_mode == MATCH_UNSPECIFIED);
769 else
770 matched = (match_mode == MATCH_VALUE &&
771 !strcmp(item->attr_match[i].value, value));
772 if (!matched)
773 return 0;
774 }
775
776 return 1;
777}
Shaoxuan Yuanb29ad382022-08-07 04:13:33778
779int pathspec_needs_expanded_index(struct index_state *istate,
780 const struct pathspec *pathspec)
781{
782 unsigned int i, pos;
783 int res = 0;
784 char *skip_worktree_seen = NULL;
785
786 /*
787 * If index is not sparse, no index expansion is needed.
788 */
789 if (!istate->sparse_index)
790 return 0;
791
792 /*
793 * When using a magic pathspec, assume for the sake of simplicity that
794 * the index needs to be expanded to match all matchable files.
795 */
796 if (pathspec->magic)
797 return 1;
798
799 for (i = 0; i < pathspec->nr; i++) {
800 struct pathspec_item item = pathspec->items[i];
801
802 /*
803 * If the pathspec item has a wildcard, the index should be expanded
804 * if the pathspec has the possibility of matching a subset of entries inside
805 * of a sparse directory (but not the entire directory).
806 *
807 * If the pathspec item is a literal path, the index only needs to be expanded
808 * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
809 * expand for in-cone files) and b) it doesn't match any sparse directories
810 * (since we can reset whole sparse directories without expanding them).
811 */
812 if (item.nowildcard_len < item.len) {
813 /*
814 * Special case: if the pattern is a path inside the cone
815 * followed by only wildcards, the pattern cannot match
816 * partial sparse directories, so we know we don't need to
817 * expand the index.
818 *
819 * Examples:
820 * - in-cone/foo***: doesn't need expanded index
821 * - not-in-cone/bar*: may need expanded index
822 * - **.c: may need expanded index
823 */
824 if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len &&
825 path_in_cone_mode_sparse_checkout(item.original, istate))
826 continue;
827
828 for (pos = 0; pos < istate->cache_nr; pos++) {
829 struct cache_entry *ce = istate->cache[pos];
830
831 if (!S_ISSPARSEDIR(ce->ce_mode))
832 continue;
833
834 /*
835 * If the pre-wildcard length is longer than the sparse
836 * directory name and the sparse directory is the first
837 * component of the pathspec, need to expand the index.
838 */
839 if (item.nowildcard_len > ce_namelen(ce) &&
840 !strncmp(item.original, ce->name, ce_namelen(ce))) {
841 res = 1;
842 break;
843 }
844
845 /*
846 * If the pre-wildcard length is shorter than the sparse
847 * directory and the pathspec does not match the whole
848 * directory, need to expand the index.
849 */
850 if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
851 wildmatch(item.original, ce->name, 0)) {
852 res = 1;
853 break;
854 }
855 }
856 } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) &&
857 !matches_skip_worktree(pathspec, i, &skip_worktree_seen))
858 res = 1;
859
860 if (res > 0)
861 break;
862 }
863
864 free(skip_worktree_seen);
865 return res;
866}