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