Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 3 | #include "dir.h" |
| 4 | #include "pathspec.h" |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 5 | #include "attr.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 6 | #include "strvec.h" |
Alexandr Miloslavskiy | 24e4750 | 2019-11-19 16:48:51 +0000 | [diff] [blame] | 7 | #include "quote.h" |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 8 | |
| 9 | /* |
| 10 | * Finds which of the given pathspecs match items in the index. |
| 11 | * |
| 12 | * For each pathspec, sets the corresponding entry in the seen[] array |
| 13 | * (which should be specs items long, i.e. the same size as pathspec) |
| 14 | * to the nature of the "closest" (i.e. most specific) match found for |
| 15 | * that pathspec in the index, if it was a closer type of match than |
| 16 | * the existing entry. As an optimization, matching is skipped |
| 17 | * altogether if seen[] already only contains non-zero entries. |
| 18 | * |
| 19 | * If seen[] has not already been written to, it may make sense |
Adam Spiers | 4b78d7b | 2013-01-06 16:58:09 +0000 | [diff] [blame] | 20 | * to use find_pathspecs_matching_against_index() instead. |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 21 | */ |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 22 | void add_pathspec_matches_against_index(const struct pathspec *pathspec, |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 23 | struct index_state *istate, |
Matheus Tavares | 719630e | 2021-04-08 17:41:25 -0300 | [diff] [blame] | 24 | char *seen, |
| 25 | enum ps_skip_worktree_action sw_action) |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 26 | { |
| 27 | int num_unmatched = 0, i; |
| 28 | |
| 29 | /* |
| 30 | * Since we are walking the index as if we were walking the directory, |
| 31 | * we have to mark the matched pathspec as seen; otherwise we will |
| 32 | * mistakenly think that the user gave a pathspec that did not match |
| 33 | * anything. |
| 34 | */ |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 35 | for (i = 0; i < pathspec->nr; i++) |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 36 | if (!seen[i]) |
| 37 | num_unmatched++; |
| 38 | if (!num_unmatched) |
| 39 | return; |
Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 40 | for (i = 0; i < istate->cache_nr; i++) { |
| 41 | const struct cache_entry *ce = istate->cache[i]; |
Derrick Stolee | 49fdd51 | 2021-09-24 15:39:07 +0000 | [diff] [blame] | 42 | if (sw_action == PS_IGNORE_SKIP_WORKTREE && |
| 43 | (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))) |
Matheus Tavares | 719630e | 2021-04-08 17:41:25 -0300 | [diff] [blame] | 44 | continue; |
Nguyễn Thái Ngọc Duy | d17ef3a | 2018-08-13 18:14:30 +0200 | [diff] [blame] | 45 | ce_path_match(istate, ce, pathspec, seen); |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Finds which of the given pathspecs match items in the index. |
| 51 | * |
Adam Spiers | 4b78d7b | 2013-01-06 16:58:09 +0000 | [diff] [blame] | 52 | * This is a one-shot wrapper around add_pathspec_matches_against_index() |
| 53 | * which allocates, populates, and returns a seen[] array indicating the |
| 54 | * nature of the "closest" (i.e. most specific) matches which each of the |
| 55 | * given pathspecs achieves against all items in the index. |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 56 | */ |
Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 57 | char *find_pathspecs_matching_against_index(const struct pathspec *pathspec, |
Junio C Hamano | fe069dc | 2021-05-07 12:47:39 +0900 | [diff] [blame] | 58 | struct index_state *istate, |
Matheus Tavares | 719630e | 2021-04-08 17:41:25 -0300 | [diff] [blame] | 59 | enum ps_skip_worktree_action sw_action) |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 60 | { |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 61 | char *seen = xcalloc(pathspec->nr, 1); |
Matheus Tavares | 719630e | 2021-04-08 17:41:25 -0300 | [diff] [blame] | 62 | add_pathspec_matches_against_index(pathspec, istate, seen, sw_action); |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 63 | return seen; |
| 64 | } |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 65 | |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 66 | char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec) |
| 67 | { |
| 68 | struct index_state *istate = the_repository->index; |
| 69 | char *seen = xcalloc(pathspec->nr, 1); |
| 70 | int i; |
| 71 | |
| 72 | for (i = 0; i < istate->cache_nr; i++) { |
| 73 | struct cache_entry *ce = istate->cache[i]; |
Derrick Stolee | 49fdd51 | 2021-09-24 15:39:07 +0000 | [diff] [blame] | 74 | if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)) |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 75 | ce_path_match(istate, ce, pathspec, seen); |
| 76 | } |
| 77 | |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 78 | return seen; |
| 79 | } |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 82 | * Magic pathspec |
| 83 | * |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 84 | * Possible future magic semantics include stuff like: |
| 85 | * |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 86 | * { PATHSPEC_RECURSIVE, '*', "recursive" }, |
| 87 | * { PATHSPEC_REGEXP, '\0', "regexp" }, |
| 88 | * |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 89 | */ |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 90 | |
| 91 | static struct pathspec_magic { |
| 92 | unsigned bit; |
| 93 | char mnemonic; /* this cannot be ':'! */ |
| 94 | const char *name; |
| 95 | } pathspec_magic[] = { |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 96 | { PATHSPEC_FROMTOP, '/', "top" }, |
| 97 | { PATHSPEC_LITERAL, '\0', "literal" }, |
| 98 | { PATHSPEC_GLOB, '\0', "glob" }, |
| 99 | { PATHSPEC_ICASE, '\0', "icase" }, |
| 100 | { PATHSPEC_EXCLUDE, '!', "exclude" }, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 101 | { PATHSPEC_ATTR, '\0', "attr" }, |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 102 | }; |
| 103 | |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 104 | static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic) |
Nguyễn Thái Ngọc Duy | 1649612 | 2013-12-06 14:30:49 +0700 | [diff] [blame] | 105 | { |
| 106 | int i; |
| 107 | strbuf_addstr(sb, ":("); |
| 108 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 109 | if (magic & pathspec_magic[i].bit) { |
Nguyễn Thái Ngọc Duy | 1649612 | 2013-12-06 14:30:49 +0700 | [diff] [blame] | 110 | if (sb->buf[sb->len - 1] != '(') |
| 111 | strbuf_addch(sb, ','); |
| 112 | strbuf_addstr(sb, pathspec_magic[i].name); |
| 113 | } |
| 114 | strbuf_addf(sb, ",prefix:%d)", prefixlen); |
| 115 | } |
| 116 | |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 117 | static size_t strcspn_escaped(const char *s, const char *stop) |
| 118 | { |
| 119 | const char *i; |
| 120 | |
| 121 | for (i = s; *i; i++) { |
| 122 | /* skip the escaped character */ |
| 123 | if (i[0] == '\\' && i[1]) { |
| 124 | i++; |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | if (strchr(stop, *i)) |
| 129 | break; |
| 130 | } |
| 131 | return i - s; |
| 132 | } |
| 133 | |
| 134 | static inline int invalid_value_char(const char ch) |
| 135 | { |
| 136 | if (isalnum(ch) || strchr(",-_", ch)) |
| 137 | return 0; |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | static char *attr_value_unescape(const char *value) |
| 142 | { |
| 143 | const char *src; |
| 144 | char *dst, *ret; |
| 145 | |
| 146 | ret = xmallocz(strlen(value)); |
| 147 | for (src = value, dst = ret; *src; src++, dst++) { |
| 148 | if (*src == '\\') { |
| 149 | if (!src[1]) |
| 150 | die(_("Escape character '\\' not allowed as " |
| 151 | "last character in attr value")); |
| 152 | src++; |
| 153 | } |
| 154 | if (invalid_value_char(*src)) |
| 155 | die("cannot use '%c' for value matching", *src); |
| 156 | *dst = *src; |
| 157 | } |
| 158 | *dst = '\0'; |
| 159 | return ret; |
| 160 | } |
| 161 | |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 162 | static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value) |
| 163 | { |
| 164 | struct string_list_item *si; |
| 165 | struct string_list list = STRING_LIST_INIT_DUP; |
| 166 | |
| 167 | if (item->attr_check || item->attr_match) |
| 168 | die(_("Only one 'attr:' specification is allowed.")); |
| 169 | |
| 170 | if (!value || !*value) |
| 171 | die(_("attr spec must not be empty")); |
| 172 | |
| 173 | string_list_split(&list, value, ' ', -1); |
| 174 | string_list_remove_empty_items(&list, 0); |
| 175 | |
| 176 | item->attr_check = attr_check_alloc(); |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 177 | CALLOC_ARRAY(item->attr_match, list.nr); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 178 | |
| 179 | for_each_string_list_item(si, &list) { |
| 180 | size_t attr_len; |
| 181 | char *attr_name; |
| 182 | const struct git_attr *a; |
| 183 | |
| 184 | int j = item->attr_match_nr++; |
| 185 | const char *attr = si->string; |
| 186 | struct attr_match *am = &item->attr_match[j]; |
| 187 | |
| 188 | switch (*attr) { |
| 189 | case '!': |
| 190 | am->match_mode = MATCH_UNSPECIFIED; |
| 191 | attr++; |
| 192 | attr_len = strlen(attr); |
| 193 | break; |
| 194 | case '-': |
| 195 | am->match_mode = MATCH_UNSET; |
| 196 | attr++; |
| 197 | attr_len = strlen(attr); |
| 198 | break; |
| 199 | default: |
| 200 | attr_len = strcspn(attr, "="); |
| 201 | if (attr[attr_len] != '=') |
| 202 | am->match_mode = MATCH_SET; |
| 203 | else { |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 204 | const char *v = &attr[attr_len + 1]; |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 205 | am->match_mode = MATCH_VALUE; |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 206 | am->value = attr_value_unescape(v); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 207 | } |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | attr_name = xmemdupz(attr, attr_len); |
| 212 | a = git_attr(attr_name); |
| 213 | if (!a) |
| 214 | die(_("invalid attribute name %s"), attr_name); |
| 215 | |
| 216 | attr_check_append(item->attr_check, a); |
| 217 | |
| 218 | free(attr_name); |
| 219 | } |
| 220 | |
| 221 | if (item->attr_check->nr != item->attr_match_nr) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 222 | BUG("should have same number of entries"); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 223 | |
| 224 | string_list_clear(&list, 0); |
| 225 | } |
| 226 | |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 227 | static inline int get_literal_global(void) |
| 228 | { |
| 229 | static int literal = -1; |
| 230 | |
| 231 | if (literal < 0) |
| 232 | literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0); |
| 233 | |
| 234 | return literal; |
| 235 | } |
| 236 | |
| 237 | static inline int get_glob_global(void) |
| 238 | { |
| 239 | static int glob = -1; |
| 240 | |
| 241 | if (glob < 0) |
| 242 | glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0); |
| 243 | |
| 244 | return glob; |
| 245 | } |
| 246 | |
| 247 | static inline int get_noglob_global(void) |
| 248 | { |
| 249 | static int noglob = -1; |
| 250 | |
| 251 | if (noglob < 0) |
| 252 | noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0); |
| 253 | |
| 254 | return noglob; |
| 255 | } |
| 256 | |
| 257 | static inline int get_icase_global(void) |
| 258 | { |
| 259 | static int icase = -1; |
| 260 | |
| 261 | if (icase < 0) |
| 262 | icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0); |
| 263 | |
| 264 | return icase; |
| 265 | } |
| 266 | |
| 267 | static int get_global_magic(int element_magic) |
| 268 | { |
| 269 | int global_magic = 0; |
| 270 | |
| 271 | if (get_literal_global()) |
| 272 | global_magic |= PATHSPEC_LITERAL; |
| 273 | |
| 274 | /* --glob-pathspec is overridden by :(literal) */ |
| 275 | if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL)) |
| 276 | global_magic |= PATHSPEC_GLOB; |
| 277 | |
| 278 | if (get_glob_global() && get_noglob_global()) |
| 279 | die(_("global 'glob' and 'noglob' pathspec settings are incompatible")); |
| 280 | |
| 281 | if (get_icase_global()) |
| 282 | global_magic |= PATHSPEC_ICASE; |
| 283 | |
| 284 | if ((global_magic & PATHSPEC_LITERAL) && |
| 285 | (global_magic & ~PATHSPEC_LITERAL)) |
| 286 | die(_("global 'literal' pathspec setting is incompatible " |
| 287 | "with all other global pathspec settings")); |
| 288 | |
| 289 | /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ |
| 290 | if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB)) |
| 291 | global_magic |= PATHSPEC_LITERAL; |
| 292 | |
| 293 | return global_magic; |
| 294 | } |
| 295 | |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 296 | /* |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 297 | * Parse the pathspec element looking for long magic |
| 298 | * |
| 299 | * saves all magic in 'magic' |
| 300 | * if prefix magic is used, save the prefix length in 'prefix_len' |
| 301 | * returns the position in 'elem' after all magic has been parsed |
| 302 | */ |
| 303 | static const char *parse_long_magic(unsigned *magic, int *prefix_len, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 304 | struct pathspec_item *item, |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 305 | const char *elem) |
| 306 | { |
| 307 | const char *pos; |
| 308 | const char *nextat; |
| 309 | |
| 310 | for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) { |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 311 | size_t len = strcspn_escaped(pos, ",)"); |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 312 | int i; |
| 313 | |
| 314 | if (pos[len] == ',') |
| 315 | nextat = pos + len + 1; /* handle ',' */ |
| 316 | else |
| 317 | nextat = pos + len; /* handle ')' and '\0' */ |
| 318 | |
| 319 | if (!len) |
| 320 | continue; |
| 321 | |
| 322 | if (starts_with(pos, "prefix:")) { |
| 323 | char *endptr; |
| 324 | *prefix_len = strtol(pos + 7, &endptr, 10); |
| 325 | if (endptr - pos != len) |
| 326 | die(_("invalid parameter for pathspec magic 'prefix'")); |
| 327 | continue; |
| 328 | } |
| 329 | |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 330 | if (starts_with(pos, "attr:")) { |
| 331 | char *attr_body = xmemdupz(pos + 5, len - 5); |
| 332 | parse_pathspec_attr_match(item, attr_body); |
| 333 | *magic |= PATHSPEC_ATTR; |
| 334 | free(attr_body); |
| 335 | continue; |
| 336 | } |
| 337 | |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 338 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) { |
| 339 | if (strlen(pathspec_magic[i].name) == len && |
| 340 | !strncmp(pathspec_magic[i].name, pos, len)) { |
| 341 | *magic |= pathspec_magic[i].bit; |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | if (ARRAY_SIZE(pathspec_magic) <= i) |
| 347 | die(_("Invalid pathspec magic '%.*s' in '%s'"), |
| 348 | (int) len, pos, elem); |
| 349 | } |
| 350 | |
| 351 | if (*pos != ')') |
| 352 | die(_("Missing ')' at the end of pathspec magic in '%s'"), |
| 353 | elem); |
| 354 | pos++; |
| 355 | |
| 356 | return pos; |
| 357 | } |
| 358 | |
| 359 | /* |
Brandon Williams | b4bebdc | 2017-01-04 10:04:06 -0800 | [diff] [blame] | 360 | * Parse the pathspec element looking for short magic |
| 361 | * |
| 362 | * saves all magic in 'magic' |
| 363 | * returns the position in 'elem' after all magic has been parsed |
| 364 | */ |
| 365 | static const char *parse_short_magic(unsigned *magic, const char *elem) |
| 366 | { |
| 367 | const char *pos; |
| 368 | |
| 369 | for (pos = elem + 1; *pos && *pos != ':'; pos++) { |
| 370 | char ch = *pos; |
| 371 | int i; |
| 372 | |
Linus Torvalds | 42ebeb9 | 2017-02-07 21:05:28 -0800 | [diff] [blame] | 373 | /* Special case alias for '!' */ |
| 374 | if (ch == '^') { |
| 375 | *magic |= PATHSPEC_EXCLUDE; |
| 376 | continue; |
| 377 | } |
| 378 | |
Brandon Williams | b4bebdc | 2017-01-04 10:04:06 -0800 | [diff] [blame] | 379 | if (!is_pathspec_magic(ch)) |
| 380 | break; |
| 381 | |
| 382 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) { |
| 383 | if (pathspec_magic[i].mnemonic == ch) { |
| 384 | *magic |= pathspec_magic[i].bit; |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (ARRAY_SIZE(pathspec_magic) <= i) |
| 390 | die(_("Unimplemented pathspec magic '%c' in '%s'"), |
| 391 | ch, elem); |
| 392 | } |
| 393 | |
| 394 | if (*pos == ':') |
| 395 | pos++; |
| 396 | |
| 397 | return pos; |
| 398 | } |
| 399 | |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 400 | static const char *parse_element_magic(unsigned *magic, int *prefix_len, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 401 | struct pathspec_item *item, |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 402 | const char *elem) |
| 403 | { |
| 404 | if (elem[0] != ':' || get_literal_global()) |
| 405 | return elem; /* nothing to do */ |
| 406 | else if (elem[1] == '(') |
| 407 | /* longhand */ |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 408 | return parse_long_magic(magic, prefix_len, item, elem); |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 409 | else |
| 410 | /* shorthand */ |
| 411 | return parse_short_magic(magic, elem); |
| 412 | } |
| 413 | |
Brandon Williams | b4bebdc | 2017-01-04 10:04:06 -0800 | [diff] [blame] | 414 | /* |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 415 | * Perform the initialization of a pathspec_item based on a pathspec element. |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 416 | */ |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 417 | static void init_pathspec_item(struct pathspec_item *item, unsigned flags, |
| 418 | const char *prefix, int prefixlen, |
| 419 | const char *elt) |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 420 | { |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 421 | unsigned magic = 0, element_magic = 0; |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 422 | const char *copyfrom = elt; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 423 | char *match; |
Brandon Williams | 5590215 | 2017-01-04 10:04:09 -0800 | [diff] [blame] | 424 | int pathspec_prefix = -1; |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 425 | |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 426 | item->attr_check = NULL; |
| 427 | item->attr_match = NULL; |
| 428 | item->attr_match_nr = 0; |
| 429 | |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 430 | /* PATHSPEC_LITERAL_PATH ignores magic */ |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 431 | if (flags & PATHSPEC_LITERAL_PATH) { |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 432 | magic = PATHSPEC_LITERAL; |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 433 | } else { |
| 434 | copyfrom = parse_element_magic(&element_magic, |
| 435 | &pathspec_prefix, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 436 | item, |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 437 | elt); |
| 438 | magic |= element_magic; |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 439 | magic |= get_global_magic(element_magic); |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 440 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 441 | |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 442 | item->magic = magic; |
| 443 | |
Nguyễn Thái Ngọc Duy | 233c3e6 | 2013-07-14 15:36:04 +0700 | [diff] [blame] | 444 | if (pathspec_prefix >= 0 && |
| 445 | (prefixlen || (prefix && *prefix))) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 446 | BUG("'prefix' magic is supposed to be used at worktree's root"); |
Nguyễn Thái Ngọc Duy | 233c3e6 | 2013-07-14 15:36:04 +0700 | [diff] [blame] | 447 | |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 448 | if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB)) |
| 449 | die(_("%s: 'literal' and 'glob' are incompatible"), elt); |
| 450 | |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 451 | /* Create match string which will be used for pathspec matching */ |
Nguyễn Thái Ngọc Duy | 233c3e6 | 2013-07-14 15:36:04 +0700 | [diff] [blame] | 452 | if (pathspec_prefix >= 0) { |
| 453 | match = xstrdup(copyfrom); |
| 454 | prefixlen = pathspec_prefix; |
| 455 | } else if (magic & PATHSPEC_FROMTOP) { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 456 | match = xstrdup(copyfrom); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 457 | prefixlen = 0; |
| 458 | } else { |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 459 | match = prefix_path_gently(prefix, prefixlen, |
| 460 | &prefixlen, copyfrom); |
Emily Shaffer | 5c20398 | 2020-03-02 20:05:06 -0800 | [diff] [blame] | 461 | if (!match) { |
| 462 | const char *hint_path = get_git_work_tree(); |
| 463 | if (!hint_path) |
| 464 | hint_path = get_git_dir(); |
Emily Shaffer | e0020b2 | 2020-02-14 17:00:13 -0800 | [diff] [blame] | 465 | die(_("%s: '%s' is outside repository at '%s'"), elt, |
Emily Shaffer | 5c20398 | 2020-03-02 20:05:06 -0800 | [diff] [blame] | 466 | copyfrom, absolute_path(hint_path)); |
| 467 | } |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 468 | } |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 469 | |
Brandon Williams | 34305f7 | 2017-01-04 10:04:00 -0800 | [diff] [blame] | 470 | item->match = match; |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 471 | item->len = strlen(item->match); |
| 472 | item->prefix = prefixlen; |
| 473 | |
Nguyễn Thái Ngọc Duy | dad2586 | 2013-07-14 15:35:35 +0700 | [diff] [blame] | 474 | /* |
| 475 | * Prefix the pathspec (keep all magic) and assign to |
| 476 | * original. Useful for passing to another command. |
| 477 | */ |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 478 | if ((flags & PATHSPEC_PREFIX_ORIGIN) && |
Patrick Steinhardt | be4dbbb | 2017-04-04 11:16:56 +0200 | [diff] [blame] | 479 | !get_literal_global()) { |
Nguyễn Thái Ngọc Duy | dad2586 | 2013-07-14 15:35:35 +0700 | [diff] [blame] | 480 | struct strbuf sb = STRBUF_INIT; |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 481 | |
| 482 | /* Preserve the actual prefix length of each pattern */ |
| 483 | prefix_magic(&sb, prefixlen, element_magic); |
| 484 | |
Nguyễn Thái Ngọc Duy | dad2586 | 2013-07-14 15:35:35 +0700 | [diff] [blame] | 485 | strbuf_addstr(&sb, match); |
| 486 | item->original = strbuf_detach(&sb, NULL); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 487 | } else { |
| 488 | item->original = xstrdup(elt); |
| 489 | } |
Nguyễn Thái Ngọc Duy | b69bb3f | 2013-07-14 15:35:33 +0700 | [diff] [blame] | 490 | |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 491 | if (magic & PATHSPEC_LITERAL) { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 492 | item->nowildcard_len = item->len; |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 493 | } else { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 494 | item->nowildcard_len = simple_length(item->match); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 495 | if (item->nowildcard_len < prefixlen) |
| 496 | item->nowildcard_len = prefixlen; |
| 497 | } |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 498 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 499 | item->flags = 0; |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 500 | if (magic & PATHSPEC_GLOB) { |
| 501 | /* |
| 502 | * FIXME: should we enable ONESTAR in _GLOB for |
| 503 | * pattern "* * / * . c"? |
| 504 | */ |
| 505 | } else { |
| 506 | if (item->nowildcard_len < item->len && |
| 507 | item->match[item->nowildcard_len] == '*' && |
| 508 | no_wildcard(item->match + item->nowildcard_len + 1)) |
| 509 | item->flags |= PATHSPEC_ONESTAR; |
| 510 | } |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 511 | |
| 512 | /* sanity checks, pathspec matchers assume these are sane */ |
Stefan Beller | 2d81c48 | 2017-01-09 15:16:50 -0800 | [diff] [blame] | 513 | if (item->nowildcard_len > item->len || |
| 514 | item->prefix > item->len) { |
Johannes Schindelin | c3c3486 | 2018-05-02 11:38:41 +0200 | [diff] [blame] | 515 | BUG("error initializing pathspec_item"); |
Stefan Beller | 2d81c48 | 2017-01-09 15:16:50 -0800 | [diff] [blame] | 516 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static int pathspec_item_cmp(const void *a_, const void *b_) |
| 520 | { |
| 521 | struct pathspec_item *a, *b; |
| 522 | |
| 523 | a = (struct pathspec_item *)a_; |
| 524 | b = (struct pathspec_item *)b_; |
| 525 | return strcmp(a->match, b->match); |
| 526 | } |
| 527 | |
| 528 | static void NORETURN unsupported_magic(const char *pattern, |
Brandon Williams | 2aee584 | 2017-01-04 10:04:03 -0800 | [diff] [blame] | 529 | unsigned magic) |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 530 | { |
| 531 | struct strbuf sb = STRBUF_INIT; |
Brandon Williams | 93f3ddb | 2017-01-04 10:04:02 -0800 | [diff] [blame] | 532 | int i; |
| 533 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 534 | const struct pathspec_magic *m = pathspec_magic + i; |
| 535 | if (!(magic & m->bit)) |
| 536 | continue; |
| 537 | if (sb.len) |
Brandon Williams | 2aee584 | 2017-01-04 10:04:03 -0800 | [diff] [blame] | 538 | strbuf_addstr(&sb, ", "); |
| 539 | |
| 540 | if (m->mnemonic) |
| 541 | strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"), |
| 542 | m->name, m->mnemonic); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 543 | else |
| 544 | strbuf_addf(&sb, "'%s'", m->name); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 545 | } |
| 546 | /* |
| 547 | * We may want to substitute "this command" with a command |
| 548 | * name. E.g. when add--interactive dies when running |
| 549 | * "checkout -p" |
| 550 | */ |
| 551 | die(_("%s: pathspec magic not supported by this command: %s"), |
| 552 | pattern, sb.buf); |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 553 | } |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 554 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 555 | void parse_pathspec(struct pathspec *pathspec, |
| 556 | unsigned magic_mask, unsigned flags, |
| 557 | const char *prefix, const char **argv) |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 558 | { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 559 | struct pathspec_item *item; |
| 560 | const char *entry = argv ? *argv : NULL; |
Emily Xie | 9e4e8a6 | 2017-06-06 23:33:08 -0400 | [diff] [blame] | 561 | int i, n, prefixlen, nr_exclude = 0; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 562 | |
| 563 | memset(pathspec, 0, sizeof(*pathspec)); |
| 564 | |
Nguyễn Thái Ngọc Duy | 6330a17 | 2013-07-14 15:35:32 +0700 | [diff] [blame] | 565 | if (flags & PATHSPEC_MAXDEPTH_VALID) |
| 566 | pathspec->magic |= PATHSPEC_MAXDEPTH; |
| 567 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 568 | /* No arguments, no prefix -> no pathspec */ |
| 569 | if (!entry && !prefix) |
| 570 | return; |
| 571 | |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 572 | if ((flags & PATHSPEC_PREFER_CWD) && |
| 573 | (flags & PATHSPEC_PREFER_FULL)) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 574 | BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible"); |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 575 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 576 | /* No arguments with prefix -> prefix pathspec */ |
| 577 | if (!entry) { |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 578 | if (flags & PATHSPEC_PREFER_FULL) |
| 579 | return; |
| 580 | |
| 581 | if (!(flags & PATHSPEC_PREFER_CWD)) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 582 | BUG("PATHSPEC_PREFER_CWD requires arguments"); |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 583 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 584 | pathspec->items = CALLOC_ARRAY(item, 1); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 585 | item->match = xstrdup(prefix); |
| 586 | item->original = xstrdup(prefix); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 587 | item->nowildcard_len = item->len = strlen(prefix); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 588 | item->prefix = item->len; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 589 | pathspec->nr = 1; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 590 | return; |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 591 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 592 | |
| 593 | n = 0; |
Emily Xie | d426430 | 2016-06-22 19:00:24 -0400 | [diff] [blame] | 594 | while (argv[n]) { |
Emily Xie | 9e4e8a6 | 2017-06-06 23:33:08 -0400 | [diff] [blame] | 595 | if (*argv[n] == '\0') |
| 596 | die("empty string is not a valid pathspec. " |
| 597 | "please use . instead if you meant to match all paths"); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 598 | n++; |
Emily Xie | d426430 | 2016-06-22 19:00:24 -0400 | [diff] [blame] | 599 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 600 | |
| 601 | pathspec->nr = n; |
Linus Torvalds | 859b7f1 | 2017-02-07 21:08:15 -0800 | [diff] [blame] | 602 | ALLOC_ARRAY(pathspec->items, n + 1); |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 603 | item = pathspec->items; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 604 | prefixlen = prefix ? strlen(prefix) : 0; |
| 605 | |
| 606 | for (i = 0; i < n; i++) { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 607 | entry = argv[i]; |
| 608 | |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 609 | init_pathspec_item(item + i, flags, prefix, prefixlen, entry); |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 610 | |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 611 | if (item[i].magic & PATHSPEC_EXCLUDE) |
| 612 | nr_exclude++; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 613 | if (item[i].magic & magic_mask) |
Brandon Williams | 2aee584 | 2017-01-04 10:04:03 -0800 | [diff] [blame] | 614 | unsupported_magic(entry, item[i].magic & magic_mask); |
Nguyễn Thái Ngọc Duy | 8745024 | 2013-07-14 15:35:34 +0700 | [diff] [blame] | 615 | |
| 616 | if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) && |
| 617 | has_symlink_leading_path(item[i].match, item[i].len)) { |
| 618 | die(_("pathspec '%s' is beyond a symbolic link"), entry); |
| 619 | } |
| 620 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 621 | if (item[i].nowildcard_len < item[i].len) |
| 622 | pathspec->has_wildcard = 1; |
| 623 | pathspec->magic |= item[i].magic; |
| 624 | } |
| 625 | |
Linus Torvalds | 859b7f1 | 2017-02-07 21:08:15 -0800 | [diff] [blame] | 626 | /* |
| 627 | * If everything is an exclude pattern, add one positive pattern |
Ville Skyttä | 6412757 | 2017-06-25 13:20:41 +0300 | [diff] [blame] | 628 | * that matches everything. We allocated an extra one for this. |
Linus Torvalds | 859b7f1 | 2017-02-07 21:08:15 -0800 | [diff] [blame] | 629 | */ |
| 630 | if (nr_exclude == n) { |
| 631 | int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen; |
Junio C Hamano | b02fdbc | 2022-05-29 15:39:51 -0700 | [diff] [blame] | 632 | init_pathspec_item(item + n, 0, prefix, plen, "."); |
Linus Torvalds | 859b7f1 | 2017-02-07 21:08:15 -0800 | [diff] [blame] | 633 | pathspec->nr++; |
| 634 | } |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 635 | |
| 636 | if (pathspec->magic & PATHSPEC_MAXDEPTH) { |
| 637 | if (flags & PATHSPEC_KEEP_ORDER) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 638 | BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible"); |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 639 | QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp); |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 640 | } |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 641 | } |
| 642 | |
Alexandr Miloslavskiy | 24e4750 | 2019-11-19 16:48:51 +0000 | [diff] [blame] | 643 | void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask, |
| 644 | unsigned flags, const char *prefix, |
| 645 | const char *file, int nul_term_line) |
| 646 | { |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 647 | struct strvec parsed_file = STRVEC_INIT; |
Alexandr Miloslavskiy | 24e4750 | 2019-11-19 16:48:51 +0000 | [diff] [blame] | 648 | strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul : |
| 649 | strbuf_getline; |
| 650 | struct strbuf buf = STRBUF_INIT; |
| 651 | struct strbuf unquoted = STRBUF_INIT; |
| 652 | FILE *in; |
| 653 | |
| 654 | if (!strcmp(file, "-")) |
| 655 | in = stdin; |
| 656 | else |
| 657 | in = xfopen(file, "r"); |
| 658 | |
| 659 | while (getline_fn(&buf, in) != EOF) { |
| 660 | if (!nul_term_line && buf.buf[0] == '"') { |
| 661 | strbuf_reset(&unquoted); |
| 662 | if (unquote_c_style(&unquoted, buf.buf, NULL)) |
| 663 | die(_("line is badly quoted: %s"), buf.buf); |
| 664 | strbuf_swap(&buf, &unquoted); |
| 665 | } |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 666 | strvec_push(&parsed_file, buf.buf); |
Alexandr Miloslavskiy | 24e4750 | 2019-11-19 16:48:51 +0000 | [diff] [blame] | 667 | strbuf_reset(&buf); |
| 668 | } |
| 669 | |
| 670 | strbuf_release(&unquoted); |
| 671 | strbuf_release(&buf); |
| 672 | if (in != stdin) |
| 673 | fclose(in); |
| 674 | |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 675 | parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v); |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 676 | strvec_clear(&parsed_file); |
Alexandr Miloslavskiy | 24e4750 | 2019-11-19 16:48:51 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Nguyễn Thái Ngọc Duy | e4d92cd | 2013-07-14 15:35:27 +0700 | [diff] [blame] | 679 | void copy_pathspec(struct pathspec *dst, const struct pathspec *src) |
| 680 | { |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 681 | int i, j; |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 682 | |
Nguyễn Thái Ngọc Duy | e4d92cd | 2013-07-14 15:35:27 +0700 | [diff] [blame] | 683 | *dst = *src; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 684 | ALLOC_ARRAY(dst->items, dst->nr); |
René Scharfe | 45ccef8 | 2016-09-25 09:24:03 +0200 | [diff] [blame] | 685 | COPY_ARRAY(dst->items, src->items, dst->nr); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 686 | |
| 687 | for (i = 0; i < dst->nr; i++) { |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 688 | struct pathspec_item *d = &dst->items[i]; |
| 689 | struct pathspec_item *s = &src->items[i]; |
| 690 | |
| 691 | d->match = xstrdup(s->match); |
| 692 | d->original = xstrdup(s->original); |
| 693 | |
| 694 | ALLOC_ARRAY(d->attr_match, d->attr_match_nr); |
| 695 | COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr); |
| 696 | for (j = 0; j < d->attr_match_nr; j++) { |
| 697 | const char *value = s->attr_match[j].value; |
| 698 | d->attr_match[j].value = xstrdup_or_null(value); |
| 699 | } |
| 700 | |
| 701 | d->attr_check = attr_check_dup(s->attr_check); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 702 | } |
Nguyễn Thái Ngọc Duy | e4d92cd | 2013-07-14 15:35:27 +0700 | [diff] [blame] | 703 | } |
Nguyễn Thái Ngọc Duy | 9a08727 | 2013-07-14 15:35:59 +0700 | [diff] [blame] | 704 | |
Junio C Hamano | ed6e803 | 2016-06-02 14:09:22 -0700 | [diff] [blame] | 705 | void clear_pathspec(struct pathspec *pathspec) |
Nguyễn Thái Ngọc Duy | 9a08727 | 2013-07-14 15:35:59 +0700 | [diff] [blame] | 706 | { |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 707 | int i, j; |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 708 | |
| 709 | for (i = 0; i < pathspec->nr; i++) { |
| 710 | free(pathspec->items[i].match); |
| 711 | free(pathspec->items[i].original); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 712 | |
Brandon Williams | 5ce10c0 | 2017-04-07 12:29:19 -0700 | [diff] [blame] | 713 | for (j = 0; j < pathspec->items[i].attr_match_nr; j++) |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 714 | free(pathspec->items[i].attr_match[j].value); |
| 715 | free(pathspec->items[i].attr_match); |
| 716 | |
| 717 | if (pathspec->items[i].attr_check) |
| 718 | attr_check_free(pathspec->items[i].attr_check); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 719 | } |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 720 | |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 721 | FREE_AND_NULL(pathspec->items); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 722 | pathspec->nr = 0; |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 723 | } |
Nguyễn Thái Ngọc Duy | 22af33b | 2018-11-18 17:47:59 +0100 | [diff] [blame] | 724 | |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 725 | int match_pathspec_attrs(struct index_state *istate, |
Nguyễn Thái Ngọc Duy | 22af33b | 2018-11-18 17:47:59 +0100 | [diff] [blame] | 726 | const char *name, int namelen, |
| 727 | const struct pathspec_item *item) |
| 728 | { |
| 729 | int i; |
| 730 | char *to_free = NULL; |
| 731 | |
| 732 | if (name[namelen]) |
| 733 | name = to_free = xmemdupz(name, namelen); |
| 734 | |
| 735 | git_check_attr(istate, name, item->attr_check); |
| 736 | |
| 737 | free(to_free); |
| 738 | |
| 739 | for (i = 0; i < item->attr_match_nr; i++) { |
| 740 | const char *value; |
| 741 | int matched; |
| 742 | enum attr_match_mode match_mode; |
| 743 | |
| 744 | value = item->attr_check->items[i].value; |
| 745 | match_mode = item->attr_match[i].match_mode; |
| 746 | |
| 747 | if (ATTR_TRUE(value)) |
| 748 | matched = (match_mode == MATCH_SET); |
| 749 | else if (ATTR_FALSE(value)) |
| 750 | matched = (match_mode == MATCH_UNSET); |
| 751 | else if (ATTR_UNSET(value)) |
| 752 | matched = (match_mode == MATCH_UNSPECIFIED); |
| 753 | else |
| 754 | matched = (match_mode == MATCH_VALUE && |
| 755 | !strcmp(item->attr_match[i].value, value)); |
| 756 | if (!matched) |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | return 1; |
| 761 | } |
Shaoxuan Yuan | b29ad38 | 2022-08-07 12:13:33 +0800 | [diff] [blame^] | 762 | |
| 763 | int pathspec_needs_expanded_index(struct index_state *istate, |
| 764 | const struct pathspec *pathspec) |
| 765 | { |
| 766 | unsigned int i, pos; |
| 767 | int res = 0; |
| 768 | char *skip_worktree_seen = NULL; |
| 769 | |
| 770 | /* |
| 771 | * If index is not sparse, no index expansion is needed. |
| 772 | */ |
| 773 | if (!istate->sparse_index) |
| 774 | return 0; |
| 775 | |
| 776 | /* |
| 777 | * When using a magic pathspec, assume for the sake of simplicity that |
| 778 | * the index needs to be expanded to match all matchable files. |
| 779 | */ |
| 780 | if (pathspec->magic) |
| 781 | return 1; |
| 782 | |
| 783 | for (i = 0; i < pathspec->nr; i++) { |
| 784 | struct pathspec_item item = pathspec->items[i]; |
| 785 | |
| 786 | /* |
| 787 | * If the pathspec item has a wildcard, the index should be expanded |
| 788 | * if the pathspec has the possibility of matching a subset of entries inside |
| 789 | * of a sparse directory (but not the entire directory). |
| 790 | * |
| 791 | * If the pathspec item is a literal path, the index only needs to be expanded |
| 792 | * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't |
| 793 | * expand for in-cone files) and b) it doesn't match any sparse directories |
| 794 | * (since we can reset whole sparse directories without expanding them). |
| 795 | */ |
| 796 | if (item.nowildcard_len < item.len) { |
| 797 | /* |
| 798 | * Special case: if the pattern is a path inside the cone |
| 799 | * followed by only wildcards, the pattern cannot match |
| 800 | * partial sparse directories, so we know we don't need to |
| 801 | * expand the index. |
| 802 | * |
| 803 | * Examples: |
| 804 | * - in-cone/foo***: doesn't need expanded index |
| 805 | * - not-in-cone/bar*: may need expanded index |
| 806 | * - **.c: may need expanded index |
| 807 | */ |
| 808 | if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len && |
| 809 | path_in_cone_mode_sparse_checkout(item.original, istate)) |
| 810 | continue; |
| 811 | |
| 812 | for (pos = 0; pos < istate->cache_nr; pos++) { |
| 813 | struct cache_entry *ce = istate->cache[pos]; |
| 814 | |
| 815 | if (!S_ISSPARSEDIR(ce->ce_mode)) |
| 816 | continue; |
| 817 | |
| 818 | /* |
| 819 | * If the pre-wildcard length is longer than the sparse |
| 820 | * directory name and the sparse directory is the first |
| 821 | * component of the pathspec, need to expand the index. |
| 822 | */ |
| 823 | if (item.nowildcard_len > ce_namelen(ce) && |
| 824 | !strncmp(item.original, ce->name, ce_namelen(ce))) { |
| 825 | res = 1; |
| 826 | break; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * If the pre-wildcard length is shorter than the sparse |
| 831 | * directory and the pathspec does not match the whole |
| 832 | * directory, need to expand the index. |
| 833 | */ |
| 834 | if (!strncmp(item.original, ce->name, item.nowildcard_len) && |
| 835 | wildmatch(item.original, ce->name, 0)) { |
| 836 | res = 1; |
| 837 | break; |
| 838 | } |
| 839 | } |
| 840 | } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) && |
| 841 | !matches_skip_worktree(pathspec, i, &skip_worktree_seen)) |
| 842 | res = 1; |
| 843 | |
| 844 | if (res > 0) |
| 845 | break; |
| 846 | } |
| 847 | |
| 848 | free(skip_worktree_seen); |
| 849 | return res; |
| 850 | } |